blob: a6fa374bc2872b376878f6bfeb513dbfebab3353 [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.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * 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.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * 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
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 char_u *(*fgetline) __ARGS((int, void *, int));
755 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 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
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 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
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# 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
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 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 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
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 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (!(flags & DOCMD_KEYTYPED)
885 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 KeyTyped = FALSE;
887
888 /*
889 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000890 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * - for multiple commands on one line, separated with '|'
892 * - when repeating until there are no more lines (for ":source")
893 */
894 next_cmdline = cmdline;
895 do
896 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100898 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899#endif
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (next_cmdline == NULL
903#ifdef FEAT_EVAL
904 && !force_abort
905 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 )
909 did_emsg = FALSE;
910
911 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * 3. If a line is given: Make a copy, so we can mess with it.
915 */
916
917#ifdef FEAT_EVAL
918 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 /* Each '|' separated command is stored separately in lines_ga, to
922 * be able to jump to it. Don't use next_cmdline now. */
923 vim_free(cmdline_copy);
924 cmdline_copy = NULL;
925
926 /* Check if a function has returned or, unless it has an unclosed
927 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000931 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 func_line_end(real_cookie);
933# endif
934 if (func_has_ended(real_cookie))
935 {
936 retval = FAIL;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 script_line_end();
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
952
953 /* If breakpoints have been added/deleted need to check for it. */
954 if (breakpoint != NULL && dbg_tick != NULL
955 && *dbg_tick != debug_tick)
956 {
957 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
962
963 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
964 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
965
966 /* Did we encounter a breakpoint? */
967 if (breakpoint != NULL && *breakpoint != 0
968 && *breakpoint <= sourcing_lnum)
969 {
970 dbg_breakpoint(fname, sourcing_lnum);
971 /* Find next breakpoint. */
972 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 fname, sourcing_lnum);
975 *dbg_tick = debug_tick;
976 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000978 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000979 {
980 if (getline_is_func)
981 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 script_line_start();
984 }
985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * below, so that it stores lines in or reads them from
993 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 * while/for loop. */
995 cmd_getline = get_loop_line;
996 cmd_cookie = (void *)&cmd_loop_cookie;
997 cmd_loop_cookie.lines_gap = &lines_ga;
998 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 cmd_loop_cookie.cookie = cookie;
1001 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 else
1004 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 cmd_cookie = cookie;
1007 }
1008#endif
1009
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (next_cmdline == NULL)
1012 {
1013 /*
1014 * Need to set msg_didout for the first line after an ":if",
1015 * otherwise the ":if" will be overwritten.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_EVAL
1021 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1022#else
1023 0
1024#endif
1025 )) == NULL)
1026 {
1027 /* Don't call wait_return for aborted command line. The NULL
1028 * returned for the end of a sourced file or executed function
1029 * doesn't do this. */
1030 if (KeyTyped && !(flags & DOCMD_REPEAT))
1031 need_wait_return = FALSE;
1032 retval = FAIL;
1033 break;
1034 }
1035 used_getline = TRUE;
1036
1037 /*
1038 * Keep the first typed line. Clear it when more lines are typed.
1039 */
1040 if (flags & DOCMD_KEEPLINE)
1041 {
1042 vim_free(repeat_cmdline);
1043 if (count == 0)
1044 repeat_cmdline = vim_strsave(next_cmdline);
1045 else
1046 repeat_cmdline = NULL;
1047 }
1048 }
1049
1050 /* 3. Make a copy of the command so we can mess with it. */
1051 else if (cmdline_copy == NULL)
1052 {
1053 next_cmdline = vim_strsave(next_cmdline);
1054 if (next_cmdline == NULL)
1055 {
1056 EMSG(_(e_outofmem));
1057 retval = FAIL;
1058 break;
1059 }
1060 }
1061 cmdline_copy = next_cmdline;
1062
1063#ifdef FEAT_EVAL
1064 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 * Save the current line when inside a ":while" or ":for", and when
1066 * the command looks like a ":while" or ":for", because we may need it
1067 * later. When there is a '|' and another command, it is stored
1068 * separately, because we need to be able to jump back to it from an
1069 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (current_line == lines_ga.ga_len
1072 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 retval = FAIL;
1077 break;
1078 }
1079 }
1080 did_endif = FALSE;
1081#endif
1082
1083 if (count++ == 0)
1084 {
1085 /*
1086 * All output from the commands is put below each other, without
1087 * waiting for a return. Don't do this when executing commands
1088 * from a script or when being called recursive (e.g. for ":e
1089 * +command file").
1090 */
1091 if (!(flags & DOCMD_NOWAIT) && !recursive)
1092 {
1093 msg_didout_before_start = msg_didout;
1094 msg_didany = FALSE; /* no output yet */
1095 msg_start();
1096 msg_scroll = TRUE; /* put messages below each other */
1097 ++no_wait_return; /* dont wait for return until finished */
1098 ++RedrawingDisabled;
1099 did_inc = TRUE;
1100 }
1101 }
1102
1103 if (p_verbose >= 15 && sourcing_name != NULL)
1104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001106 verbose_enter_scroll();
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 smsg((char_u *)_("line %ld: %s"),
1109 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 if (msg_silent == 0)
1111 msg_puts((char_u *)"\n"); /* don't overwrite this */
1112
1113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 --no_wait_return;
1115 }
1116
1117 /*
1118 * 2. Execute one '|' separated command.
1119 * do_one_cmd() will return NULL if there is no trailing '|'.
1120 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1121 */
1122 ++recursive;
1123 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1124#ifdef FEAT_EVAL
1125 &cstack,
1126#endif
1127 cmd_getline, cmd_cookie);
1128 --recursive;
1129
1130#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cmd_cookie == (void *)&cmd_loop_cookie)
1132 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 if (next_cmdline == NULL)
1138 {
1139 vim_free(cmdline_copy);
1140 cmdline_copy = NULL;
1141#ifdef FEAT_CMDHIST
1142 /*
1143 * If the command was typed, remember it for the ':' register.
1144 * Do this AFTER executing the command to make :@: work.
1145 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001146 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 && new_last_cmdline != NULL)
1148 {
1149 vim_free(last_cmdline);
1150 last_cmdline = new_last_cmdline;
1151 new_last_cmdline = NULL;
1152 }
1153#endif
1154 }
1155 else
1156 {
1157 /* need to copy the command after the '|' to cmdline_copy, for the
1158 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001159 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 next_cmdline = cmdline_copy;
1161 }
1162
1163
1164#ifdef FEAT_EVAL
1165 /* reset did_emsg for a function that is not aborted by an error */
1166 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 && !func_has_abort(real_cookie))
1169 did_emsg = FALSE;
1170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 ++current_line;
1174
1175 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 * An ":endwhile", ":endfor" and ":continue" is handled here.
1177 * If we were executing commands, jump back to the ":while" or
1178 * ":for".
1179 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* Jump back to the matching ":while" or ":for". Be careful
1186 * not to use a cs_line[] from an entry that isn't a ":while"
1187 * or ":for": It would make "current_line" invalid and can
1188 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (!did_emsg && !got_int && !did_throw
1190 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 && (cstack.cs_flags[cstack.cs_idx]
1192 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && cstack.cs_line[cstack.cs_idx] >= 0
1194 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1195 {
1196 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 /* remember we jumped there */
1198 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 line_breakcheck(); /* check if CTRL-C typed */
1200
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* Check for the next breakpoint at or after the ":while"
1202 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (breakpoint != NULL)
1204 {
1205 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 fname,
1208 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1209 *dbg_tick = debug_tick;
1210 }
1211 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1217 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220
1221 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1228 }
1229 }
1230
1231 /*
1232 * When not inside any ":while" loop, clear remembered lines.
1233 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (lines_ga.ga_len > 0)
1237 {
1238 sourcing_lnum =
1239 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1240 free_cmdlines(&lines_ga);
1241 }
1242 current_line = 0;
1243 }
1244
1245 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1247 * being restored at the ":endtry". Reset them here and set the
1248 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1249 * This includes the case where a missing ":endif", ":endwhile" or
1250 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 cstack.cs_lflags &= ~CSL_HAD_FINA;
1255 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1256 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 did_throw ? (void *)current_exception : NULL);
1258 did_emsg = got_int = did_throw = FALSE;
1259 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1260 }
1261
1262 /* Update global "trylevel" for recursive calls to do_cmdline() from
1263 * within this loop. */
1264 trylevel = initial_trylevel + cstack.cs_trylevel;
1265
1266 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001267 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * files) is aborted because of an error, an interrupt, or an uncaught
1269 * exception, cancel everything. If it is left normally, reset
1270 * force_abort to get the non-EH compatible abortion behavior for
1271 * the rest of the script.
1272 */
1273 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1274 force_abort = FALSE;
1275
1276 /* Convert an interrupt to an exception if appropriate. */
1277 (void)do_intthrow(&cstack);
1278#endif /* FEAT_EVAL */
1279
1280 }
1281 /*
1282 * Continue executing command lines when:
1283 * - no CTRL-C typed, no aborting error, no exception thrown or try
1284 * conditionals need to be checked for executing finally clauses or
1285 * catching an interrupt exception
1286 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * looping for ":source" command or function call.
1289 */
1290 while (!((got_int
1291#ifdef FEAT_EVAL
1292 || (did_emsg && force_abort) || did_throw
1293#endif
1294 )
1295#ifdef FEAT_EVAL
1296 && cstack.cs_trylevel == 0
1297#endif
1298 )
1299 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001300 && (getline_equal(fgetline, cookie, getexmodeline)
1301 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (next_cmdline != NULL
1303#ifdef FEAT_EVAL
1304 || cstack.cs_idx >= 0
1305#endif
1306 || (flags & DOCMD_REPEAT)));
1307
1308 vim_free(cmdline_copy);
1309#ifdef FEAT_EVAL
1310 free_cmdlines(&lines_ga);
1311 ga_clear(&lines_ga);
1312
1313 if (cstack.cs_idx >= 0)
1314 {
1315 /*
1316 * If a sourced file or executed function ran to its end, report the
1317 * unclosed conditional.
1318 */
1319 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 && ((getline_equal(fgetline, cookie, getsourceline)
1321 && !source_finished(fgetline, cookie))
1322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && !func_has_ended(real_cookie))))
1324 {
1325 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1326 EMSG(_(e_endtry));
1327 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1328 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1330 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 EMSG(_(e_endif));
1333 }
1334
1335 /*
1336 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1337 * ":endtry" in a sourced file or executed function. If the try
1338 * conditional is in its finally clause, ignore anything pending.
1339 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001340 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 {
1344 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1345
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001346 if (idx >= 0)
1347 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1349 &cstack.cs_looplevel);
1350 }
1351 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 trylevel = initial_trylevel;
1353 }
1354
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001355 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1356 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001358 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ? (char_u *)"endfunction" : (char_u *)NULL);
1360
1361 if (trylevel == 0)
1362 {
1363 /*
1364 * When an exception is being thrown out of the outermost try
1365 * conditional, discard the uncaught exception, disable the conversion
1366 * of interrupts or errors to exceptions, and ensure that no more
1367 * commands are executed.
1368 */
1369 if (did_throw)
1370 {
1371 void *p = NULL;
1372 char_u *saved_sourcing_name;
1373 int saved_sourcing_lnum;
1374 struct msglist *messages = NULL, *next;
1375
1376 /*
1377 * If the uncaught exception is a user exception, report it as an
1378 * error. If it is an error exception, display the saved error
1379 * message now. For an interrupt exception, do nothing; the
1380 * interrupt message is given elsewhere.
1381 */
1382 switch (current_exception->type)
1383 {
1384 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001385 vim_snprintf((char *)IObuff, IOSIZE,
1386 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 current_exception->value);
1388 p = vim_strsave(IObuff);
1389 break;
1390 case ET_ERROR:
1391 messages = current_exception->messages;
1392 current_exception->messages = NULL;
1393 break;
1394 case ET_INTERRUPT:
1395 break;
1396 default:
1397 p = vim_strsave((char_u *)_(e_internal));
1398 }
1399
1400 saved_sourcing_name = sourcing_name;
1401 saved_sourcing_lnum = sourcing_lnum;
1402 sourcing_name = current_exception->throw_name;
1403 sourcing_lnum = current_exception->throw_lnum;
1404 current_exception->throw_name = NULL;
1405
1406 discard_current_exception(); /* uses IObuff if 'verbose' */
1407 suppress_errthrow = TRUE;
1408 force_abort = TRUE;
1409
1410 if (messages != NULL)
1411 {
1412 do
1413 {
1414 next = messages->next;
1415 emsg(messages->msg);
1416 vim_free(messages->msg);
1417 vim_free(messages);
1418 messages = next;
1419 }
1420 while (messages != NULL);
1421 }
1422 else if (p != NULL)
1423 {
1424 emsg(p);
1425 vim_free(p);
1426 }
1427 vim_free(sourcing_name);
1428 sourcing_name = saved_sourcing_name;
1429 sourcing_lnum = saved_sourcing_lnum;
1430 }
1431
1432 /*
1433 * On an interrupt or an aborting error not converted to an exception,
1434 * disable the conversion of errors to exceptions. (Interrupts are not
1435 * converted any more, here.) This enables also the interrupt message
1436 * when force_abort is set and did_emsg unset in case of an interrupt
1437 * from a finally clause after an error.
1438 */
1439 else if (got_int || (did_emsg && force_abort))
1440 suppress_errthrow = TRUE;
1441 }
1442
1443 /*
1444 * The current cstack will be freed when do_cmdline() returns. An uncaught
1445 * exception will have to be rethrown in the previous cstack. If a function
1446 * has just returned or a script file was just finished and the previous
1447 * cstack belongs to the same function or, respectively, script file, it
1448 * will have to be checked for finally clauses to be executed due to the
1449 * ":return" or ":finish". This is done in do_one_cmd().
1450 */
1451 if (did_throw)
1452 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001453 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001455 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 && ex_nesting_level > func_level(real_cookie) + 1))
1457 {
1458 if (!did_throw)
1459 check_cstack = TRUE;
1460 }
1461 else
1462 {
1463 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 --ex_nesting_level;
1466 /*
1467 * Go to debug mode when returning from a function in which we are
1468 * single-stepping.
1469 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 if ((getline_equal(fgetline, cookie, getsourceline)
1471 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ? (char_u *)_("End of sourced file")
1475 : (char_u *)_("End of function"));
1476 }
1477
1478 /*
1479 * Restore the exception environment (done after returning from the
1480 * debugger).
1481 */
1482 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001483 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 msg_list = saved_msg_list;
1486#endif /* FEAT_EVAL */
1487
1488 /*
1489 * If there was too much output to fit on the command line, ask the user to
1490 * hit return before redrawing the screen. With the ":global" command we do
1491 * this only once after the command is finished.
1492 */
1493 if (did_inc)
1494 {
1495 --RedrawingDisabled;
1496 --no_wait_return;
1497 msg_scroll = FALSE;
1498
1499 /*
1500 * When just finished an ":if"-":else" which was typed, no need to
1501 * wait for hit-return. Also for an error situation.
1502 */
1503 if (retval == FAIL
1504#ifdef FEAT_EVAL
1505 || (did_endif && KeyTyped && !did_emsg)
1506#endif
1507 )
1508 {
1509 need_wait_return = FALSE;
1510 msg_didany = FALSE; /* don't wait when restarting edit */
1511 }
1512 else if (need_wait_return)
1513 {
1514 /*
1515 * The msg_start() above clears msg_didout. The wait_return we do
1516 * here should not overwrite the command that may be shown before
1517 * doing that.
1518 */
1519 msg_didout |= msg_didout_before_start;
1520 wait_return(FALSE);
1521 }
1522 }
1523
1524#ifndef FEAT_EVAL
1525 /*
1526 * Reset if_level, in case a sourced script file contains more ":if" than
1527 * ":endif" (could be ":if x | foo | endif").
1528 */
1529 if_level = 0;
1530#endif
1531
1532 --call_depth;
1533 return retval;
1534}
1535
1536#ifdef FEAT_EVAL
1537/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int c;
1543 void *cookie;
1544 int indent;
1545{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 wcmd_T *wp;
1548 char_u *line;
1549
1550 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1551 {
1552 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001553 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (cp->getline == NULL)
1557 line = getcmdline(c, 0L, indent);
1558 else
1559 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001560 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 ++cp->current_line;
1562
1563 return line;
1564 }
1565
1566 KeyTyped = FALSE;
1567 ++cp->current_line;
1568 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1569 sourcing_lnum = wp->lnum;
1570 return vim_strsave(wp->line);
1571}
1572
1573/*
1574 * Store a line in "gap" so that a ":while" loop can execute it again.
1575 */
1576 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 garray_T *gap;
1579 char_u *line;
1580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
1593free_cmdlines(gap)
1594 garray_T *gap;
1595{
1596 while (gap->ga_len > 0)
1597 {
1598 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1599 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601}
1602#endif
1603
1604/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001605 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1606 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609getline_equal(fgetline, cookie, func)
1610 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001611 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 char_u *(*func) __ARGS((int, void *, int));
1613{
1614#ifdef FEAT_EVAL
1615 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001619 * function that's originally used to obtain the lines. This may be
1620 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 cp = (struct loop_cookie *)cookie;
1623 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 gp = cp->getline;
1626 cp = cp->cookie;
1627 }
1628 return gp == func;
1629#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#endif
1632}
1633
1634#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1635/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * getline function. Otherwise return "cookie".
1638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001641 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644# ifdef FEAT_EVAL
1645 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001646 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar89d40322006-08-29 15:30:07 +00001648 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001649 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001652 cp = (struct loop_cookie *)cookie;
1653 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655 gp = cp->getline;
1656 cp = cp->cookie;
1657 }
1658 return cp;
1659# else
1660 return cookie;
1661# endif
1662}
1663#endif
1664
1665/*
1666 * Execute one Ex command.
1667 *
1668 * If 'sourcing' is TRUE, the command will be included in the error message.
1669 *
1670 * 1. skip comment lines and leading space
1671 * 2. handle command modifiers
1672 * 3. parse range
1673 * 4. parse command
1674 * 5. parse arguments
1675 * 6. switch on command name
1676 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
1679 * This function may be called recursively!
1680 */
1681#if (_MSC_VER == 1200)
1682/*
Bram Moolenaared203462004-06-16 11:19:22 +00001683 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001685 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687 static char_u *
1688do_one_cmd(cmdlinep, sourcing,
1689#ifdef FEAT_EVAL
1690 cstack,
1691#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001692 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 char_u **cmdlinep;
1694 int sourcing;
1695#ifdef FEAT_EVAL
1696 struct condstack *cstack;
1697#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001698 char_u *(*fgetline) __ARGS((int, void *, int));
1699 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 char_u *p;
1702 linenr_T lnum;
1703 long n;
1704 char_u *errormsg = NULL; /* error message */
1705 exarg_T ea; /* Ex command arguments */
1706 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001707 int save_msg_scroll = msg_scroll;
1708 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001710#ifdef HAVE_SANDBOX
1711 int did_sandbox = FALSE;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 cmdmod_T save_cmdmod;
1714 int ni; /* set when Not Implemented */
1715
1716 vim_memset(&ea, 0, sizeof(ea));
1717 ea.line1 = 1;
1718 ea.line2 = 1;
1719#ifdef FEAT_EVAL
1720 ++ex_nesting_level;
1721#endif
1722
1723 /* when not editing the last file :q has to be typed twice */
1724 if (quitmore
1725#ifdef FEAT_EVAL
1726 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001727 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
1729 )
1730 --quitmore;
1731
1732 /*
1733 * Reset browse, confirm, etc.. They are restored when returning, for
1734 * recursive calls.
1735 */
1736 save_cmdmod = cmdmod;
1737 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1738
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001739 /* "#!anything" is handled like a comment. */
1740 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1741 goto doend;
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /*
1744 * Repeat until no more command modifiers are found.
1745 */
1746 ea.cmd = *cmdlinep;
1747 for (;;)
1748 {
1749/*
1750 * 1. skip comment lines and leading white space and colons
1751 */
1752 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1753 ++ea.cmd;
1754
1755 /* in ex mode, an empty line works like :+ */
1756 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 && (getline_equal(fgetline, cookie, getexmodeline)
1758 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001759 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
1761 ea.cmd = (char_u *)"+";
1762 ex_pressedreturn = TRUE;
1763 }
1764
1765 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001766 if (*ea.cmd == '"')
1767 goto doend;
1768 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001769 {
1770 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774/*
1775 * 2. handle command modifiers.
1776 */
1777 p = ea.cmd;
1778 if (VIM_ISDIGIT(*ea.cmd))
1779 p = skipwhite(skipdigits(ea.cmd));
1780 switch (*p)
1781 {
1782 /* When adding an entry, also modify cmd_exists(). */
1783 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1784 break;
1785#ifdef FEAT_WINDOWS
1786 cmdmod.split |= WSP_ABOVE;
1787#endif
1788 continue;
1789
1790 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1791 {
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_BELOW;
1794#endif
1795 continue;
1796 }
1797 if (checkforcmd(&ea.cmd, "browse", 3))
1798 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001799#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 cmdmod.browse = TRUE;
1801#endif
1802 continue;
1803 }
1804 if (!checkforcmd(&ea.cmd, "botright", 2))
1805 break;
1806#ifdef FEAT_WINDOWS
1807 cmdmod.split |= WSP_BOT;
1808#endif
1809 continue;
1810
1811 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1812 break;
1813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1814 cmdmod.confirm = TRUE;
1815#endif
1816 continue;
1817
1818 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1819 {
1820 cmdmod.keepmarks = TRUE;
1821 continue;
1822 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001823 if (checkforcmd(&ea.cmd, "keepalt", 5))
1824 {
1825 cmdmod.keepalt = TRUE;
1826 continue;
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1829 break;
1830 cmdmod.keepjumps = TRUE;
1831 continue;
1832
1833 /* ":hide" and ":hide | cmd" are not modifiers */
1834 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1835 || *p == NUL || ends_excmd(*p))
1836 break;
1837 ea.cmd = p;
1838 cmdmod.hide = TRUE;
1839 continue;
1840
1841 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1842 {
1843 cmdmod.lockmarks = TRUE;
1844 continue;
1845 }
1846
1847 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1848 break;
1849#ifdef FEAT_WINDOWS
1850 cmdmod.split |= WSP_ABOVE;
1851#endif
1852 continue;
1853
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001854 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1855 break;
1856#ifdef FEAT_AUTOCMD
1857 if (cmdmod.save_ei == NULL)
1858 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001859 /* Set 'eventignore' to "all". Restore the
1860 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001861 cmdmod.save_ei = vim_strsave(p_ei);
1862 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001863 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001864 }
1865#endif
1866 continue;
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1869 break;
1870#ifdef FEAT_WINDOWS
1871 cmdmod.split |= WSP_BELOW;
1872#endif
1873 continue;
1874
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001875 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1876 {
1877#ifdef HAVE_SANDBOX
1878 if (!did_sandbox)
1879 ++sandbox;
1880 did_sandbox = TRUE;
1881#endif
1882 continue;
1883 }
1884 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001886 if (save_msg_silent == -1)
1887 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1890 {
1891 /* ":silent!", but not "silent !cmd" */
1892 ea.cmd = skipwhite(ea.cmd + 1);
1893 ++emsg_silent;
1894 ++did_esilent;
1895 }
1896 continue;
1897
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001898 case 't': if (checkforcmd(&p, "tab", 3))
1899 {
1900#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001901 if (vim_isdigit(*ea.cmd))
1902 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1903 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001904 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001905 ea.cmd = p;
1906#endif
1907 continue;
1908 }
1909 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 break;
1911#ifdef FEAT_WINDOWS
1912 cmdmod.split |= WSP_TOP;
1913#endif
1914 continue;
1915
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001916 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1917 break;
1918 if (save_msg_silent == -1)
1919 save_msg_silent = msg_silent;
1920 msg_silent = 0;
1921 continue;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1924 {
1925#ifdef FEAT_VERTSPLIT
1926 cmdmod.split |= WSP_VERT;
1927#endif
1928 continue;
1929 }
1930 if (!checkforcmd(&p, "verbose", 4))
1931 break;
1932 if (verbose_save < 0)
1933 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001934 if (vim_isdigit(*ea.cmd))
1935 p_verbose = atoi((char *)ea.cmd);
1936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 p_verbose = 1;
1938 ea.cmd = p;
1939 continue;
1940 }
1941 break;
1942 }
1943
1944#ifdef FEAT_EVAL
1945 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1946 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1947#else
1948 ea.skip = (if_level > 0);
1949#endif
1950
1951#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001952# ifdef FEAT_PROFILE
1953 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001954 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001955 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001956 if (getline_equal(fgetline, cookie, get_func_line))
1957 func_line_exec(getline_cookie(fgetline, cookie));
1958 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 script_line_exec();
1960 }
1961#endif
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* May go to debug mode. If this happens and the ">quit" debug command is
1964 * used, throw an interrupt exception and skip the next command. */
1965 dbg_check_breakpoint(&ea);
1966 if (!ea.skip && got_int)
1967 {
1968 ea.skip = TRUE;
1969 (void)do_intthrow(cstack);
1970 }
1971#endif
1972
1973/*
1974 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1975 *
1976 * where 'addr' is:
1977 *
1978 * % (entire file)
1979 * $ [+-NUM]
1980 * 'x [+-NUM] (where x denotes a currently defined mark)
1981 * . [+-NUM]
1982 * [+-NUM]..
1983 * NUM
1984 *
1985 * The ea.cmd pointer is updated to point to the first character following the
1986 * range spec. If an initial address is found, but no second, the upper bound
1987 * is equal to the lower.
1988 */
1989
1990 /* repeat for all ',' or ';' separated addresses */
1991 for (;;)
1992 {
1993 ea.line1 = ea.line2;
1994 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1995 ea.cmd = skipwhite(ea.cmd);
1996 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1997 if (ea.cmd == NULL) /* error detected */
1998 goto doend;
1999 if (lnum == MAXLNUM)
2000 {
2001 if (*ea.cmd == '%') /* '%' - all lines */
2002 {
2003 ++ea.cmd;
2004 ea.line1 = 1;
2005 ea.line2 = curbuf->b_ml.ml_line_count;
2006 ++ea.addr_count;
2007 }
2008 /* '*' - visual area */
2009 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2010 {
2011 pos_T *fp;
2012
2013 ++ea.cmd;
2014 if (!ea.skip)
2015 {
2016 fp = getmark('<', FALSE);
2017 if (check_mark(fp) == FAIL)
2018 goto doend;
2019 ea.line1 = fp->lnum;
2020 fp = getmark('>', FALSE);
2021 if (check_mark(fp) == FAIL)
2022 goto doend;
2023 ea.line2 = fp->lnum;
2024 ++ea.addr_count;
2025 }
2026 }
2027 }
2028 else
2029 ea.line2 = lnum;
2030 ea.addr_count++;
2031
2032 if (*ea.cmd == ';')
2033 {
2034 if (!ea.skip)
2035 curwin->w_cursor.lnum = ea.line2;
2036 }
2037 else if (*ea.cmd != ',')
2038 break;
2039 ++ea.cmd;
2040 }
2041
2042 /* One address given: set start and end lines */
2043 if (ea.addr_count == 1)
2044 {
2045 ea.line1 = ea.line2;
2046 /* ... but only implicit: really no address given */
2047 if (lnum == MAXLNUM)
2048 ea.addr_count = 0;
2049 }
2050
2051 /* Don't leave the cursor on an illegal line (caused by ';') */
2052 check_cursor_lnum();
2053
2054/*
2055 * 4. parse command
2056 */
2057
2058 /*
2059 * Skip ':' and any white space
2060 */
2061 ea.cmd = skipwhite(ea.cmd);
2062 while (*ea.cmd == ':')
2063 ea.cmd = skipwhite(ea.cmd + 1);
2064
2065 /*
2066 * If we got a line, but no command, then go to the line.
2067 * If we find a '|' or '\n' we set ea.nextcmd.
2068 */
2069 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2070 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2071 {
2072 /*
2073 * strange vi behaviour:
2074 * ":3" jumps to line 3
2075 * ":3|..." prints line 3
2076 * ":|" prints current line
2077 */
2078 if (ea.skip) /* skip this if inside :if */
2079 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002080 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 ea.cmdidx = CMD_print;
2083 ea.argt = RANGE+COUNT+TRLBAR;
2084 if ((errormsg = invalid_range(&ea)) == NULL)
2085 {
2086 correct_range(&ea);
2087 ex_print(&ea);
2088 }
2089 }
2090 else if (ea.addr_count != 0)
2091 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002092 if (ea.line2 > curbuf->b_ml.ml_line_count)
2093 {
2094 /* With '-' in 'cpoptions' a line number past the file is an
2095 * error, otherwise put it at the end of the file. */
2096 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2097 ea.line2 = -1;
2098 else
2099 ea.line2 = curbuf->b_ml.ml_line_count;
2100 }
2101
2102 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002103 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 else
2105 {
2106 if (ea.line2 == 0)
2107 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
2109 curwin->w_cursor.lnum = ea.line2;
2110 beginline(BL_SOL | BL_FIX);
2111 }
2112 }
2113 goto doend;
2114 }
2115
2116 /* Find the command and let "p" point to after it. */
2117 p = find_command(&ea, NULL);
2118
2119#ifdef FEAT_USR_CMDS
2120 if (p == NULL)
2121 {
2122 if (!ea.skip)
2123 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2124 goto doend;
2125 }
2126 /* Check for wrong commands. */
2127 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2128 {
2129 errormsg = uc_fun_cmd();
2130 goto doend;
2131 }
2132#endif
2133 if (ea.cmdidx == CMD_SIZE)
2134 {
2135 if (!ea.skip)
2136 {
2137 STRCPY(IObuff, _("E492: Not an editor command"));
2138 if (!sourcing)
2139 {
2140 STRCAT(IObuff, ": ");
2141 STRNCAT(IObuff, *cmdlinep, 40);
2142 }
2143 errormsg = IObuff;
2144 }
2145 goto doend;
2146 }
2147
2148 ni = (
2149#ifdef FEAT_USR_CMDS
2150 !USER_CMDIDX(ea.cmdidx) &&
2151#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002152 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002153#ifdef HAVE_EX_SCRIPT_NI
2154 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2155#endif
2156 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158#ifndef FEAT_EVAL
2159 /*
2160 * When the expression evaluation is disabled, recognize the ":if" and
2161 * ":endif" commands and ignore everything in between it.
2162 */
2163 if (ea.cmdidx == CMD_if)
2164 ++if_level;
2165 if (if_level)
2166 {
2167 if (ea.cmdidx == CMD_endif)
2168 --if_level;
2169 goto doend;
2170 }
2171
2172#endif
2173
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002174 /* forced commands */
2175 if (*p == '!' && ea.cmdidx != CMD_substitute
2176 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 ++p;
2179 ea.forceit = TRUE;
2180 }
2181 else
2182 ea.forceit = FALSE;
2183
2184/*
2185 * 5. parse arguments
2186 */
2187#ifdef FEAT_USR_CMDS
2188 if (!USER_CMDIDX(ea.cmdidx))
2189#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002190 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 if (!ea.skip)
2193 {
2194#ifdef HAVE_SANDBOX
2195 if (sandbox != 0 && !(ea.argt & SBOXOK))
2196 {
2197 /* Command not allowed in sandbox. */
2198 errormsg = (char_u *)_(e_sandbox);
2199 goto doend;
2200 }
2201#endif
2202 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2203 {
2204 /* Command not allowed in non-'modifiable' buffer */
2205 errormsg = (char_u *)_(e_modifiable);
2206 goto doend;
2207 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002208
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002209 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# ifdef FEAT_USR_CMDS
2211 && !USER_CMDIDX(ea.cmdidx)
2212# endif
2213 )
2214 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002215 /* Command not allowed when editing the command line. */
2216#ifdef FEAT_CMDWIN
2217 if (cmdwin_type != 0)
2218 errormsg = (char_u *)_(e_cmdwin);
2219 else
2220#endif
2221 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto doend;
2223 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002225 /* Disallow editing another buffer when "curbuf_lock" is set.
2226 * Do allow ":edit" (check for argument later).
2227 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002228 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002229 && ea.cmdidx != CMD_edit
2230 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231# ifdef FEAT_USR_CMDS
2232 && !USER_CMDIDX(ea.cmdidx)
2233# endif
2234 && curbuf_locked())
2235 goto doend;
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2239 {
2240 /* no range allowed */
2241 errormsg = (char_u *)_(e_norange);
2242 goto doend;
2243 }
2244 }
2245
2246 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2247 {
2248 errormsg = (char_u *)_(e_nobang);
2249 goto doend;
2250 }
2251
2252 /*
2253 * Don't complain about the range if it is not used
2254 * (could happen if line_count is accidentally set to 0).
2255 */
2256 if (!ea.skip && !ni)
2257 {
2258 /*
2259 * If the range is backwards, ask for confirmation and, if given, swap
2260 * ea.line1 & ea.line2 so it's forwards again.
2261 * When global command is busy, don't ask, will fail below.
2262 */
2263 if (!global_busy && ea.line1 > ea.line2)
2264 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002265 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002267 if (sourcing || exmode_active)
2268 {
2269 errormsg = (char_u *)_("E493: Backwards range given");
2270 goto doend;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (ask_yesno((char_u *)
2273 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002274 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 lnum = ea.line1;
2277 ea.line1 = ea.line2;
2278 ea.line2 = lnum;
2279 }
2280 if ((errormsg = invalid_range(&ea)) != NULL)
2281 goto doend;
2282 }
2283
2284 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2285 ea.line2 = 1;
2286
2287 correct_range(&ea);
2288
2289#ifdef FEAT_FOLDING
2290 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2291 {
2292 /* Put the first line at the start of a closed fold, put the last line
2293 * at the end of a closed fold. */
2294 (void)hasFolding(ea.line1, &ea.line1, NULL);
2295 (void)hasFolding(ea.line2, NULL, &ea.line2);
2296 }
2297#endif
2298
2299#ifdef FEAT_QUICKFIX
2300 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002301 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * option here, so things like % get expanded.
2303 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002304 p = replace_makeprg(&ea, p, cmdlinep);
2305 if (p == NULL)
2306 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308
2309 /*
2310 * Skip to start of argument.
2311 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2312 */
2313 if (ea.cmdidx == CMD_bang)
2314 ea.arg = p;
2315 else
2316 ea.arg = skipwhite(p);
2317
2318 /*
2319 * Check for "++opt=val" argument.
2320 * Must be first, allow ":w ++enc=utf8 !cmd"
2321 */
2322 if (ea.argt & ARGOPT)
2323 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2324 if (getargopt(&ea) == FAIL && !ni)
2325 {
2326 errormsg = (char_u *)_(e_invarg);
2327 goto doend;
2328 }
2329
2330 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2331 {
2332 if (*ea.arg == '>') /* append */
2333 {
2334 if (*++ea.arg != '>') /* typed wrong */
2335 {
2336 errormsg = (char_u *)_("E494: Use w or w>>");
2337 goto doend;
2338 }
2339 ea.arg = skipwhite(ea.arg + 1);
2340 ea.append = TRUE;
2341 }
2342 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2343 {
2344 ++ea.arg;
2345 ea.usefilter = TRUE;
2346 }
2347 }
2348
2349 if (ea.cmdidx == CMD_read)
2350 {
2351 if (ea.forceit)
2352 {
2353 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2354 ea.forceit = FALSE;
2355 }
2356 else if (*ea.arg == '!') /* :r !filter */
2357 {
2358 ++ea.arg;
2359 ea.usefilter = TRUE;
2360 }
2361 }
2362
2363 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2364 {
2365 ea.amount = 1;
2366 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2367 {
2368 ++ea.arg;
2369 ++ea.amount;
2370 }
2371 ea.arg = skipwhite(ea.arg);
2372 }
2373
2374 /*
2375 * Check for "+command" argument, before checking for next command.
2376 * Don't do this for ":read !cmd" and ":write !cmd".
2377 */
2378 if ((ea.argt & EDITCMD) && !ea.usefilter)
2379 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2380
2381 /*
2382 * Check for '|' to separate commands and '"' to start comments.
2383 * Don't do this for ":read !cmd" and ":write !cmd".
2384 */
2385 if ((ea.argt & TRLBAR) && !ea.usefilter)
2386 separate_nextcmd(&ea);
2387
2388 /*
2389 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2391 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 else if (ea.cmdidx == CMD_bang
2394 || ea.cmdidx == CMD_global
2395 || ea.cmdidx == CMD_vglobal
2396 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 for (p = ea.arg; *p; ++p)
2399 {
2400 /* Remove one backslash before a newline, so that it's possible to
2401 * pass a newline to the shell and also a newline that is preceded
2402 * with a backslash. This makes it impossible to end a shell
2403 * command in a backslash, but that doesn't appear useful.
2404 * Halving the number of backslashes is incompatible with previous
2405 * versions. */
2406 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002407 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 else if (*p == '\n')
2409 {
2410 ea.nextcmd = p + 1;
2411 *p = NUL;
2412 break;
2413 }
2414 }
2415 }
2416
2417 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2418 {
2419 ea.line1 = 1;
2420 ea.line2 = curbuf->b_ml.ml_line_count;
2421 }
2422
2423 /* accept numbered register only when no count allowed (:put) */
2424 if ( (ea.argt & REGSTR)
2425 && *ea.arg != NUL
2426#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* Do not allow register = for user commands */
2428 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
2430 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2431 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002432#ifndef FEAT_CLIPBOARD
2433 /* check these explicitly for a more specific error message */
2434 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002436 errormsg = (char_u *)_(e_invalidreg);
2437 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002440 if (
2441#ifdef FEAT_USR_CMDS
2442 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2443 && USER_CMDIDX(ea.cmdidx)))
2444#else
2445 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2446#endif
2447 )
2448 {
2449 ea.regname = *ea.arg++;
2450#ifdef FEAT_EVAL
2451 /* for '=' register: accept the rest of the line as an expression */
2452 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2453 {
2454 set_expr_line(vim_strsave(ea.arg));
2455 ea.arg += STRLEN(ea.arg);
2456 }
2457#endif
2458 ea.arg = skipwhite(ea.arg);
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461
2462 /*
2463 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2464 * count, it's a buffer name.
2465 */
2466 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2467 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2468 || vim_iswhite(*p)))
2469 {
2470 n = getdigits(&ea.arg);
2471 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002472 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 errormsg = (char_u *)_(e_zerocount);
2475 goto doend;
2476 }
2477 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2478 {
2479 ea.line2 = n;
2480 if (ea.addr_count == 0)
2481 ea.addr_count = 1;
2482 }
2483 else
2484 {
2485 ea.line1 = ea.line2;
2486 ea.line2 += n - 1;
2487 ++ea.addr_count;
2488 /*
2489 * Be vi compatible: no error message for out of range.
2490 */
2491 if (ea.line2 > curbuf->b_ml.ml_line_count)
2492 ea.line2 = curbuf->b_ml.ml_line_count;
2493 }
2494 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002495
2496 /*
2497 * Check for flags: 'l', 'p' and '#'.
2498 */
2499 if (ea.argt & EXFLAGS)
2500 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002502 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002503 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 errormsg = (char_u *)_(e_trailing);
2506 goto doend;
2507 }
2508
2509 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2510 {
2511 errormsg = (char_u *)_(e_argreq);
2512 goto doend;
2513 }
2514
2515#ifdef FEAT_EVAL
2516 /*
2517 * Skip the command when it's not going to be executed.
2518 * The commands like :if, :endif, etc. always need to be executed.
2519 * Also make an exception for commands that handle a trailing command
2520 * themselves.
2521 */
2522 if (ea.skip)
2523 {
2524 switch (ea.cmdidx)
2525 {
2526 /* commands that need evaluation */
2527 case CMD_while:
2528 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002529 case CMD_for:
2530 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 case CMD_if:
2532 case CMD_elseif:
2533 case CMD_else:
2534 case CMD_endif:
2535 case CMD_try:
2536 case CMD_catch:
2537 case CMD_finally:
2538 case CMD_endtry:
2539 case CMD_function:
2540 break;
2541
2542 /* Commands that handle '|' themselves. Check: A command should
2543 * either have the TRLBAR flag, appear in this list or appear in
2544 * the list at ":help :bar". */
2545 case CMD_aboveleft:
2546 case CMD_and:
2547 case CMD_belowright:
2548 case CMD_botright:
2549 case CMD_browse:
2550 case CMD_call:
2551 case CMD_confirm:
2552 case CMD_delfunction:
2553 case CMD_djump:
2554 case CMD_dlist:
2555 case CMD_dsearch:
2556 case CMD_dsplit:
2557 case CMD_echo:
2558 case CMD_echoerr:
2559 case CMD_echomsg:
2560 case CMD_echon:
2561 case CMD_execute:
2562 case CMD_help:
2563 case CMD_hide:
2564 case CMD_ijump:
2565 case CMD_ilist:
2566 case CMD_isearch:
2567 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002568 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 case CMD_keepjumps:
2570 case CMD_keepmarks:
2571 case CMD_leftabove:
2572 case CMD_let:
2573 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002574 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002576 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 case CMD_perl:
2578 case CMD_psearch:
2579 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002580 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002581 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_return:
2583 case CMD_rightbelow:
2584 case CMD_ruby:
2585 case CMD_silent:
2586 case CMD_smagic:
2587 case CMD_snomagic:
2588 case CMD_substitute:
2589 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002590 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 case CMD_tcl:
2592 case CMD_throw:
2593 case CMD_tilde:
2594 case CMD_topleft:
2595 case CMD_unlet:
2596 case CMD_verbose:
2597 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002598 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600
2601 default: goto doend;
2602 }
2603 }
2604#endif
2605
2606 if (ea.argt & XFILE)
2607 {
2608 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2609 goto doend;
2610 }
2611
2612#ifdef FEAT_LISTCMDS
2613 /*
2614 * Accept buffer name. Cannot be used at the same time with a buffer
2615 * number. Don't do this for a user command.
2616 */
2617 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2618# ifdef FEAT_USR_CMDS
2619 && !USER_CMDIDX(ea.cmdidx)
2620# endif
2621 )
2622 {
2623 /*
2624 * :bdelete, :bwipeout and :bunload take several arguments, separated
2625 * by spaces: find next space (skipping over escaped characters).
2626 * The others take one argument: ignore trailing spaces.
2627 */
2628 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2629 || ea.cmdidx == CMD_bunload)
2630 p = skiptowhite_esc(ea.arg);
2631 else
2632 {
2633 p = ea.arg + STRLEN(ea.arg);
2634 while (p > ea.arg && vim_iswhite(p[-1]))
2635 --p;
2636 }
2637 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2638 if (ea.line2 < 0) /* failed */
2639 goto doend;
2640 ea.addr_count = 1;
2641 ea.arg = skipwhite(p);
2642 }
2643#endif
2644
2645/*
2646 * 6. switch on command name
2647 *
2648 * The "ea" structure holds the arguments that can be used.
2649 */
2650 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 ea.cookie = cookie;
2653#ifdef FEAT_EVAL
2654 ea.cstack = cstack;
2655#endif
2656
2657#ifdef FEAT_USR_CMDS
2658 if (USER_CMDIDX(ea.cmdidx))
2659 {
2660 /*
2661 * Execute a user-defined command.
2662 */
2663 do_ucmd(&ea);
2664 }
2665 else
2666#endif
2667 {
2668 /*
2669 * Call the function to execute the command.
2670 */
2671 ea.errmsg = NULL;
2672 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2673 if (ea.errmsg != NULL)
2674 errormsg = (char_u *)_(ea.errmsg);
2675 }
2676
2677#ifdef FEAT_EVAL
2678 /*
2679 * If the command just executed called do_cmdline(), any throw or ":return"
2680 * or ":finish" encountered there must also check the cstack of the still
2681 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2682 * exception, or reanimate a returned function or finished script file and
2683 * return or finish it again.
2684 */
2685 if (need_rethrow)
2686 do_throw(cstack);
2687 else if (check_cstack)
2688 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002691 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 && current_func_returned())
2693 do_return(&ea, TRUE, FALSE, NULL);
2694 }
2695 need_rethrow = check_cstack = FALSE;
2696#endif
2697
2698doend:
2699 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2700 curwin->w_cursor.lnum = 1;
2701
2702 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2703 {
2704 if (sourcing)
2705 {
2706 if (errormsg != IObuff)
2707 {
2708 STRCPY(IObuff, errormsg);
2709 errormsg = IObuff;
2710 }
2711 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002712 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 emsg(errormsg);
2715 }
2716#ifdef FEAT_EVAL
2717 do_errthrow(cstack,
2718 (ea.cmdidx != CMD_SIZE
2719# ifdef FEAT_USR_CMDS
2720 && !USER_CMDIDX(ea.cmdidx)
2721# endif
2722 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2723#endif
2724
2725 if (verbose_save >= 0)
2726 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002727#ifdef FEAT_AUTOCMD
2728 if (cmdmod.save_ei != NULL)
2729 {
2730 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002731 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2732 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002733 free_string_option(cmdmod.save_ei);
2734 }
2735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 cmdmod = save_cmdmod;
2738
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002739 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 /* messages could be enabled for a serious error, need to check if the
2742 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002743 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002744 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 emsg_silent -= did_esilent;
2746 if (emsg_silent < 0)
2747 emsg_silent = 0;
2748 /* Restore msg_scroll, it's set by file I/O commands, even when no
2749 * message is actually displayed. */
2750 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002751
2752 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2753 * somewhere in the line. Put it back in the first column. */
2754 if (redirecting())
2755 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002758#ifdef HAVE_SANDBOX
2759 if (did_sandbox)
2760 --sandbox;
2761#endif
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2764 ea.nextcmd = NULL;
2765
2766#ifdef FEAT_EVAL
2767 --ex_nesting_level;
2768#endif
2769
2770 return ea.nextcmd;
2771}
2772#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002773 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#endif
2775
2776/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002777 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 * If there is a match advance "pp" to the argument and return TRUE.
2779 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002780 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002782 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 char *cmd; /* name of command */
2784 int len; /* required length */
2785{
2786 int i;
2787
2788 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002789 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 break;
2791 if (i >= len && !isalpha((*pp)[i]))
2792 {
2793 *pp = skipwhite(*pp + i);
2794 return TRUE;
2795 }
2796 return FALSE;
2797}
2798
2799/*
2800 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002801 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002803 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 * Returns NULL for an ambiguous user command.
2805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 static char_u *
2807find_command(eap, full)
2808 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002809 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 int len;
2812 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002813 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 /*
2816 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002817 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 * - the 'k' command can directly be followed by any character.
2819 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2820 * but :sre[wind] is another command, as are :scrip[tnames],
2821 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002822 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 p = eap->cmd;
2825 if (*p == 'k')
2826 {
2827 eap->cmdidx = CMD_k;
2828 ++p;
2829 }
2830 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002831 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2832 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 || p[1] == 'g'
2834 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2835 || p[1] == 'I'
2836 || (p[1] == 'r' && p[2] != 'e')))
2837 {
2838 eap->cmdidx = CMD_substitute;
2839 ++p;
2840 }
2841 else
2842 {
2843 while (ASCII_ISALPHA(*p))
2844 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002845 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002846 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002847 while (ASCII_ISALNUM(*p))
2848 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 /* check for non-alpha command */
2851 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2852 ++p;
2853 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002854 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2855 {
2856 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2857 * :delete with the 'l' flag. Same for 'p'. */
2858 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002859 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002860 break;
2861 if (i == len - 1)
2862 {
2863 --len;
2864 if (p[-1] == 'l')
2865 eap->flags |= EXFLAG_LIST;
2866 else
2867 eap->flags |= EXFLAG_PRINT;
2868 }
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 if (ASCII_ISLOWER(*eap->cmd))
2872 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2873 else
2874 eap->cmdidx = cmdidxs[26];
2875
2876 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2877 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2878 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2879 (size_t)len) == 0)
2880 {
2881#ifdef FEAT_EVAL
2882 if (full != NULL
2883 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2884 *full = TRUE;
2885#endif
2886 break;
2887 }
2888
2889#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002890 /* Look for a user defined command as a last resort. Let ":Print" be
2891 * overruled by a user defined command. */
2892 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2893 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002895 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 while (ASCII_ISALNUM(*p))
2897 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002898 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002901 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 eap->cmdidx = CMD_SIZE;
2903 }
2904
2905 return p;
2906}
2907
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002908#ifdef FEAT_USR_CMDS
2909/*
2910 * Search for a user command that matches "eap->cmd".
2911 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2912 * Return a pointer to just after the command.
2913 * Return NULL if there is no matching command.
2914 */
2915 static char_u *
2916find_ucmd(eap, p, full, xp, compl)
2917 exarg_T *eap;
2918 char_u *p; /* end of the command (possibly including count) */
2919 int *full; /* set to TRUE for a full match */
2920 expand_T *xp; /* used for completion, NULL otherwise */
2921 int *compl; /* completion flags or NULL */
2922{
2923 int len = (int)(p - eap->cmd);
2924 int j, k, matchlen = 0;
2925 ucmd_T *uc;
2926 int found = FALSE;
2927 int possible = FALSE;
2928 char_u *cp, *np; /* Point into typed cmd and test name */
2929 garray_T *gap;
2930 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2931 only full match global is accepted. */
2932
2933 /*
2934 * Look for buffer-local user commands first, then global ones.
2935 */
2936 gap = &curbuf->b_ucmds;
2937 for (;;)
2938 {
2939 for (j = 0; j < gap->ga_len; ++j)
2940 {
2941 uc = USER_CMD_GA(gap, j);
2942 cp = eap->cmd;
2943 np = uc->uc_name;
2944 k = 0;
2945 while (k < len && *np != NUL && *cp++ == *np++)
2946 k++;
2947 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2948 {
2949 /* If finding a second match, the command is ambiguous. But
2950 * not if a buffer-local command wasn't a full match and a
2951 * global command is a full match. */
2952 if (k == len && found && *np != NUL)
2953 {
2954 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002956 amb_local = TRUE;
2957 }
2958
2959 if (!found || (k == len && *np == NUL))
2960 {
2961 /* If we matched up to a digit, then there could
2962 * be another command including the digit that we
2963 * should use instead.
2964 */
2965 if (k == len)
2966 found = TRUE;
2967 else
2968 possible = TRUE;
2969
2970 if (gap == &ucmds)
2971 eap->cmdidx = CMD_USER;
2972 else
2973 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002974 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002975 eap->useridx = j;
2976
2977# ifdef FEAT_CMDL_COMPL
2978 if (compl != NULL)
2979 *compl = uc->uc_compl;
2980# ifdef FEAT_EVAL
2981 if (xp != NULL)
2982 {
2983 xp->xp_arg = uc->uc_compl_arg;
2984 xp->xp_scriptID = uc->uc_scriptID;
2985 }
2986# endif
2987# endif
2988 /* Do not search for further abbreviations
2989 * if this is an exact match. */
2990 matchlen = k;
2991 if (k == len && *np == NUL)
2992 {
2993 if (full != NULL)
2994 *full = TRUE;
2995 amb_local = FALSE;
2996 break;
2997 }
2998 }
2999 }
3000 }
3001
3002 /* Stop if we found a full match or searched all. */
3003 if (j < gap->ga_len || gap == &ucmds)
3004 break;
3005 gap = &ucmds;
3006 }
3007
3008 /* Only found ambiguous matches. */
3009 if (amb_local)
3010 {
3011 if (xp != NULL)
3012 xp->xp_context = EXPAND_UNSUCCESSFUL;
3013 return NULL;
3014 }
3015
3016 /* The match we found may be followed immediately by a number. Move "p"
3017 * back to point to it. */
3018 if (found || possible)
3019 return p + (matchlen - len);
3020 return p;
3021}
3022#endif
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003025static struct cmdmod
3026{
3027 char *name;
3028 int minlen;
3029 int has_count; /* :123verbose :3tab */
3030} cmdmods[] = {
3031 {"aboveleft", 3, FALSE},
3032 {"belowright", 3, FALSE},
3033 {"botright", 2, FALSE},
3034 {"browse", 3, FALSE},
3035 {"confirm", 4, FALSE},
3036 {"hide", 3, FALSE},
3037 {"keepalt", 5, FALSE},
3038 {"keepjumps", 5, FALSE},
3039 {"keepmarks", 3, FALSE},
3040 {"leftabove", 5, FALSE},
3041 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003042 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003043 {"rightbelow", 6, FALSE},
3044 {"sandbox", 3, FALSE},
3045 {"silent", 3, FALSE},
3046 {"tab", 3, TRUE},
3047 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003048 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003049 {"verbose", 4, TRUE},
3050 {"vertical", 4, FALSE},
3051};
3052
3053/*
3054 * Return length of a command modifier (including optional count).
3055 * Return zero when it's not a modifier.
3056 */
3057 int
3058modifier_len(cmd)
3059 char_u *cmd;
3060{
3061 int i, j;
3062 char_u *p = cmd;
3063
3064 if (VIM_ISDIGIT(*cmd))
3065 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003066 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003067 {
3068 for (j = 0; p[j] != NUL; ++j)
3069 if (p[j] != cmdmods[i].name[j])
3070 break;
3071 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3072 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003073 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003074 }
3075 return 0;
3076}
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078/*
3079 * Return > 0 if an Ex command "name" exists.
3080 * Return 2 if there is an exact match.
3081 * Return 3 if there is an ambiguous match.
3082 */
3083 int
3084cmd_exists(name)
3085 char_u *name;
3086{
3087 exarg_T ea;
3088 int full = FALSE;
3089 int i;
3090 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003091 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003094 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 for (j = 0; name[j] != NUL; ++j)
3097 if (name[j] != cmdmods[i].name[j])
3098 break;
3099 if (name[j] == NUL && j >= cmdmods[i].minlen)
3100 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3101 }
3102
Bram Moolenaara9587612006-05-04 21:47:50 +00003103 /* Check built-in commands and user defined commands.
3104 * For ":2match" and ":3match" we need to skip the number. */
3105 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003107 p = find_command(&ea, &full);
3108 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003110 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3111 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003112 if (*skipwhite(p) != NUL)
3113 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3115}
3116#endif
3117
3118/*
3119 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3120 * we don't need/want deleted. Maybe this could be done better if we didn't
3121 * repeat all this stuff. The only problem is that they may not stay
3122 * perfectly compatible with each other, but then the command line syntax
3123 * probably won't change that much -- webb.
3124 */
3125 char_u *
3126set_one_cmd_context(xp, buff)
3127 expand_T *xp;
3128 char_u *buff; /* buffer for command string */
3129{
3130 char_u *p;
3131 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 int len = 0;
3133 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3135 int compl = EXPAND_NOTHING;
3136#endif
3137#ifdef FEAT_CMDL_COMPL
3138 int delim;
3139#endif
3140 int forceit = FALSE;
3141 int usefilter = FALSE; /* filter instead of file name */
3142
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003143 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 xp->xp_pattern = buff;
3145 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148/*
3149 * 2. skip comment lines and leading space, colons or bars
3150 */
3151 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3152 ;
3153 xp->xp_pattern = cmd;
3154
3155 if (*cmd == NUL)
3156 return NULL;
3157 if (*cmd == '"') /* ignore comment lines */
3158 {
3159 xp->xp_context = EXPAND_NOTHING;
3160 return NULL;
3161 }
3162
3163/*
3164 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3165 */
3166 cmd = skip_range(cmd, &xp->xp_context);
3167
3168/*
3169 * 4. parse command
3170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 xp->xp_pattern = cmd;
3172 if (*cmd == NUL)
3173 return NULL;
3174 if (*cmd == '"')
3175 {
3176 xp->xp_context = EXPAND_NOTHING;
3177 return NULL;
3178 }
3179
3180 if (*cmd == '|' || *cmd == '\n')
3181 return cmd + 1; /* There's another command */
3182
3183 /*
3184 * Isolate the command and search for it in the command table.
3185 * Exceptions:
3186 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003187 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3189 */
3190 if (*cmd == 'k' && cmd[1] != 'e')
3191 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 p = cmd + 1;
3194 }
3195 else
3196 {
3197 p = cmd;
3198 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3199 ++p;
3200 /* check for non-alpha command */
3201 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3202 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
3207 xp->xp_context = EXPAND_UNSUCCESSFUL;
3208 return NULL;
3209 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003211 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3212 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3213 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 break;
3215
3216#ifdef FEAT_USR_CMDS
3217 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3219 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#endif
3221 }
3222
3223 /*
3224 * If the cursor is touching the command, and it ends in an alpha-numeric
3225 * character, complete the command name.
3226 */
3227 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3228 return NULL;
3229
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3233 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 p = cmd + 1;
3236 }
3237#ifdef FEAT_USR_CMDS
3238 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3239 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 ea.cmd = cmd;
3241 p = find_ucmd(&ea, p, NULL, xp,
3242# if defined(FEAT_CMDL_COMPL)
3243 &compl
3244# else
3245 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003248 if (p == NULL)
3249 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251#endif
3252 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
3255 /* Not still touching the command and it was an illegal one */
3256 xp->xp_context = EXPAND_UNSUCCESSFUL;
3257 return NULL;
3258 }
3259
3260 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3261
3262 if (*p == '!') /* forced commands */
3263 {
3264 forceit = TRUE;
3265 ++p;
3266 }
3267
3268/*
3269 * 5. parse arguments
3270 */
3271#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003274 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 arg = skipwhite(p);
3277
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 if (*arg == '>') /* append */
3281 {
3282 if (*++arg == '>')
3283 ++arg;
3284 arg = skipwhite(arg);
3285 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003286 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 ++arg;
3289 usefilter = TRUE;
3290 }
3291 }
3292
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
3295 usefilter = forceit; /* :r! filter if forced */
3296 if (*arg == '!') /* :r !filter */
3297 {
3298 ++arg;
3299 usefilter = TRUE;
3300 }
3301 }
3302
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 while (*arg == *cmd) /* allow any number of '>' or '<' */
3306 ++arg;
3307 arg = skipwhite(arg);
3308 }
3309
3310 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 /* Check if we're in the +command */
3314 p = arg + 1;
3315 arg = skip_cmd_arg(arg, FALSE);
3316
3317 /* Still touching the command after '+'? */
3318 if (*arg == NUL)
3319 return p;
3320
3321 /* Skip space(s) after +command to get to the real argument */
3322 arg = skipwhite(arg);
3323 }
3324
3325 /*
3326 * Check for '|' to separate commands and '"' to start comments.
3327 * Don't do this for ":read !cmd" and ":write !cmd".
3328 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
3331 p = arg;
3332 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 p += 2;
3335 while (*p)
3336 {
3337 if (*p == Ctrl_V)
3338 {
3339 if (p[1] != NUL)
3340 ++p;
3341 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 || *p == '|' || *p == '\n')
3344 {
3345 if (*(p - 1) != '\\')
3346 {
3347 if (*p == '|' || *p == '\n')
3348 return p + 1;
3349 return NULL; /* It's a comment */
3350 }
3351 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003352 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 }
3355
3356 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003357 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 vim_strchr((char_u *)"|\"", *arg) == NULL)
3359 return NULL;
3360
3361 /* Find start of last argument (argument just before cursor): */
3362 p = buff + STRLEN(buff);
3363 while (p != arg && *p != ' ' && *p != TAB)
3364 p--;
3365 if (*p == ' ' || *p == TAB)
3366 p++;
3367 xp->xp_pattern = p;
3368
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003371 int c;
3372 int in_quote = FALSE;
3373 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 /*
3376 * Allow spaces within back-quotes to count as part of the argument
3377 * being expanded.
3378 */
3379 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003380 p = xp->xp_pattern;
3381 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003383#ifdef FEAT_MBYTE
3384 if (has_mbyte)
3385 c = mb_ptr2char(p);
3386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003388 c = *p;
3389 if (c == '\\' && p[1] != NUL)
3390 ++p;
3391 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 if (!in_quote)
3394 {
3395 xp->xp_pattern = p;
3396 bow = p + 1;
3397 }
3398 in_quote = !in_quote;
3399 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003400 /* An argument can contain just about everything, except
3401 * characters that end the command and white space. */
3402 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003403#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003404 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003405#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003406 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003407 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003408 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003409 while (*p != NUL)
3410 {
3411#ifdef FEAT_MBYTE
3412 if (has_mbyte)
3413 c = mb_ptr2char(p);
3414 else
3415#endif
3416 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003417 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003418 break;
3419#ifdef FEAT_MBYTE
3420 if (has_mbyte)
3421 len = (*mb_ptr2len)(p);
3422 else
3423#endif
3424 len = 1;
3425 mb_ptr_adv(p);
3426 }
3427 if (in_quote)
3428 bow = p;
3429 else
3430 xp->xp_pattern = p;
3431 p -= len;
3432 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003433 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435
3436 /*
3437 * If we are still inside the quotes, and we passed a space, just
3438 * expand from there.
3439 */
3440 if (bow != NULL && in_quote)
3441 xp->xp_pattern = bow;
3442 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003443
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003444 /* For a shell command more chars need to be escaped. */
3445 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003446 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003447#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003448 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003450 /* When still after the command name expand executables. */
3451 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003452 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 /* Check for environment variable */
3456 if (*xp->xp_pattern == '$'
3457#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3458 || *xp->xp_pattern == '%'
3459#endif
3460 )
3461 {
3462 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3463 if (!vim_isIDc(*p))
3464 break;
3465 if (*p == NUL)
3466 {
3467 xp->xp_context = EXPAND_ENV_VARS;
3468 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003469#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3470 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003471 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003472 compl = EXPAND_ENV_VARS;
3473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 }
3476 }
3477
3478/*
3479 * 6. switch on command name
3480 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003481 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003483 case CMD_find:
3484 case CMD_sfind:
3485 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003486 if (xp->xp_context == EXPAND_FILES)
3487 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 case CMD_cd:
3490 case CMD_chdir:
3491 case CMD_lcd:
3492 case CMD_lchdir:
3493 if (xp->xp_context == EXPAND_FILES)
3494 xp->xp_context = EXPAND_DIRECTORIES;
3495 break;
3496 case CMD_help:
3497 xp->xp_context = EXPAND_HELP;
3498 xp->xp_pattern = arg;
3499 break;
3500
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003501 /* Command modifiers: return the argument.
3502 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003504 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 case CMD_belowright:
3506 case CMD_botright:
3507 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003508 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003510 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 case CMD_folddoclosed:
3512 case CMD_folddoopen:
3513 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003514 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 case CMD_keepjumps:
3516 case CMD_keepmarks:
3517 case CMD_leftabove:
3518 case CMD_lockmarks:
3519 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003520 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003522 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 case CMD_topleft:
3524 case CMD_verbose:
3525 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003526 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 return arg;
3528
Bram Moolenaar4f688582007-07-24 12:34:30 +00003529#ifdef FEAT_CMDL_COMPL
3530# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 case CMD_match:
3532 if (*arg == NUL || !ends_excmd(*arg))
3533 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003534 /* also complete "None" */
3535 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 arg = skipwhite(skiptowhite(arg));
3537 if (*arg != NUL)
3538 {
3539 xp->xp_context = EXPAND_NOTHING;
3540 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3541 }
3542 }
3543 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546/*
3547 * All completion for the +cmdline_compl feature goes here.
3548 */
3549
3550# ifdef FEAT_USR_CMDS
3551 case CMD_command:
3552 /* Check for attributes */
3553 while (*arg == '-')
3554 {
3555 arg++; /* Skip "-" */
3556 p = skiptowhite(arg);
3557 if (*p == NUL)
3558 {
3559 /* Cursor is still in the attribute */
3560 p = vim_strchr(arg, '=');
3561 if (p == NULL)
3562 {
3563 /* No "=", so complete attribute names */
3564 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3565 xp->xp_pattern = arg;
3566 return NULL;
3567 }
3568
3569 /* For the -complete and -nargs attributes, we complete
3570 * their arguments as well.
3571 */
3572 if (STRNICMP(arg, "complete", p - arg) == 0)
3573 {
3574 xp->xp_context = EXPAND_USER_COMPLETE;
3575 xp->xp_pattern = p + 1;
3576 return NULL;
3577 }
3578 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3579 {
3580 xp->xp_context = EXPAND_USER_NARGS;
3581 xp->xp_pattern = p + 1;
3582 return NULL;
3583 }
3584 return NULL;
3585 }
3586 arg = skipwhite(p);
3587 }
3588
3589 /* After the attributes comes the new command name */
3590 p = skiptowhite(arg);
3591 if (*p == NUL)
3592 {
3593 xp->xp_context = EXPAND_USER_COMMANDS;
3594 xp->xp_pattern = arg;
3595 break;
3596 }
3597
3598 /* And finally comes a normal command */
3599 return skipwhite(p);
3600
3601 case CMD_delcommand:
3602 xp->xp_context = EXPAND_USER_COMMANDS;
3603 xp->xp_pattern = arg;
3604 break;
3605# endif
3606
3607 case CMD_global:
3608 case CMD_vglobal:
3609 delim = *arg; /* get the delimiter */
3610 if (delim)
3611 ++arg; /* skip delimiter if there is one */
3612
3613 while (arg[0] != NUL && arg[0] != delim)
3614 {
3615 if (arg[0] == '\\' && arg[1] != NUL)
3616 ++arg;
3617 ++arg;
3618 }
3619 if (arg[0] != NUL)
3620 return arg + 1;
3621 break;
3622 case CMD_and:
3623 case CMD_substitute:
3624 delim = *arg;
3625 if (delim)
3626 {
3627 /* skip "from" part */
3628 ++arg;
3629 arg = skip_regexp(arg, delim, p_magic, NULL);
3630 }
3631 /* skip "to" part */
3632 while (arg[0] != NUL && arg[0] != delim)
3633 {
3634 if (arg[0] == '\\' && arg[1] != NUL)
3635 ++arg;
3636 ++arg;
3637 }
3638 if (arg[0] != NUL) /* skip delimiter */
3639 ++arg;
3640 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3641 ++arg;
3642 if (arg[0] != NUL)
3643 return arg;
3644 break;
3645 case CMD_isearch:
3646 case CMD_dsearch:
3647 case CMD_ilist:
3648 case CMD_dlist:
3649 case CMD_ijump:
3650 case CMD_psearch:
3651 case CMD_djump:
3652 case CMD_isplit:
3653 case CMD_dsplit:
3654 arg = skipwhite(skipdigits(arg)); /* skip count */
3655 if (*arg == '/') /* Match regexp, not just whole words */
3656 {
3657 for (++arg; *arg && *arg != '/'; arg++)
3658 if (*arg == '\\' && arg[1] != NUL)
3659 arg++;
3660 if (*arg)
3661 {
3662 arg = skipwhite(arg + 1);
3663
3664 /* Check for trailing illegal characters */
3665 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3666 xp->xp_context = EXPAND_NOTHING;
3667 else
3668 return arg;
3669 }
3670 }
3671 break;
3672#ifdef FEAT_AUTOCMD
3673 case CMD_autocmd:
3674 return set_context_in_autocmd(xp, arg, FALSE);
3675
3676 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003677 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 return set_context_in_autocmd(xp, arg, TRUE);
3679#endif
3680 case CMD_set:
3681 set_context_in_set_cmd(xp, arg, 0);
3682 break;
3683 case CMD_setglobal:
3684 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3685 break;
3686 case CMD_setlocal:
3687 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3688 break;
3689 case CMD_tag:
3690 case CMD_stag:
3691 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003692 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 case CMD_tselect:
3694 case CMD_stselect:
3695 case CMD_ptselect:
3696 case CMD_tjump:
3697 case CMD_stjump:
3698 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003699 if (*p_wop != NUL)
3700 xp->xp_context = EXPAND_TAGS_LISTFILES;
3701 else
3702 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 xp->xp_pattern = arg;
3704 break;
3705 case CMD_augroup:
3706 xp->xp_context = EXPAND_AUGROUP;
3707 xp->xp_pattern = arg;
3708 break;
3709#ifdef FEAT_SYN_HL
3710 case CMD_syntax:
3711 set_context_in_syntax_cmd(xp, arg);
3712 break;
3713#endif
3714#ifdef FEAT_EVAL
3715 case CMD_let:
3716 case CMD_if:
3717 case CMD_elseif:
3718 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003719 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 case CMD_echo:
3721 case CMD_echon:
3722 case CMD_execute:
3723 case CMD_echomsg:
3724 case CMD_echoerr:
3725 case CMD_call:
3726 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003727 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 break;
3729
3730 case CMD_unlet:
3731 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3732 arg = xp->xp_pattern + 1;
3733 xp->xp_context = EXPAND_USER_VARS;
3734 xp->xp_pattern = arg;
3735 break;
3736
3737 case CMD_function:
3738 case CMD_delfunction:
3739 xp->xp_context = EXPAND_USER_FUNC;
3740 xp->xp_pattern = arg;
3741 break;
3742
3743 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003744 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 break;
3746#endif
3747 case CMD_highlight:
3748 set_context_in_highlight_cmd(xp, arg);
3749 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003750#ifdef FEAT_CSCOPE
3751 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003752 case CMD_lcscope:
3753 case CMD_scscope:
3754 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003755 break;
3756#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003757#ifdef FEAT_SIGNS
3758 case CMD_sign:
3759 set_context_in_sign_cmd(xp, arg);
3760 break;
3761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#ifdef FEAT_LISTCMDS
3763 case CMD_bdelete:
3764 case CMD_bwipeout:
3765 case CMD_bunload:
3766 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3767 arg = xp->xp_pattern + 1;
3768 /*FALLTHROUGH*/
3769 case CMD_buffer:
3770 case CMD_sbuffer:
3771 case CMD_checktime:
3772 xp->xp_context = EXPAND_BUFFERS;
3773 xp->xp_pattern = arg;
3774 break;
3775#endif
3776#ifdef FEAT_USR_CMDS
3777 case CMD_USER:
3778 case CMD_USER_BUF:
3779 if (compl != EXPAND_NOTHING)
3780 {
3781 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003782 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
3784# ifdef FEAT_MENU
3785 if (compl == EXPAND_MENUS)
3786 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3787# endif
3788 if (compl == EXPAND_COMMANDS)
3789 return arg;
3790 if (compl == EXPAND_MAPPINGS)
3791 return set_context_in_map_cmd(xp, (char_u *)"map",
3792 arg, forceit, FALSE, FALSE, CMD_map);
3793 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3794 arg = xp->xp_pattern + 1;
3795 xp->xp_pattern = arg;
3796 }
3797 xp->xp_context = compl;
3798 }
3799 break;
3800#endif
3801 case CMD_map: case CMD_noremap:
3802 case CMD_nmap: case CMD_nnoremap:
3803 case CMD_vmap: case CMD_vnoremap:
3804 case CMD_omap: case CMD_onoremap:
3805 case CMD_imap: case CMD_inoremap:
3806 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003807 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003809 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 case CMD_unmap:
3811 case CMD_nunmap:
3812 case CMD_vunmap:
3813 case CMD_ounmap:
3814 case CMD_iunmap:
3815 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003816 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003818 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case CMD_abbreviate: case CMD_noreabbrev:
3820 case CMD_cabbrev: case CMD_cnoreabbrev:
3821 case CMD_iabbrev: case CMD_inoreabbrev:
3822 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003823 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_unabbreviate:
3825 case CMD_cunabbrev:
3826 case CMD_iunabbrev:
3827 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003828 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#ifdef FEAT_MENU
3830 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3831 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3832 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3833 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3834 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3835 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3836 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3837 case CMD_tmenu: case CMD_tunmenu:
3838 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3839 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3840#endif
3841
3842 case CMD_colorscheme:
3843 xp->xp_context = EXPAND_COLORS;
3844 xp->xp_pattern = arg;
3845 break;
3846
3847 case CMD_compiler:
3848 xp->xp_context = EXPAND_COMPILER;
3849 xp->xp_pattern = arg;
3850 break;
3851
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003852 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003853 xp->xp_context = EXPAND_OWNSYNTAX;
3854 xp->xp_pattern = arg;
3855 break;
3856
3857 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003858 xp->xp_context = EXPAND_FILETYPE;
3859 xp->xp_pattern = arg;
3860 break;
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3863 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3864 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003865 p = skiptowhite(arg);
3866 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868 xp->xp_context = EXPAND_LANGUAGE;
3869 xp->xp_pattern = arg;
3870 }
3871 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003872 {
3873 if ( STRNCMP(arg, "messages", p - arg) == 0
3874 || STRNCMP(arg, "ctype", p - arg) == 0
3875 || STRNCMP(arg, "time", p - arg) == 0)
3876 {
3877 xp->xp_context = EXPAND_LOCALES;
3878 xp->xp_pattern = skipwhite(p);
3879 }
3880 else
3881 xp->xp_context = EXPAND_NOTHING;
3882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 break;
3884#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003885#if defined(FEAT_PROFILE)
3886 case CMD_profile:
3887 set_context_in_profile_cmd(xp, arg);
3888 break;
3889#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003890 case CMD_behave:
3891 xp->xp_context = EXPAND_BEHAVE;
3892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
3894#endif /* FEAT_CMDL_COMPL */
3895
3896 default:
3897 break;
3898 }
3899 return NULL;
3900}
3901
3902/*
3903 * skip a range specifier of the form: addr [,addr] [;addr] ..
3904 *
3905 * Backslashed delimiters after / or ? will be skipped, and commands will
3906 * not be expanded between /'s and ?'s or after "'".
3907 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003908 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 * Returns the "cmd" pointer advanced to beyond the range.
3910 */
3911 char_u *
3912skip_range(cmd, ctx)
3913 char_u *cmd;
3914 int *ctx; /* pointer to xp_context or NULL */
3915{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003916 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaardf177f62005-02-22 08:39:57 +00003918 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920 if (*cmd == '\'')
3921 {
3922 if (*++cmd == NUL && ctx != NULL)
3923 *ctx = EXPAND_NOTHING;
3924 }
3925 else if (*cmd == '/' || *cmd == '?')
3926 {
3927 delim = *cmd++;
3928 while (*cmd != NUL && *cmd != delim)
3929 if (*cmd++ == '\\' && *cmd != NUL)
3930 ++cmd;
3931 if (*cmd == NUL && ctx != NULL)
3932 *ctx = EXPAND_NOTHING;
3933 }
3934 if (*cmd != NUL)
3935 ++cmd;
3936 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003937
3938 /* Skip ":" and white space. */
3939 while (*cmd == ':')
3940 cmd = skipwhite(cmd + 1);
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 return cmd;
3943}
3944
3945/*
3946 * get a single EX address
3947 *
3948 * Set ptr to the next character after the part that was interpreted.
3949 * Set ptr to NULL when an error is encountered.
3950 *
3951 * Return MAXLNUM when no Ex address was found.
3952 */
3953 static linenr_T
3954get_address(ptr, skip, to_other_file)
3955 char_u **ptr;
3956 int skip; /* only skip the address, don't use it */
3957 int to_other_file; /* flag: may jump to other file */
3958{
3959 int c;
3960 int i;
3961 long n;
3962 char_u *cmd;
3963 pos_T pos;
3964 pos_T *fp;
3965 linenr_T lnum;
3966
3967 cmd = skipwhite(*ptr);
3968 lnum = MAXLNUM;
3969 do
3970 {
3971 switch (*cmd)
3972 {
3973 case '.': /* '.' - Cursor position */
3974 ++cmd;
3975 lnum = curwin->w_cursor.lnum;
3976 break;
3977
3978 case '$': /* '$' - last line */
3979 ++cmd;
3980 lnum = curbuf->b_ml.ml_line_count;
3981 break;
3982
3983 case '\'': /* ''' - mark */
3984 if (*++cmd == NUL)
3985 {
3986 cmd = NULL;
3987 goto error;
3988 }
3989 if (skip)
3990 ++cmd;
3991 else
3992 {
3993 /* Only accept a mark in another file when it is
3994 * used by itself: ":'M". */
3995 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3996 ++cmd;
3997 if (fp == (pos_T *)-1)
3998 /* Jumped to another file. */
3999 lnum = curwin->w_cursor.lnum;
4000 else
4001 {
4002 if (check_mark(fp) == FAIL)
4003 {
4004 cmd = NULL;
4005 goto error;
4006 }
4007 lnum = fp->lnum;
4008 }
4009 }
4010 break;
4011
4012 case '/':
4013 case '?': /* '/' or '?' - search */
4014 c = *cmd++;
4015 if (skip) /* skip "/pat/" */
4016 {
4017 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4018 if (*cmd == c)
4019 ++cmd;
4020 }
4021 else
4022 {
4023 pos = curwin->w_cursor; /* save curwin->w_cursor */
4024 /*
4025 * When '/' or '?' follows another address, start
4026 * from there.
4027 */
4028 if (lnum != MAXLNUM)
4029 curwin->w_cursor.lnum = lnum;
4030 /*
4031 * Start a forward search at the end of the line.
4032 * Start a backward search at the start of the line.
4033 * This makes sure we never match in the current
4034 * line, and can match anywhere in the
4035 * next/previous line.
4036 */
4037 if (c == '/')
4038 curwin->w_cursor.col = MAXCOL;
4039 else
4040 curwin->w_cursor.col = 0;
4041 searchcmdlen = 0;
4042 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004043 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
4045 curwin->w_cursor = pos;
4046 cmd = NULL;
4047 goto error;
4048 }
4049 lnum = curwin->w_cursor.lnum;
4050 curwin->w_cursor = pos;
4051 /* adjust command string pointer */
4052 cmd += searchcmdlen;
4053 }
4054 break;
4055
4056 case '\\': /* "\?", "\/" or "\&", repeat search */
4057 ++cmd;
4058 if (*cmd == '&')
4059 i = RE_SUBST;
4060 else if (*cmd == '?' || *cmd == '/')
4061 i = RE_SEARCH;
4062 else
4063 {
4064 EMSG(_(e_backslash));
4065 cmd = NULL;
4066 goto error;
4067 }
4068
4069 if (!skip)
4070 {
4071 /*
4072 * When search follows another address, start from
4073 * there.
4074 */
4075 if (lnum != MAXLNUM)
4076 pos.lnum = lnum;
4077 else
4078 pos.lnum = curwin->w_cursor.lnum;
4079
4080 /*
4081 * Start the search just like for the above
4082 * do_search().
4083 */
4084 if (*cmd != '?')
4085 pos.col = MAXCOL;
4086 else
4087 pos.col = 0;
4088 if (searchit(curwin, curbuf, &pos,
4089 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004090 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004091 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 lnum = pos.lnum;
4093 else
4094 {
4095 cmd = NULL;
4096 goto error;
4097 }
4098 }
4099 ++cmd;
4100 break;
4101
4102 default:
4103 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4104 lnum = getdigits(&cmd);
4105 }
4106
4107 for (;;)
4108 {
4109 cmd = skipwhite(cmd);
4110 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4111 break;
4112
4113 if (lnum == MAXLNUM)
4114 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4115 if (VIM_ISDIGIT(*cmd))
4116 i = '+'; /* "number" is same as "+number" */
4117 else
4118 i = *cmd++;
4119 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4120 n = 1;
4121 else
4122 n = getdigits(&cmd);
4123 if (i == '-')
4124 lnum -= n;
4125 else
4126 lnum += n;
4127 }
4128 } while (*cmd == '/' || *cmd == '?');
4129
4130error:
4131 *ptr = cmd;
4132 return lnum;
4133}
4134
4135/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004136 * Get flags from an Ex command argument.
4137 */
4138 static void
4139get_flags(eap)
4140 exarg_T *eap;
4141{
4142 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4143 {
4144 if (*eap->arg == 'l')
4145 eap->flags |= EXFLAG_LIST;
4146 else if (*eap->arg == 'p')
4147 eap->flags |= EXFLAG_PRINT;
4148 else
4149 eap->flags |= EXFLAG_NR;
4150 eap->arg = skipwhite(eap->arg + 1);
4151 }
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Function called for command which is Not Implemented. NI!
4156 */
4157 void
4158ex_ni(eap)
4159 exarg_T *eap;
4160{
4161 if (!eap->skip)
4162 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4163}
4164
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004165#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166/*
4167 * Function called for script command which is Not Implemented. NI!
4168 * Skips over ":perl <<EOF" constructs.
4169 */
4170 static void
4171ex_script_ni(eap)
4172 exarg_T *eap;
4173{
4174 if (!eap->skip)
4175 ex_ni(eap);
4176 else
4177 vim_free(script_get(eap, eap->arg));
4178}
4179#endif
4180
4181/*
4182 * Check range in Ex command for validity.
4183 * Return NULL when valid, error message when invalid.
4184 */
4185 static char_u *
4186invalid_range(eap)
4187 exarg_T *eap;
4188{
4189 if ( eap->line1 < 0
4190 || eap->line2 < 0
4191 || eap->line1 > eap->line2
4192 || ((eap->argt & RANGE)
4193 && !(eap->argt & NOTADR)
4194 && eap->line2 > curbuf->b_ml.ml_line_count
4195#ifdef FEAT_DIFF
4196 + (eap->cmdidx == CMD_diffget)
4197#endif
4198 ))
4199 return (char_u *)_(e_invrange);
4200 return NULL;
4201}
4202
4203/*
4204 * Correct the range for zero line number, if required.
4205 */
4206 static void
4207correct_range(eap)
4208 exarg_T *eap;
4209{
4210 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4211 {
4212 if (eap->line1 == 0)
4213 eap->line1 = 1;
4214 if (eap->line2 == 0)
4215 eap->line2 = 1;
4216 }
4217}
4218
Bram Moolenaar748bf032005-02-02 23:04:36 +00004219#ifdef FEAT_QUICKFIX
4220static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4221
4222/*
4223 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4224 * pattern. Otherwise return eap->arg.
4225 */
4226 static char_u *
4227skip_grep_pat(eap)
4228 exarg_T *eap;
4229{
4230 char_u *p = eap->arg;
4231
Bram Moolenaara37420f2006-02-04 22:37:47 +00004232 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4233 || eap->cmdidx == CMD_vimgrepadd
4234 || eap->cmdidx == CMD_lvimgrepadd
4235 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004236 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004238 if (p == NULL)
4239 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004240 }
4241 return p;
4242}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004243
4244/*
4245 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4246 * in the command line, so that things like % get expanded.
4247 */
4248 static char_u *
4249replace_makeprg(eap, p, cmdlinep)
4250 exarg_T *eap;
4251 char_u *p;
4252 char_u **cmdlinep;
4253{
4254 char_u *new_cmdline;
4255 char_u *program;
4256 char_u *pos;
4257 char_u *ptr;
4258 int len;
4259 int i;
4260
4261 /*
4262 * Don't do it when ":vimgrep" is used for ":grep".
4263 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004264 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4265 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4266 || eap->cmdidx == CMD_grepadd
4267 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004268 && !grep_internal(eap->cmdidx))
4269 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004270 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4271 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004272 {
4273 if (*curbuf->b_p_gp == NUL)
4274 program = p_gp;
4275 else
4276 program = curbuf->b_p_gp;
4277 }
4278 else
4279 {
4280 if (*curbuf->b_p_mp == NUL)
4281 program = p_mp;
4282 else
4283 program = curbuf->b_p_mp;
4284 }
4285
4286 p = skipwhite(p);
4287
4288 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4289 {
4290 /* replace $* by given arguments */
4291 i = 1;
4292 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4293 ++i;
4294 len = (int)STRLEN(p);
4295 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4296 if (new_cmdline == NULL)
4297 return NULL; /* out of memory */
4298 ptr = new_cmdline;
4299 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4300 {
4301 i = (int)(pos - program);
4302 STRNCPY(ptr, program, i);
4303 STRCPY(ptr += i, p);
4304 ptr += len;
4305 program = pos + 2;
4306 }
4307 STRCPY(ptr, program);
4308 }
4309 else
4310 {
4311 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4312 if (new_cmdline == NULL)
4313 return NULL; /* out of memory */
4314 STRCPY(new_cmdline, program);
4315 STRCAT(new_cmdline, " ");
4316 STRCAT(new_cmdline, p);
4317 }
4318 msg_make(p);
4319
4320 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4321 vim_free(*cmdlinep);
4322 *cmdlinep = new_cmdline;
4323 p = new_cmdline;
4324 }
4325 return p;
4326}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004327#endif
4328
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329/*
4330 * Expand file name in Ex command argument.
4331 * Return FAIL for failure, OK otherwise.
4332 */
4333 int
4334expand_filename(eap, cmdlinep, errormsgp)
4335 exarg_T *eap;
4336 char_u **cmdlinep;
4337 char_u **errormsgp;
4338{
4339 int has_wildcards; /* need to expand wildcards */
4340 char_u *repl;
4341 int srclen;
4342 char_u *p;
4343 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004344 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar748bf032005-02-02 23:04:36 +00004346#ifdef FEAT_QUICKFIX
4347 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4348 p = skip_grep_pat(eap);
4349#else
4350 p = eap->arg;
4351#endif
4352
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 /*
4354 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4355 * the file name contains a wildcard it should not cause expanding.
4356 * (it will be expanded anyway if there is a wildcard before replacing).
4357 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004358 has_wildcards = mch_has_wildcard(p);
4359 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004361#ifdef FEAT_EVAL
4362 /* Skip over `=expr`, wildcards in it are not expanded. */
4363 if (p[0] == '`' && p[1] == '=')
4364 {
4365 p += 2;
4366 (void)skip_expr(&p);
4367 if (*p == '`')
4368 ++p;
4369 continue;
4370 }
4371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 /*
4373 * Quick check if this cannot be the start of a special string.
4374 * Also removes backslash before '%', '#' and '<'.
4375 */
4376 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4377 {
4378 ++p;
4379 continue;
4380 }
4381
4382 /*
4383 * Try to find a match at this position.
4384 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004385 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4386 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (*errormsgp != NULL) /* error detected */
4388 return FAIL;
4389 if (repl == NULL) /* no match found */
4390 {
4391 p += srclen;
4392 continue;
4393 }
4394
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004395 /* Wildcards won't be expanded below, the replacement is taken
4396 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4397 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4398 {
4399 char_u *l = repl;
4400
4401 repl = expand_env_save(repl);
4402 vim_free(l);
4403 }
4404
Bram Moolenaar63b92542007-03-27 14:57:09 +00004405 /* Need to escape white space et al. with a backslash.
4406 * Don't do this for:
4407 * - replacement that already has been escaped: "##"
4408 * - shell commands (may have to use quotes instead).
4409 * - non-unix systems when there is a single argument (spaces don't
4410 * separate arguments then).
4411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004413 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 && eap->cmdidx != CMD_bang
4415 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004416 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004418 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004420 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421#ifndef UNIX
4422 && !(eap->argt & NOSPC)
4423#endif
4424 )
4425 {
4426 char_u *l;
4427#ifdef BACKSLASH_IN_FILENAME
4428 /* Don't escape a backslash here, because rem_backslash() doesn't
4429 * remove it later. */
4430 static char_u *nobslash = (char_u *)" \t\"|";
4431# define ESCAPE_CHARS nobslash
4432#else
4433# define ESCAPE_CHARS escape_chars
4434#endif
4435
4436 for (l = repl; *l; ++l)
4437 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4438 {
4439 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4440 if (l != NULL)
4441 {
4442 vim_free(repl);
4443 repl = l;
4444 }
4445 break;
4446 }
4447 }
4448
4449 /* For a shell command a '!' must be escaped. */
4450 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004451 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 char_u *l;
4454
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004455 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if (l != NULL)
4457 {
4458 vim_free(repl);
4459 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004460 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (strstr((char *)p_sh, "sh") != NULL)
4462 {
4463 l = vim_strsave_escaped(repl, (char_u *)"!");
4464 if (l != NULL)
4465 {
4466 vim_free(repl);
4467 repl = l;
4468 }
4469 }
4470 }
4471 }
4472
4473 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4474 vim_free(repl);
4475 if (p == NULL)
4476 return FAIL;
4477 }
4478
4479 /*
4480 * One file argument: Expand wildcards.
4481 * Don't do this with ":r !command" or ":w !command".
4482 */
4483 if ((eap->argt & NOSPC) && !eap->usefilter)
4484 {
4485 /*
4486 * May do this twice:
4487 * 1. Replace environment variables.
4488 * 2. Replace any other wildcards, remove backslashes.
4489 */
4490 for (n = 1; n <= 2; ++n)
4491 {
4492 if (n == 2)
4493 {
4494#ifdef UNIX
4495 /*
4496 * Only for Unix we check for more than one file name.
4497 * For other systems spaces are considered to be part
4498 * of the file name.
4499 * Only check here if there is no wildcard, otherwise
4500 * ExpandOne() will check for errors. This allows
4501 * ":e `ls ve*.c`" on Unix.
4502 */
4503 if (!has_wildcards)
4504 for (p = eap->arg; *p; ++p)
4505 {
4506 /* skip escaped characters */
4507 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4508 ++p;
4509 else if (vim_iswhite(*p))
4510 {
4511 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4512 return FAIL;
4513 }
4514 }
4515#endif
4516
4517 /*
4518 * Halve the number of backslashes (this is Vi compatible).
4519 * For Unix and OS/2, when wildcards are expanded, this is
4520 * done by ExpandOne() below.
4521 */
4522#if defined(UNIX) || defined(OS2)
4523 if (!has_wildcards)
4524#endif
4525 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
4528 if (has_wildcards)
4529 {
4530 if (n == 1)
4531 {
4532 /*
4533 * First loop: May expand environment variables. This
4534 * can be done much faster with expand_env() than with
4535 * something else (e.g., calling a shell).
4536 * After expanding environment variables, check again
4537 * if there are still wildcards present.
4538 */
4539 if (vim_strchr(eap->arg, '$') != NULL
4540 || vim_strchr(eap->arg, '~') != NULL)
4541 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004542 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004543 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 has_wildcards = mch_has_wildcard(NameBuff);
4545 p = NameBuff;
4546 }
4547 else
4548 p = NULL;
4549 }
4550 else /* n == 2 */
4551 {
4552 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004553 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 ExpandInit(&xpc);
4556 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004557 if (p_wic)
4558 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004560 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (p == NULL)
4562 return FAIL;
4563 }
4564 if (p != NULL)
4565 {
4566 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4567 p, cmdlinep);
4568 if (n == 2) /* p came from ExpandOne() */
4569 vim_free(p);
4570 }
4571 }
4572 }
4573 }
4574 return OK;
4575}
4576
4577/*
4578 * Replace part of the command line, keeping eap->cmd, eap->arg and
4579 * eap->nextcmd correct.
4580 * "src" points to the part that is to be replaced, of length "srclen".
4581 * "repl" is the replacement string.
4582 * Returns a pointer to the character after the replaced string.
4583 * Returns NULL for failure.
4584 */
4585 static char_u *
4586repl_cmdline(eap, src, srclen, repl, cmdlinep)
4587 exarg_T *eap;
4588 char_u *src;
4589 int srclen;
4590 char_u *repl;
4591 char_u **cmdlinep;
4592{
4593 int len;
4594 int i;
4595 char_u *new_cmdline;
4596
4597 /*
4598 * The new command line is build in new_cmdline[].
4599 * First allocate it.
4600 * Careful: a "+cmd" argument may have been NUL terminated.
4601 */
4602 len = (int)STRLEN(repl);
4603 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004604 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4606 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4607 return NULL; /* out of memory! */
4608
4609 /*
4610 * Copy the stuff before the expanded part.
4611 * Copy the expanded stuff.
4612 * Copy what came after the expanded part.
4613 * Copy the next commands, if there are any.
4614 */
4615 i = (int)(src - *cmdlinep); /* length of part before match */
4616 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 mch_memmove(new_cmdline + i, repl, (size_t)len);
4619 i += len; /* remember the end of the string */
4620 STRCPY(new_cmdline + i, src + srclen);
4621 src = new_cmdline + i; /* remember where to continue */
4622
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004623 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
4625 i = (int)STRLEN(new_cmdline) + 1;
4626 STRCPY(new_cmdline + i, eap->nextcmd);
4627 eap->nextcmd = new_cmdline + i;
4628 }
4629 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4630 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4631 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4632 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4633 vim_free(*cmdlinep);
4634 *cmdlinep = new_cmdline;
4635
4636 return src;
4637}
4638
4639/*
4640 * Check for '|' to separate commands and '"' to start comments.
4641 */
4642 void
4643separate_nextcmd(eap)
4644 exarg_T *eap;
4645{
4646 char_u *p;
4647
Bram Moolenaar86b68352004-12-27 21:59:20 +00004648#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004649 p = skip_grep_pat(eap);
4650#else
4651 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004652#endif
4653
4654 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
4656 if (*p == Ctrl_V)
4657 {
4658 if (eap->argt & (USECTRLV | XFILE))
4659 ++p; /* skip CTRL-V and next char */
4660 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004661 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004662 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (*p == NUL) /* stop at NUL after CTRL-V */
4664 break;
4665 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004666
4667#ifdef FEAT_EVAL
4668 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004670 {
4671 p += 2;
4672 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004673 }
4674#endif
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 /* Check for '"': start of comment or '|': next command */
4677 /* :@" and :*" do not start a comment!
4678 * :redir @" doesn't either. */
4679 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4680 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4681 || p != eap->arg)
4682 && (eap->cmdidx != CMD_redir
4683 || p != eap->arg + 1 || p[-1] != '@'))
4684 || *p == '|' || *p == '\n')
4685 {
4686 /*
4687 * We remove the '\' before the '|', unless USECTRLV is used
4688 * AND 'b' is present in 'cpoptions'.
4689 */
4690 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4691 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4692 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004693 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 --p;
4695 }
4696 else
4697 {
4698 eap->nextcmd = check_nextcmd(p);
4699 *p = NUL;
4700 break;
4701 }
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4706 del_trailing_spaces(eap->arg);
4707}
4708
4709/*
4710 * get + command from ex argument
4711 */
4712 static char_u *
4713getargcmd(argp)
4714 char_u **argp;
4715{
4716 char_u *arg = *argp;
4717 char_u *command = NULL;
4718
4719 if (*arg == '+') /* +[command] */
4720 {
4721 ++arg;
4722 if (vim_isspace(*arg))
4723 command = dollar_command;
4724 else
4725 {
4726 command = arg;
4727 arg = skip_cmd_arg(command, TRUE);
4728 if (*arg != NUL)
4729 *arg++ = NUL; /* terminate command with NUL */
4730 }
4731
4732 arg = skipwhite(arg); /* skip over spaces */
4733 *argp = arg;
4734 }
4735 return command;
4736}
4737
4738/*
4739 * Find end of "+command" argument. Skip over "\ " and "\\".
4740 */
4741 static char_u *
4742skip_cmd_arg(p, rembs)
4743 char_u *p;
4744 int rembs; /* TRUE to halve the number of backslashes */
4745{
4746 while (*p && !vim_isspace(*p))
4747 {
4748 if (*p == '\\' && p[1] != NUL)
4749 {
4750 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004751 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 else
4753 ++p;
4754 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004755 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 return p;
4758}
4759
4760/*
4761 * Get "++opt=arg" argument.
4762 * Return FAIL or OK.
4763 */
4764 static int
4765getargopt(eap)
4766 exarg_T *eap;
4767{
4768 char_u *arg = eap->arg + 2;
4769 int *pp = NULL;
4770#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004771 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 char_u *p;
4773#endif
4774
4775 /* ":edit ++[no]bin[ary] file" */
4776 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4777 {
4778 if (*arg == 'n')
4779 {
4780 arg += 2;
4781 eap->force_bin = FORCE_NOBIN;
4782 }
4783 else
4784 eap->force_bin = FORCE_BIN;
4785 if (!checkforcmd(&arg, "binary", 3))
4786 return FAIL;
4787 eap->arg = skipwhite(arg);
4788 return OK;
4789 }
4790
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004791 /* ":read ++edit file" */
4792 if (STRNCMP(arg, "edit", 4) == 0)
4793 {
4794 eap->read_edit = TRUE;
4795 eap->arg = skipwhite(arg + 4);
4796 return OK;
4797 }
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (STRNCMP(arg, "ff", 2) == 0)
4800 {
4801 arg += 2;
4802 pp = &eap->force_ff;
4803 }
4804 else if (STRNCMP(arg, "fileformat", 10) == 0)
4805 {
4806 arg += 10;
4807 pp = &eap->force_ff;
4808 }
4809#ifdef FEAT_MBYTE
4810 else if (STRNCMP(arg, "enc", 3) == 0)
4811 {
4812 arg += 3;
4813 pp = &eap->force_enc;
4814 }
4815 else if (STRNCMP(arg, "encoding", 8) == 0)
4816 {
4817 arg += 8;
4818 pp = &eap->force_enc;
4819 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004820 else if (STRNCMP(arg, "bad", 3) == 0)
4821 {
4822 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004823 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#endif
4826
4827 if (pp == NULL || *arg != '=')
4828 return FAIL;
4829
4830 ++arg;
4831 *pp = (int)(arg - eap->cmd);
4832 arg = skip_cmd_arg(arg, FALSE);
4833 eap->arg = skipwhite(arg);
4834 *arg = NUL;
4835
4836#ifdef FEAT_MBYTE
4837 if (pp == &eap->force_ff)
4838 {
4839#endif
4840 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4841 return FAIL;
4842#ifdef FEAT_MBYTE
4843 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004844 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 /* Make 'fileencoding' lower case. */
4847 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4848 *p = TOLOWER_ASC(*p);
4849 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004850 else
4851 {
4852 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4853 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004854 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004855 if (STRICMP(p, "keep") == 0)
4856 eap->bad_char = BAD_KEEP;
4857 else if (STRICMP(p, "drop") == 0)
4858 eap->bad_char = BAD_DROP;
4859 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4860 eap->bad_char = *p;
4861 else
4862 return FAIL;
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
4865
4866 return OK;
4867}
4868
4869/*
4870 * ":abbreviate" and friends.
4871 */
4872 static void
4873ex_abbreviate(eap)
4874 exarg_T *eap;
4875{
4876 do_exmap(eap, TRUE); /* almost the same as mapping */
4877}
4878
4879/*
4880 * ":map" and friends.
4881 */
4882 static void
4883ex_map(eap)
4884 exarg_T *eap;
4885{
4886 /*
4887 * If we are sourcing .exrc or .vimrc in current directory we
4888 * print the mappings for security reasons.
4889 */
4890 if (secure)
4891 {
4892 secure = 2;
4893 msg_outtrans(eap->cmd);
4894 msg_putchar('\n');
4895 }
4896 do_exmap(eap, FALSE);
4897}
4898
4899/*
4900 * ":unmap" and friends.
4901 */
4902 static void
4903ex_unmap(eap)
4904 exarg_T *eap;
4905{
4906 do_exmap(eap, FALSE);
4907}
4908
4909/*
4910 * ":mapclear" and friends.
4911 */
4912 static void
4913ex_mapclear(eap)
4914 exarg_T *eap;
4915{
4916 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4917}
4918
4919/*
4920 * ":abclear" and friends.
4921 */
4922 static void
4923ex_abclear(eap)
4924 exarg_T *eap;
4925{
4926 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4927}
4928
4929#ifdef FEAT_AUTOCMD
4930 static void
4931ex_autocmd(eap)
4932 exarg_T *eap;
4933{
4934 /*
4935 * Disallow auto commands from .exrc and .vimrc in current
4936 * directory for security reasons.
4937 */
4938 if (secure)
4939 {
4940 secure = 2;
4941 eap->errmsg = e_curdir;
4942 }
4943 else if (eap->cmdidx == CMD_autocmd)
4944 do_autocmd(eap->arg, eap->forceit);
4945 else
4946 do_augroup(eap->arg, eap->forceit);
4947}
4948
4949/*
4950 * ":doautocmd": Apply the automatic commands to the current buffer.
4951 */
4952 static void
4953ex_doautocmd(eap)
4954 exarg_T *eap;
4955{
4956 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004957 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958}
4959#endif
4960
4961#ifdef FEAT_LISTCMDS
4962/*
4963 * :[N]bunload[!] [N] [bufname] unload buffer
4964 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4965 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4966 */
4967 static void
4968ex_bunload(eap)
4969 exarg_T *eap;
4970{
4971 eap->errmsg = do_bufdel(
4972 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4973 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4974 : DOBUF_UNLOAD, eap->arg,
4975 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4976}
4977
4978/*
4979 * :[N]buffer [N] to buffer N
4980 * :[N]sbuffer [N] to buffer N
4981 */
4982 static void
4983ex_buffer(eap)
4984 exarg_T *eap;
4985{
4986 if (*eap->arg)
4987 eap->errmsg = e_trailing;
4988 else
4989 {
4990 if (eap->addr_count == 0) /* default is current buffer */
4991 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4992 else
4993 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4994 }
4995}
4996
4997/*
4998 * :[N]bmodified [N] to next mod. buffer
4999 * :[N]sbmodified [N] to next mod. buffer
5000 */
5001 static void
5002ex_bmodified(eap)
5003 exarg_T *eap;
5004{
5005 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5006}
5007
5008/*
5009 * :[N]bnext [N] to next buffer
5010 * :[N]sbnext [N] split and to next buffer
5011 */
5012 static void
5013ex_bnext(eap)
5014 exarg_T *eap;
5015{
5016 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5017}
5018
5019/*
5020 * :[N]bNext [N] to previous buffer
5021 * :[N]bprevious [N] to previous buffer
5022 * :[N]sbNext [N] split and to previous buffer
5023 * :[N]sbprevious [N] split and to previous buffer
5024 */
5025 static void
5026ex_bprevious(eap)
5027 exarg_T *eap;
5028{
5029 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5030}
5031
5032/*
5033 * :brewind to first buffer
5034 * :bfirst to first buffer
5035 * :sbrewind split and to first buffer
5036 * :sbfirst split and to first buffer
5037 */
5038 static void
5039ex_brewind(eap)
5040 exarg_T *eap;
5041{
5042 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5043}
5044
5045/*
5046 * :blast to last buffer
5047 * :sblast split and to last buffer
5048 */
5049 static void
5050ex_blast(eap)
5051 exarg_T *eap;
5052{
5053 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5054}
5055#endif
5056
5057 int
5058ends_excmd(c)
5059 int c;
5060{
5061 return (c == NUL || c == '|' || c == '"' || c == '\n');
5062}
5063
5064#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5065 || defined(PROTO)
5066/*
5067 * Return the next command, after the first '|' or '\n'.
5068 * Return NULL if not found.
5069 */
5070 char_u *
5071find_nextcmd(p)
5072 char_u *p;
5073{
5074 while (*p != '|' && *p != '\n')
5075 {
5076 if (*p == NUL)
5077 return NULL;
5078 ++p;
5079 }
5080 return (p + 1);
5081}
5082#endif
5083
5084/*
5085 * Check if *p is a separator between Ex commands.
5086 * Return NULL if it isn't, (p + 1) if it is.
5087 */
5088 char_u *
5089check_nextcmd(p)
5090 char_u *p;
5091{
5092 p = skipwhite(p);
5093 if (*p == '|' || *p == '\n')
5094 return (p + 1);
5095 else
5096 return NULL;
5097}
5098
5099/*
5100 * - if there are more files to edit
5101 * - and this is the last window
5102 * - and forceit not used
5103 * - and not repeated twice on a row
5104 * return FAIL and give error message if 'message' TRUE
5105 * return OK otherwise
5106 */
5107 static int
5108check_more(message, forceit)
5109 int message; /* when FALSE check only, no messages */
5110 int forceit;
5111{
5112 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5113
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005114 if (!forceit && only_one_window()
5115 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 if (message)
5118 {
5119#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5120 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5121 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005122 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005125 vim_strncpy(buff,
5126 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005127 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005129 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 _("%d more files to edit. Quit anyway?"), n);
5131 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5132 return OK;
5133 return FAIL;
5134 }
5135#endif
5136 if (n == 1)
5137 EMSG(_("E173: 1 more file to edit"));
5138 else
5139 EMSGN(_("E173: %ld more files to edit"), n);
5140 quitmore = 2; /* next try to quit is allowed */
5141 }
5142 return FAIL;
5143 }
5144 return OK;
5145}
5146
5147#ifdef FEAT_CMDL_COMPL
5148/*
5149 * Function given to ExpandGeneric() to obtain the list of command names.
5150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 char_u *
5152get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005153 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 int idx;
5155{
5156 if (idx >= (int)CMD_SIZE)
5157# ifdef FEAT_USR_CMDS
5158 return get_user_command_name(idx);
5159# else
5160 return NULL;
5161# endif
5162 return cmdnames[idx].cmd_name;
5163}
5164#endif
5165
5166#if defined(FEAT_USR_CMDS) || defined(PROTO)
5167static 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));
5168static void uc_list __ARGS((char_u *name, size_t name_len));
5169static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5170static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5171static 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));
5172
5173 static int
5174uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5175 char_u *name;
5176 size_t name_len;
5177 char_u *rep;
5178 long argt;
5179 long def;
5180 int flags;
5181 int compl;
5182 char_u *compl_arg;
5183 int force;
5184{
5185 ucmd_T *cmd = NULL;
5186 char_u *p;
5187 int i;
5188 int cmp = 1;
5189 char_u *rep_buf = NULL;
5190 garray_T *gap;
5191
Bram Moolenaar9c102382006-05-03 21:26:49 +00005192 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (rep_buf == NULL)
5194 {
5195 /* Can't replace termcodes - try using the string as is */
5196 rep_buf = vim_strsave(rep);
5197
5198 /* Give up if out of memory */
5199 if (rep_buf == NULL)
5200 return FAIL;
5201 }
5202
5203 /* get address of growarray: global or in curbuf */
5204 if (flags & UC_BUFFER)
5205 {
5206 gap = &curbuf->b_ucmds;
5207 if (gap->ga_itemsize == 0)
5208 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5209 }
5210 else
5211 gap = &ucmds;
5212
5213 /* Search for the command in the already defined commands. */
5214 for (i = 0; i < gap->ga_len; ++i)
5215 {
5216 size_t len;
5217
5218 cmd = USER_CMD_GA(gap, i);
5219 len = STRLEN(cmd->uc_name);
5220 cmp = STRNCMP(name, cmd->uc_name, name_len);
5221 if (cmp == 0)
5222 {
5223 if (name_len < len)
5224 cmp = -1;
5225 else if (name_len > len)
5226 cmp = 1;
5227 }
5228
5229 if (cmp == 0)
5230 {
5231 if (!force)
5232 {
5233 EMSG(_("E174: Command already exists: add ! to replace it"));
5234 goto fail;
5235 }
5236
5237 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005238 cmd->uc_rep = NULL;
5239#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5240 vim_free(cmd->uc_compl_arg);
5241 cmd->uc_compl_arg = NULL;
5242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 break;
5244 }
5245
5246 /* Stop as soon as we pass the name to add */
5247 if (cmp < 0)
5248 break;
5249 }
5250
5251 /* Extend the array unless we're replacing an existing command */
5252 if (cmp != 0)
5253 {
5254 if (ga_grow(gap, 1) != OK)
5255 goto fail;
5256 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5257 goto fail;
5258
5259 cmd = USER_CMD_GA(gap, i);
5260 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5261
5262 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 cmd->uc_name = p;
5265 }
5266
5267 cmd->uc_rep = rep_buf;
5268 cmd->uc_argt = argt;
5269 cmd->uc_def = def;
5270 cmd->uc_compl = compl;
5271#ifdef FEAT_EVAL
5272 cmd->uc_scriptID = current_SID;
5273# ifdef FEAT_CMDL_COMPL
5274 cmd->uc_compl_arg = compl_arg;
5275# endif
5276#endif
5277
5278 return OK;
5279
5280fail:
5281 vim_free(rep_buf);
5282#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5283 vim_free(compl_arg);
5284#endif
5285 return FAIL;
5286}
5287
5288/*
5289 * List of names for completion for ":command" with the EXPAND_ flag.
5290 * Must be alphabetical for completion.
5291 */
5292static struct
5293{
5294 int expand;
5295 char *name;
5296} command_complete[] =
5297{
5298 {EXPAND_AUGROUP, "augroup"},
5299 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005300 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005302 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005303#if defined(FEAT_CSCOPE)
5304 {EXPAND_CSCOPE, "cscope"},
5305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5307 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005308 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309#endif
5310 {EXPAND_DIRECTORIES, "dir"},
5311 {EXPAND_ENV_VARS, "environment"},
5312 {EXPAND_EVENTS, "event"},
5313 {EXPAND_EXPRESSION, "expression"},
5314 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005315 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005316 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {EXPAND_FUNCTIONS, "function"},
5318 {EXPAND_HELP, "help"},
5319 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005320#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5321 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
5322 {EXPAND_LOCALES, "locale"},
5323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {EXPAND_MAPPINGS, "mapping"},
5325 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005326 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005328 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005329#if defined(FEAT_SIGNS)
5330 {EXPAND_SIGN, "sign"},
5331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {EXPAND_TAGS, "tag"},
5333 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5334 {EXPAND_USER_VARS, "var"},
5335 {0, NULL}
5336};
5337
5338 static void
5339uc_list(name, name_len)
5340 char_u *name;
5341 size_t name_len;
5342{
5343 int i, j;
5344 int found = FALSE;
5345 ucmd_T *cmd;
5346 int len;
5347 long a;
5348 garray_T *gap;
5349
5350 gap = &curbuf->b_ucmds;
5351 for (;;)
5352 {
5353 for (i = 0; i < gap->ga_len; ++i)
5354 {
5355 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005356 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 /* Skip commands which don't match the requested prefix */
5359 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5360 continue;
5361
5362 /* Put out the title first time */
5363 if (!found)
5364 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5365 found = TRUE;
5366 msg_putchar('\n');
5367 if (got_int)
5368 break;
5369
5370 /* Special cases */
5371 msg_putchar(a & BANG ? '!' : ' ');
5372 msg_putchar(a & REGSTR ? '"' : ' ');
5373 msg_putchar(gap != &ucmds ? 'b' : ' ');
5374 msg_putchar(' ');
5375
5376 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5377 len = (int)STRLEN(cmd->uc_name) + 4;
5378
5379 do {
5380 msg_putchar(' ');
5381 ++len;
5382 } while (len < 16);
5383
5384 len = 0;
5385
5386 /* Arguments */
5387 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5388 {
5389 case 0: IObuff[len++] = '0'; break;
5390 case (EXTRA): IObuff[len++] = '*'; break;
5391 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5392 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5393 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5394 }
5395
5396 do {
5397 IObuff[len++] = ' ';
5398 } while (len < 5);
5399
5400 /* Range */
5401 if (a & (RANGE|COUNT))
5402 {
5403 if (a & COUNT)
5404 {
5405 /* -count=N */
5406 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5407 len += (int)STRLEN(IObuff + len);
5408 }
5409 else if (a & DFLALL)
5410 IObuff[len++] = '%';
5411 else if (cmd->uc_def >= 0)
5412 {
5413 /* -range=N */
5414 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5415 len += (int)STRLEN(IObuff + len);
5416 }
5417 else
5418 IObuff[len++] = '.';
5419 }
5420
5421 do {
5422 IObuff[len++] = ' ';
5423 } while (len < 11);
5424
5425 /* Completion */
5426 for (j = 0; command_complete[j].expand != 0; ++j)
5427 if (command_complete[j].expand == cmd->uc_compl)
5428 {
5429 STRCPY(IObuff + len, command_complete[j].name);
5430 len += (int)STRLEN(IObuff + len);
5431 break;
5432 }
5433
5434 do {
5435 IObuff[len++] = ' ';
5436 } while (len < 21);
5437
5438 IObuff[len] = '\0';
5439 msg_outtrans(IObuff);
5440
5441 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005442#ifdef FEAT_EVAL
5443 if (p_verbose > 0)
5444 last_set_msg(cmd->uc_scriptID);
5445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 out_flush();
5447 ui_breakcheck();
5448 if (got_int)
5449 break;
5450 }
5451 if (gap == &ucmds || i < gap->ga_len)
5452 break;
5453 gap = &ucmds;
5454 }
5455
5456 if (!found)
5457 MSG(_("No user-defined commands found"));
5458}
5459
5460 static char_u *
5461uc_fun_cmd()
5462{
5463 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5464 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5465 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5466 0xb9, 0x7f, 0};
5467 int i;
5468
5469 for (i = 0; fcmd[i]; ++i)
5470 IObuff[i] = fcmd[i] - 0x40;
5471 IObuff[i] = 0;
5472 return IObuff;
5473}
5474
5475 static int
5476uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5477 char_u *attr;
5478 size_t len;
5479 long *argt;
5480 long *def;
5481 int *flags;
5482 int *compl;
5483 char_u **compl_arg;
5484{
5485 char_u *p;
5486
5487 if (len == 0)
5488 {
5489 EMSG(_("E175: No attribute specified"));
5490 return FAIL;
5491 }
5492
5493 /* First, try the simple attributes (no arguments) */
5494 if (STRNICMP(attr, "bang", len) == 0)
5495 *argt |= BANG;
5496 else if (STRNICMP(attr, "buffer", len) == 0)
5497 *flags |= UC_BUFFER;
5498 else if (STRNICMP(attr, "register", len) == 0)
5499 *argt |= REGSTR;
5500 else if (STRNICMP(attr, "bar", len) == 0)
5501 *argt |= TRLBAR;
5502 else
5503 {
5504 int i;
5505 char_u *val = NULL;
5506 size_t vallen = 0;
5507 size_t attrlen = len;
5508
5509 /* Look for the attribute name - which is the part before any '=' */
5510 for (i = 0; i < (int)len; ++i)
5511 {
5512 if (attr[i] == '=')
5513 {
5514 val = &attr[i + 1];
5515 vallen = len - i - 1;
5516 attrlen = i;
5517 break;
5518 }
5519 }
5520
5521 if (STRNICMP(attr, "nargs", attrlen) == 0)
5522 {
5523 if (vallen == 1)
5524 {
5525 if (*val == '0')
5526 /* Do nothing - this is the default */;
5527 else if (*val == '1')
5528 *argt |= (EXTRA | NOSPC | NEEDARG);
5529 else if (*val == '*')
5530 *argt |= EXTRA;
5531 else if (*val == '?')
5532 *argt |= (EXTRA | NOSPC);
5533 else if (*val == '+')
5534 *argt |= (EXTRA | NEEDARG);
5535 else
5536 goto wrong_nargs;
5537 }
5538 else
5539 {
5540wrong_nargs:
5541 EMSG(_("E176: Invalid number of arguments"));
5542 return FAIL;
5543 }
5544 }
5545 else if (STRNICMP(attr, "range", attrlen) == 0)
5546 {
5547 *argt |= RANGE;
5548 if (vallen == 1 && *val == '%')
5549 *argt |= DFLALL;
5550 else if (val != NULL)
5551 {
5552 p = val;
5553 if (*def >= 0)
5554 {
5555two_count:
5556 EMSG(_("E177: Count cannot be specified twice"));
5557 return FAIL;
5558 }
5559
5560 *def = getdigits(&p);
5561 *argt |= (ZEROR | NOTADR);
5562
5563 if (p != val + vallen || vallen == 0)
5564 {
5565invalid_count:
5566 EMSG(_("E178: Invalid default value for count"));
5567 return FAIL;
5568 }
5569 }
5570 }
5571 else if (STRNICMP(attr, "count", attrlen) == 0)
5572 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005573 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 if (val != NULL)
5576 {
5577 p = val;
5578 if (*def >= 0)
5579 goto two_count;
5580
5581 *def = getdigits(&p);
5582
5583 if (p != val + vallen)
5584 goto invalid_count;
5585 }
5586
5587 if (*def < 0)
5588 *def = 0;
5589 }
5590 else if (STRNICMP(attr, "complete", attrlen) == 0)
5591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 if (val == NULL)
5593 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005594 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 return FAIL;
5596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005598 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5599 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else
5603 {
5604 char_u ch = attr[len];
5605 attr[len] = '\0';
5606 EMSG2(_("E181: Invalid attribute: %s"), attr);
5607 attr[len] = ch;
5608 return FAIL;
5609 }
5610 }
5611
5612 return OK;
5613}
5614
Bram Moolenaara850a712009-01-28 14:42:59 +00005615/*
5616 * ":command ..."
5617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 static void
5619ex_command(eap)
5620 exarg_T *eap;
5621{
5622 char_u *name;
5623 char_u *end;
5624 char_u *p;
5625 long argt = 0;
5626 long def = -1;
5627 int flags = 0;
5628 int compl = EXPAND_NOTHING;
5629 char_u *compl_arg = NULL;
5630 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005631 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633 p = eap->arg;
5634
5635 /* Check for attributes */
5636 while (*p == '-')
5637 {
5638 ++p;
5639 end = skiptowhite(p);
5640 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5641 == FAIL)
5642 return;
5643 p = skipwhite(end);
5644 }
5645
5646 /* Get the name (if any) and skip to the following argument */
5647 name = p;
5648 if (ASCII_ISALPHA(*p))
5649 while (ASCII_ISALNUM(*p))
5650 ++p;
5651 if (!ends_excmd(*p) && !vim_iswhite(*p))
5652 {
5653 EMSG(_("E182: Invalid command name"));
5654 return;
5655 }
5656 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005657 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 /* If there is nothing after the name, and no attributes were specified,
5660 * we are listing commands
5661 */
5662 p = skipwhite(end);
5663 if (!has_attr && ends_excmd(*p))
5664 {
5665 uc_list(name, end - name);
5666 }
5667 else if (!ASCII_ISUPPER(*name))
5668 {
5669 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5670 return;
5671 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005672 else if ((name_len == 1 && *name == 'X')
5673 || (name_len <= 4
5674 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5675 {
5676 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5677 return;
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 else
5680 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5681 eap->forceit);
5682}
5683
5684/*
5685 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 * Clear all user commands, global and for current buffer.
5687 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005688 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005690 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 uc_clear(&ucmds);
5693 uc_clear(&curbuf->b_ucmds);
5694}
5695
5696/*
5697 * Clear all user commands for "gap".
5698 */
5699 void
5700uc_clear(gap)
5701 garray_T *gap;
5702{
5703 int i;
5704 ucmd_T *cmd;
5705
5706 for (i = 0; i < gap->ga_len; ++i)
5707 {
5708 cmd = USER_CMD_GA(gap, i);
5709 vim_free(cmd->uc_name);
5710 vim_free(cmd->uc_rep);
5711# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5712 vim_free(cmd->uc_compl_arg);
5713# endif
5714 }
5715 ga_clear(gap);
5716}
5717
5718 static void
5719ex_delcommand(eap)
5720 exarg_T *eap;
5721{
5722 int i = 0;
5723 ucmd_T *cmd = NULL;
5724 int cmp = -1;
5725 garray_T *gap;
5726
5727 gap = &curbuf->b_ucmds;
5728 for (;;)
5729 {
5730 for (i = 0; i < gap->ga_len; ++i)
5731 {
5732 cmd = USER_CMD_GA(gap, i);
5733 cmp = STRCMP(eap->arg, cmd->uc_name);
5734 if (cmp <= 0)
5735 break;
5736 }
5737 if (gap == &ucmds || cmp == 0)
5738 break;
5739 gap = &ucmds;
5740 }
5741
5742 if (cmp != 0)
5743 {
5744 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5745 return;
5746 }
5747
5748 vim_free(cmd->uc_name);
5749 vim_free(cmd->uc_rep);
5750# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5751 vim_free(cmd->uc_compl_arg);
5752# endif
5753
5754 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
5756 if (i < gap->ga_len)
5757 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5758}
5759
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005760/*
5761 * split and quote args for <f-args>
5762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 static char_u *
5764uc_split_args(arg, lenp)
5765 char_u *arg;
5766 size_t *lenp;
5767{
5768 char_u *buf;
5769 char_u *p;
5770 char_u *q;
5771 int len;
5772
5773 /* Precalculate length */
5774 p = arg;
5775 len = 2; /* Initial and final quotes */
5776
5777 while (*p)
5778 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005779 if (p[0] == '\\' && p[1] == '\\')
5780 {
5781 len += 2;
5782 p += 2;
5783 }
5784 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
5786 len += 1;
5787 p += 2;
5788 }
5789 else if (*p == '\\' || *p == '"')
5790 {
5791 len += 2;
5792 p += 1;
5793 }
5794 else if (vim_iswhite(*p))
5795 {
5796 p = skipwhite(p);
5797 if (*p == NUL)
5798 break;
5799 len += 3; /* "," */
5800 }
5801 else
5802 {
5803 ++len;
5804 ++p;
5805 }
5806 }
5807
5808 buf = alloc(len + 1);
5809 if (buf == NULL)
5810 {
5811 *lenp = 0;
5812 return buf;
5813 }
5814
5815 p = arg;
5816 q = buf;
5817 *q++ = '"';
5818 while (*p)
5819 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005820 if (p[0] == '\\' && p[1] == '\\')
5821 {
5822 *q++ = '\\';
5823 *q++ = '\\';
5824 p += 2;
5825 }
5826 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 {
5828 *q++ = p[1];
5829 p += 2;
5830 }
5831 else if (*p == '\\' || *p == '"')
5832 {
5833 *q++ = '\\';
5834 *q++ = *p++;
5835 }
5836 else if (vim_iswhite(*p))
5837 {
5838 p = skipwhite(p);
5839 if (*p == NUL)
5840 break;
5841 *q++ = '"';
5842 *q++ = ',';
5843 *q++ = '"';
5844 }
5845 else
5846 {
5847 *q++ = *p++;
5848 }
5849 }
5850 *q++ = '"';
5851 *q = 0;
5852
5853 *lenp = len;
5854 return buf;
5855}
5856
5857/*
5858 * Check for a <> code in a user command.
5859 * "code" points to the '<'. "len" the length of the <> (inclusive).
5860 * "buf" is where the result is to be added.
5861 * "split_buf" points to a buffer used for splitting, caller should free it.
5862 * "split_len" is the length of what "split_buf" contains.
5863 * Returns the length of the replacement, which has been added to "buf".
5864 * Returns -1 if there was no match, and only the "<" has been copied.
5865 */
5866 static size_t
5867uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5868 char_u *code;
5869 size_t len;
5870 char_u *buf;
5871 ucmd_T *cmd; /* the user command we're expanding */
5872 exarg_T *eap; /* ex arguments */
5873 char_u **split_buf;
5874 size_t *split_len;
5875{
5876 size_t result = 0;
5877 char_u *p = code + 1;
5878 size_t l = len - 2;
5879 int quote = 0;
5880 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5881 ct_LT, ct_NONE } type = ct_NONE;
5882
5883 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5884 {
5885 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5886 p += 2;
5887 l -= 2;
5888 }
5889
Bram Moolenaar371d5402006-03-20 21:47:49 +00005890 ++l;
5891 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005893 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005895 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005897 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005899 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005901 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005903 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005905 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 type = ct_REGISTER;
5907
5908 switch (type)
5909 {
5910 case ct_ARGS:
5911 /* Simple case first */
5912 if (*eap->arg == NUL)
5913 {
5914 if (quote == 1)
5915 {
5916 result = 2;
5917 if (buf != NULL)
5918 STRCPY(buf, "''");
5919 }
5920 else
5921 result = 0;
5922 break;
5923 }
5924
5925 /* When specified there is a single argument don't split it.
5926 * Works for ":Cmd %" when % is "a b c". */
5927 if ((eap->argt & NOSPC) && quote == 2)
5928 quote = 1;
5929
5930 switch (quote)
5931 {
5932 case 0: /* No quoting, no splitting */
5933 result = STRLEN(eap->arg);
5934 if (buf != NULL)
5935 STRCPY(buf, eap->arg);
5936 break;
5937 case 1: /* Quote, but don't split */
5938 result = STRLEN(eap->arg) + 2;
5939 for (p = eap->arg; *p; ++p)
5940 {
5941 if (*p == '\\' || *p == '"')
5942 ++result;
5943 }
5944
5945 if (buf != NULL)
5946 {
5947 *buf++ = '"';
5948 for (p = eap->arg; *p; ++p)
5949 {
5950 if (*p == '\\' || *p == '"')
5951 *buf++ = '\\';
5952 *buf++ = *p;
5953 }
5954 *buf = '"';
5955 }
5956
5957 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005958 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 /* This is hard, so only do it once, and cache the result */
5960 if (*split_buf == NULL)
5961 *split_buf = uc_split_args(eap->arg, split_len);
5962
5963 result = *split_len;
5964 if (buf != NULL && result != 0)
5965 STRCPY(buf, *split_buf);
5966
5967 break;
5968 }
5969 break;
5970
5971 case ct_BANG:
5972 result = eap->forceit ? 1 : 0;
5973 if (quote)
5974 result += 2;
5975 if (buf != NULL)
5976 {
5977 if (quote)
5978 *buf++ = '"';
5979 if (eap->forceit)
5980 *buf++ = '!';
5981 if (quote)
5982 *buf = '"';
5983 }
5984 break;
5985
5986 case ct_LINE1:
5987 case ct_LINE2:
5988 case ct_COUNT:
5989 {
5990 char num_buf[20];
5991 long num = (type == ct_LINE1) ? eap->line1 :
5992 (type == ct_LINE2) ? eap->line2 :
5993 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5994 size_t num_len;
5995
5996 sprintf(num_buf, "%ld", num);
5997 num_len = STRLEN(num_buf);
5998 result = num_len;
5999
6000 if (quote)
6001 result += 2;
6002
6003 if (buf != NULL)
6004 {
6005 if (quote)
6006 *buf++ = '"';
6007 STRCPY(buf, num_buf);
6008 buf += num_len;
6009 if (quote)
6010 *buf = '"';
6011 }
6012
6013 break;
6014 }
6015
6016 case ct_REGISTER:
6017 result = eap->regname ? 1 : 0;
6018 if (quote)
6019 result += 2;
6020 if (buf != NULL)
6021 {
6022 if (quote)
6023 *buf++ = '\'';
6024 if (eap->regname)
6025 *buf++ = eap->regname;
6026 if (quote)
6027 *buf = '\'';
6028 }
6029 break;
6030
6031 case ct_LT:
6032 result = 1;
6033 if (buf != NULL)
6034 *buf = '<';
6035 break;
6036
6037 default:
6038 /* Not recognized: just copy the '<' and return -1. */
6039 result = (size_t)-1;
6040 if (buf != NULL)
6041 *buf = '<';
6042 break;
6043 }
6044
6045 return result;
6046}
6047
6048 static void
6049do_ucmd(eap)
6050 exarg_T *eap;
6051{
6052 char_u *buf;
6053 char_u *p;
6054 char_u *q;
6055
6056 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006057 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006058 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 size_t len, totlen;
6060
6061 size_t split_len = 0;
6062 char_u *split_buf = NULL;
6063 ucmd_T *cmd;
6064#ifdef FEAT_EVAL
6065 scid_T save_current_SID = current_SID;
6066#endif
6067
6068 if (eap->cmdidx == CMD_USER)
6069 cmd = USER_CMD(eap->useridx);
6070 else
6071 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6072
6073 /*
6074 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006075 * First round: "buf" is NULL, compute length, allocate "buf".
6076 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 */
6078 buf = NULL;
6079 for (;;)
6080 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006081 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006082 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006084
6085 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006087 start = vim_strchr(p, '<');
6088 if (start != NULL)
6089 end = vim_strchr(start + 1, '>');
6090 if (buf != NULL)
6091 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006092 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6093 ;
6094 if (*ksp == K_SPECIAL
6095 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006096 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6097# ifdef FEAT_GUI
6098 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6099# endif
6100 ))
6101 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006102 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006103 * KS_SPECIAL KE_FILLER, like for mappings, but
6104 * do_cmdline() doesn't handle that, so convert it back.
6105 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6106 len = ksp - p;
6107 if (len > 0)
6108 {
6109 mch_memmove(q, p, len);
6110 q += len;
6111 }
6112 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6113 p = ksp + 3;
6114 continue;
6115 }
6116 }
6117
6118 /* break if there no <item> is found */
6119 if (start == NULL || end == NULL)
6120 break;
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 /* Include the '>' */
6123 ++end;
6124
6125 /* Take everything up to the '<' */
6126 len = start - p;
6127 if (buf == NULL)
6128 totlen += len;
6129 else
6130 {
6131 mch_memmove(q, p, len);
6132 q += len;
6133 }
6134
6135 len = uc_check_code(start, end - start, q, cmd, eap,
6136 &split_buf, &split_len);
6137 if (len == (size_t)-1)
6138 {
6139 /* no match, continue after '<' */
6140 p = start + 1;
6141 len = 1;
6142 }
6143 else
6144 p = end;
6145 if (buf == NULL)
6146 totlen += len;
6147 else
6148 q += len;
6149 }
6150 if (buf != NULL) /* second time here, finished */
6151 {
6152 STRCPY(q, p);
6153 break;
6154 }
6155
6156 totlen += STRLEN(p); /* Add on the trailing characters */
6157 buf = alloc((unsigned)(totlen + 1));
6158 if (buf == NULL)
6159 {
6160 vim_free(split_buf);
6161 return;
6162 }
6163 }
6164
6165#ifdef FEAT_EVAL
6166 current_SID = cmd->uc_scriptID;
6167#endif
6168 (void)do_cmdline(buf, eap->getline, eap->cookie,
6169 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6170#ifdef FEAT_EVAL
6171 current_SID = save_current_SID;
6172#endif
6173 vim_free(buf);
6174 vim_free(split_buf);
6175}
6176
6177# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6178 static char_u *
6179get_user_command_name(idx)
6180 int idx;
6181{
6182 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6183}
6184
6185/*
6186 * Function given to ExpandGeneric() to obtain the list of user command names.
6187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 char_u *
6189get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006190 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 int idx;
6192{
6193 if (idx < curbuf->b_ucmds.ga_len)
6194 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6195 idx -= curbuf->b_ucmds.ga_len;
6196 if (idx < ucmds.ga_len)
6197 return USER_CMD(idx)->uc_name;
6198 return NULL;
6199}
6200
6201/*
6202 * Function given to ExpandGeneric() to obtain the list of user command
6203 * attributes.
6204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 char_u *
6206get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006207 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 int idx;
6209{
6210 static char *user_cmd_flags[] =
6211 {"bang", "bar", "buffer", "complete", "count",
6212 "nargs", "range", "register"};
6213
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006214 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 return NULL;
6216 return (char_u *)user_cmd_flags[idx];
6217}
6218
6219/*
6220 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 char_u *
6223get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006224 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 int idx;
6226{
6227 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6228
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006229 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return NULL;
6231 return (char_u *)user_cmd_nargs[idx];
6232}
6233
6234/*
6235 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 char_u *
6238get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006239 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 int idx;
6241{
6242 return (char_u *)command_complete[idx].name;
6243}
6244# endif /* FEAT_CMDL_COMPL */
6245
6246#endif /* FEAT_USR_CMDS */
6247
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006248#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6249/*
6250 * Parse a completion argument "value[vallen]".
6251 * The detected completion goes in "*complp", argument type in "*argt".
6252 * When there is an argument, for function and user defined completion, it's
6253 * copied to allocated memory and stored in "*compl_arg".
6254 * Returns FAIL if something is wrong.
6255 */
6256 int
6257parse_compl_arg(value, vallen, complp, argt, compl_arg)
6258 char_u *value;
6259 int vallen;
6260 int *complp;
6261 long *argt;
6262 char_u **compl_arg;
6263{
6264 char_u *arg = NULL;
6265 size_t arglen = 0;
6266 int i;
6267 int valend = vallen;
6268
6269 /* Look for any argument part - which is the part after any ',' */
6270 for (i = 0; i < vallen; ++i)
6271 {
6272 if (value[i] == ',')
6273 {
6274 arg = &value[i + 1];
6275 arglen = vallen - i - 1;
6276 valend = i;
6277 break;
6278 }
6279 }
6280
6281 for (i = 0; command_complete[i].expand != 0; ++i)
6282 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006283 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006284 && STRNCMP(value, command_complete[i].name, valend) == 0)
6285 {
6286 *complp = command_complete[i].expand;
6287 if (command_complete[i].expand == EXPAND_BUFFERS)
6288 *argt |= BUFNAME;
6289 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6290 || command_complete[i].expand == EXPAND_FILES)
6291 *argt |= XFILE;
6292 break;
6293 }
6294 }
6295
6296 if (command_complete[i].expand == 0)
6297 {
6298 EMSG2(_("E180: Invalid complete value: %s"), value);
6299 return FAIL;
6300 }
6301
6302# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6303 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6304 && arg != NULL)
6305# else
6306 if (arg != NULL)
6307# endif
6308 {
6309 EMSG(_("E468: Completion argument only allowed for custom completion"));
6310 return FAIL;
6311 }
6312
6313# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6314 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6315 && arg == NULL)
6316 {
6317 EMSG(_("E467: Custom completion requires a function argument"));
6318 return FAIL;
6319 }
6320
6321 if (arg != NULL)
6322 *compl_arg = vim_strnsave(arg, (int)arglen);
6323# endif
6324 return OK;
6325}
6326#endif
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 static void
6329ex_colorscheme(eap)
6330 exarg_T *eap;
6331{
Bram Moolenaare6850792010-05-14 15:28:44 +02006332 if (*eap->arg == NUL)
6333 {
6334#ifdef FEAT_EVAL
6335 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6336 char_u *p = NULL;
6337
6338 if (expr != NULL)
6339 {
6340 ++emsg_off;
6341 p = eval_to_string(expr, NULL, FALSE);
6342 --emsg_off;
6343 vim_free(expr);
6344 }
6345 if (p != NULL)
6346 {
6347 MSG(p);
6348 vim_free(p);
6349 }
6350 else
6351 MSG("default");
6352#else
6353 MSG(_("unknown"));
6354#endif
6355 }
6356 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6358}
6359
6360 static void
6361ex_highlight(eap)
6362 exarg_T *eap;
6363{
6364 if (*eap->arg == NUL && eap->cmd[2] == '!')
6365 MSG(_("Greetings, Vim user!"));
6366 do_highlight(eap->arg, eap->forceit, FALSE);
6367}
6368
6369
6370/*
6371 * Call this function if we thought we were going to exit, but we won't
6372 * (because of an error). May need to restore the terminal mode.
6373 */
6374 void
6375not_exiting()
6376{
6377 exiting = FALSE;
6378 settmode(TMODE_RAW);
6379}
6380
6381/*
6382 * ":quit": quit current window, quit Vim if closed the last window.
6383 */
6384 static void
6385ex_quit(eap)
6386 exarg_T *eap;
6387{
6388#ifdef FEAT_CMDWIN
6389 if (cmdwin_type != 0)
6390 {
6391 cmdwin_result = Ctrl_C;
6392 return;
6393 }
6394#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006395 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006396 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006397 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006398 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006399 return;
6400 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006401#ifdef FEAT_AUTOCMD
6402 if (curbuf_locked())
6403 return;
6404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406#ifdef FEAT_NETBEANS_INTG
6407 netbeansForcedQuit = eap->forceit;
6408#endif
6409
6410 /*
6411 * If there are more files or windows we won't exit.
6412 */
6413 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6414 exiting = TRUE;
6415 if ((!P_HID(curbuf)
6416 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6417 || check_more(TRUE, eap->forceit) == FAIL
6418 || (only_one_window() && check_changed_any(eap->forceit)))
6419 {
6420 not_exiting();
6421 }
6422 else
6423 {
6424#ifdef FEAT_WINDOWS
6425 if (only_one_window()) /* quit last window */
6426#endif
6427 getout(0);
6428#ifdef FEAT_WINDOWS
6429# ifdef FEAT_GUI
6430 need_mouse_correct = TRUE;
6431# endif
6432 /* close window; may free buffer */
6433 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6434#endif
6435 }
6436}
6437
6438/*
6439 * ":cquit".
6440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 static void
6442ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006443 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 getout(1); /* this does not always pass on the exit code to the Manx
6446 compiler. why? */
6447}
6448
6449/*
6450 * ":qall": try to quit all windows
6451 */
6452 static void
6453ex_quit_all(eap)
6454 exarg_T *eap;
6455{
6456# ifdef FEAT_CMDWIN
6457 if (cmdwin_type != 0)
6458 {
6459 if (eap->forceit)
6460 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6461 else
6462 cmdwin_result = K_XF2;
6463 return;
6464 }
6465# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006466
6467 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006468 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006469 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006470 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006471 return;
6472 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006473#ifdef FEAT_AUTOCMD
6474 if (curbuf_locked())
6475 return;
6476#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 exiting = TRUE;
6479 if (eap->forceit || !check_changed_any(FALSE))
6480 getout(0);
6481 not_exiting();
6482}
6483
Bram Moolenaard9967712006-03-11 21:18:15 +00006484#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485/*
6486 * ":close": close current window, unless it is the last one
6487 */
6488 static void
6489ex_close(eap)
6490 exarg_T *eap;
6491{
6492# ifdef FEAT_CMDWIN
6493 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006494 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 else
6496# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006497 if (!text_locked()
6498#ifdef FEAT_AUTOCMD
6499 && !curbuf_locked()
6500#endif
6501 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006502 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
Bram Moolenaard9967712006-03-11 21:18:15 +00006505# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506/*
6507 * ":pclose": Close any preview window.
6508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512{
6513 win_T *win;
6514
6515 for (win = firstwin; win != NULL; win = win->w_next)
6516 if (win->w_p_pvw)
6517 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006518 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 break;
6520 }
6521}
Bram Moolenaard9967712006-03-11 21:18:15 +00006522# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524/*
6525 * Close window "win" and take care of handling closing the last window for a
6526 * modified buffer.
6527 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006528 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006529ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006532 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 int need_hide;
6535 buf_T *buf = win->w_buffer;
6536
6537 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006538 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006540# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if ((p_confirm || cmdmod.confirm) && p_write)
6542 {
6543 dialog_changed(buf, FALSE);
6544 if (buf_valid(buf) && bufIsChanged(buf))
6545 return;
6546 need_hide = FALSE;
6547 }
6548 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551 EMSG(_(e_nowrtmsg));
6552 return;
6553 }
6554 }
6555
Bram Moolenaard9967712006-03-11 21:18:15 +00006556# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006558# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006561 if (tp == NULL)
6562 win_close(win, !need_hide && !P_HID(buf));
6563 else
6564 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568 * ":tabclose": close current tab page, unless it is the last one.
6569 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 */
6571 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006572ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 exarg_T *eap;
6574{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006575 tabpage_T *tp;
6576
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006577# ifdef FEAT_CMDWIN
6578 if (cmdwin_type != 0)
6579 cmdwin_result = K_IGNORE;
6580 else
6581# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006582 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006583 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006584 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006586 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006587 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006588 tp = find_tabpage((int)eap->line2);
6589 if (tp == NULL)
6590 {
6591 beep_flush();
6592 return;
6593 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006594 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006595 {
6596 tabpage_close_other(tp, eap->forceit);
6597 return;
6598 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006599 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006600 if (!text_locked()
6601#ifdef FEAT_AUTOCMD
6602 && !curbuf_locked()
6603#endif
6604 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006605 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006607}
6608
6609/*
6610 * ":tabonly": close all tab pages except the current one
6611 */
6612 static void
6613ex_tabonly(eap)
6614 exarg_T *eap;
6615{
6616 tabpage_T *tp;
6617 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006618
6619# ifdef FEAT_CMDWIN
6620 if (cmdwin_type != 0)
6621 cmdwin_result = K_IGNORE;
6622 else
6623# endif
6624 if (first_tabpage->tp_next == NULL)
6625 MSG(_("Already only one tab page"));
6626 else
6627 {
6628 /* Repeat this up to a 1000 times, because autocommands may mess
6629 * up the lists. */
6630 for (done = 0; done < 1000; ++done)
6631 {
6632 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6633 if (tp->tp_topframe != topframe)
6634 {
6635 tabpage_close_other(tp, eap->forceit);
6636 /* if we failed to close it quit */
6637 if (valid_tabpage(tp))
6638 done = 1000;
6639 /* start over, "tp" is now invalid */
6640 break;
6641 }
6642 if (first_tabpage->tp_next == NULL)
6643 break;
6644 }
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006649 * Close the current tab page.
6650 */
6651 void
6652tabpage_close(forceit)
6653 int forceit;
6654{
6655 /* First close all the windows but the current one. If that worked then
6656 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006657 if (lastwin != firstwin)
6658 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006659 if (lastwin == firstwin)
6660 ex_win_close(forceit, curwin, NULL);
6661# ifdef FEAT_GUI
6662 need_mouse_correct = TRUE;
6663# endif
6664}
6665
6666/*
6667 * Close tab page "tp", which is not the current tab page.
6668 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006669 * Also takes care of the tab pages line disappearing when closing the
6670 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006671 */
6672 void
6673tabpage_close_other(tp, forceit)
6674 tabpage_T *tp;
6675 int forceit;
6676{
6677 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006678 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006679 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006680
6681 /* Limit to 1000 windows, autocommands may add a window while we close
6682 * one. OK, so I'm paranoid... */
6683 while (++done < 1000)
6684 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006685 wp = tp->tp_firstwin;
6686 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006687
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006688 /* Autocommands may delete the tab page under our fingers and we may
6689 * fail to close a window with a modified buffer. */
6690 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006691 break;
6692 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006693
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006694 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006695 if (h != tabline_height())
6696 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697}
6698
6699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 * ":only".
6701 */
6702 static void
6703ex_only(eap)
6704 exarg_T *eap;
6705{
6706# ifdef FEAT_GUI
6707 need_mouse_correct = TRUE;
6708# endif
6709 close_others(TRUE, eap->forceit);
6710}
6711
6712/*
6713 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006714 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006716 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717ex_all(eap)
6718 exarg_T *eap;
6719{
6720 if (eap->addr_count == 0)
6721 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006722 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724#endif /* FEAT_WINDOWS */
6725
6726 static void
6727ex_hide(eap)
6728 exarg_T *eap;
6729{
6730 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6731 eap->errmsg = e_invarg;
6732 else
6733 {
6734 /* ":hide" or ":hide | cmd": hide current window */
6735 eap->nextcmd = check_nextcmd(eap->arg);
6736#ifdef FEAT_WINDOWS
6737 if (!eap->skip)
6738 {
6739# ifdef FEAT_GUI
6740 need_mouse_correct = TRUE;
6741# endif
6742 win_close(curwin, FALSE); /* don't free buffer */
6743 }
6744#endif
6745 }
6746}
6747
6748/*
6749 * ":stop" and ":suspend": Suspend Vim.
6750 */
6751 static void
6752ex_stop(eap)
6753 exarg_T *eap;
6754{
6755 /*
6756 * Disallow suspending for "rvim".
6757 */
6758 if (!check_restricted()
6759#ifdef WIN3264
6760 /*
6761 * Check if external commands are allowed now.
6762 */
6763 && can_end_termcap_mode(TRUE)
6764#endif
6765 )
6766 {
6767 if (!eap->forceit)
6768 autowrite_all();
6769 windgoto((int)Rows - 1, 0);
6770 out_char('\n');
6771 out_flush();
6772 stoptermcap();
6773 out_flush(); /* needed for SUN to restore xterm buffer */
6774#ifdef FEAT_TITLE
6775 mch_restore_title(3); /* restore window titles */
6776#endif
6777 ui_suspend(); /* call machine specific function */
6778#ifdef FEAT_TITLE
6779 maketitle();
6780 resettitle(); /* force updating the title */
6781#endif
6782 starttermcap();
6783 scroll_start(); /* scroll screen before redrawing */
6784 redraw_later_clear();
6785 shell_resized(); /* may have resized window */
6786 }
6787}
6788
6789/*
6790 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6791 */
6792 static void
6793ex_exit(eap)
6794 exarg_T *eap;
6795{
6796#ifdef FEAT_CMDWIN
6797 if (cmdwin_type != 0)
6798 {
6799 cmdwin_result = Ctrl_C;
6800 return;
6801 }
6802#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006803 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006804 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006805 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006806 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006807 return;
6808 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006809#ifdef FEAT_AUTOCMD
6810 if (curbuf_locked())
6811 return;
6812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 /*
6815 * if more files or windows we won't exit
6816 */
6817 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6818 exiting = TRUE;
6819 if ( ((eap->cmdidx == CMD_wq
6820 || curbufIsChanged())
6821 && do_write(eap) == FAIL)
6822 || check_more(TRUE, eap->forceit) == FAIL
6823 || (only_one_window() && check_changed_any(eap->forceit)))
6824 {
6825 not_exiting();
6826 }
6827 else
6828 {
6829#ifdef FEAT_WINDOWS
6830 if (only_one_window()) /* quit last window, exit Vim */
6831#endif
6832 getout(0);
6833#ifdef FEAT_WINDOWS
6834# ifdef FEAT_GUI
6835 need_mouse_correct = TRUE;
6836# endif
6837 /* quit current window, may free buffer */
6838 win_close(curwin, !P_HID(curwin->w_buffer));
6839#endif
6840 }
6841}
6842
6843/*
6844 * ":print", ":list", ":number".
6845 */
6846 static void
6847ex_print(eap)
6848 exarg_T *eap;
6849{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006850 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6851 EMSG(_(e_emptybuf));
6852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006854 for ( ;!got_int; ui_breakcheck())
6855 {
6856 print_line(eap->line1,
6857 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6858 || (eap->flags & EXFLAG_NR)),
6859 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6860 if (++eap->line1 > eap->line2)
6861 break;
6862 out_flush(); /* show one line at a time */
6863 }
6864 setpcmark();
6865 /* put cursor at last line */
6866 curwin->w_cursor.lnum = eap->line2;
6867 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871}
6872
6873#ifdef FEAT_BYTEOFF
6874 static void
6875ex_goto(eap)
6876 exarg_T *eap;
6877{
6878 goto_byte(eap->line2);
6879}
6880#endif
6881
6882/*
6883 * ":shell".
6884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 static void
6886ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006887 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889 do_shell(NULL, 0);
6890}
6891
6892#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6893 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006894 || defined(FEAT_GUI_MSWIN) \
6895 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 || defined(PROTO)
6897
6898/*
6899 * Handle a file drop. The code is here because a drop is *nearly* like an
6900 * :args command, but not quite (we have a list of exact filenames, so we
6901 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6902 * code is very similar to :args and hence needs access to a lot of the static
6903 * functions in this file.
6904 *
6905 * The list should be allocated using alloc(), as should each item in the
6906 * list. This function takes over responsibility for freeing the list.
6907 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006908 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6910 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6911 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6912 * file functionality is (currently) not in EMX this is not presently a
6913 * problem.
6914 */
6915 void
6916handle_drop(filec, filev, split)
6917 int filec; /* the number of files dropped */
6918 char_u **filev; /* the list of files dropped */
6919 int split; /* force splitting the window */
6920{
6921 exarg_T ea;
6922 int save_msg_scroll = msg_scroll;
6923
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006924 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006925 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927#ifdef FEAT_AUTOCMD
6928 if (curbuf_locked())
6929 return;
6930#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006931 /* When the screen is being updated we should not change buffers and
6932 * windows structures, it may cause freed memory to be used. */
6933 if (updating_screen)
6934 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
6936 /* Check whether the current buffer is changed. If so, we will need
6937 * to split the current window or data could be lost.
6938 * We don't need to check if the 'hidden' option is set, as in this
6939 * case the buffer won't be lost.
6940 */
6941 if (!P_HID(curbuf) && !split)
6942 {
6943 ++emsg_off;
6944 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6945 --emsg_off;
6946 }
6947 if (split)
6948 {
6949# ifdef FEAT_WINDOWS
6950 if (win_split(0, 0) == FAIL)
6951 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006952 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 /* When splitting the window, create a new alist. Otherwise the
6955 * existing one is overwritten. */
6956 alist_unlink(curwin->w_alist);
6957 alist_new();
6958# else
6959 return; /* can't split, always fail */
6960# endif
6961 }
6962
6963 /*
6964 * Set up the new argument list.
6965 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006966 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
6968 /*
6969 * Move to the first file.
6970 */
6971 /* Fake up a minimal "next" command for do_argfile() */
6972 vim_memset(&ea, 0, sizeof(ea));
6973 ea.cmd = (char_u *)"next";
6974 do_argfile(&ea, 0);
6975
6976 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6977 * mode that is not needed here. */
6978 need_start_insertmode = FALSE;
6979
6980 /* Restore msg_scroll, otherwise a following command may cause scrolling
6981 * unexpectedly. The screen will be redrawn by the caller, thus
6982 * msg_scroll being set by displaying a message is irrelevant. */
6983 msg_scroll = save_msg_scroll;
6984}
6985#endif
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987/*
6988 * Clear an argument list: free all file names and reset it to zero entries.
6989 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006990 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991alist_clear(al)
6992 alist_T *al;
6993{
6994 while (--al->al_ga.ga_len >= 0)
6995 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6996 ga_clear(&al->al_ga);
6997}
6998
6999/*
7000 * Init an argument list.
7001 */
7002 void
7003alist_init(al)
7004 alist_T *al;
7005{
7006 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7007}
7008
7009#if defined(FEAT_WINDOWS) || defined(PROTO)
7010
7011/*
7012 * Remove a reference from an argument list.
7013 * Ignored when the argument list is the global one.
7014 * If the argument list is no longer used by any window, free it.
7015 */
7016 void
7017alist_unlink(al)
7018 alist_T *al;
7019{
7020 if (al != &global_alist && --al->al_refcount <= 0)
7021 {
7022 alist_clear(al);
7023 vim_free(al);
7024 }
7025}
7026
7027# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7028/*
7029 * Create a new argument list and use it for the current window.
7030 */
7031 void
7032alist_new()
7033{
7034 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7035 if (curwin->w_alist == NULL)
7036 {
7037 curwin->w_alist = &global_alist;
7038 ++global_alist.al_refcount;
7039 }
7040 else
7041 {
7042 curwin->w_alist->al_refcount = 1;
7043 alist_init(curwin->w_alist);
7044 }
7045}
7046# endif
7047#endif
7048
7049#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7050/*
7051 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007052 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7053 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 */
7055 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007056alist_expand(fnum_list, fnum_len)
7057 int *fnum_list;
7058 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007061 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 char_u **new_arg_files;
7063 int new_arg_file_count;
7064 char_u *save_p_su = p_su;
7065 int i;
7066
7067 /* Don't use 'suffixes' here. This should work like the shell did the
7068 * expansion. Also, the vimrc file isn't read yet, thus the user
7069 * can't set the options. */
7070 p_su = empty_option;
7071 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7072 if (old_arg_files != NULL)
7073 {
7074 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007075 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7076 old_arg_count = GARGCOUNT;
7077 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007079 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 && new_arg_file_count > 0)
7081 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007082 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7083 TRUE, fnum_list, fnum_len);
7084 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 p_su = save_p_su;
7088}
7089#endif
7090
7091/*
7092 * Set the argument list for the current window.
7093 * Takes over the allocated files[] and the allocated fnames in it.
7094 */
7095 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007096alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 alist_T *al;
7098 int count;
7099 char_u **files;
7100 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007101 int *fnum_list;
7102 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 int i;
7105
7106 alist_clear(al);
7107 if (ga_grow(&al->al_ga, count) == OK)
7108 {
7109 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007110 {
7111 if (got_int)
7112 {
7113 /* When adding many buffers this can take a long time. Allow
7114 * interrupting here. */
7115 while (i < count)
7116 vim_free(files[i++]);
7117 break;
7118 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007119
7120 /* May set buffer name of a buffer previously used for the
7121 * argument list, so that it's re-used by alist_add. */
7122 if (fnum_list != NULL && i < fnum_len)
7123 buf_set_name(fnum_list[i], files[i]);
7124
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007126 ui_breakcheck();
7127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 vim_free(files);
7129 }
7130 else
7131 FreeWild(count, files);
7132#ifdef FEAT_WINDOWS
7133 if (al == &global_alist)
7134#endif
7135 arg_had_last = FALSE;
7136}
7137
7138/*
7139 * Add file "fname" to argument list "al".
7140 * "fname" must have been allocated and "al" must have been checked for room.
7141 */
7142 void
7143alist_add(al, fname, set_fnum)
7144 alist_T *al;
7145 char_u *fname;
7146 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7147{
7148 if (fname == NULL) /* don't add NULL file names */
7149 return;
7150#ifdef BACKSLASH_IN_FILENAME
7151 slash_adjust(fname);
7152#endif
7153 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7154 if (set_fnum > 0)
7155 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7156 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7157 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
7160#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7161/*
7162 * Adjust slashes in file names. Called after 'shellslash' was set.
7163 */
7164 void
7165alist_slash_adjust()
7166{
7167 int i;
7168# ifdef FEAT_WINDOWS
7169 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007170 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171# endif
7172
7173 for (i = 0; i < GARGCOUNT; ++i)
7174 if (GARGLIST[i].ae_fname != NULL)
7175 slash_adjust(GARGLIST[i].ae_fname);
7176# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007177 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 if (wp->w_alist != &global_alist)
7179 for (i = 0; i < WARGCOUNT(wp); ++i)
7180 if (WARGLIST(wp)[i].ae_fname != NULL)
7181 slash_adjust(WARGLIST(wp)[i].ae_fname);
7182# endif
7183}
7184#endif
7185
7186/*
7187 * ":preserve".
7188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 static void
7190ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007191 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007193 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 ml_preserve(curbuf, TRUE);
7195}
7196
7197/*
7198 * ":recover".
7199 */
7200 static void
7201ex_recover(eap)
7202 exarg_T *eap;
7203{
7204 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7205 recoverymode = TRUE;
7206 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7207 && (*eap->arg == NUL
7208 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7209 ml_recover();
7210 recoverymode = FALSE;
7211}
7212
7213/*
7214 * Command modifier used in a wrong way.
7215 */
7216 static void
7217ex_wrongmodifier(eap)
7218 exarg_T *eap;
7219{
7220 eap->errmsg = e_invcmd;
7221}
7222
7223#ifdef FEAT_WINDOWS
7224/*
7225 * :sview [+command] file split window with new file, read-only
7226 * :split [[+command] file] split window with current or new file
7227 * :vsplit [[+command] file] split window vertically with current or new file
7228 * :new [[+command] file] split window with no or new file
7229 * :vnew [[+command] file] split vertically window with no or new file
7230 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007231 *
7232 * :tabedit open new Tab page with empty window
7233 * :tabedit [+command] file open new Tab page and edit "file"
7234 * :tabnew [[+command] file] just like :tabedit
7235 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 */
7237 void
7238ex_splitview(eap)
7239 exarg_T *eap;
7240{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007241 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007242# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007244# endif
7245# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007249# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7251 {
7252 ex_ni(eap);
7253 return;
7254 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007257# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007263 * quickfix windows. But it's OK when doing ":tab split". */
7264 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 if (eap->cmdidx == CMD_split)
7267 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007268# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (eap->cmdidx == CMD_vsplit)
7270 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007275# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007276 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
7278 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7279 FNAME_MESS, TRUE, curbuf->b_ffname);
7280 if (fname == NULL)
7281 goto theend;
7282 eap->arg = fname;
7283 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007290# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007292# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 && eap->cmdidx != CMD_new)
7294 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007295# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007296 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007297# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007298 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007299# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007300 au_has_group((char_u *)"FileExplorer"))
7301 {
7302 /* No browsing supported but we do have the file explorer:
7303 * Edit the directory. */
7304 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7305 eap->arg = (char_u *)".";
7306 }
7307 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007308# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007309 {
7310 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007312 if (fname == NULL)
7313 goto theend;
7314 eap->arg = fname;
7315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007320 /*
7321 * Either open new tab page or split the window.
7322 */
7323 if (eap->cmdidx == CMD_tabedit
7324 || eap->cmdidx == CMD_tabfind
7325 || eap->cmdidx == CMD_tabnew)
7326 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007327 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7328 : eap->addr_count == 0 ? 0
7329 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007330 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007331 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007332
7333 /* set the alternate buffer for the window we came from */
7334 if (curwin != old_curwin
7335 && win_valid(old_curwin)
7336 && old_curwin->w_buffer != curbuf
7337 && !cmdmod.keepalt)
7338 old_curwin->w_alt_fnum = curbuf->b_fnum;
7339 }
7340 }
7341 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7343 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007344# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 /* Reset 'scrollbind' when editing another file, but keep it when
7346 * doing ":split" without arguments. */
7347 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007348# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007352 {
7353 RESET_BINDING(curwin);
7354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 else
7356 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 do_exedit(eap, old_curwin);
7359 }
7360
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007365# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366theend:
7367 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007370
7371/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007372 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373 */
7374 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007375tabpage_new()
7376{
7377 exarg_T ea;
7378
7379 vim_memset(&ea, 0, sizeof(ea));
7380 ea.cmdidx = CMD_tabnew;
7381 ea.cmd = (char_u *)"tabn";
7382 ea.arg = (char_u *)"";
7383 ex_splitview(&ea);
7384}
7385
7386/*
7387 * :tabnext command
7388 */
7389 static void
7390ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007391 exarg_T *eap;
7392{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007393 switch (eap->cmdidx)
7394 {
7395 case CMD_tabfirst:
7396 case CMD_tabrewind:
7397 goto_tabpage(1);
7398 break;
7399 case CMD_tablast:
7400 goto_tabpage(9999);
7401 break;
7402 case CMD_tabprevious:
7403 case CMD_tabNext:
7404 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7405 break;
7406 default: /* CMD_tabnext */
7407 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7408 break;
7409 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007410}
7411
7412/*
7413 * :tabmove command
7414 */
7415 static void
7416ex_tabmove(eap)
7417 exarg_T *eap;
7418{
7419 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420}
7421
7422/*
7423 * :tabs command: List tabs and their contents.
7424 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425 static void
7426ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007427 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428{
7429 tabpage_T *tp;
7430 win_T *wp;
7431 int tabcount = 1;
7432
7433 msg_start();
7434 msg_scroll = TRUE;
7435 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7436 {
7437 msg_putchar('\n');
7438 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7439 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7440 out_flush(); /* output one line at a time */
7441 ui_breakcheck();
7442
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007443 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007444 wp = firstwin;
7445 else
7446 wp = tp->tp_firstwin;
7447 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7448 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007449 msg_putchar('\n');
7450 msg_putchar(wp == curwin ? '>' : ' ');
7451 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007452 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7453 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007454 if (buf_spname(wp->w_buffer) != NULL)
7455 STRCPY(IObuff, buf_spname(wp->w_buffer));
7456 else
7457 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7458 IObuff, IOSIZE, TRUE);
7459 msg_outtrans(IObuff);
7460 out_flush(); /* output one line at a time */
7461 ui_breakcheck();
7462 }
7463 }
7464}
7465
7466#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468/*
7469 * ":mode": Set screen mode.
7470 * If no argument given, just get the screen size and redraw.
7471 */
7472 static void
7473ex_mode(eap)
7474 exarg_T *eap;
7475{
7476 if (*eap->arg == NUL)
7477 shell_resized();
7478 else
7479 mch_screenmode(eap->arg);
7480}
7481
7482#ifdef FEAT_WINDOWS
7483/*
7484 * ":resize".
7485 * set, increment or decrement current window height
7486 */
7487 static void
7488ex_resize(eap)
7489 exarg_T *eap;
7490{
7491 int n;
7492 win_T *wp = curwin;
7493
7494 if (eap->addr_count > 0)
7495 {
7496 n = eap->line2;
7497 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7498 ;
7499 }
7500
7501#ifdef FEAT_GUI
7502 need_mouse_correct = TRUE;
7503#endif
7504 n = atol((char *)eap->arg);
7505#ifdef FEAT_VERTSPLIT
7506 if (cmdmod.split & WSP_VERT)
7507 {
7508 if (*eap->arg == '-' || *eap->arg == '+')
7509 n += W_WIDTH(curwin);
7510 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7511 n = 9999;
7512 win_setwidth_win((int)n, wp);
7513 }
7514 else
7515#endif
7516 {
7517 if (*eap->arg == '-' || *eap->arg == '+')
7518 n += curwin->w_height;
7519 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7520 n = 9999;
7521 win_setheight_win((int)n, wp);
7522 }
7523}
7524#endif
7525
7526/*
7527 * ":find [+command] <file>" command.
7528 */
7529 static void
7530ex_find(eap)
7531 exarg_T *eap;
7532{
7533#ifdef FEAT_SEARCHPATH
7534 char_u *fname;
7535 int count;
7536
7537 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7538 TRUE, curbuf->b_ffname);
7539 if (eap->addr_count > 0)
7540 {
7541 /* Repeat finding the file "count" times. This matters when it
7542 * appears several times in the path. */
7543 count = eap->line2;
7544 while (fname != NULL && --count > 0)
7545 {
7546 vim_free(fname);
7547 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7548 FALSE, curbuf->b_ffname);
7549 }
7550 }
7551
7552 if (fname != NULL)
7553 {
7554 eap->arg = fname;
7555#endif
7556 do_exedit(eap, NULL);
7557#ifdef FEAT_SEARCHPATH
7558 vim_free(fname);
7559 }
7560#endif
7561}
7562
7563/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007564 * ":open" simulation: for now just work like ":visual".
7565 */
7566 static void
7567ex_open(eap)
7568 exarg_T *eap;
7569{
7570 regmatch_T regmatch;
7571 char_u *p;
7572
7573 curwin->w_cursor.lnum = eap->line2;
7574 beginline(BL_SOL | BL_FIX);
7575 if (*eap->arg == '/')
7576 {
7577 /* ":open /pattern/": put cursor in column found with pattern */
7578 ++eap->arg;
7579 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7580 *p = NUL;
7581 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7582 if (regmatch.regprog != NULL)
7583 {
7584 regmatch.rm_ic = p_ic;
7585 p = ml_get_curline();
7586 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007587 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007588 else
7589 EMSG(_(e_nomatch));
7590 vim_free(regmatch.regprog);
7591 }
7592 /* Move to the NUL, ignore any other arguments. */
7593 eap->arg += STRLEN(eap->arg);
7594 }
7595 check_cursor();
7596
7597 eap->cmdidx = CMD_visual;
7598 do_exedit(eap, NULL);
7599}
7600
7601/*
7602 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 */
7604 static void
7605ex_edit(eap)
7606 exarg_T *eap;
7607{
7608 do_exedit(eap, NULL);
7609}
7610
7611/*
7612 * ":edit <file>" command and alikes.
7613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 void
7615do_exedit(eap, old_curwin)
7616 exarg_T *eap;
7617 win_T *old_curwin; /* curwin before doing a split or NULL */
7618{
7619 int n;
7620#ifdef FEAT_WINDOWS
7621 int need_hide;
7622#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007623 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625 /*
7626 * ":vi" command ends Ex mode.
7627 */
7628 if (exmode_active && (eap->cmdidx == CMD_visual
7629 || eap->cmdidx == CMD_view))
7630 {
7631 exmode_active = FALSE;
7632 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007633 {
7634 /* Special case: ":global/pat/visual\NLvi-commands" */
7635 if (global_busy)
7636 {
7637 int rd = RedrawingDisabled;
7638 int nwr = no_wait_return;
7639 int ms = msg_scroll;
7640#ifdef FEAT_GUI
7641 int he = hold_gui_events;
7642#endif
7643
7644 if (eap->nextcmd != NULL)
7645 {
7646 stuffReadbuff(eap->nextcmd);
7647 eap->nextcmd = NULL;
7648 }
7649
7650 if (exmode_was != EXMODE_VIM)
7651 settmode(TMODE_RAW);
7652 RedrawingDisabled = 0;
7653 no_wait_return = 0;
7654 need_wait_return = FALSE;
7655 msg_scroll = 0;
7656#ifdef FEAT_GUI
7657 hold_gui_events = 0;
7658#endif
7659 must_redraw = CLEAR;
7660
7661 main_loop(FALSE, TRUE);
7662
7663 RedrawingDisabled = rd;
7664 no_wait_return = nwr;
7665 msg_scroll = ms;
7666#ifdef FEAT_GUI
7667 hold_gui_events = he;
7668#endif
7669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 }
7673
7674 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007675 || eap->cmdidx == CMD_tabnew
7676 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677#ifdef FEAT_VERTSPLIT
7678 || eap->cmdidx == CMD_vnew
7679#endif
7680 ) && *eap->arg == NUL)
7681 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007682 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 setpcmark();
7684 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007685 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7686 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 else if ((eap->cmdidx != CMD_split
7689#ifdef FEAT_VERTSPLIT
7690 && eap->cmdidx != CMD_vsplit
7691#endif
7692 )
7693 || *eap->arg != NUL
7694#ifdef FEAT_BROWSE
7695 || cmdmod.browse
7696#endif
7697 )
7698 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007699#ifdef FEAT_AUTOCMD
7700 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7701 * can bring us here, others are stopped earlier. */
7702 if (*eap->arg != NUL && curbuf_locked())
7703 return;
7704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 n = readonlymode;
7706 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7707 readonlymode = TRUE;
7708 else if (eap->cmdidx == CMD_enew)
7709 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7710 empty buffer */
7711 setpcmark();
7712 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7713 NULL, eap,
7714 /* ":edit" goes to first line if Vi compatible */
7715 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7716 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7717 ? ECMD_ONE : eap->do_ecmd_lnum,
7718 (P_HID(curbuf) ? ECMD_HIDE : 0)
7719 + (eap->forceit ? ECMD_FORCEIT : 0)
7720#ifdef FEAT_LISTCMDS
7721 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7722#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007723 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {
7725 /* Editing the file failed. If the window was split, close it. */
7726#ifdef FEAT_WINDOWS
7727 if (old_curwin != NULL)
7728 {
7729 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7730 if (!need_hide || P_HID(curbuf))
7731 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007732# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7733 cleanup_T cs;
7734
7735 /* Reset the error/interrupt/exception state here so that
7736 * aborting() returns FALSE when closing a window. */
7737 enter_cleanup(&cs);
7738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739# ifdef FEAT_GUI
7740 need_mouse_correct = TRUE;
7741# endif
7742 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007743
7744# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7745 /* Restore the error/interrupt/exception state if not
7746 * discarded by a new aborting error, interrupt, or
7747 * uncaught exception. */
7748 leave_cleanup(&cs);
7749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 }
7751 }
7752#endif
7753 }
7754 else if (readonlymode && curbuf->b_nwindows == 1)
7755 {
7756 /* When editing an already visited buffer, 'readonly' won't be set
7757 * but the previous value is kept. With ":view" and ":sview" we
7758 * want the file to be readonly, except when another window is
7759 * editing the same buffer. */
7760 curbuf->b_p_ro = TRUE;
7761 }
7762 readonlymode = n;
7763 }
7764 else
7765 {
7766 if (eap->do_ecmd_cmd != NULL)
7767 do_cmdline_cmd(eap->do_ecmd_cmd);
7768#ifdef FEAT_TITLE
7769 n = curwin->w_arg_idx_invalid;
7770#endif
7771 check_arg_idx(curwin);
7772#ifdef FEAT_TITLE
7773 if (n != curwin->w_arg_idx_invalid)
7774 maketitle();
7775#endif
7776 }
7777
7778#ifdef FEAT_WINDOWS
7779 /*
7780 * if ":split file" worked, set alternate file name in old window to new
7781 * file
7782 */
7783 if (old_curwin != NULL
7784 && *eap->arg != NUL
7785 && curwin != old_curwin
7786 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007787 && old_curwin->w_buffer != curbuf
7788 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 old_curwin->w_alt_fnum = curbuf->b_fnum;
7790#endif
7791
7792 ex_no_reprint = TRUE;
7793}
7794
7795#ifndef FEAT_GUI
7796/*
7797 * ":gui" and ":gvim" when there is no GUI.
7798 */
7799 static void
7800ex_nogui(eap)
7801 exarg_T *eap;
7802{
7803 eap->errmsg = e_nogvim;
7804}
7805#endif
7806
7807#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7808 static void
7809ex_tearoff(eap)
7810 exarg_T *eap;
7811{
7812 gui_make_tearoff(eap->arg);
7813}
7814#endif
7815
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007816#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 static void
7818ex_popup(eap)
7819 exarg_T *eap;
7820{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007821 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823#endif
7824
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 static void
7826ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007827 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7830 MSG(_("No swap file"));
7831 else
7832 msg(curbuf->b_ml.ml_mfp->mf_fname);
7833}
7834
7835/*
7836 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7837 * offset.
7838 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 static void
7841ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007842 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
7844#ifdef FEAT_SCROLLBIND
7845 win_T *wp;
7846 long topline;
7847 long y;
7848 linenr_T old_linenr = curwin->w_cursor.lnum;
7849
7850 setpcmark();
7851
7852 /*
7853 * determine max topline
7854 */
7855 if (curwin->w_p_scb)
7856 {
7857 topline = curwin->w_topline;
7858 for (wp = firstwin; wp; wp = wp->w_next)
7859 {
7860 if (wp->w_p_scb && wp->w_buffer)
7861 {
7862 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7863 if (topline > y)
7864 topline = y;
7865 }
7866 }
7867 if (topline < 1)
7868 topline = 1;
7869 }
7870 else
7871 {
7872 topline = 1;
7873 }
7874
7875
7876 /*
7877 * set all scrollbind windows to the same topline
7878 */
7879 wp = curwin;
7880 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7881 {
7882 if (curwin->w_p_scb)
7883 {
7884 y = topline - curwin->w_topline;
7885 if (y > 0)
7886 scrollup(y, TRUE);
7887 else
7888 scrolldown(-y, TRUE);
7889 curwin->w_scbind_pos = topline;
7890 redraw_later(VALID);
7891 cursor_correct();
7892#ifdef FEAT_WINDOWS
7893 curwin->w_redr_status = TRUE;
7894#endif
7895 }
7896 }
7897 curwin = wp;
7898 if (curwin->w_p_scb)
7899 {
7900 did_syncbind = TRUE;
7901 checkpcmark();
7902 if (old_linenr != curwin->w_cursor.lnum)
7903 {
7904 char_u ctrl_o[2];
7905
7906 ctrl_o[0] = Ctrl_O;
7907 ctrl_o[1] = 0;
7908 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7909 }
7910 }
7911#endif
7912}
7913
7914
7915 static void
7916ex_read(eap)
7917 exarg_T *eap;
7918{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007919 int i;
7920 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7921 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923 if (eap->usefilter) /* :r!cmd */
7924 do_bang(1, eap, FALSE, FALSE, TRUE);
7925 else
7926 {
7927 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7928 return;
7929
7930#ifdef FEAT_BROWSE
7931 if (cmdmod.browse)
7932 {
7933 char_u *browseFile;
7934
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007935 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 NULL, NULL, NULL, curbuf);
7937 if (browseFile != NULL)
7938 {
7939 i = readfile(browseFile, NULL,
7940 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7941 vim_free(browseFile);
7942 }
7943 else
7944 i = OK;
7945 }
7946 else
7947#endif
7948 if (*eap->arg == NUL)
7949 {
7950 if (check_fname() == FAIL) /* check for no file name */
7951 return;
7952 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7953 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7954 }
7955 else
7956 {
7957 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7958 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7959 i = readfile(eap->arg, NULL,
7960 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7961
7962 }
7963 if (i == FAIL)
7964 {
7965#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7966 if (!aborting())
7967#endif
7968 EMSG2(_(e_notopen), eap->arg);
7969 }
7970 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007971 {
7972 if (empty && exmode_active)
7973 {
7974 /* Delete the empty line that remains. Historically ex does
7975 * this but vi doesn't. */
7976 if (eap->line2 == 0)
7977 lnum = curbuf->b_ml.ml_line_count;
7978 else
7979 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007980 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007981 {
7982 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007983 if (curwin->w_cursor.lnum > 1
7984 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007985 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007986 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007987 }
7988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 }
7992}
7993
Bram Moolenaarea408852005-06-25 22:49:46 +00007994static char_u *prev_dir = NULL;
7995
7996#if defined(EXITFREE) || defined(PROTO)
7997 void
7998free_cd_dir()
7999{
8000 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008001 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008002
8003 vim_free(globaldir);
8004 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008005}
8006#endif
8007
8008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009/*
8010 * ":cd", ":lcd", ":chdir" and ":lchdir".
8011 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008012 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013ex_cd(eap)
8014 exarg_T *eap;
8015{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 char_u *new_dir;
8017 char_u *tofree;
8018
8019 new_dir = eap->arg;
8020#if !defined(UNIX) && !defined(VMS)
8021 /* for non-UNIX ":cd" means: print current directory */
8022 if (*new_dir == NUL)
8023 ex_pwd(NULL);
8024 else
8025#endif
8026 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008027#ifdef FEAT_AUTOCMD
8028 if (allbuf_locked())
8029 return;
8030#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008031 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8032 && !eap->forceit)
8033 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008034 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008035 return;
8036 }
8037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 /* ":cd -": Change to previous directory */
8039 if (STRCMP(new_dir, "-") == 0)
8040 {
8041 if (prev_dir == NULL)
8042 {
8043 EMSG(_("E186: No previous directory"));
8044 return;
8045 }
8046 new_dir = prev_dir;
8047 }
8048
8049 /* Save current directory for next ":cd -" */
8050 tofree = prev_dir;
8051 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8052 prev_dir = vim_strsave(NameBuff);
8053 else
8054 prev_dir = NULL;
8055
8056#if defined(UNIX) || defined(VMS)
8057 /* for UNIX ":cd" means: go to home directory */
8058 if (*new_dir == NUL)
8059 {
8060 /* use NameBuff for home directory name */
8061# ifdef VMS
8062 char_u *p;
8063
8064 p = mch_getenv((char_u *)"SYS$LOGIN");
8065 if (p == NULL || *p == NUL) /* empty is the same as not set */
8066 NameBuff[0] = NUL;
8067 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008068 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069# else
8070 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8071# endif
8072 new_dir = NameBuff;
8073 }
8074#endif
8075 if (new_dir == NULL || vim_chdir(new_dir))
8076 EMSG(_(e_failed));
8077 else
8078 {
8079 vim_free(curwin->w_localdir);
8080 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8081 {
8082 /* If still in global directory, need to remember current
8083 * directory as global directory. */
8084 if (globaldir == NULL && prev_dir != NULL)
8085 globaldir = vim_strsave(prev_dir);
8086 /* Remember this local directory for the window. */
8087 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8088 curwin->w_localdir = vim_strsave(NameBuff);
8089 }
8090 else
8091 {
8092 /* We are now in the global directory, no need to remember its
8093 * name. */
8094 vim_free(globaldir);
8095 globaldir = NULL;
8096 curwin->w_localdir = NULL;
8097 }
8098
8099 shorten_fnames(TRUE);
8100
8101 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008102 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 ex_pwd(eap);
8104 }
8105 vim_free(tofree);
8106 }
8107}
8108
8109/*
8110 * ":pwd".
8111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 static void
8113ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008114 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
8116 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8117 {
8118#ifdef BACKSLASH_IN_FILENAME
8119 slash_adjust(NameBuff);
8120#endif
8121 msg(NameBuff);
8122 }
8123 else
8124 EMSG(_("E187: Unknown"));
8125}
8126
8127/*
8128 * ":=".
8129 */
8130 static void
8131ex_equal(eap)
8132 exarg_T *eap;
8133{
8134 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008135 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136}
8137
8138 static void
8139ex_sleep(eap)
8140 exarg_T *eap;
8141{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008142 int n;
8143 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145 if (cursor_valid())
8146 {
8147 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8148 if (n >= 0)
8149 windgoto((int)n, curwin->w_wcol);
8150 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008151
8152 len = eap->line2;
8153 switch (*eap->arg)
8154 {
8155 case 'm': break;
8156 case NUL: len *= 1000L; break;
8157 default: EMSG2(_(e_invarg2), eap->arg); return;
8158 }
8159 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160}
8161
8162/*
8163 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8164 */
8165 void
8166do_sleep(msec)
8167 long msec;
8168{
8169 long done;
8170
8171 cursor_on();
8172 out_flush();
8173 for (done = 0; !got_int && done < msec; done += 1000L)
8174 {
8175 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8176 ui_breakcheck();
8177 }
8178}
8179
8180 static void
8181do_exmap(eap, isabbrev)
8182 exarg_T *eap;
8183 int isabbrev;
8184{
8185 int mode;
8186 char_u *cmdp;
8187
8188 cmdp = eap->cmd;
8189 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8190
8191 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8192 eap->arg, mode, isabbrev))
8193 {
8194 case 1: EMSG(_(e_invarg));
8195 break;
8196 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8197 break;
8198 }
8199}
8200
8201/*
8202 * ":winsize" command (obsolete).
8203 */
8204 static void
8205ex_winsize(eap)
8206 exarg_T *eap;
8207{
8208 int w, h;
8209 char_u *arg = eap->arg;
8210 char_u *p;
8211
8212 w = getdigits(&arg);
8213 arg = skipwhite(arg);
8214 p = arg;
8215 h = getdigits(&arg);
8216 if (*p != NUL && *arg == NUL)
8217 set_shellsize(w, h, TRUE);
8218 else
8219 EMSG(_("E465: :winsize requires two number arguments"));
8220}
8221
8222#ifdef FEAT_WINDOWS
8223 static void
8224ex_wincmd(eap)
8225 exarg_T *eap;
8226{
8227 int xchar = NUL;
8228 char_u *p;
8229
8230 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8231 {
8232 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8233 if (eap->arg[1] == NUL)
8234 {
8235 EMSG(_(e_invarg));
8236 return;
8237 }
8238 xchar = eap->arg[1];
8239 p = eap->arg + 2;
8240 }
8241 else
8242 p = eap->arg + 1;
8243
8244 eap->nextcmd = check_nextcmd(p);
8245 p = skipwhite(p);
8246 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8247 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008248 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {
8250 /* Pass flags on for ":vertical wincmd ]". */
8251 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008252 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8254 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008255 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 }
8257}
8258#endif
8259
Bram Moolenaar843ee412004-06-30 16:16:41 +00008260#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261/*
8262 * ":winpos".
8263 */
8264 static void
8265ex_winpos(eap)
8266 exarg_T *eap;
8267{
8268 int x, y;
8269 char_u *arg = eap->arg;
8270 char_u *p;
8271
8272 if (*arg == NUL)
8273 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008274# if defined(FEAT_GUI) || defined(MSWIN)
8275# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008277# else
8278 if (mch_get_winpos(&x, &y) != FAIL)
8279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8282 msg(IObuff);
8283 }
8284 else
8285# endif
8286 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8287 }
8288 else
8289 {
8290 x = getdigits(&arg);
8291 arg = skipwhite(arg);
8292 p = arg;
8293 y = getdigits(&arg);
8294 if (*p == NUL || *arg != NUL)
8295 {
8296 EMSG(_("E466: :winpos requires two number arguments"));
8297 return;
8298 }
8299# ifdef FEAT_GUI
8300 if (gui.in_use)
8301 gui_mch_set_winpos(x, y);
8302 else if (gui.starting)
8303 {
8304 /* Remember the coordinates for when the window is opened. */
8305 gui_win_x = x;
8306 gui_win_y = y;
8307 }
8308# ifdef HAVE_TGETENT
8309 else
8310# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008311# else
8312# ifdef MSWIN
8313 mch_set_winpos(x, y);
8314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315# endif
8316# ifdef HAVE_TGETENT
8317 if (*T_CWP)
8318 term_set_winpos(x, y);
8319# endif
8320 }
8321}
8322#endif
8323
8324/*
8325 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8326 */
8327 static void
8328ex_operators(eap)
8329 exarg_T *eap;
8330{
8331 oparg_T oa;
8332
8333 clear_oparg(&oa);
8334 oa.regname = eap->regname;
8335 oa.start.lnum = eap->line1;
8336 oa.end.lnum = eap->line2;
8337 oa.line_count = eap->line2 - eap->line1 + 1;
8338 oa.motion_type = MLINE;
8339#ifdef FEAT_VIRTUALEDIT
8340 virtual_op = FALSE;
8341#endif
8342 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8343 {
8344 setpcmark();
8345 curwin->w_cursor.lnum = eap->line1;
8346 beginline(BL_SOL | BL_FIX);
8347 }
8348
8349 switch (eap->cmdidx)
8350 {
8351 case CMD_delete:
8352 oa.op_type = OP_DELETE;
8353 op_delete(&oa);
8354 break;
8355
8356 case CMD_yank:
8357 oa.op_type = OP_YANK;
8358 (void)op_yank(&oa, FALSE, TRUE);
8359 break;
8360
8361 default: /* CMD_rshift or CMD_lshift */
8362 if ((eap->cmdidx == CMD_rshift)
8363#ifdef FEAT_RIGHTLEFT
8364 ^ curwin->w_p_rl
8365#endif
8366 )
8367 oa.op_type = OP_RSHIFT;
8368 else
8369 oa.op_type = OP_LSHIFT;
8370 op_shift(&oa, FALSE, eap->amount);
8371 break;
8372 }
8373#ifdef FEAT_VIRTUALEDIT
8374 virtual_op = MAYBE;
8375#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008376 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377}
8378
8379/*
8380 * ":put".
8381 */
8382 static void
8383ex_put(eap)
8384 exarg_T *eap;
8385{
8386 /* ":0put" works like ":1put!". */
8387 if (eap->line2 == 0)
8388 {
8389 eap->line2 = 1;
8390 eap->forceit = TRUE;
8391 }
8392 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008393 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8394 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395}
8396
8397/*
8398 * Handle ":copy" and ":move".
8399 */
8400 static void
8401ex_copymove(eap)
8402 exarg_T *eap;
8403{
8404 long n;
8405
8406 n = get_address(&eap->arg, FALSE, FALSE);
8407 if (eap->arg == NULL) /* error detected */
8408 {
8409 eap->nextcmd = NULL;
8410 return;
8411 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008412 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 /*
8415 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8416 */
8417 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8418 {
8419 EMSG(_(e_invaddr));
8420 return;
8421 }
8422
8423 if (eap->cmdidx == CMD_move)
8424 {
8425 if (do_move(eap->line1, eap->line2, n) == FAIL)
8426 return;
8427 }
8428 else
8429 ex_copy(eap->line1, eap->line2, n);
8430 u_clearline();
8431 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008432 ex_may_print(eap);
8433}
8434
8435/*
8436 * Print the current line if flags were given to the Ex command.
8437 */
8438 static void
8439ex_may_print(eap)
8440 exarg_T *eap;
8441{
8442 if (eap->flags != 0)
8443 {
8444 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8445 (eap->flags & EXFLAG_LIST));
8446 ex_no_reprint = TRUE;
8447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448}
8449
8450/*
8451 * ":smagic" and ":snomagic".
8452 */
8453 static void
8454ex_submagic(eap)
8455 exarg_T *eap;
8456{
8457 int magic_save = p_magic;
8458
8459 p_magic = (eap->cmdidx == CMD_smagic);
8460 do_sub(eap);
8461 p_magic = magic_save;
8462}
8463
8464/*
8465 * ":join".
8466 */
8467 static void
8468ex_join(eap)
8469 exarg_T *eap;
8470{
8471 curwin->w_cursor.lnum = eap->line1;
8472 if (eap->line1 == eap->line2)
8473 {
8474 if (eap->addr_count >= 2) /* :2,2join does nothing */
8475 return;
8476 if (eap->line2 == curbuf->b_ml.ml_line_count)
8477 {
8478 beep_flush();
8479 return;
8480 }
8481 ++eap->line2;
8482 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008483 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008485 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486}
8487
8488/*
8489 * ":[addr]@r" or ":[addr]*r": execute register
8490 */
8491 static void
8492ex_at(eap)
8493 exarg_T *eap;
8494{
8495 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008496 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
8498 curwin->w_cursor.lnum = eap->line2;
8499
8500#ifdef USE_ON_FLY_SCROLL
8501 dont_scroll = TRUE; /* disallow scrolling here */
8502#endif
8503
8504 /* get the register name. No name means to use the previous one */
8505 c = *eap->arg;
8506 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8507 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008508 /* Put the register in the typeahead buffer with the "silent" flag. */
8509 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8510 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 else
8515 {
8516 int save_efr = exec_from_reg;
8517
8518 exec_from_reg = TRUE;
8519
8520 /*
8521 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008522 * Continue until the stuff buffer is empty and all added characters
8523 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008525 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8527
8528 exec_from_reg = save_efr;
8529 }
8530}
8531
8532/*
8533 * ":!".
8534 */
8535 static void
8536ex_bang(eap)
8537 exarg_T *eap;
8538{
8539 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8540}
8541
8542/*
8543 * ":undo".
8544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 static void
8546ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008547 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008549 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008550 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008551 else
8552 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008555#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008556 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008557ex_wundo(eap)
8558 exarg_T *eap;
8559{
8560 char_u hash[UNDO_HASH_SIZE];
8561
8562 u_compute_hash(hash);
8563 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8564}
8565
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008566 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008567ex_rundo(eap)
8568 exarg_T *eap;
8569{
8570 char_u hash[UNDO_HASH_SIZE];
8571
8572 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008573 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008574}
8575#endif
8576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577/*
8578 * ":redo".
8579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 static void
8581ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008582 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 u_redo(1);
8585}
8586
8587/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008588 * ":earlier" and ":later".
8589 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008590 static void
8591ex_later(eap)
8592 exarg_T *eap;
8593{
8594 long count = 0;
8595 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008596 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008597 char_u *p = eap->arg;
8598
8599 if (*p == NUL)
8600 count = 1;
8601 else if (isdigit(*p))
8602 {
8603 count = getdigits(&p);
8604 switch (*p)
8605 {
8606 case 's': ++p; sec = TRUE; break;
8607 case 'm': ++p; sec = TRUE; count *= 60; break;
8608 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008609 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8610 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008611 }
8612 }
8613
8614 if (*p != NUL)
8615 EMSG2(_(e_invarg2), eap->arg);
8616 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008617 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8618 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008619}
8620
8621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 * ":redir": start/stop redirection.
8623 */
8624 static void
8625ex_redir(eap)
8626 exarg_T *eap;
8627{
8628 char *mode;
8629 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008630 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
8632 if (STRICMP(eap->arg, "END") == 0)
8633 close_redir();
8634 else
8635 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008636 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008638 ++arg;
8639 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008641 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 mode = "a";
8643 }
8644 else
8645 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008646 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 close_redir();
8649
8650 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008651 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 if (fname == NULL)
8653 return;
8654#ifdef FEAT_BROWSE
8655 if (cmdmod.browse)
8656 {
8657 char_u *browseFile;
8658
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008659 browseFile = do_browse(BROWSE_SAVE,
8660 (char_u *)_("Save Redirection"),
8661 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 if (browseFile == NULL)
8663 return; /* operation cancelled */
8664 vim_free(fname);
8665 fname = browseFile;
8666 eap->forceit = TRUE; /* since dialog already asked */
8667 }
8668#endif
8669
8670 redir_fd = open_exfile(fname, eap->forceit, mode);
8671 vim_free(fname);
8672 }
8673#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008674 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 {
8676 /* redirect to a register a-z (resp. A-Z for appending) */
8677 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008678 ++arg;
8679 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008681 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008682 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008684 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008686 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008687 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008688 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008691 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008692 if (*arg == '>')
8693 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008694 /* Make register empty when not using @A-@Z and the
8695 * command is valid. */
8696 if (*arg == NUL && !isupper(redir_reg))
8697 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008699 }
8700 if (*arg != NUL)
8701 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008702 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008703 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008704 }
8705 }
8706 else if (*arg == '=' && arg[1] == '>')
8707 {
8708 int append;
8709
8710 /* redirect to a variable */
8711 close_redir();
8712 arg += 2;
8713
8714 if (*arg == '>')
8715 {
8716 ++arg;
8717 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
8719 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008720 append = FALSE;
8721
8722 if (var_redir_start(skipwhite(arg), append) == OK)
8723 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 }
8725#endif
8726
8727 /* TODO: redirect to a buffer */
8728
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008730 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008732
8733 /* Make sure redirection is not off. Can happen for cmdline completion
8734 * that indirectly invokes a command to catch its output. */
8735 if (redir_fd != NULL
8736#ifdef FEAT_EVAL
8737 || redir_reg || redir_vname
8738#endif
8739 )
8740 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741}
8742
8743/*
8744 * ":redraw": force redraw
8745 */
8746 static void
8747ex_redraw(eap)
8748 exarg_T *eap;
8749{
8750 int r = RedrawingDisabled;
8751 int p = p_lz;
8752
8753 RedrawingDisabled = 0;
8754 p_lz = FALSE;
8755 update_topline();
8756 update_screen(eap->forceit ? CLEAR :
8757#ifdef FEAT_VISUAL
8758 VIsual_active ? INVERTED :
8759#endif
8760 0);
8761#ifdef FEAT_TITLE
8762 if (need_maketitle)
8763 maketitle();
8764#endif
8765 RedrawingDisabled = r;
8766 p_lz = p;
8767
8768 /* Reset msg_didout, so that a message that's there is overwritten. */
8769 msg_didout = FALSE;
8770 msg_col = 0;
8771
8772 out_flush();
8773}
8774
8775/*
8776 * ":redrawstatus": force redraw of status line(s)
8777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 static void
8779ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008780 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782#if defined(FEAT_WINDOWS)
8783 int r = RedrawingDisabled;
8784 int p = p_lz;
8785
8786 RedrawingDisabled = 0;
8787 p_lz = FALSE;
8788 if (eap->forceit)
8789 status_redraw_all();
8790 else
8791 status_redraw_curbuf();
8792 update_screen(
8793# ifdef FEAT_VISUAL
8794 VIsual_active ? INVERTED :
8795# endif
8796 0);
8797 RedrawingDisabled = r;
8798 p_lz = p;
8799 out_flush();
8800#endif
8801}
8802
8803 static void
8804close_redir()
8805{
8806 if (redir_fd != NULL)
8807 {
8808 fclose(redir_fd);
8809 redir_fd = NULL;
8810 }
8811#ifdef FEAT_EVAL
8812 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008813 if (redir_vname)
8814 {
8815 var_redir_stop();
8816 redir_vname = 0;
8817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818#endif
8819}
8820
8821#if defined(FEAT_SESSION) && defined(USE_CRNL)
8822# define MKSESSION_NL
8823static int mksession_nl = FALSE; /* use NL only in put_eol() */
8824#endif
8825
8826/*
8827 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8828 */
8829 static void
8830ex_mkrc(eap)
8831 exarg_T *eap;
8832{
8833 FILE *fd;
8834 int failed = FALSE;
8835 char_u *fname;
8836#ifdef FEAT_BROWSE
8837 char_u *browseFile = NULL;
8838#endif
8839#ifdef FEAT_SESSION
8840 int view_session = FALSE;
8841 int using_vdir = FALSE; /* using 'viewdir'? */
8842 char_u *viewFile = NULL;
8843 unsigned *flagp;
8844#endif
8845
8846 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8847 {
8848#ifdef FEAT_SESSION
8849 view_session = TRUE;
8850#else
8851 ex_ni(eap);
8852 return;
8853#endif
8854 }
8855
8856#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008857 /* Use the short file name until ":lcd" is used. We also don't use the
8858 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008859 did_lcd = FALSE;
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8862 if (eap->cmdidx == CMD_mkview
8863 && (*eap->arg == NUL
8864 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8865 {
8866 eap->forceit = TRUE;
8867 fname = get_view_file(*eap->arg);
8868 if (fname == NULL)
8869 return;
8870 viewFile = fname;
8871 using_vdir = TRUE;
8872 }
8873 else
8874#endif
8875 if (*eap->arg != NUL)
8876 fname = eap->arg;
8877 else if (eap->cmdidx == CMD_mkvimrc)
8878 fname = (char_u *)VIMRC_FILE;
8879#ifdef FEAT_SESSION
8880 else if (eap->cmdidx == CMD_mksession)
8881 fname = (char_u *)SESSION_FILE;
8882#endif
8883 else
8884 fname = (char_u *)EXRC_FILE;
8885
8886#ifdef FEAT_BROWSE
8887 if (cmdmod.browse)
8888 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008889 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890# ifdef FEAT_SESSION
8891 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8892 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8893# endif
8894 (char_u *)_("Save Setup"),
8895 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8896 if (browseFile == NULL)
8897 goto theend;
8898 fname = browseFile;
8899 eap->forceit = TRUE; /* since dialog already asked */
8900 }
8901#endif
8902
8903#if defined(FEAT_SESSION) && defined(vim_mkdir)
8904 /* When using 'viewdir' may have to create the directory. */
8905 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008906 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907#endif
8908
8909 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8910 if (fd != NULL)
8911 {
8912#ifdef FEAT_SESSION
8913 if (eap->cmdidx == CMD_mkview)
8914 flagp = &vop_flags;
8915 else
8916 flagp = &ssop_flags;
8917#endif
8918
8919#ifdef MKSESSION_NL
8920 /* "unix" in 'sessionoptions': use NL line separator */
8921 if (view_session && (*flagp & SSOP_UNIX))
8922 mksession_nl = TRUE;
8923#endif
8924
8925 /* Write the version command for :mkvimrc */
8926 if (eap->cmdidx == CMD_mkvimrc)
8927 (void)put_line(fd, "version 6.0");
8928
8929#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008930 if (eap->cmdidx == CMD_mksession)
8931 {
8932 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8933 failed = TRUE;
8934 }
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (eap->cmdidx != CMD_mkview)
8937#endif
8938 {
8939 /* Write setting 'compatible' first, because it has side effects.
8940 * For that same reason only do it when needed. */
8941 if (p_cp)
8942 (void)put_line(fd, "if !&cp | set cp | endif");
8943 else
8944 (void)put_line(fd, "if &cp | set nocp | endif");
8945 }
8946
8947#ifdef FEAT_SESSION
8948 if (!view_session
8949 || (eap->cmdidx == CMD_mksession
8950 && (*flagp & SSOP_OPTIONS)))
8951#endif
8952 failed |= (makemap(fd, NULL) == FAIL
8953 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8954
8955#ifdef FEAT_SESSION
8956 if (!failed && view_session)
8957 {
8958 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8959 failed = TRUE;
8960 if (eap->cmdidx == CMD_mksession)
8961 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008962 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
Bram Moolenaard9462e32011-04-11 21:35:11 +02008964 dirnow = alloc(MAXPATHL);
8965 if (dirnow == NULL)
8966 failed = TRUE;
8967 else
8968 {
8969 /*
8970 * Change to session file's dir.
8971 */
8972 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008974 *dirnow = NUL;
8975 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8976 {
8977 if (vim_chdirfile(fname) == OK)
8978 shorten_fnames(TRUE);
8979 }
8980 else if (*dirnow != NUL
8981 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8982 {
8983 if (mch_chdir((char *)globaldir) == 0)
8984 shorten_fnames(TRUE);
8985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986
Bram Moolenaard9462e32011-04-11 21:35:11 +02008987 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988
Bram Moolenaard9462e32011-04-11 21:35:11 +02008989 /* restore original dir */
8990 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008992 {
8993 if (mch_chdir((char *)dirnow) != 0)
8994 EMSG(_(e_prev_dir));
8995 shorten_fnames(TRUE);
8996 }
8997 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 }
8999 }
9000 else
9001 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009002 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9003 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 }
9005 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9006 == FAIL)
9007 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009008 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9009 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009010 if (eap->cmdidx == CMD_mksession)
9011 {
9012 if (put_line(fd, "unlet SessionLoad") == FAIL)
9013 failed = TRUE;
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 }
9016#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009017 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9018 failed = TRUE;
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 failed |= fclose(fd);
9021
9022 if (failed)
9023 EMSG(_(e_write));
9024#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9025 else if (eap->cmdidx == CMD_mksession)
9026 {
9027 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009028 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029
Bram Moolenaard9462e32011-04-11 21:35:11 +02009030 tbuf = alloc(MAXPATHL);
9031 if (tbuf != NULL)
9032 {
9033 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9034 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9035 vim_free(tbuf);
9036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
9038#endif
9039#ifdef MKSESSION_NL
9040 mksession_nl = FALSE;
9041#endif
9042 }
9043
9044#ifdef FEAT_BROWSE
9045theend:
9046 vim_free(browseFile);
9047#endif
9048#ifdef FEAT_SESSION
9049 vim_free(viewFile);
9050#endif
9051}
9052
Bram Moolenaardf177f62005-02-22 08:39:57 +00009053#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9054 || defined(PROTO)
9055 int
9056vim_mkdir_emsg(name, prot)
9057 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009058 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009059{
9060 if (vim_mkdir(name, prot) != 0)
9061 {
9062 EMSG2(_("E739: Cannot create directory: %s"), name);
9063 return FAIL;
9064 }
9065 return OK;
9066}
9067#endif
9068
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069/*
9070 * Open a file for writing for an Ex command, with some checks.
9071 * Return file descriptor, or NULL on failure.
9072 */
9073 FILE *
9074open_exfile(fname, forceit, mode)
9075 char_u *fname;
9076 int forceit;
9077 char *mode; /* "w" for create new file or "a" for append */
9078{
9079 FILE *fd;
9080
9081#ifdef UNIX
9082 /* with Unix it is possible to open a directory */
9083 if (mch_isdir(fname))
9084 {
9085 EMSG2(_(e_isadir2), fname);
9086 return NULL;
9087 }
9088#endif
9089 if (!forceit && *mode != 'a' && vim_fexists(fname))
9090 {
9091 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9092 return NULL;
9093 }
9094
9095 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9096 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9097
9098 return fd;
9099}
9100
9101/*
9102 * ":mark" and ":k".
9103 */
9104 static void
9105ex_mark(eap)
9106 exarg_T *eap;
9107{
9108 pos_T pos;
9109
9110 if (*eap->arg == NUL) /* No argument? */
9111 EMSG(_(e_argreq));
9112 else if (eap->arg[1] != NUL) /* more than one character? */
9113 EMSG(_(e_trailing));
9114 else
9115 {
9116 pos = curwin->w_cursor; /* save curwin->w_cursor */
9117 curwin->w_cursor.lnum = eap->line2;
9118 beginline(BL_WHITE | BL_FIX);
9119 if (setmark(*eap->arg) == FAIL) /* set mark */
9120 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9121 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9122 }
9123}
9124
9125/*
9126 * Update w_topline, w_leftcol and the cursor position.
9127 */
9128 void
9129update_topline_cursor()
9130{
9131 check_cursor(); /* put cursor on valid line */
9132 update_topline();
9133 if (!curwin->w_p_wrap)
9134 validate_cursor();
9135 update_curswant();
9136}
9137
9138#ifdef FEAT_EX_EXTRA
9139/*
9140 * ":normal[!] {commands}": Execute normal mode commands.
9141 */
9142 static void
9143ex_normal(eap)
9144 exarg_T *eap;
9145{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 int save_msg_scroll = msg_scroll;
9147 int save_restart_edit = restart_edit;
9148 int save_msg_didout = msg_didout;
9149 int save_State = State;
9150 tasave_T tabuf;
9151 int save_insertmode = p_im;
9152 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009153 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#ifdef FEAT_MBYTE
9155 char_u *arg = NULL;
9156 int l;
9157 char_u *p;
9158#endif
9159
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009160 if (ex_normal_lock > 0)
9161 {
9162 EMSG(_(e_secure));
9163 return;
9164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (ex_normal_busy >= p_mmd)
9166 {
9167 EMSG(_("E192: Recursive use of :normal too deep"));
9168 return;
9169 }
9170 ++ex_normal_busy;
9171
9172 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9173 restart_edit = 0; /* don't go to Insert mode */
9174 p_im = FALSE; /* don't use 'insertmode' */
9175
9176#ifdef FEAT_MBYTE
9177 /*
9178 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9179 * this for the K_SPECIAL leading byte, otherwise special keys will not
9180 * work.
9181 */
9182 if (has_mbyte)
9183 {
9184 int len = 0;
9185
9186 /* Count the number of characters to be escaped. */
9187 for (p = eap->arg; *p != NUL; ++p)
9188 {
9189# ifdef FEAT_GUI
9190 if (*p == CSI) /* leadbyte CSI */
9191 len += 2;
9192# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009193 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9195# ifdef FEAT_GUI
9196 || *p == CSI
9197# endif
9198 )
9199 len += 2;
9200 }
9201 if (len > 0)
9202 {
9203 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9204 if (arg != NULL)
9205 {
9206 len = 0;
9207 for (p = eap->arg; *p != NUL; ++p)
9208 {
9209 arg[len++] = *p;
9210# ifdef FEAT_GUI
9211 if (*p == CSI)
9212 {
9213 arg[len++] = KS_EXTRA;
9214 arg[len++] = (int)KE_CSI;
9215 }
9216# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009217 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 {
9219 arg[len++] = *++p;
9220 if (*p == K_SPECIAL)
9221 {
9222 arg[len++] = KS_SPECIAL;
9223 arg[len++] = KE_FILLER;
9224 }
9225# ifdef FEAT_GUI
9226 else if (*p == CSI)
9227 {
9228 arg[len++] = KS_EXTRA;
9229 arg[len++] = (int)KE_CSI;
9230 }
9231# endif
9232 }
9233 arg[len] = NUL;
9234 }
9235 }
9236 }
9237 }
9238#endif
9239
9240 /*
9241 * Save the current typeahead. This is required to allow using ":normal"
9242 * from an event handler and makes sure we don't hang when the argument
9243 * ends with half a command.
9244 */
9245 save_typeahead(&tabuf);
9246 if (tabuf.typebuf_valid)
9247 {
9248 /*
9249 * Repeat the :normal command for each line in the range. When no
9250 * range given, execute it just once, without positioning the cursor
9251 * first.
9252 */
9253 do
9254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (eap->addr_count != 0)
9256 {
9257 curwin->w_cursor.lnum = eap->line1++;
9258 curwin->w_cursor.col = 0;
9259 }
9260
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009261 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262#ifdef FEAT_MBYTE
9263 arg != NULL ? arg :
9264#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009265 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
9267 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9268 }
9269
9270 /* Might not return to the main loop when in an event handler. */
9271 update_topline_cursor();
9272
9273 /* Restore the previous typeahead. */
9274 restore_typeahead(&tabuf);
9275
9276 --ex_normal_busy;
9277 msg_scroll = save_msg_scroll;
9278 restart_edit = save_restart_edit;
9279 p_im = save_insertmode;
9280 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009281 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9283
9284 /* Restore the state (needed when called from a function executed for
9285 * 'indentexpr'). */
9286 State = save_State;
9287#ifdef FEAT_MBYTE
9288 vim_free(arg);
9289#endif
9290}
9291
9292/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009293 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 */
9295 static void
9296ex_startinsert(eap)
9297 exarg_T *eap;
9298{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009299 if (eap->forceit)
9300 {
9301 coladvance((colnr_T)MAXCOL);
9302 curwin->w_curswant = MAXCOL;
9303 curwin->w_set_curswant = FALSE;
9304 }
9305
Bram Moolenaara40c5002005-01-09 21:16:21 +00009306 /* Ignore the command when already in Insert mode. Inserting an
9307 * expression register that invokes a function can do this. */
9308 if (State & INSERT)
9309 return;
9310
Bram Moolenaar64969662005-12-14 21:59:55 +00009311 if (eap->cmdidx == CMD_startinsert)
9312 restart_edit = 'a';
9313 else if (eap->cmdidx == CMD_startreplace)
9314 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009316 restart_edit = 'V';
9317
9318 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009320 if (eap->cmdidx == CMD_startinsert)
9321 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 curwin->w_curswant = 0; /* avoid MAXCOL */
9323 }
9324}
9325
9326/*
9327 * ":stopinsert"
9328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 static void
9330ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009331 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332{
9333 restart_edit = 0;
9334 stop_insert_mode = TRUE;
9335}
9336#endif
9337
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009338#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9339/*
9340 * Execute normal mode command "cmd".
9341 * "remap" can be REMAP_NONE or REMAP_YES.
9342 */
9343 void
9344exec_normal_cmd(cmd, remap, silent)
9345 char_u *cmd;
9346 int remap;
9347 int silent;
9348{
9349 oparg_T oa;
9350
9351 /*
9352 * Stuff the argument into the typeahead buffer.
9353 * Execute normal_cmd() until there is no typeahead left.
9354 */
9355 clear_oparg(&oa);
9356 finish_op = FALSE;
9357 ins_typebuf(cmd, remap, 0, TRUE, silent);
9358 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9359 && !got_int)
9360 {
9361 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009362 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009363 }
9364}
9365#endif
9366
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367#ifdef FEAT_FIND_ID
9368 static void
9369ex_checkpath(eap)
9370 exarg_T *eap;
9371{
9372 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9373 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9374 (linenr_T)1, (linenr_T)MAXLNUM);
9375}
9376
9377#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9378/*
9379 * ":psearch"
9380 */
9381 static void
9382ex_psearch(eap)
9383 exarg_T *eap;
9384{
9385 g_do_tagpreview = p_pvh;
9386 ex_findpat(eap);
9387 g_do_tagpreview = 0;
9388}
9389#endif
9390
9391 static void
9392ex_findpat(eap)
9393 exarg_T *eap;
9394{
9395 int whole = TRUE;
9396 long n;
9397 char_u *p;
9398 int action;
9399
9400 switch (cmdnames[eap->cmdidx].cmd_name[2])
9401 {
9402 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9403 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9404 action = ACTION_GOTO;
9405 else
9406 action = ACTION_SHOW;
9407 break;
9408 case 'i': /* ":ilist" and ":dlist" */
9409 action = ACTION_SHOW_ALL;
9410 break;
9411 case 'u': /* ":ijump" and ":djump" */
9412 action = ACTION_GOTO;
9413 break;
9414 default: /* ":isplit" and ":dsplit" */
9415 action = ACTION_SPLIT;
9416 break;
9417 }
9418
9419 n = 1;
9420 if (vim_isdigit(*eap->arg)) /* get count */
9421 {
9422 n = getdigits(&eap->arg);
9423 eap->arg = skipwhite(eap->arg);
9424 }
9425 if (*eap->arg == '/') /* Match regexp, not just whole words */
9426 {
9427 whole = FALSE;
9428 ++eap->arg;
9429 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9430 if (*p)
9431 {
9432 *p++ = NUL;
9433 p = skipwhite(p);
9434
9435 /* Check for trailing illegal characters */
9436 if (!ends_excmd(*p))
9437 eap->errmsg = e_trailing;
9438 else
9439 eap->nextcmd = check_nextcmd(p);
9440 }
9441 }
9442 if (!eap->skip)
9443 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9444 whole, !eap->forceit,
9445 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9446 n, action, eap->line1, eap->line2);
9447}
9448#endif
9449
9450#ifdef FEAT_WINDOWS
9451
9452# ifdef FEAT_QUICKFIX
9453/*
9454 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9455 */
9456 static void
9457ex_ptag(eap)
9458 exarg_T *eap;
9459{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009460 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9462}
9463
9464/*
9465 * ":pedit"
9466 */
9467 static void
9468ex_pedit(eap)
9469 exarg_T *eap;
9470{
9471 win_T *curwin_save = curwin;
9472
9473 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009474 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 keep_help_flag = curwin_save->w_buffer->b_help;
9476 do_exedit(eap, NULL);
9477 keep_help_flag = FALSE;
9478 if (curwin != curwin_save && win_valid(curwin_save))
9479 {
9480 /* Return cursor to where we were */
9481 validate_cursor();
9482 redraw_later(VALID);
9483 win_enter(curwin_save, TRUE);
9484 }
9485 g_do_tagpreview = 0;
9486}
9487# endif
9488
9489/*
9490 * ":stag", ":stselect" and ":stjump".
9491 */
9492 static void
9493ex_stag(eap)
9494 exarg_T *eap;
9495{
9496 postponed_split = -1;
9497 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009498 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9500 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009501 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503#endif
9504
9505/*
9506 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9507 */
9508 static void
9509ex_tag(eap)
9510 exarg_T *eap;
9511{
9512 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9513}
9514
9515 static void
9516ex_tag_cmd(eap, name)
9517 exarg_T *eap;
9518 char_u *name;
9519{
9520 int cmd;
9521
9522 switch (name[1])
9523 {
9524 case 'j': cmd = DT_JUMP; /* ":tjump" */
9525 break;
9526 case 's': cmd = DT_SELECT; /* ":tselect" */
9527 break;
9528 case 'p': cmd = DT_PREV; /* ":tprevious" */
9529 break;
9530 case 'N': cmd = DT_PREV; /* ":tNext" */
9531 break;
9532 case 'n': cmd = DT_NEXT; /* ":tnext" */
9533 break;
9534 case 'o': cmd = DT_POP; /* ":pop" */
9535 break;
9536 case 'f': /* ":tfirst" */
9537 case 'r': cmd = DT_FIRST; /* ":trewind" */
9538 break;
9539 case 'l': cmd = DT_LAST; /* ":tlast" */
9540 break;
9541 default: /* ":tag" */
9542#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009543 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 {
9545 do_cstag(eap);
9546 return;
9547 }
9548#endif
9549 cmd = DT_TAG;
9550 break;
9551 }
9552
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009553 if (name[0] == 'l')
9554 {
9555#ifndef FEAT_QUICKFIX
9556 ex_ni(eap);
9557 return;
9558#else
9559 cmd = DT_LTAG;
9560#endif
9561 }
9562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9564 eap->forceit, TRUE);
9565}
9566
9567/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009568 * Check "str" for starting with a special cmdline variable.
9569 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9570 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9571 */
9572 int
9573find_cmdline_var(src, usedlen)
9574 char_u *src;
9575 int *usedlen;
9576{
9577 int len;
9578 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009579 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009580 "%",
9581#define SPEC_PERC 0
9582 "#",
9583#define SPEC_HASH 1
9584 "<cword>", /* cursor word */
9585#define SPEC_CWORD 2
9586 "<cWORD>", /* cursor WORD */
9587#define SPEC_CCWORD 3
9588 "<cfile>", /* cursor path name */
9589#define SPEC_CFILE 4
9590 "<sfile>", /* ":so" file name */
9591#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009592 "<slnum>", /* ":so" file line number */
9593#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009594#ifdef FEAT_AUTOCMD
9595 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009596# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009597 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009598# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009599 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009600# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009601#endif
9602#ifdef FEAT_CLIENTSERVER
9603 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009604# ifdef FEAT_AUTOCMD
9605# define SPEC_CLIENT 10
9606# else
9607# define SPEC_CLIENT 7
9608# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009609#endif
9610 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009611
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009612 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009613 {
9614 len = (int)STRLEN(spec_str[i]);
9615 if (STRNCMP(src, spec_str[i], len) == 0)
9616 {
9617 *usedlen = len;
9618 return i;
9619 }
9620 }
9621 return -1;
9622}
9623
9624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 * Evaluate cmdline variables.
9626 *
9627 * change '%' to curbuf->b_ffname
9628 * '#' to curwin->w_altfile
9629 * '<cword>' to word under the cursor
9630 * '<cWORD>' to WORD under the cursor
9631 * '<cfile>' to path name under the cursor
9632 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009633 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 * '<afile>' to file name for autocommand
9635 * '<abuf>' to buffer number for autocommand
9636 * '<amatch>' to matching name for autocommand
9637 *
9638 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9639 * "" for error without a message) and NULL is returned.
9640 * Returns an allocated string if a valid match was found.
9641 * Returns NULL if no match was found. "usedlen" then still contains the
9642 * number of characters to skip.
9643 */
9644 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009645eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009647 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 int *usedlen; /* characters after src that are used */
9649 linenr_T *lnump; /* line number for :e command, or NULL */
9650 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009651 int *escaped; /* return value has escaped white space (can
9652 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 int i;
9655 char_u *s;
9656 char_u *result;
9657 char_u *resultbuf = NULL;
9658 int resultlen;
9659 buf_T *buf;
9660 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9661 int spec_idx;
9662#ifdef FEAT_MODIFY_FNAME
9663 int skip_mod = FALSE;
9664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666
9667 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009668 if (escaped != NULL)
9669 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670
9671 /*
9672 * Check if there is something to do.
9673 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009674 spec_idx = find_cmdline_var(src, usedlen);
9675 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
9677 *usedlen = 1;
9678 return NULL;
9679 }
9680
9681 /*
9682 * Skip when preceded with a backslash "\%" and "\#".
9683 * Note: In "\\%" the % is also not recognized!
9684 */
9685 if (src > srcstart && src[-1] == '\\')
9686 {
9687 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009688 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 return NULL;
9690 }
9691
9692 /*
9693 * word or WORD under cursor
9694 */
9695 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9696 {
9697 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9698 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9699 if (resultlen == 0)
9700 {
9701 *errormsg = (char_u *)"";
9702 return NULL;
9703 }
9704 }
9705
9706 /*
9707 * '#': Alternate file name
9708 * '%': Current file name
9709 * File name under the cursor
9710 * File name for autocommand
9711 * and following modifiers
9712 */
9713 else
9714 {
9715 switch (spec_idx)
9716 {
9717 case SPEC_PERC: /* '%': current file */
9718 if (curbuf->b_fname == NULL)
9719 {
9720 result = (char_u *)"";
9721 valid = 0; /* Must have ":p:h" to be valid */
9722 }
9723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 break;
9726
9727 case SPEC_HASH: /* '#' or "#99": alternate file */
9728 if (src[1] == '#') /* "##": the argument list */
9729 {
9730 result = arg_all();
9731 resultbuf = result;
9732 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009733 if (escaped != NULL)
9734 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735#ifdef FEAT_MODIFY_FNAME
9736 skip_mod = TRUE;
9737#endif
9738 break;
9739 }
9740 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009741 if (*s == '<') /* "#<99" uses v:oldfiles */
9742 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 i = (int)getdigits(&s);
9744 *usedlen = (int)(s - src); /* length of what we expand */
9745
Bram Moolenaard812df62008-11-09 12:46:09 +00009746 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009748 if (*usedlen < 2)
9749 {
9750 /* Should we give an error message for #<text? */
9751 *usedlen = 1;
9752 return NULL;
9753 }
9754#ifdef FEAT_EVAL
9755 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9756 (long)i);
9757 if (result == NULL)
9758 {
9759 *errormsg = (char_u *)"";
9760 return NULL;
9761 }
9762#else
9763 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 }
9767 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009768 {
9769 buf = buflist_findnr(i);
9770 if (buf == NULL)
9771 {
9772 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9773 return NULL;
9774 }
9775 if (lnump != NULL)
9776 *lnump = ECMD_LAST;
9777 if (buf->b_fname == NULL)
9778 {
9779 result = (char_u *)"";
9780 valid = 0; /* Must have ":p:h" to be valid */
9781 }
9782 else
9783 result = buf->b_fname;
9784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 break;
9786
9787#ifdef FEAT_SEARCHPATH
9788 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009789 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 if (result == NULL)
9791 {
9792 *errormsg = (char_u *)"";
9793 return NULL;
9794 }
9795 resultbuf = result; /* remember allocated string */
9796 break;
9797#endif
9798
9799#ifdef FEAT_AUTOCMD
9800 case SPEC_AFILE: /* file name for autocommand */
9801 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009802 if (result != NULL && !autocmd_fname_full)
9803 {
9804 /* Still need to turn the fname into a full path. It is
9805 * postponed to avoid a delay when <afile> is not used. */
9806 autocmd_fname_full = TRUE;
9807 result = FullName_save(autocmd_fname, FALSE);
9808 vim_free(autocmd_fname);
9809 autocmd_fname = result;
9810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 if (result == NULL)
9812 {
9813 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9814 return NULL;
9815 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009816 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 break;
9818
9819 case SPEC_ABUF: /* buffer number for autocommand */
9820 if (autocmd_bufnr <= 0)
9821 {
9822 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9823 return NULL;
9824 }
9825 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9826 result = strbuf;
9827 break;
9828
9829 case SPEC_AMATCH: /* match name for autocommand */
9830 result = autocmd_match;
9831 if (result == NULL)
9832 {
9833 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9834 return NULL;
9835 }
9836 break;
9837
9838#endif
9839 case SPEC_SFILE: /* file name for ":so" command */
9840 result = sourcing_name;
9841 if (result == NULL)
9842 {
9843 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9844 return NULL;
9845 }
9846 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009847 case SPEC_SLNUM: /* line in file for ":so" command */
9848 if (sourcing_name == NULL || sourcing_lnum == 0)
9849 {
9850 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9851 return NULL;
9852 }
9853 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9854 result = strbuf;
9855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856#if defined(FEAT_CLIENTSERVER)
9857 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009858 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9859 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 result = strbuf;
9861 break;
9862#endif
9863 }
9864
9865 resultlen = (int)STRLEN(result); /* length of new string */
9866 if (src[*usedlen] == '<') /* remove the file name extension */
9867 {
9868 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 resultlen = (int)(s - result);
9871 }
9872#ifdef FEAT_MODIFY_FNAME
9873 else if (!skip_mod)
9874 {
9875 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9876 &resultlen);
9877 if (result == NULL)
9878 {
9879 *errormsg = (char_u *)"";
9880 return NULL;
9881 }
9882 }
9883#endif
9884 }
9885
9886 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9887 {
9888 if (valid != VALID_HEAD + VALID_PATH)
9889 /* xgettext:no-c-format */
9890 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9891 else
9892 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9893 result = NULL;
9894 }
9895 else
9896 result = vim_strnsave(result, resultlen);
9897 vim_free(resultbuf);
9898 return result;
9899}
9900
9901/*
9902 * Concatenate all files in the argument list, separated by spaces, and return
9903 * it in one allocated string.
9904 * Spaces and backslashes in the file names are escaped with a backslash.
9905 * Returns NULL when out of memory.
9906 */
9907 static char_u *
9908arg_all()
9909{
9910 int len;
9911 int idx;
9912 char_u *retval = NULL;
9913 char_u *p;
9914
9915 /*
9916 * Do this loop two times:
9917 * first time: compute the total length
9918 * second time: concatenate the names
9919 */
9920 for (;;)
9921 {
9922 len = 0;
9923 for (idx = 0; idx < ARGCOUNT; ++idx)
9924 {
9925 p = alist_name(&ARGLIST[idx]);
9926 if (p != NULL)
9927 {
9928 if (len > 0)
9929 {
9930 /* insert a space in between names */
9931 if (retval != NULL)
9932 retval[len] = ' ';
9933 ++len;
9934 }
9935 for ( ; *p != NUL; ++p)
9936 {
9937 if (*p == ' ' || *p == '\\')
9938 {
9939 /* insert a backslash */
9940 if (retval != NULL)
9941 retval[len] = '\\';
9942 ++len;
9943 }
9944 if (retval != NULL)
9945 retval[len] = *p;
9946 ++len;
9947 }
9948 }
9949 }
9950
9951 /* second time: break here */
9952 if (retval != NULL)
9953 {
9954 retval[len] = NUL;
9955 break;
9956 }
9957
9958 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009959 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 if (retval == NULL)
9961 break;
9962 }
9963
9964 return retval;
9965}
9966
9967#if defined(FEAT_AUTOCMD) || defined(PROTO)
9968/*
9969 * Expand the <sfile> string in "arg".
9970 *
9971 * Returns an allocated string, or NULL for any error.
9972 */
9973 char_u *
9974expand_sfile(arg)
9975 char_u *arg;
9976{
9977 char_u *errormsg;
9978 int len;
9979 char_u *result;
9980 char_u *newres;
9981 char_u *repl;
9982 int srclen;
9983 char_u *p;
9984
9985 result = vim_strsave(arg);
9986 if (result == NULL)
9987 return NULL;
9988
9989 for (p = result; *p; )
9990 {
9991 if (STRNCMP(p, "<sfile>", 7) != 0)
9992 ++p;
9993 else
9994 {
9995 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009996 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 if (errormsg != NULL)
9998 {
9999 if (*errormsg)
10000 emsg(errormsg);
10001 vim_free(result);
10002 return NULL;
10003 }
10004 if (repl == NULL) /* no match (cannot happen) */
10005 {
10006 p += srclen;
10007 continue;
10008 }
10009 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10010 newres = alloc(len);
10011 if (newres == NULL)
10012 {
10013 vim_free(repl);
10014 vim_free(result);
10015 return NULL;
10016 }
10017 mch_memmove(newres, result, (size_t)(p - result));
10018 STRCPY(newres + (p - result), repl);
10019 len = (int)STRLEN(newres);
10020 STRCAT(newres, p + srclen);
10021 vim_free(repl);
10022 vim_free(result);
10023 result = newres;
10024 p = newres + len; /* continue after the match */
10025 }
10026 }
10027
10028 return result;
10029}
10030#endif
10031
10032#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010033static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10034 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10036static frame_T *ses_skipframe __ARGS((frame_T *fr));
10037static int ses_do_frame __ARGS((frame_T *fr));
10038static int ses_do_win __ARGS((win_T *wp));
10039static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10040static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10041static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10042
10043/*
10044 * Write openfile commands for the current buffers to an .exrc file.
10045 * Return FAIL on error, OK otherwise.
10046 */
10047 static int
10048makeopens(fd, dirnow)
10049 FILE *fd;
10050 char_u *dirnow; /* Current directory name */
10051{
10052 buf_T *buf;
10053 int only_save_windows = TRUE;
10054 int nr;
10055 int cnr = 1;
10056 int restore_size = TRUE;
10057 win_T *wp;
10058 char_u *sname;
10059 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010060 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010061 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010062 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010063 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010064 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065
10066 if (ssop_flags & SSOP_BUFFERS)
10067 only_save_windows = FALSE; /* Save ALL buffers */
10068
10069 /*
10070 * Begin by setting the this_session variable, and then other
10071 * sessionable variables.
10072 */
10073#ifdef FEAT_EVAL
10074 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10075 return FAIL;
10076 if (ssop_flags & SSOP_GLOBALS)
10077 if (store_session_globals(fd) == FAIL)
10078 return FAIL;
10079#endif
10080
10081 /*
10082 * Close all windows but one.
10083 */
10084 if (put_line(fd, "silent only") == FAIL)
10085 return FAIL;
10086
10087 /*
10088 * Now a :cd command to the session directory or the current directory
10089 */
10090 if (ssop_flags & SSOP_SESDIR)
10091 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010092 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10093 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 return FAIL;
10095 }
10096 else if (ssop_flags & SSOP_CURDIR)
10097 {
10098 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10099 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010100 || fputs("cd ", fd) < 0
10101 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10102 || put_eol(fd) == FAIL)
10103 {
10104 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 vim_free(sname);
10108 }
10109
10110 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010111 * If there is an empty, unnamed buffer we will wipe it out later.
10112 * Remember the buffer number.
10113 */
10114 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10115 return FAIL;
10116 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10117 return FAIL;
10118 if (put_line(fd, "endif") == FAIL)
10119 return FAIL;
10120
10121 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 * Now save the current files, current buffer first.
10123 */
10124 if (put_line(fd, "set shortmess=aoO") == FAIL)
10125 return FAIL;
10126
10127 /* Now put the other buffers into the buffer list */
10128 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10129 {
10130 if (!(only_save_windows && buf->b_nwindows == 0)
10131 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10132 && buf->b_fname != NULL
10133 && buf->b_p_bl)
10134 {
10135 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10136 : buf->b_wininfo->wi_fpos.lnum) < 0
10137 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10138 return FAIL;
10139 }
10140 }
10141
10142 /* the global argument list */
10143 if (ses_arglist(fd, "args", &global_alist.al_ga,
10144 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10145 return FAIL;
10146
10147 if (ssop_flags & SSOP_RESIZE)
10148 {
10149 /* Note: after the restore we still check it worked!*/
10150 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10151 || put_eol(fd) == FAIL)
10152 return FAIL;
10153 }
10154
10155#ifdef FEAT_GUI
10156 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10157 {
10158 int x, y;
10159
10160 if (gui_mch_get_winpos(&x, &y) == OK)
10161 {
10162 /* Note: after the restore we still check it worked!*/
10163 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10164 return FAIL;
10165 }
10166 }
10167#endif
10168
10169 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010170 * May repeat putting Windows for each tab, when "tabpages" is in
10171 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010172 * Don't use goto_tabpage(), it may change directory and trigger
10173 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010175 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010176 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010177 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010179 int need_tabnew = FALSE;
10180
Bram Moolenaar18144c82006-04-12 21:52:12 +000010181 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010183 tabpage_T *tp = find_tabpage(tabnr);
10184
10185 if (tp == NULL)
10186 break; /* done all tab pages */
10187 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010188 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010189 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010190 tab_topframe = topframe;
10191 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010192 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010193 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010194 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010195 tab_topframe = tp->tp_topframe;
10196 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010197 if (tabnr > 1)
10198 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200
Bram Moolenaar18144c82006-04-12 21:52:12 +000010201 /*
10202 * Before creating the window layout, try loading one file. If this
10203 * is aborted we don't end up with a number of useless windows.
10204 * This may have side effects! (e.g., compressed or network file).
10205 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010206 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010207 {
10208 if (ses_do_win(wp)
10209 && wp->w_buffer->b_ffname != NULL
10210 && !wp->w_buffer->b_help
10211#ifdef FEAT_QUICKFIX
10212 && !bt_nofile(wp->w_buffer)
10213#endif
10214 )
10215 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010216 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010217 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10218 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010219 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010220 if (!wp->w_arg_idx_invalid)
10221 edited_win = wp;
10222 break;
10223 }
10224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010226 /* If no file got edited create an empty tab page. */
10227 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10228 return FAIL;
10229
Bram Moolenaar18144c82006-04-12 21:52:12 +000010230 /*
10231 * Save current window layout.
10232 */
10233 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010235 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010237 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10238 return FAIL;
10239 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241
Bram Moolenaar18144c82006-04-12 21:52:12 +000010242 /*
10243 * Check if window sizes can be restored (no windows omitted).
10244 * Remember the window number of the current window after restoring.
10245 */
10246 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010247 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010248 {
10249 if (ses_do_win(wp))
10250 ++nr;
10251 else
10252 restore_size = FALSE;
10253 if (curwin == wp)
10254 cnr = nr;
10255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256
Bram Moolenaar18144c82006-04-12 21:52:12 +000010257 /* Go to the first window. */
10258 if (put_line(fd, "wincmd t") == FAIL)
10259 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010260
Bram Moolenaar18144c82006-04-12 21:52:12 +000010261 /*
10262 * If more than one window, see if sizes can be restored.
10263 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10264 * resized when moving between windows.
10265 * Do this before restoring the view, so that the topline and the
10266 * cursor can be set. This is done again below.
10267 */
10268 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10269 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010270 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272
Bram Moolenaar18144c82006-04-12 21:52:12 +000010273 /*
10274 * Restore the view of the window (options, file, cursor, etc.).
10275 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010276 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010277 {
10278 if (!ses_do_win(wp))
10279 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010280 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10281 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010282 return FAIL;
10283 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10284 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010285 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010286 }
10287
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010288 /* The argument index in the first tab page is zero, need to set it in
10289 * each window. For further tab pages it's the window where we do
10290 * "tabedit". */
10291 cur_arg_idx = next_arg_idx;
10292
Bram Moolenaar18144c82006-04-12 21:52:12 +000010293 /*
10294 * Restore cursor to the current window if it's not the first one.
10295 */
10296 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10297 || put_eol(fd) == FAIL))
10298 return FAIL;
10299
10300 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010301 * Restore window sizes again after jumping around in windows, because
10302 * the current window has a minimum size while others may not.
10303 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010304 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010305 return FAIL;
10306
Bram Moolenaar18144c82006-04-12 21:52:12 +000010307 /* Don't continue in another tab page when doing only the current one
10308 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010309 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010310 break;
10311 }
10312
10313 if (ssop_flags & SSOP_TABPAGES)
10314 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010315 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10316 || put_eol(fd) == FAIL)
10317 return FAIL;
10318 }
10319
Bram Moolenaar9c102382006-05-03 21:26:49 +000010320 /*
10321 * Wipe out an empty unnamed buffer we started in.
10322 */
10323 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10324 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010325 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010326 return FAIL;
10327 if (put_line(fd, "endif") == FAIL)
10328 return FAIL;
10329 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10330 return FAIL;
10331
10332 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10333 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10334 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336
10337 /*
10338 * Lastly, execute the x.vim file if it exists.
10339 */
10340 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10341 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010342 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 || put_line(fd, "endif") == FAIL)
10344 return FAIL;
10345
10346 return OK;
10347}
10348
10349 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010350ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 FILE *fd;
10352 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010353 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
10355 int n = 0;
10356 win_T *wp;
10357
10358 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10359 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010360 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
10362 if (!ses_do_win(wp))
10363 continue;
10364 ++n;
10365
10366 /* restore height when not full height */
10367 if (wp->w_height + wp->w_status_height < topframe->fr_height
10368 && (fprintf(fd,
10369 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10370 n, (long)wp->w_height, Rows / 2, Rows) < 0
10371 || put_eol(fd) == FAIL))
10372 return FAIL;
10373
10374 /* restore width when not full width */
10375 if (wp->w_width < Columns && (fprintf(fd,
10376 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10377 n, (long)wp->w_width, Columns / 2, Columns) < 0
10378 || put_eol(fd) == FAIL))
10379 return FAIL;
10380 }
10381 }
10382 else
10383 {
10384 /* Just equalise window sizes */
10385 if (put_line(fd, "wincmd =") == FAIL)
10386 return FAIL;
10387 }
10388 return OK;
10389}
10390
10391/*
10392 * Write commands to "fd" to recursively create windows for frame "fr",
10393 * horizontally and vertically split.
10394 * After the commands the last window in the frame is the current window.
10395 * Returns FAIL when writing the commands to "fd" fails.
10396 */
10397 static int
10398ses_win_rec(fd, fr)
10399 FILE *fd;
10400 frame_T *fr;
10401{
10402 frame_T *frc;
10403 int count = 0;
10404
10405 if (fr->fr_layout != FR_LEAF)
10406 {
10407 /* Find first frame that's not skipped and then create a window for
10408 * each following one (first frame is already there). */
10409 frc = ses_skipframe(fr->fr_child);
10410 if (frc != NULL)
10411 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10412 {
10413 /* Make window as big as possible so that we have lots of room
10414 * to split. */
10415 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10416 || put_line(fd, fr->fr_layout == FR_COL
10417 ? "split" : "vsplit") == FAIL)
10418 return FAIL;
10419 ++count;
10420 }
10421
10422 /* Go back to the first window. */
10423 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10424 ? "%dwincmd k" : "%dwincmd h", count) < 0
10425 || put_eol(fd) == FAIL))
10426 return FAIL;
10427
10428 /* Recursively create frames/windows in each window of this column or
10429 * row. */
10430 frc = ses_skipframe(fr->fr_child);
10431 while (frc != NULL)
10432 {
10433 ses_win_rec(fd, frc);
10434 frc = ses_skipframe(frc->fr_next);
10435 /* Go to next window. */
10436 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10437 return FAIL;
10438 }
10439 }
10440 return OK;
10441}
10442
10443/*
10444 * Skip frames that don't contain windows we want to save in the Session.
10445 * Returns NULL when there none.
10446 */
10447 static frame_T *
10448ses_skipframe(fr)
10449 frame_T *fr;
10450{
10451 frame_T *frc;
10452
10453 for (frc = fr; frc != NULL; frc = frc->fr_next)
10454 if (ses_do_frame(frc))
10455 break;
10456 return frc;
10457}
10458
10459/*
10460 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10461 * the Session.
10462 */
10463 static int
10464ses_do_frame(fr)
10465 frame_T *fr;
10466{
10467 frame_T *frc;
10468
10469 if (fr->fr_layout == FR_LEAF)
10470 return ses_do_win(fr->fr_win);
10471 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10472 if (ses_do_frame(frc))
10473 return TRUE;
10474 return FALSE;
10475}
10476
10477/*
10478 * Return non-zero if window "wp" is to be stored in the Session.
10479 */
10480 static int
10481ses_do_win(wp)
10482 win_T *wp;
10483{
10484 if (wp->w_buffer->b_fname == NULL
10485#ifdef FEAT_QUICKFIX
10486 /* When 'buftype' is "nofile" can't restore the window contents. */
10487 || bt_nofile(wp->w_buffer)
10488#endif
10489 )
10490 return (ssop_flags & SSOP_BLANK);
10491 if (wp->w_buffer->b_help)
10492 return (ssop_flags & SSOP_HELP);
10493 return TRUE;
10494}
10495
10496/*
10497 * Write commands to "fd" to restore the view of a window.
10498 * Caller must make sure 'scrolloff' is zero.
10499 */
10500 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010501put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 FILE *fd;
10503 win_T *wp;
10504 int add_edit; /* add ":edit" command to view */
10505 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010506 int current_arg_idx; /* current argument index of the window, use
10507 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 win_T *save_curwin;
10510 int f;
10511 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010512 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
10514 /* Always restore cursor position for ":mksession". For ":mkview" only
10515 * when 'viewoptions' contains "cursor". */
10516 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10517
10518 /*
10519 * Local argument list.
10520 */
10521 if (wp->w_alist == &global_alist)
10522 {
10523 if (put_line(fd, "argglobal") == FAIL)
10524 return FAIL;
10525 }
10526 else
10527 {
10528 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10529 flagp == &vop_flags
10530 || !(*flagp & SSOP_CURDIR)
10531 || wp->w_localdir != NULL, flagp) == FAIL)
10532 return FAIL;
10533 }
10534
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010535 /* Only when part of a session: restore the argument index. Some
10536 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010537 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010538 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010540 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 || put_eol(fd) == FAIL)
10542 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010543 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 }
10545
10546 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010547 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 {
10549 /*
10550 * Load the file.
10551 */
10552 if (wp->w_buffer->b_ffname != NULL
10553#ifdef FEAT_QUICKFIX
10554 && !bt_nofile(wp->w_buffer)
10555#endif
10556 )
10557 {
10558 /*
10559 * Editing a file in this buffer: use ":edit file".
10560 * This may have side effects! (e.g., compressed or network file).
10561 */
10562 if (fputs("edit ", fd) < 0
10563 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10564 return FAIL;
10565 }
10566 else
10567 {
10568 /* No file in this buffer, just make it empty. */
10569 if (put_line(fd, "enew") == FAIL)
10570 return FAIL;
10571#ifdef FEAT_QUICKFIX
10572 if (wp->w_buffer->b_ffname != NULL)
10573 {
10574 /* The buffer does have a name, but it's not a file name. */
10575 if (fputs("file ", fd) < 0
10576 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10577 return FAIL;
10578 }
10579#endif
10580 do_cursor = FALSE;
10581 }
10582 }
10583
10584 /*
10585 * Local mappings and abbreviations.
10586 */
10587 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10588 && makemap(fd, wp->w_buffer) == FAIL)
10589 return FAIL;
10590
10591 /*
10592 * Local options. Need to go to the window temporarily.
10593 * Store only local values when using ":mkview" and when ":mksession" is
10594 * used and 'sessionoptions' doesn't include "options".
10595 * Some folding options are always stored when "folds" is included,
10596 * otherwise the folds would not be restored correctly.
10597 */
10598 save_curwin = curwin;
10599 curwin = wp;
10600 curbuf = curwin->w_buffer;
10601 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10602 f = makeset(fd, OPT_LOCAL,
10603 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10604#ifdef FEAT_FOLDING
10605 else if (*flagp & SSOP_FOLDS)
10606 f = makefoldset(fd);
10607#endif
10608 else
10609 f = OK;
10610 curwin = save_curwin;
10611 curbuf = curwin->w_buffer;
10612 if (f == FAIL)
10613 return FAIL;
10614
10615#ifdef FEAT_FOLDING
10616 /*
10617 * Save Folds when 'buftype' is empty and for help files.
10618 */
10619 if ((*flagp & SSOP_FOLDS)
10620 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010621# ifdef FEAT_QUICKFIX
10622 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10623# endif
10624 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
10626 if (put_folds(fd, wp) == FAIL)
10627 return FAIL;
10628 }
10629#endif
10630
10631 /*
10632 * Set the cursor after creating folds, since that moves the cursor.
10633 */
10634 if (do_cursor)
10635 {
10636
10637 /* Restore the cursor line in the file and relatively in the
10638 * window. Don't use "G", it changes the jumplist. */
10639 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10640 (long)wp->w_cursor.lnum,
10641 (long)(wp->w_cursor.lnum - wp->w_topline),
10642 (long)wp->w_height / 2, (long)wp->w_height) < 0
10643 || put_eol(fd) == FAIL
10644 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10645 || put_line(fd, "exe s:l") == FAIL
10646 || put_line(fd, "normal! zt") == FAIL
10647 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10648 || put_eol(fd) == FAIL)
10649 return FAIL;
10650 /* Restore the cursor column and left offset when not wrapping. */
10651 if (wp->w_cursor.col == 0)
10652 {
10653 if (put_line(fd, "normal! 0") == FAIL)
10654 return FAIL;
10655 }
10656 else
10657 {
10658 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10659 {
10660 if (fprintf(fd,
10661 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10662 (long)wp->w_cursor.col,
10663 (long)(wp->w_cursor.col - wp->w_leftcol),
10664 (long)wp->w_width / 2, (long)wp->w_width) < 0
10665 || put_eol(fd) == FAIL
10666 || put_line(fd, "if s:c > 0") == FAIL
10667 || fprintf(fd,
10668 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10669 (long)wp->w_cursor.col) < 0
10670 || put_eol(fd) == FAIL
10671 || put_line(fd, "else") == FAIL
10672 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10673 || put_eol(fd) == FAIL
10674 || put_line(fd, "endif") == FAIL)
10675 return FAIL;
10676 }
10677 else
10678 {
10679 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10680 || put_eol(fd) == FAIL)
10681 return FAIL;
10682 }
10683 }
10684 }
10685
10686 /*
10687 * Local directory.
10688 */
10689 if (wp->w_localdir != NULL)
10690 {
10691 if (fputs("lcd ", fd) < 0
10692 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10693 || put_eol(fd) == FAIL)
10694 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010695 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 }
10697
10698 return OK;
10699}
10700
10701/*
10702 * Write an argument list to the session file.
10703 * Returns FAIL if writing fails.
10704 */
10705 static int
10706ses_arglist(fd, cmd, gap, fullname, flagp)
10707 FILE *fd;
10708 char *cmd;
10709 garray_T *gap;
10710 int fullname; /* TRUE: use full path name */
10711 unsigned *flagp;
10712{
10713 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010714 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 char_u *s;
10716
10717 if (gap->ga_len == 0)
10718 return put_line(fd, "silent! argdel *");
10719 if (fputs(cmd, fd) < 0)
10720 return FAIL;
10721 for (i = 0; i < gap->ga_len; ++i)
10722 {
10723 /* NULL file names are skipped (only happens when out of memory). */
10724 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10725 if (s != NULL)
10726 {
10727 if (fullname)
10728 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010729 buf = alloc(MAXPATHL);
10730 if (buf != NULL)
10731 {
10732 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10733 s = buf;
10734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 }
10736 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010737 {
10738 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010740 }
10741 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 }
10743 }
10744 return put_eol(fd);
10745}
10746
10747/*
10748 * Write a buffer name to the session file.
10749 * Also ends the line.
10750 * Returns FAIL if writing fails.
10751 */
10752 static int
10753ses_fname(fd, buf, flagp)
10754 FILE *fd;
10755 buf_T *buf;
10756 unsigned *flagp;
10757{
10758 char_u *name;
10759
10760 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010761 * the session file will be sourced.
10762 * Don't do this for ":mkview", we don't know the current directory.
10763 * Don't do this after ":lcd", we don't keep track of what the current
10764 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 if (buf->b_sfname != NULL
10766 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010767 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010768#ifdef FEAT_AUTOCHDIR
10769 && !p_acd
10770#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010771 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 name = buf->b_sfname;
10773 else
10774 name = buf->b_ffname;
10775 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10776 return FAIL;
10777 return OK;
10778}
10779
10780/*
10781 * Write a file name to the session file.
10782 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10783 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010784 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 */
10786 static int
10787ses_put_fname(fd, name, flagp)
10788 FILE *fd;
10789 char_u *name;
10790 unsigned *flagp;
10791{
10792 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010793 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795
10796 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010797 if (sname == NULL)
10798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010800 if (*flagp & SSOP_SLASH)
10801 {
10802 /* change all backslashes to forward slashes */
10803 for (p = sname; *p != NUL; mb_ptr_adv(p))
10804 if (*p == '\\')
10805 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010807
10808 /* escapse special characters */
10809 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010811 if (p == NULL)
10812 return FAIL;
10813
10814 /* write the result */
10815 if (fputs((char *)p, fd) < 0)
10816 retval = FAIL;
10817
10818 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 return retval;
10820}
10821
10822/*
10823 * ":loadview [nr]"
10824 */
10825 static void
10826ex_loadview(eap)
10827 exarg_T *eap;
10828{
10829 char_u *fname;
10830
10831 fname = get_view_file(*eap->arg);
10832 if (fname != NULL)
10833 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010834 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 vim_free(fname);
10836 }
10837}
10838
10839/*
10840 * Get the name of the view file for the current buffer.
10841 */
10842 static char_u *
10843get_view_file(c)
10844 int c;
10845{
10846 int len = 0;
10847 char_u *p, *s;
10848 char_u *retval;
10849 char_u *sname;
10850
10851 if (curbuf->b_ffname == NULL)
10852 {
10853 EMSG(_(e_noname));
10854 return NULL;
10855 }
10856 sname = home_replace_save(NULL, curbuf->b_ffname);
10857 if (sname == NULL)
10858 return NULL;
10859
10860 /*
10861 * We want a file name without separators, because we're not going to make
10862 * a directory.
10863 * "normal" path separator -> "=+"
10864 * "=" -> "=="
10865 * ":" path separator -> "=-"
10866 */
10867 for (p = sname; *p; ++p)
10868 if (*p == '=' || vim_ispathsep(*p))
10869 ++len;
10870 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10871 if (retval != NULL)
10872 {
10873 STRCPY(retval, p_vdir);
10874 add_pathsep(retval);
10875 s = retval + STRLEN(retval);
10876 for (p = sname; *p; ++p)
10877 {
10878 if (*p == '=')
10879 {
10880 *s++ = '=';
10881 *s++ = '=';
10882 }
10883 else if (vim_ispathsep(*p))
10884 {
10885 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010886#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 if (*p == ':')
10888 *s++ = '-';
10889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010891 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 }
10893 else
10894 *s++ = *p;
10895 }
10896 *s++ = '=';
10897 *s++ = c;
10898 STRCPY(s, ".vim");
10899 }
10900
10901 vim_free(sname);
10902 return retval;
10903}
10904
10905#endif /* FEAT_SESSION */
10906
10907/*
10908 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10909 * Return FAIL for a write error.
10910 */
10911 int
10912put_eol(fd)
10913 FILE *fd;
10914{
10915 if (
10916#ifdef USE_CRNL
10917 (
10918# ifdef MKSESSION_NL
10919 !mksession_nl &&
10920# endif
10921 (putc('\r', fd) < 0)) ||
10922#endif
10923 (putc('\n', fd) < 0))
10924 return FAIL;
10925 return OK;
10926}
10927
10928/*
10929 * Write a line to "fd".
10930 * Return FAIL for a write error.
10931 */
10932 int
10933put_line(fd, s)
10934 FILE *fd;
10935 char *s;
10936{
10937 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10938 return FAIL;
10939 return OK;
10940}
10941
10942#ifdef FEAT_VIMINFO
10943/*
10944 * ":rviminfo" and ":wviminfo".
10945 */
10946 static void
10947ex_viminfo(eap)
10948 exarg_T *eap;
10949{
10950 char_u *save_viminfo;
10951
10952 save_viminfo = p_viminfo;
10953 if (*p_viminfo == NUL)
10954 p_viminfo = (char_u *)"'100";
10955 if (eap->cmdidx == CMD_rviminfo)
10956 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010957 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10958 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 EMSG(_("E195: Cannot open viminfo file for reading"));
10960 }
10961 else
10962 write_viminfo(eap->arg, eap->forceit);
10963 p_viminfo = save_viminfo;
10964}
10965#endif
10966
10967#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010968/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010969 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010970 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 void
10973dialog_msg(buff, format, fname)
10974 char_u *buff;
10975 char *format;
10976 char_u *fname;
10977{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 if (fname == NULL)
10979 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010980 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981}
10982#endif
10983
10984/*
10985 * ":behave {mswin,xterm}"
10986 */
10987 static void
10988ex_behave(eap)
10989 exarg_T *eap;
10990{
10991 if (STRCMP(eap->arg, "mswin") == 0)
10992 {
10993 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10994 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10995 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10996 set_option_value((char_u *)"keymodel", 0L,
10997 (char_u *)"startsel,stopsel", 0);
10998 }
10999 else if (STRCMP(eap->arg, "xterm") == 0)
11000 {
11001 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11002 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11003 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11004 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11005 }
11006 else
11007 EMSG2(_(e_invarg2), eap->arg);
11008}
11009
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011010#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11011/*
11012 * Function given to ExpandGeneric() to obtain the possible arguments of the
11013 * ":behave {mswin,xterm}" command.
11014 */
11015 char_u *
11016get_behave_arg(xp, idx)
11017 expand_T *xp UNUSED;
11018 int idx;
11019{
11020 if (idx == 0)
11021 return (char_u *)"mswin";
11022 if (idx == 1)
11023 return (char_u *)"xterm";
11024 return NULL;
11025}
11026#endif
11027
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028#ifdef FEAT_AUTOCMD
11029static int filetype_detect = FALSE;
11030static int filetype_plugin = FALSE;
11031static int filetype_indent = FALSE;
11032
11033/*
11034 * ":filetype [plugin] [indent] {on,off,detect}"
11035 * on: Load the filetype.vim file to install autocommands for file types.
11036 * off: Load the ftoff.vim file to remove all autocommands for file types.
11037 * plugin on: load filetype.vim and ftplugin.vim
11038 * plugin off: load ftplugof.vim
11039 * indent on: load filetype.vim and indent.vim
11040 * indent off: load indoff.vim
11041 */
11042 static void
11043ex_filetype(eap)
11044 exarg_T *eap;
11045{
11046 char_u *arg = eap->arg;
11047 int plugin = FALSE;
11048 int indent = FALSE;
11049
11050 if (*eap->arg == NUL)
11051 {
11052 /* Print current status. */
11053 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11054 filetype_detect ? "ON" : "OFF",
11055 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11056 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11057 return;
11058 }
11059
11060 /* Accept "plugin" and "indent" in any order. */
11061 for (;;)
11062 {
11063 if (STRNCMP(arg, "plugin", 6) == 0)
11064 {
11065 plugin = TRUE;
11066 arg = skipwhite(arg + 6);
11067 continue;
11068 }
11069 if (STRNCMP(arg, "indent", 6) == 0)
11070 {
11071 indent = TRUE;
11072 arg = skipwhite(arg + 6);
11073 continue;
11074 }
11075 break;
11076 }
11077 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11078 {
11079 if (*arg == 'o' || !filetype_detect)
11080 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011081 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 filetype_detect = TRUE;
11083 if (plugin)
11084 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011085 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 filetype_plugin = TRUE;
11087 }
11088 if (indent)
11089 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011090 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 filetype_indent = TRUE;
11092 }
11093 }
11094 if (*arg == 'd')
11095 {
11096 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011097 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 }
11099 }
11100 else if (STRCMP(arg, "off") == 0)
11101 {
11102 if (plugin || indent)
11103 {
11104 if (plugin)
11105 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011106 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 filetype_plugin = FALSE;
11108 }
11109 if (indent)
11110 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011111 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 filetype_indent = FALSE;
11113 }
11114 }
11115 else
11116 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011117 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 filetype_detect = FALSE;
11119 }
11120 }
11121 else
11122 EMSG2(_(e_invarg2), arg);
11123}
11124
11125/*
11126 * ":setfiletype {name}"
11127 */
11128 static void
11129ex_setfiletype(eap)
11130 exarg_T *eap;
11131{
11132 if (!did_filetype)
11133 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11134}
11135#endif
11136
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 static void
11138ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011139 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140{
11141#ifdef FEAT_DIGRAPHS
11142 if (*eap->arg != NUL)
11143 putdigraph(eap->arg);
11144 else
11145 listdigraphs();
11146#else
11147 EMSG(_("E196: No digraphs in this version"));
11148#endif
11149}
11150
11151 static void
11152ex_set(eap)
11153 exarg_T *eap;
11154{
11155 int flags = 0;
11156
11157 if (eap->cmdidx == CMD_setlocal)
11158 flags = OPT_LOCAL;
11159 else if (eap->cmdidx == CMD_setglobal)
11160 flags = OPT_GLOBAL;
11161#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11162 if (cmdmod.browse && flags == 0)
11163 ex_options(eap);
11164 else
11165#endif
11166 (void)do_set(eap->arg, flags);
11167}
11168
11169#ifdef FEAT_SEARCH_EXTRA
11170/*
11171 * ":nohlsearch"
11172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 static void
11174ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011175 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176{
11177 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011178 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179}
11180
11181/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011182 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 * Sets nextcmd to the start of the next command, if any. Also called when
11184 * skipping commands to find the next command.
11185 */
11186 static void
11187ex_match(eap)
11188 exarg_T *eap;
11189{
11190 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011191 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 char_u *end;
11193 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011194 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011195
11196 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011197 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011198 else
11199 {
11200 EMSG(e_invcmd);
11201 return;
11202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203
11204 /* First clear any old pattern. */
11205 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011206 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207
11208 if (ends_excmd(*eap->arg))
11209 end = eap->arg;
11210 else if ((STRNICMP(eap->arg, "none", 4) == 0
11211 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11212 end = eap->arg + 4;
11213 else
11214 {
11215 p = skiptowhite(eap->arg);
11216 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011217 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 p = skipwhite(p);
11219 if (*p == NUL)
11220 {
11221 /* There must be two arguments. */
11222 EMSG2(_(e_invarg2), eap->arg);
11223 return;
11224 }
11225 end = skip_regexp(p + 1, *p, TRUE, NULL);
11226 if (!eap->skip)
11227 {
11228 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11229 {
11230 eap->errmsg = e_trailing;
11231 return;
11232 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011233 if (*end != *p)
11234 {
11235 EMSG2(_(e_invarg2), p);
11236 return;
11237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238
11239 c = *end;
11240 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011241 match_add(curwin, g, p + 1, 10, id);
11242 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011243 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 }
11245 }
11246 eap->nextcmd = find_nextcmd(end);
11247}
11248#endif
11249
11250#ifdef FEAT_CRYPT
11251/*
11252 * ":X": Get crypt key
11253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 static void
11255ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011256 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011258 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011259 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260}
11261#endif
11262
11263#ifdef FEAT_FOLDING
11264 static void
11265ex_fold(eap)
11266 exarg_T *eap;
11267{
11268 if (foldManualAllowed(TRUE))
11269 foldCreate(eap->line1, eap->line2);
11270}
11271
11272 static void
11273ex_foldopen(eap)
11274 exarg_T *eap;
11275{
11276 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11277 eap->forceit, FALSE);
11278}
11279
11280 static void
11281ex_folddo(eap)
11282 exarg_T *eap;
11283{
11284 linenr_T lnum;
11285
11286 /* First set the marks for all lines closed/open. */
11287 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11288 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11289 ml_setmarked(lnum);
11290
11291 /* Execute the command on the marked lines. */
11292 global_exe(eap->arg);
11293 ml_clearmarked(); /* clear rest of the marks */
11294}
11295#endif