blob: b9ec6c53062468cffb69c57e0ff57e2116f3d659 [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:
2598 break;
2599
2600 default: goto doend;
2601 }
2602 }
2603#endif
2604
2605 if (ea.argt & XFILE)
2606 {
2607 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2608 goto doend;
2609 }
2610
2611#ifdef FEAT_LISTCMDS
2612 /*
2613 * Accept buffer name. Cannot be used at the same time with a buffer
2614 * number. Don't do this for a user command.
2615 */
2616 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2617# ifdef FEAT_USR_CMDS
2618 && !USER_CMDIDX(ea.cmdidx)
2619# endif
2620 )
2621 {
2622 /*
2623 * :bdelete, :bwipeout and :bunload take several arguments, separated
2624 * by spaces: find next space (skipping over escaped characters).
2625 * The others take one argument: ignore trailing spaces.
2626 */
2627 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2628 || ea.cmdidx == CMD_bunload)
2629 p = skiptowhite_esc(ea.arg);
2630 else
2631 {
2632 p = ea.arg + STRLEN(ea.arg);
2633 while (p > ea.arg && vim_iswhite(p[-1]))
2634 --p;
2635 }
2636 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2637 if (ea.line2 < 0) /* failed */
2638 goto doend;
2639 ea.addr_count = 1;
2640 ea.arg = skipwhite(p);
2641 }
2642#endif
2643
2644/*
2645 * 6. switch on command name
2646 *
2647 * The "ea" structure holds the arguments that can be used.
2648 */
2649 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 ea.cookie = cookie;
2652#ifdef FEAT_EVAL
2653 ea.cstack = cstack;
2654#endif
2655
2656#ifdef FEAT_USR_CMDS
2657 if (USER_CMDIDX(ea.cmdidx))
2658 {
2659 /*
2660 * Execute a user-defined command.
2661 */
2662 do_ucmd(&ea);
2663 }
2664 else
2665#endif
2666 {
2667 /*
2668 * Call the function to execute the command.
2669 */
2670 ea.errmsg = NULL;
2671 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2672 if (ea.errmsg != NULL)
2673 errormsg = (char_u *)_(ea.errmsg);
2674 }
2675
2676#ifdef FEAT_EVAL
2677 /*
2678 * If the command just executed called do_cmdline(), any throw or ":return"
2679 * or ":finish" encountered there must also check the cstack of the still
2680 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2681 * exception, or reanimate a returned function or finished script file and
2682 * return or finish it again.
2683 */
2684 if (need_rethrow)
2685 do_throw(cstack);
2686 else if (check_cstack)
2687 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002688 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002690 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 && current_func_returned())
2692 do_return(&ea, TRUE, FALSE, NULL);
2693 }
2694 need_rethrow = check_cstack = FALSE;
2695#endif
2696
2697doend:
2698 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2699 curwin->w_cursor.lnum = 1;
2700
2701 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2702 {
2703 if (sourcing)
2704 {
2705 if (errormsg != IObuff)
2706 {
2707 STRCPY(IObuff, errormsg);
2708 errormsg = IObuff;
2709 }
2710 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002711 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 emsg(errormsg);
2714 }
2715#ifdef FEAT_EVAL
2716 do_errthrow(cstack,
2717 (ea.cmdidx != CMD_SIZE
2718# ifdef FEAT_USR_CMDS
2719 && !USER_CMDIDX(ea.cmdidx)
2720# endif
2721 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2722#endif
2723
2724 if (verbose_save >= 0)
2725 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002726#ifdef FEAT_AUTOCMD
2727 if (cmdmod.save_ei != NULL)
2728 {
2729 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002730 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2731 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002732 free_string_option(cmdmod.save_ei);
2733 }
2734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 cmdmod = save_cmdmod;
2737
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002738 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 /* messages could be enabled for a serious error, need to check if the
2741 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002742 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002743 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 emsg_silent -= did_esilent;
2745 if (emsg_silent < 0)
2746 emsg_silent = 0;
2747 /* Restore msg_scroll, it's set by file I/O commands, even when no
2748 * message is actually displayed. */
2749 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002750
2751 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2752 * somewhere in the line. Put it back in the first column. */
2753 if (redirecting())
2754 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002757#ifdef HAVE_SANDBOX
2758 if (did_sandbox)
2759 --sandbox;
2760#endif
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2763 ea.nextcmd = NULL;
2764
2765#ifdef FEAT_EVAL
2766 --ex_nesting_level;
2767#endif
2768
2769 return ea.nextcmd;
2770}
2771#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002772 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
2774
2775/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002776 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 * If there is a match advance "pp" to the argument and return TRUE.
2778 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002779 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002781 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 char *cmd; /* name of command */
2783 int len; /* required length */
2784{
2785 int i;
2786
2787 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002788 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 break;
2790 if (i >= len && !isalpha((*pp)[i]))
2791 {
2792 *pp = skipwhite(*pp + i);
2793 return TRUE;
2794 }
2795 return FALSE;
2796}
2797
2798/*
2799 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002800 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002802 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * Returns NULL for an ambiguous user command.
2804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 static char_u *
2806find_command(eap, full)
2807 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002808 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 int len;
2811 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002812 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814 /*
2815 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002816 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 * - the 'k' command can directly be followed by any character.
2818 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2819 * but :sre[wind] is another command, as are :scrip[tnames],
2820 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002821 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
2823 p = eap->cmd;
2824 if (*p == 'k')
2825 {
2826 eap->cmdidx = CMD_k;
2827 ++p;
2828 }
2829 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002830 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2831 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 || p[1] == 'g'
2833 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2834 || p[1] == 'I'
2835 || (p[1] == 'r' && p[2] != 'e')))
2836 {
2837 eap->cmdidx = CMD_substitute;
2838 ++p;
2839 }
2840 else
2841 {
2842 while (ASCII_ISALPHA(*p))
2843 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002844 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002845 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002846 while (ASCII_ISALNUM(*p))
2847 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 /* check for non-alpha command */
2850 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2851 ++p;
2852 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002853 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2854 {
2855 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2856 * :delete with the 'l' flag. Same for 'p'. */
2857 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002858 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002859 break;
2860 if (i == len - 1)
2861 {
2862 --len;
2863 if (p[-1] == 'l')
2864 eap->flags |= EXFLAG_LIST;
2865 else
2866 eap->flags |= EXFLAG_PRINT;
2867 }
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 if (ASCII_ISLOWER(*eap->cmd))
2871 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2872 else
2873 eap->cmdidx = cmdidxs[26];
2874
2875 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2876 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2877 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2878 (size_t)len) == 0)
2879 {
2880#ifdef FEAT_EVAL
2881 if (full != NULL
2882 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2883 *full = TRUE;
2884#endif
2885 break;
2886 }
2887
2888#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002889 /* Look for a user defined command as a last resort. Let ":Print" be
2890 * overruled by a user defined command. */
2891 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2892 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002894 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 while (ASCII_ISALNUM(*p))
2896 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002897 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002900 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 eap->cmdidx = CMD_SIZE;
2902 }
2903
2904 return p;
2905}
2906
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002907#ifdef FEAT_USR_CMDS
2908/*
2909 * Search for a user command that matches "eap->cmd".
2910 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2911 * Return a pointer to just after the command.
2912 * Return NULL if there is no matching command.
2913 */
2914 static char_u *
2915find_ucmd(eap, p, full, xp, compl)
2916 exarg_T *eap;
2917 char_u *p; /* end of the command (possibly including count) */
2918 int *full; /* set to TRUE for a full match */
2919 expand_T *xp; /* used for completion, NULL otherwise */
2920 int *compl; /* completion flags or NULL */
2921{
2922 int len = (int)(p - eap->cmd);
2923 int j, k, matchlen = 0;
2924 ucmd_T *uc;
2925 int found = FALSE;
2926 int possible = FALSE;
2927 char_u *cp, *np; /* Point into typed cmd and test name */
2928 garray_T *gap;
2929 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2930 only full match global is accepted. */
2931
2932 /*
2933 * Look for buffer-local user commands first, then global ones.
2934 */
2935 gap = &curbuf->b_ucmds;
2936 for (;;)
2937 {
2938 for (j = 0; j < gap->ga_len; ++j)
2939 {
2940 uc = USER_CMD_GA(gap, j);
2941 cp = eap->cmd;
2942 np = uc->uc_name;
2943 k = 0;
2944 while (k < len && *np != NUL && *cp++ == *np++)
2945 k++;
2946 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2947 {
2948 /* If finding a second match, the command is ambiguous. But
2949 * not if a buffer-local command wasn't a full match and a
2950 * global command is a full match. */
2951 if (k == len && found && *np != NUL)
2952 {
2953 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002954 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955 amb_local = TRUE;
2956 }
2957
2958 if (!found || (k == len && *np == NUL))
2959 {
2960 /* If we matched up to a digit, then there could
2961 * be another command including the digit that we
2962 * should use instead.
2963 */
2964 if (k == len)
2965 found = TRUE;
2966 else
2967 possible = TRUE;
2968
2969 if (gap == &ucmds)
2970 eap->cmdidx = CMD_USER;
2971 else
2972 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002973 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002974 eap->useridx = j;
2975
2976# ifdef FEAT_CMDL_COMPL
2977 if (compl != NULL)
2978 *compl = uc->uc_compl;
2979# ifdef FEAT_EVAL
2980 if (xp != NULL)
2981 {
2982 xp->xp_arg = uc->uc_compl_arg;
2983 xp->xp_scriptID = uc->uc_scriptID;
2984 }
2985# endif
2986# endif
2987 /* Do not search for further abbreviations
2988 * if this is an exact match. */
2989 matchlen = k;
2990 if (k == len && *np == NUL)
2991 {
2992 if (full != NULL)
2993 *full = TRUE;
2994 amb_local = FALSE;
2995 break;
2996 }
2997 }
2998 }
2999 }
3000
3001 /* Stop if we found a full match or searched all. */
3002 if (j < gap->ga_len || gap == &ucmds)
3003 break;
3004 gap = &ucmds;
3005 }
3006
3007 /* Only found ambiguous matches. */
3008 if (amb_local)
3009 {
3010 if (xp != NULL)
3011 xp->xp_context = EXPAND_UNSUCCESSFUL;
3012 return NULL;
3013 }
3014
3015 /* The match we found may be followed immediately by a number. Move "p"
3016 * back to point to it. */
3017 if (found || possible)
3018 return p + (matchlen - len);
3019 return p;
3020}
3021#endif
3022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003024static struct cmdmod
3025{
3026 char *name;
3027 int minlen;
3028 int has_count; /* :123verbose :3tab */
3029} cmdmods[] = {
3030 {"aboveleft", 3, FALSE},
3031 {"belowright", 3, FALSE},
3032 {"botright", 2, FALSE},
3033 {"browse", 3, FALSE},
3034 {"confirm", 4, FALSE},
3035 {"hide", 3, FALSE},
3036 {"keepalt", 5, FALSE},
3037 {"keepjumps", 5, FALSE},
3038 {"keepmarks", 3, FALSE},
3039 {"leftabove", 5, FALSE},
3040 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003041 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003042 {"rightbelow", 6, FALSE},
3043 {"sandbox", 3, FALSE},
3044 {"silent", 3, FALSE},
3045 {"tab", 3, TRUE},
3046 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003047 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003048 {"verbose", 4, TRUE},
3049 {"vertical", 4, FALSE},
3050};
3051
3052/*
3053 * Return length of a command modifier (including optional count).
3054 * Return zero when it's not a modifier.
3055 */
3056 int
3057modifier_len(cmd)
3058 char_u *cmd;
3059{
3060 int i, j;
3061 char_u *p = cmd;
3062
3063 if (VIM_ISDIGIT(*cmd))
3064 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003065 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003066 {
3067 for (j = 0; p[j] != NUL; ++j)
3068 if (p[j] != cmdmods[i].name[j])
3069 break;
3070 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3071 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003072 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003073 }
3074 return 0;
3075}
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077/*
3078 * Return > 0 if an Ex command "name" exists.
3079 * Return 2 if there is an exact match.
3080 * Return 3 if there is an ambiguous match.
3081 */
3082 int
3083cmd_exists(name)
3084 char_u *name;
3085{
3086 exarg_T ea;
3087 int full = FALSE;
3088 int i;
3089 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003090 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003093 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 for (j = 0; name[j] != NUL; ++j)
3096 if (name[j] != cmdmods[i].name[j])
3097 break;
3098 if (name[j] == NUL && j >= cmdmods[i].minlen)
3099 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3100 }
3101
Bram Moolenaara9587612006-05-04 21:47:50 +00003102 /* Check built-in commands and user defined commands.
3103 * For ":2match" and ":3match" we need to skip the number. */
3104 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003106 p = find_command(&ea, &full);
3107 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003109 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3110 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003111 if (*skipwhite(p) != NUL)
3112 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3114}
3115#endif
3116
3117/*
3118 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3119 * we don't need/want deleted. Maybe this could be done better if we didn't
3120 * repeat all this stuff. The only problem is that they may not stay
3121 * perfectly compatible with each other, but then the command line syntax
3122 * probably won't change that much -- webb.
3123 */
3124 char_u *
3125set_one_cmd_context(xp, buff)
3126 expand_T *xp;
3127 char_u *buff; /* buffer for command string */
3128{
3129 char_u *p;
3130 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 int len = 0;
3132 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3134 int compl = EXPAND_NOTHING;
3135#endif
3136#ifdef FEAT_CMDL_COMPL
3137 int delim;
3138#endif
3139 int forceit = FALSE;
3140 int usefilter = FALSE; /* filter instead of file name */
3141
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003142 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 xp->xp_pattern = buff;
3144 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003145 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147/*
3148 * 2. skip comment lines and leading space, colons or bars
3149 */
3150 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3151 ;
3152 xp->xp_pattern = cmd;
3153
3154 if (*cmd == NUL)
3155 return NULL;
3156 if (*cmd == '"') /* ignore comment lines */
3157 {
3158 xp->xp_context = EXPAND_NOTHING;
3159 return NULL;
3160 }
3161
3162/*
3163 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3164 */
3165 cmd = skip_range(cmd, &xp->xp_context);
3166
3167/*
3168 * 4. parse command
3169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 xp->xp_pattern = cmd;
3171 if (*cmd == NUL)
3172 return NULL;
3173 if (*cmd == '"')
3174 {
3175 xp->xp_context = EXPAND_NOTHING;
3176 return NULL;
3177 }
3178
3179 if (*cmd == '|' || *cmd == '\n')
3180 return cmd + 1; /* There's another command */
3181
3182 /*
3183 * Isolate the command and search for it in the command table.
3184 * Exceptions:
3185 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003186 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3188 */
3189 if (*cmd == 'k' && cmd[1] != 'e')
3190 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 p = cmd + 1;
3193 }
3194 else
3195 {
3196 p = cmd;
3197 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3198 ++p;
3199 /* check for non-alpha command */
3200 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3201 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003202 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003204 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 xp->xp_context = EXPAND_UNSUCCESSFUL;
3207 return NULL;
3208 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003209 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003210 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3211 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3212 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 break;
3214
3215#ifdef FEAT_USR_CMDS
3216 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3218 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220 }
3221
3222 /*
3223 * If the cursor is touching the command, and it ends in an alpha-numeric
3224 * character, complete the command name.
3225 */
3226 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3227 return NULL;
3228
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3232 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 p = cmd + 1;
3235 }
3236#ifdef FEAT_USR_CMDS
3237 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3238 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 ea.cmd = cmd;
3240 p = find_ucmd(&ea, p, NULL, xp,
3241# if defined(FEAT_CMDL_COMPL)
3242 &compl
3243# else
3244 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003247 if (p == NULL)
3248 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250#endif
3251 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 /* Not still touching the command and it was an illegal one */
3255 xp->xp_context = EXPAND_UNSUCCESSFUL;
3256 return NULL;
3257 }
3258
3259 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3260
3261 if (*p == '!') /* forced commands */
3262 {
3263 forceit = TRUE;
3264 ++p;
3265 }
3266
3267/*
3268 * 5. parse arguments
3269 */
3270#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003273 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 arg = skipwhite(p);
3276
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 if (*arg == '>') /* append */
3280 {
3281 if (*++arg == '>')
3282 ++arg;
3283 arg = skipwhite(arg);
3284 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 ++arg;
3288 usefilter = TRUE;
3289 }
3290 }
3291
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 usefilter = forceit; /* :r! filter if forced */
3295 if (*arg == '!') /* :r !filter */
3296 {
3297 ++arg;
3298 usefilter = TRUE;
3299 }
3300 }
3301
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 while (*arg == *cmd) /* allow any number of '>' or '<' */
3305 ++arg;
3306 arg = skipwhite(arg);
3307 }
3308
3309 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003310 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 /* Check if we're in the +command */
3313 p = arg + 1;
3314 arg = skip_cmd_arg(arg, FALSE);
3315
3316 /* Still touching the command after '+'? */
3317 if (*arg == NUL)
3318 return p;
3319
3320 /* Skip space(s) after +command to get to the real argument */
3321 arg = skipwhite(arg);
3322 }
3323
3324 /*
3325 * Check for '|' to separate commands and '"' to start comments.
3326 * Don't do this for ":read !cmd" and ":write !cmd".
3327 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
3330 p = arg;
3331 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003332 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 p += 2;
3334 while (*p)
3335 {
3336 if (*p == Ctrl_V)
3337 {
3338 if (p[1] != NUL)
3339 ++p;
3340 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 || *p == '|' || *p == '\n')
3343 {
3344 if (*(p - 1) != '\\')
3345 {
3346 if (*p == '|' || *p == '\n')
3347 return p + 1;
3348 return NULL; /* It's a comment */
3349 }
3350 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003351 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353 }
3354
3355 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003356 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 vim_strchr((char_u *)"|\"", *arg) == NULL)
3358 return NULL;
3359
3360 /* Find start of last argument (argument just before cursor): */
3361 p = buff + STRLEN(buff);
3362 while (p != arg && *p != ' ' && *p != TAB)
3363 p--;
3364 if (*p == ' ' || *p == TAB)
3365 p++;
3366 xp->xp_pattern = p;
3367
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003368 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370 int c;
3371 int in_quote = FALSE;
3372 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /*
3375 * Allow spaces within back-quotes to count as part of the argument
3376 * being expanded.
3377 */
3378 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003379 p = xp->xp_pattern;
3380 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003382#ifdef FEAT_MBYTE
3383 if (has_mbyte)
3384 c = mb_ptr2char(p);
3385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003387 c = *p;
3388 if (c == '\\' && p[1] != NUL)
3389 ++p;
3390 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
3392 if (!in_quote)
3393 {
3394 xp->xp_pattern = p;
3395 bow = p + 1;
3396 }
3397 in_quote = !in_quote;
3398 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003399 /* An argument can contain just about everything, except
3400 * characters that end the command and white space. */
3401 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003402#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003403 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003404#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003405 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003406 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003407 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003408 while (*p != NUL)
3409 {
3410#ifdef FEAT_MBYTE
3411 if (has_mbyte)
3412 c = mb_ptr2char(p);
3413 else
3414#endif
3415 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003416 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003417 break;
3418#ifdef FEAT_MBYTE
3419 if (has_mbyte)
3420 len = (*mb_ptr2len)(p);
3421 else
3422#endif
3423 len = 1;
3424 mb_ptr_adv(p);
3425 }
3426 if (in_quote)
3427 bow = p;
3428 else
3429 xp->xp_pattern = p;
3430 p -= len;
3431 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003432 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434
3435 /*
3436 * If we are still inside the quotes, and we passed a space, just
3437 * expand from there.
3438 */
3439 if (bow != NULL && in_quote)
3440 xp->xp_pattern = bow;
3441 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003442
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003443 /* For a shell command more chars need to be escaped. */
3444 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003445 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003446#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003447 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003448#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003449 /* When still after the command name expand executables. */
3450 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003451 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /* Check for environment variable */
3455 if (*xp->xp_pattern == '$'
3456#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3457 || *xp->xp_pattern == '%'
3458#endif
3459 )
3460 {
3461 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3462 if (!vim_isIDc(*p))
3463 break;
3464 if (*p == NUL)
3465 {
3466 xp->xp_context = EXPAND_ENV_VARS;
3467 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003468#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3469 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003470 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003471 compl = EXPAND_ENV_VARS;
3472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 }
3475 }
3476
3477/*
3478 * 6. switch on command name
3479 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003480 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003482 case CMD_find:
3483 case CMD_sfind:
3484 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003485 if (xp->xp_context == EXPAND_FILES)
3486 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 case CMD_cd:
3489 case CMD_chdir:
3490 case CMD_lcd:
3491 case CMD_lchdir:
3492 if (xp->xp_context == EXPAND_FILES)
3493 xp->xp_context = EXPAND_DIRECTORIES;
3494 break;
3495 case CMD_help:
3496 xp->xp_context = EXPAND_HELP;
3497 xp->xp_pattern = arg;
3498 break;
3499
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003500 /* Command modifiers: return the argument.
3501 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003503 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 case CMD_belowright:
3505 case CMD_botright:
3506 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003507 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003509 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 case CMD_folddoclosed:
3511 case CMD_folddoopen:
3512 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003513 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 case CMD_keepjumps:
3515 case CMD_keepmarks:
3516 case CMD_leftabove:
3517 case CMD_lockmarks:
3518 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003519 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003521 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 case CMD_topleft:
3523 case CMD_verbose:
3524 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003525 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 return arg;
3527
Bram Moolenaar4f688582007-07-24 12:34:30 +00003528#ifdef FEAT_CMDL_COMPL
3529# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 case CMD_match:
3531 if (*arg == NUL || !ends_excmd(*arg))
3532 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003533 /* also complete "None" */
3534 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 arg = skipwhite(skiptowhite(arg));
3536 if (*arg != NUL)
3537 {
3538 xp->xp_context = EXPAND_NOTHING;
3539 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3540 }
3541 }
3542 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
3546 * All completion for the +cmdline_compl feature goes here.
3547 */
3548
3549# ifdef FEAT_USR_CMDS
3550 case CMD_command:
3551 /* Check for attributes */
3552 while (*arg == '-')
3553 {
3554 arg++; /* Skip "-" */
3555 p = skiptowhite(arg);
3556 if (*p == NUL)
3557 {
3558 /* Cursor is still in the attribute */
3559 p = vim_strchr(arg, '=');
3560 if (p == NULL)
3561 {
3562 /* No "=", so complete attribute names */
3563 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3564 xp->xp_pattern = arg;
3565 return NULL;
3566 }
3567
3568 /* For the -complete and -nargs attributes, we complete
3569 * their arguments as well.
3570 */
3571 if (STRNICMP(arg, "complete", p - arg) == 0)
3572 {
3573 xp->xp_context = EXPAND_USER_COMPLETE;
3574 xp->xp_pattern = p + 1;
3575 return NULL;
3576 }
3577 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3578 {
3579 xp->xp_context = EXPAND_USER_NARGS;
3580 xp->xp_pattern = p + 1;
3581 return NULL;
3582 }
3583 return NULL;
3584 }
3585 arg = skipwhite(p);
3586 }
3587
3588 /* After the attributes comes the new command name */
3589 p = skiptowhite(arg);
3590 if (*p == NUL)
3591 {
3592 xp->xp_context = EXPAND_USER_COMMANDS;
3593 xp->xp_pattern = arg;
3594 break;
3595 }
3596
3597 /* And finally comes a normal command */
3598 return skipwhite(p);
3599
3600 case CMD_delcommand:
3601 xp->xp_context = EXPAND_USER_COMMANDS;
3602 xp->xp_pattern = arg;
3603 break;
3604# endif
3605
3606 case CMD_global:
3607 case CMD_vglobal:
3608 delim = *arg; /* get the delimiter */
3609 if (delim)
3610 ++arg; /* skip delimiter if there is one */
3611
3612 while (arg[0] != NUL && arg[0] != delim)
3613 {
3614 if (arg[0] == '\\' && arg[1] != NUL)
3615 ++arg;
3616 ++arg;
3617 }
3618 if (arg[0] != NUL)
3619 return arg + 1;
3620 break;
3621 case CMD_and:
3622 case CMD_substitute:
3623 delim = *arg;
3624 if (delim)
3625 {
3626 /* skip "from" part */
3627 ++arg;
3628 arg = skip_regexp(arg, delim, p_magic, NULL);
3629 }
3630 /* skip "to" part */
3631 while (arg[0] != NUL && arg[0] != delim)
3632 {
3633 if (arg[0] == '\\' && arg[1] != NUL)
3634 ++arg;
3635 ++arg;
3636 }
3637 if (arg[0] != NUL) /* skip delimiter */
3638 ++arg;
3639 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3640 ++arg;
3641 if (arg[0] != NUL)
3642 return arg;
3643 break;
3644 case CMD_isearch:
3645 case CMD_dsearch:
3646 case CMD_ilist:
3647 case CMD_dlist:
3648 case CMD_ijump:
3649 case CMD_psearch:
3650 case CMD_djump:
3651 case CMD_isplit:
3652 case CMD_dsplit:
3653 arg = skipwhite(skipdigits(arg)); /* skip count */
3654 if (*arg == '/') /* Match regexp, not just whole words */
3655 {
3656 for (++arg; *arg && *arg != '/'; arg++)
3657 if (*arg == '\\' && arg[1] != NUL)
3658 arg++;
3659 if (*arg)
3660 {
3661 arg = skipwhite(arg + 1);
3662
3663 /* Check for trailing illegal characters */
3664 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3665 xp->xp_context = EXPAND_NOTHING;
3666 else
3667 return arg;
3668 }
3669 }
3670 break;
3671#ifdef FEAT_AUTOCMD
3672 case CMD_autocmd:
3673 return set_context_in_autocmd(xp, arg, FALSE);
3674
3675 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003676 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return set_context_in_autocmd(xp, arg, TRUE);
3678#endif
3679 case CMD_set:
3680 set_context_in_set_cmd(xp, arg, 0);
3681 break;
3682 case CMD_setglobal:
3683 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3684 break;
3685 case CMD_setlocal:
3686 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3687 break;
3688 case CMD_tag:
3689 case CMD_stag:
3690 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003691 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 case CMD_tselect:
3693 case CMD_stselect:
3694 case CMD_ptselect:
3695 case CMD_tjump:
3696 case CMD_stjump:
3697 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003698 if (*p_wop != NUL)
3699 xp->xp_context = EXPAND_TAGS_LISTFILES;
3700 else
3701 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 xp->xp_pattern = arg;
3703 break;
3704 case CMD_augroup:
3705 xp->xp_context = EXPAND_AUGROUP;
3706 xp->xp_pattern = arg;
3707 break;
3708#ifdef FEAT_SYN_HL
3709 case CMD_syntax:
3710 set_context_in_syntax_cmd(xp, arg);
3711 break;
3712#endif
3713#ifdef FEAT_EVAL
3714 case CMD_let:
3715 case CMD_if:
3716 case CMD_elseif:
3717 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003718 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 case CMD_echo:
3720 case CMD_echon:
3721 case CMD_execute:
3722 case CMD_echomsg:
3723 case CMD_echoerr:
3724 case CMD_call:
3725 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003726 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 break;
3728
3729 case CMD_unlet:
3730 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3731 arg = xp->xp_pattern + 1;
3732 xp->xp_context = EXPAND_USER_VARS;
3733 xp->xp_pattern = arg;
3734 break;
3735
3736 case CMD_function:
3737 case CMD_delfunction:
3738 xp->xp_context = EXPAND_USER_FUNC;
3739 xp->xp_pattern = arg;
3740 break;
3741
3742 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003743 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 break;
3745#endif
3746 case CMD_highlight:
3747 set_context_in_highlight_cmd(xp, arg);
3748 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003749#ifdef FEAT_CSCOPE
3750 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003751 case CMD_lcscope:
3752 case CMD_scscope:
3753 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003754 break;
3755#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003756#ifdef FEAT_SIGNS
3757 case CMD_sign:
3758 set_context_in_sign_cmd(xp, arg);
3759 break;
3760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#ifdef FEAT_LISTCMDS
3762 case CMD_bdelete:
3763 case CMD_bwipeout:
3764 case CMD_bunload:
3765 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3766 arg = xp->xp_pattern + 1;
3767 /*FALLTHROUGH*/
3768 case CMD_buffer:
3769 case CMD_sbuffer:
3770 case CMD_checktime:
3771 xp->xp_context = EXPAND_BUFFERS;
3772 xp->xp_pattern = arg;
3773 break;
3774#endif
3775#ifdef FEAT_USR_CMDS
3776 case CMD_USER:
3777 case CMD_USER_BUF:
3778 if (compl != EXPAND_NOTHING)
3779 {
3780 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003781 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783# ifdef FEAT_MENU
3784 if (compl == EXPAND_MENUS)
3785 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3786# endif
3787 if (compl == EXPAND_COMMANDS)
3788 return arg;
3789 if (compl == EXPAND_MAPPINGS)
3790 return set_context_in_map_cmd(xp, (char_u *)"map",
3791 arg, forceit, FALSE, FALSE, CMD_map);
3792 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3793 arg = xp->xp_pattern + 1;
3794 xp->xp_pattern = arg;
3795 }
3796 xp->xp_context = compl;
3797 }
3798 break;
3799#endif
3800 case CMD_map: case CMD_noremap:
3801 case CMD_nmap: case CMD_nnoremap:
3802 case CMD_vmap: case CMD_vnoremap:
3803 case CMD_omap: case CMD_onoremap:
3804 case CMD_imap: case CMD_inoremap:
3805 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003806 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003808 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_unmap:
3810 case CMD_nunmap:
3811 case CMD_vunmap:
3812 case CMD_ounmap:
3813 case CMD_iunmap:
3814 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003815 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003817 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case CMD_abbreviate: case CMD_noreabbrev:
3819 case CMD_cabbrev: case CMD_cnoreabbrev:
3820 case CMD_iabbrev: case CMD_inoreabbrev:
3821 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003822 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_unabbreviate:
3824 case CMD_cunabbrev:
3825 case CMD_iunabbrev:
3826 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003827 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#ifdef FEAT_MENU
3829 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3830 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3831 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3832 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3833 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3834 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3835 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3836 case CMD_tmenu: case CMD_tunmenu:
3837 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3838 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3839#endif
3840
3841 case CMD_colorscheme:
3842 xp->xp_context = EXPAND_COLORS;
3843 xp->xp_pattern = arg;
3844 break;
3845
3846 case CMD_compiler:
3847 xp->xp_context = EXPAND_COMPILER;
3848 xp->xp_pattern = arg;
3849 break;
3850
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003851 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003852 xp->xp_context = EXPAND_OWNSYNTAX;
3853 xp->xp_pattern = arg;
3854 break;
3855
3856 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003857 xp->xp_context = EXPAND_FILETYPE;
3858 xp->xp_pattern = arg;
3859 break;
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3862 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3863 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003864 p = skiptowhite(arg);
3865 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 xp->xp_context = EXPAND_LANGUAGE;
3868 xp->xp_pattern = arg;
3869 }
3870 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003871 {
3872 if ( STRNCMP(arg, "messages", p - arg) == 0
3873 || STRNCMP(arg, "ctype", p - arg) == 0
3874 || STRNCMP(arg, "time", p - arg) == 0)
3875 {
3876 xp->xp_context = EXPAND_LOCALES;
3877 xp->xp_pattern = skipwhite(p);
3878 }
3879 else
3880 xp->xp_context = EXPAND_NOTHING;
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 break;
3883#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003884#if defined(FEAT_PROFILE)
3885 case CMD_profile:
3886 set_context_in_profile_cmd(xp, arg);
3887 break;
3888#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003889 case CMD_behave:
3890 xp->xp_context = EXPAND_BEHAVE;
3891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893#endif /* FEAT_CMDL_COMPL */
3894
3895 default:
3896 break;
3897 }
3898 return NULL;
3899}
3900
3901/*
3902 * skip a range specifier of the form: addr [,addr] [;addr] ..
3903 *
3904 * Backslashed delimiters after / or ? will be skipped, and commands will
3905 * not be expanded between /'s and ?'s or after "'".
3906 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003907 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * Returns the "cmd" pointer advanced to beyond the range.
3909 */
3910 char_u *
3911skip_range(cmd, ctx)
3912 char_u *cmd;
3913 int *ctx; /* pointer to xp_context or NULL */
3914{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003915 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaardf177f62005-02-22 08:39:57 +00003917 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 if (*cmd == '\'')
3920 {
3921 if (*++cmd == NUL && ctx != NULL)
3922 *ctx = EXPAND_NOTHING;
3923 }
3924 else if (*cmd == '/' || *cmd == '?')
3925 {
3926 delim = *cmd++;
3927 while (*cmd != NUL && *cmd != delim)
3928 if (*cmd++ == '\\' && *cmd != NUL)
3929 ++cmd;
3930 if (*cmd == NUL && ctx != NULL)
3931 *ctx = EXPAND_NOTHING;
3932 }
3933 if (*cmd != NUL)
3934 ++cmd;
3935 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003936
3937 /* Skip ":" and white space. */
3938 while (*cmd == ':')
3939 cmd = skipwhite(cmd + 1);
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 return cmd;
3942}
3943
3944/*
3945 * get a single EX address
3946 *
3947 * Set ptr to the next character after the part that was interpreted.
3948 * Set ptr to NULL when an error is encountered.
3949 *
3950 * Return MAXLNUM when no Ex address was found.
3951 */
3952 static linenr_T
3953get_address(ptr, skip, to_other_file)
3954 char_u **ptr;
3955 int skip; /* only skip the address, don't use it */
3956 int to_other_file; /* flag: may jump to other file */
3957{
3958 int c;
3959 int i;
3960 long n;
3961 char_u *cmd;
3962 pos_T pos;
3963 pos_T *fp;
3964 linenr_T lnum;
3965
3966 cmd = skipwhite(*ptr);
3967 lnum = MAXLNUM;
3968 do
3969 {
3970 switch (*cmd)
3971 {
3972 case '.': /* '.' - Cursor position */
3973 ++cmd;
3974 lnum = curwin->w_cursor.lnum;
3975 break;
3976
3977 case '$': /* '$' - last line */
3978 ++cmd;
3979 lnum = curbuf->b_ml.ml_line_count;
3980 break;
3981
3982 case '\'': /* ''' - mark */
3983 if (*++cmd == NUL)
3984 {
3985 cmd = NULL;
3986 goto error;
3987 }
3988 if (skip)
3989 ++cmd;
3990 else
3991 {
3992 /* Only accept a mark in another file when it is
3993 * used by itself: ":'M". */
3994 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3995 ++cmd;
3996 if (fp == (pos_T *)-1)
3997 /* Jumped to another file. */
3998 lnum = curwin->w_cursor.lnum;
3999 else
4000 {
4001 if (check_mark(fp) == FAIL)
4002 {
4003 cmd = NULL;
4004 goto error;
4005 }
4006 lnum = fp->lnum;
4007 }
4008 }
4009 break;
4010
4011 case '/':
4012 case '?': /* '/' or '?' - search */
4013 c = *cmd++;
4014 if (skip) /* skip "/pat/" */
4015 {
4016 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4017 if (*cmd == c)
4018 ++cmd;
4019 }
4020 else
4021 {
4022 pos = curwin->w_cursor; /* save curwin->w_cursor */
4023 /*
4024 * When '/' or '?' follows another address, start
4025 * from there.
4026 */
4027 if (lnum != MAXLNUM)
4028 curwin->w_cursor.lnum = lnum;
4029 /*
4030 * Start a forward search at the end of the line.
4031 * Start a backward search at the start of the line.
4032 * This makes sure we never match in the current
4033 * line, and can match anywhere in the
4034 * next/previous line.
4035 */
4036 if (c == '/')
4037 curwin->w_cursor.col = MAXCOL;
4038 else
4039 curwin->w_cursor.col = 0;
4040 searchcmdlen = 0;
4041 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004042 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
4044 curwin->w_cursor = pos;
4045 cmd = NULL;
4046 goto error;
4047 }
4048 lnum = curwin->w_cursor.lnum;
4049 curwin->w_cursor = pos;
4050 /* adjust command string pointer */
4051 cmd += searchcmdlen;
4052 }
4053 break;
4054
4055 case '\\': /* "\?", "\/" or "\&", repeat search */
4056 ++cmd;
4057 if (*cmd == '&')
4058 i = RE_SUBST;
4059 else if (*cmd == '?' || *cmd == '/')
4060 i = RE_SEARCH;
4061 else
4062 {
4063 EMSG(_(e_backslash));
4064 cmd = NULL;
4065 goto error;
4066 }
4067
4068 if (!skip)
4069 {
4070 /*
4071 * When search follows another address, start from
4072 * there.
4073 */
4074 if (lnum != MAXLNUM)
4075 pos.lnum = lnum;
4076 else
4077 pos.lnum = curwin->w_cursor.lnum;
4078
4079 /*
4080 * Start the search just like for the above
4081 * do_search().
4082 */
4083 if (*cmd != '?')
4084 pos.col = MAXCOL;
4085 else
4086 pos.col = 0;
4087 if (searchit(curwin, curbuf, &pos,
4088 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004089 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004090 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 lnum = pos.lnum;
4092 else
4093 {
4094 cmd = NULL;
4095 goto error;
4096 }
4097 }
4098 ++cmd;
4099 break;
4100
4101 default:
4102 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4103 lnum = getdigits(&cmd);
4104 }
4105
4106 for (;;)
4107 {
4108 cmd = skipwhite(cmd);
4109 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4110 break;
4111
4112 if (lnum == MAXLNUM)
4113 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4114 if (VIM_ISDIGIT(*cmd))
4115 i = '+'; /* "number" is same as "+number" */
4116 else
4117 i = *cmd++;
4118 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4119 n = 1;
4120 else
4121 n = getdigits(&cmd);
4122 if (i == '-')
4123 lnum -= n;
4124 else
4125 lnum += n;
4126 }
4127 } while (*cmd == '/' || *cmd == '?');
4128
4129error:
4130 *ptr = cmd;
4131 return lnum;
4132}
4133
4134/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004135 * Get flags from an Ex command argument.
4136 */
4137 static void
4138get_flags(eap)
4139 exarg_T *eap;
4140{
4141 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4142 {
4143 if (*eap->arg == 'l')
4144 eap->flags |= EXFLAG_LIST;
4145 else if (*eap->arg == 'p')
4146 eap->flags |= EXFLAG_PRINT;
4147 else
4148 eap->flags |= EXFLAG_NR;
4149 eap->arg = skipwhite(eap->arg + 1);
4150 }
4151}
4152
4153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 * Function called for command which is Not Implemented. NI!
4155 */
4156 void
4157ex_ni(eap)
4158 exarg_T *eap;
4159{
4160 if (!eap->skip)
4161 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4162}
4163
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004164#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165/*
4166 * Function called for script command which is Not Implemented. NI!
4167 * Skips over ":perl <<EOF" constructs.
4168 */
4169 static void
4170ex_script_ni(eap)
4171 exarg_T *eap;
4172{
4173 if (!eap->skip)
4174 ex_ni(eap);
4175 else
4176 vim_free(script_get(eap, eap->arg));
4177}
4178#endif
4179
4180/*
4181 * Check range in Ex command for validity.
4182 * Return NULL when valid, error message when invalid.
4183 */
4184 static char_u *
4185invalid_range(eap)
4186 exarg_T *eap;
4187{
4188 if ( eap->line1 < 0
4189 || eap->line2 < 0
4190 || eap->line1 > eap->line2
4191 || ((eap->argt & RANGE)
4192 && !(eap->argt & NOTADR)
4193 && eap->line2 > curbuf->b_ml.ml_line_count
4194#ifdef FEAT_DIFF
4195 + (eap->cmdidx == CMD_diffget)
4196#endif
4197 ))
4198 return (char_u *)_(e_invrange);
4199 return NULL;
4200}
4201
4202/*
4203 * Correct the range for zero line number, if required.
4204 */
4205 static void
4206correct_range(eap)
4207 exarg_T *eap;
4208{
4209 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4210 {
4211 if (eap->line1 == 0)
4212 eap->line1 = 1;
4213 if (eap->line2 == 0)
4214 eap->line2 = 1;
4215 }
4216}
4217
Bram Moolenaar748bf032005-02-02 23:04:36 +00004218#ifdef FEAT_QUICKFIX
4219static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4220
4221/*
4222 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4223 * pattern. Otherwise return eap->arg.
4224 */
4225 static char_u *
4226skip_grep_pat(eap)
4227 exarg_T *eap;
4228{
4229 char_u *p = eap->arg;
4230
Bram Moolenaara37420f2006-02-04 22:37:47 +00004231 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4232 || eap->cmdidx == CMD_vimgrepadd
4233 || eap->cmdidx == CMD_lvimgrepadd
4234 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004235 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004236 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004237 if (p == NULL)
4238 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004239 }
4240 return p;
4241}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004242
4243/*
4244 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4245 * in the command line, so that things like % get expanded.
4246 */
4247 static char_u *
4248replace_makeprg(eap, p, cmdlinep)
4249 exarg_T *eap;
4250 char_u *p;
4251 char_u **cmdlinep;
4252{
4253 char_u *new_cmdline;
4254 char_u *program;
4255 char_u *pos;
4256 char_u *ptr;
4257 int len;
4258 int i;
4259
4260 /*
4261 * Don't do it when ":vimgrep" is used for ":grep".
4262 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004263 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4264 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4265 || eap->cmdidx == CMD_grepadd
4266 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004267 && !grep_internal(eap->cmdidx))
4268 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004269 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4270 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004271 {
4272 if (*curbuf->b_p_gp == NUL)
4273 program = p_gp;
4274 else
4275 program = curbuf->b_p_gp;
4276 }
4277 else
4278 {
4279 if (*curbuf->b_p_mp == NUL)
4280 program = p_mp;
4281 else
4282 program = curbuf->b_p_mp;
4283 }
4284
4285 p = skipwhite(p);
4286
4287 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4288 {
4289 /* replace $* by given arguments */
4290 i = 1;
4291 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4292 ++i;
4293 len = (int)STRLEN(p);
4294 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4295 if (new_cmdline == NULL)
4296 return NULL; /* out of memory */
4297 ptr = new_cmdline;
4298 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4299 {
4300 i = (int)(pos - program);
4301 STRNCPY(ptr, program, i);
4302 STRCPY(ptr += i, p);
4303 ptr += len;
4304 program = pos + 2;
4305 }
4306 STRCPY(ptr, program);
4307 }
4308 else
4309 {
4310 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4311 if (new_cmdline == NULL)
4312 return NULL; /* out of memory */
4313 STRCPY(new_cmdline, program);
4314 STRCAT(new_cmdline, " ");
4315 STRCAT(new_cmdline, p);
4316 }
4317 msg_make(p);
4318
4319 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4320 vim_free(*cmdlinep);
4321 *cmdlinep = new_cmdline;
4322 p = new_cmdline;
4323 }
4324 return p;
4325}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004326#endif
4327
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328/*
4329 * Expand file name in Ex command argument.
4330 * Return FAIL for failure, OK otherwise.
4331 */
4332 int
4333expand_filename(eap, cmdlinep, errormsgp)
4334 exarg_T *eap;
4335 char_u **cmdlinep;
4336 char_u **errormsgp;
4337{
4338 int has_wildcards; /* need to expand wildcards */
4339 char_u *repl;
4340 int srclen;
4341 char_u *p;
4342 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004343 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar748bf032005-02-02 23:04:36 +00004345#ifdef FEAT_QUICKFIX
4346 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4347 p = skip_grep_pat(eap);
4348#else
4349 p = eap->arg;
4350#endif
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 /*
4353 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4354 * the file name contains a wildcard it should not cause expanding.
4355 * (it will be expanded anyway if there is a wildcard before replacing).
4356 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004357 has_wildcards = mch_has_wildcard(p);
4358 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004360#ifdef FEAT_EVAL
4361 /* Skip over `=expr`, wildcards in it are not expanded. */
4362 if (p[0] == '`' && p[1] == '=')
4363 {
4364 p += 2;
4365 (void)skip_expr(&p);
4366 if (*p == '`')
4367 ++p;
4368 continue;
4369 }
4370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 /*
4372 * Quick check if this cannot be the start of a special string.
4373 * Also removes backslash before '%', '#' and '<'.
4374 */
4375 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4376 {
4377 ++p;
4378 continue;
4379 }
4380
4381 /*
4382 * Try to find a match at this position.
4383 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004384 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4385 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (*errormsgp != NULL) /* error detected */
4387 return FAIL;
4388 if (repl == NULL) /* no match found */
4389 {
4390 p += srclen;
4391 continue;
4392 }
4393
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004394 /* Wildcards won't be expanded below, the replacement is taken
4395 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4396 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4397 {
4398 char_u *l = repl;
4399
4400 repl = expand_env_save(repl);
4401 vim_free(l);
4402 }
4403
Bram Moolenaar63b92542007-03-27 14:57:09 +00004404 /* Need to escape white space et al. with a backslash.
4405 * Don't do this for:
4406 * - replacement that already has been escaped: "##"
4407 * - shell commands (may have to use quotes instead).
4408 * - non-unix systems when there is a single argument (spaces don't
4409 * separate arguments then).
4410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004412 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 && eap->cmdidx != CMD_bang
4414 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004415 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004417 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004419 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420#ifndef UNIX
4421 && !(eap->argt & NOSPC)
4422#endif
4423 )
4424 {
4425 char_u *l;
4426#ifdef BACKSLASH_IN_FILENAME
4427 /* Don't escape a backslash here, because rem_backslash() doesn't
4428 * remove it later. */
4429 static char_u *nobslash = (char_u *)" \t\"|";
4430# define ESCAPE_CHARS nobslash
4431#else
4432# define ESCAPE_CHARS escape_chars
4433#endif
4434
4435 for (l = repl; *l; ++l)
4436 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4437 {
4438 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4439 if (l != NULL)
4440 {
4441 vim_free(repl);
4442 repl = l;
4443 }
4444 break;
4445 }
4446 }
4447
4448 /* For a shell command a '!' must be escaped. */
4449 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004450 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 char_u *l;
4453
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004454 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (l != NULL)
4456 {
4457 vim_free(repl);
4458 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004459 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (strstr((char *)p_sh, "sh") != NULL)
4461 {
4462 l = vim_strsave_escaped(repl, (char_u *)"!");
4463 if (l != NULL)
4464 {
4465 vim_free(repl);
4466 repl = l;
4467 }
4468 }
4469 }
4470 }
4471
4472 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4473 vim_free(repl);
4474 if (p == NULL)
4475 return FAIL;
4476 }
4477
4478 /*
4479 * One file argument: Expand wildcards.
4480 * Don't do this with ":r !command" or ":w !command".
4481 */
4482 if ((eap->argt & NOSPC) && !eap->usefilter)
4483 {
4484 /*
4485 * May do this twice:
4486 * 1. Replace environment variables.
4487 * 2. Replace any other wildcards, remove backslashes.
4488 */
4489 for (n = 1; n <= 2; ++n)
4490 {
4491 if (n == 2)
4492 {
4493#ifdef UNIX
4494 /*
4495 * Only for Unix we check for more than one file name.
4496 * For other systems spaces are considered to be part
4497 * of the file name.
4498 * Only check here if there is no wildcard, otherwise
4499 * ExpandOne() will check for errors. This allows
4500 * ":e `ls ve*.c`" on Unix.
4501 */
4502 if (!has_wildcards)
4503 for (p = eap->arg; *p; ++p)
4504 {
4505 /* skip escaped characters */
4506 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4507 ++p;
4508 else if (vim_iswhite(*p))
4509 {
4510 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4511 return FAIL;
4512 }
4513 }
4514#endif
4515
4516 /*
4517 * Halve the number of backslashes (this is Vi compatible).
4518 * For Unix and OS/2, when wildcards are expanded, this is
4519 * done by ExpandOne() below.
4520 */
4521#if defined(UNIX) || defined(OS2)
4522 if (!has_wildcards)
4523#endif
4524 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
4527 if (has_wildcards)
4528 {
4529 if (n == 1)
4530 {
4531 /*
4532 * First loop: May expand environment variables. This
4533 * can be done much faster with expand_env() than with
4534 * something else (e.g., calling a shell).
4535 * After expanding environment variables, check again
4536 * if there are still wildcards present.
4537 */
4538 if (vim_strchr(eap->arg, '$') != NULL
4539 || vim_strchr(eap->arg, '~') != NULL)
4540 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004541 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004542 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 has_wildcards = mch_has_wildcard(NameBuff);
4544 p = NameBuff;
4545 }
4546 else
4547 p = NULL;
4548 }
4549 else /* n == 2 */
4550 {
4551 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004552 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
4554 ExpandInit(&xpc);
4555 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004556 if (p_wic)
4557 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004559 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (p == NULL)
4561 return FAIL;
4562 }
4563 if (p != NULL)
4564 {
4565 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4566 p, cmdlinep);
4567 if (n == 2) /* p came from ExpandOne() */
4568 vim_free(p);
4569 }
4570 }
4571 }
4572 }
4573 return OK;
4574}
4575
4576/*
4577 * Replace part of the command line, keeping eap->cmd, eap->arg and
4578 * eap->nextcmd correct.
4579 * "src" points to the part that is to be replaced, of length "srclen".
4580 * "repl" is the replacement string.
4581 * Returns a pointer to the character after the replaced string.
4582 * Returns NULL for failure.
4583 */
4584 static char_u *
4585repl_cmdline(eap, src, srclen, repl, cmdlinep)
4586 exarg_T *eap;
4587 char_u *src;
4588 int srclen;
4589 char_u *repl;
4590 char_u **cmdlinep;
4591{
4592 int len;
4593 int i;
4594 char_u *new_cmdline;
4595
4596 /*
4597 * The new command line is build in new_cmdline[].
4598 * First allocate it.
4599 * Careful: a "+cmd" argument may have been NUL terminated.
4600 */
4601 len = (int)STRLEN(repl);
4602 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004603 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4605 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4606 return NULL; /* out of memory! */
4607
4608 /*
4609 * Copy the stuff before the expanded part.
4610 * Copy the expanded stuff.
4611 * Copy what came after the expanded part.
4612 * Copy the next commands, if there are any.
4613 */
4614 i = (int)(src - *cmdlinep); /* length of part before match */
4615 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 mch_memmove(new_cmdline + i, repl, (size_t)len);
4618 i += len; /* remember the end of the string */
4619 STRCPY(new_cmdline + i, src + srclen);
4620 src = new_cmdline + i; /* remember where to continue */
4621
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004622 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 i = (int)STRLEN(new_cmdline) + 1;
4625 STRCPY(new_cmdline + i, eap->nextcmd);
4626 eap->nextcmd = new_cmdline + i;
4627 }
4628 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4629 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4630 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4631 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4632 vim_free(*cmdlinep);
4633 *cmdlinep = new_cmdline;
4634
4635 return src;
4636}
4637
4638/*
4639 * Check for '|' to separate commands and '"' to start comments.
4640 */
4641 void
4642separate_nextcmd(eap)
4643 exarg_T *eap;
4644{
4645 char_u *p;
4646
Bram Moolenaar86b68352004-12-27 21:59:20 +00004647#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004648 p = skip_grep_pat(eap);
4649#else
4650 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004651#endif
4652
4653 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
4655 if (*p == Ctrl_V)
4656 {
4657 if (eap->argt & (USECTRLV | XFILE))
4658 ++p; /* skip CTRL-V and next char */
4659 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004660 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004661 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (*p == NUL) /* stop at NUL after CTRL-V */
4663 break;
4664 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004665
4666#ifdef FEAT_EVAL
4667 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004669 {
4670 p += 2;
4671 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004672 }
4673#endif
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 /* Check for '"': start of comment or '|': next command */
4676 /* :@" and :*" do not start a comment!
4677 * :redir @" doesn't either. */
4678 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4679 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4680 || p != eap->arg)
4681 && (eap->cmdidx != CMD_redir
4682 || p != eap->arg + 1 || p[-1] != '@'))
4683 || *p == '|' || *p == '\n')
4684 {
4685 /*
4686 * We remove the '\' before the '|', unless USECTRLV is used
4687 * AND 'b' is present in 'cpoptions'.
4688 */
4689 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4690 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4691 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004692 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 --p;
4694 }
4695 else
4696 {
4697 eap->nextcmd = check_nextcmd(p);
4698 *p = NUL;
4699 break;
4700 }
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4705 del_trailing_spaces(eap->arg);
4706}
4707
4708/*
4709 * get + command from ex argument
4710 */
4711 static char_u *
4712getargcmd(argp)
4713 char_u **argp;
4714{
4715 char_u *arg = *argp;
4716 char_u *command = NULL;
4717
4718 if (*arg == '+') /* +[command] */
4719 {
4720 ++arg;
4721 if (vim_isspace(*arg))
4722 command = dollar_command;
4723 else
4724 {
4725 command = arg;
4726 arg = skip_cmd_arg(command, TRUE);
4727 if (*arg != NUL)
4728 *arg++ = NUL; /* terminate command with NUL */
4729 }
4730
4731 arg = skipwhite(arg); /* skip over spaces */
4732 *argp = arg;
4733 }
4734 return command;
4735}
4736
4737/*
4738 * Find end of "+command" argument. Skip over "\ " and "\\".
4739 */
4740 static char_u *
4741skip_cmd_arg(p, rembs)
4742 char_u *p;
4743 int rembs; /* TRUE to halve the number of backslashes */
4744{
4745 while (*p && !vim_isspace(*p))
4746 {
4747 if (*p == '\\' && p[1] != NUL)
4748 {
4749 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004750 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else
4752 ++p;
4753 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004754 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 return p;
4757}
4758
4759/*
4760 * Get "++opt=arg" argument.
4761 * Return FAIL or OK.
4762 */
4763 static int
4764getargopt(eap)
4765 exarg_T *eap;
4766{
4767 char_u *arg = eap->arg + 2;
4768 int *pp = NULL;
4769#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004770 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 char_u *p;
4772#endif
4773
4774 /* ":edit ++[no]bin[ary] file" */
4775 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4776 {
4777 if (*arg == 'n')
4778 {
4779 arg += 2;
4780 eap->force_bin = FORCE_NOBIN;
4781 }
4782 else
4783 eap->force_bin = FORCE_BIN;
4784 if (!checkforcmd(&arg, "binary", 3))
4785 return FAIL;
4786 eap->arg = skipwhite(arg);
4787 return OK;
4788 }
4789
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004790 /* ":read ++edit file" */
4791 if (STRNCMP(arg, "edit", 4) == 0)
4792 {
4793 eap->read_edit = TRUE;
4794 eap->arg = skipwhite(arg + 4);
4795 return OK;
4796 }
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (STRNCMP(arg, "ff", 2) == 0)
4799 {
4800 arg += 2;
4801 pp = &eap->force_ff;
4802 }
4803 else if (STRNCMP(arg, "fileformat", 10) == 0)
4804 {
4805 arg += 10;
4806 pp = &eap->force_ff;
4807 }
4808#ifdef FEAT_MBYTE
4809 else if (STRNCMP(arg, "enc", 3) == 0)
4810 {
4811 arg += 3;
4812 pp = &eap->force_enc;
4813 }
4814 else if (STRNCMP(arg, "encoding", 8) == 0)
4815 {
4816 arg += 8;
4817 pp = &eap->force_enc;
4818 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004819 else if (STRNCMP(arg, "bad", 3) == 0)
4820 {
4821 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004822 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#endif
4825
4826 if (pp == NULL || *arg != '=')
4827 return FAIL;
4828
4829 ++arg;
4830 *pp = (int)(arg - eap->cmd);
4831 arg = skip_cmd_arg(arg, FALSE);
4832 eap->arg = skipwhite(arg);
4833 *arg = NUL;
4834
4835#ifdef FEAT_MBYTE
4836 if (pp == &eap->force_ff)
4837 {
4838#endif
4839 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4840 return FAIL;
4841#ifdef FEAT_MBYTE
4842 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004843 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 /* Make 'fileencoding' lower case. */
4846 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4847 *p = TOLOWER_ASC(*p);
4848 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004849 else
4850 {
4851 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4852 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004853 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004854 if (STRICMP(p, "keep") == 0)
4855 eap->bad_char = BAD_KEEP;
4856 else if (STRICMP(p, "drop") == 0)
4857 eap->bad_char = BAD_DROP;
4858 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4859 eap->bad_char = *p;
4860 else
4861 return FAIL;
4862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#endif
4864
4865 return OK;
4866}
4867
4868/*
4869 * ":abbreviate" and friends.
4870 */
4871 static void
4872ex_abbreviate(eap)
4873 exarg_T *eap;
4874{
4875 do_exmap(eap, TRUE); /* almost the same as mapping */
4876}
4877
4878/*
4879 * ":map" and friends.
4880 */
4881 static void
4882ex_map(eap)
4883 exarg_T *eap;
4884{
4885 /*
4886 * If we are sourcing .exrc or .vimrc in current directory we
4887 * print the mappings for security reasons.
4888 */
4889 if (secure)
4890 {
4891 secure = 2;
4892 msg_outtrans(eap->cmd);
4893 msg_putchar('\n');
4894 }
4895 do_exmap(eap, FALSE);
4896}
4897
4898/*
4899 * ":unmap" and friends.
4900 */
4901 static void
4902ex_unmap(eap)
4903 exarg_T *eap;
4904{
4905 do_exmap(eap, FALSE);
4906}
4907
4908/*
4909 * ":mapclear" and friends.
4910 */
4911 static void
4912ex_mapclear(eap)
4913 exarg_T *eap;
4914{
4915 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4916}
4917
4918/*
4919 * ":abclear" and friends.
4920 */
4921 static void
4922ex_abclear(eap)
4923 exarg_T *eap;
4924{
4925 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4926}
4927
4928#ifdef FEAT_AUTOCMD
4929 static void
4930ex_autocmd(eap)
4931 exarg_T *eap;
4932{
4933 /*
4934 * Disallow auto commands from .exrc and .vimrc in current
4935 * directory for security reasons.
4936 */
4937 if (secure)
4938 {
4939 secure = 2;
4940 eap->errmsg = e_curdir;
4941 }
4942 else if (eap->cmdidx == CMD_autocmd)
4943 do_autocmd(eap->arg, eap->forceit);
4944 else
4945 do_augroup(eap->arg, eap->forceit);
4946}
4947
4948/*
4949 * ":doautocmd": Apply the automatic commands to the current buffer.
4950 */
4951 static void
4952ex_doautocmd(eap)
4953 exarg_T *eap;
4954{
4955 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004956 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957}
4958#endif
4959
4960#ifdef FEAT_LISTCMDS
4961/*
4962 * :[N]bunload[!] [N] [bufname] unload buffer
4963 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4964 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4965 */
4966 static void
4967ex_bunload(eap)
4968 exarg_T *eap;
4969{
4970 eap->errmsg = do_bufdel(
4971 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4972 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4973 : DOBUF_UNLOAD, eap->arg,
4974 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4975}
4976
4977/*
4978 * :[N]buffer [N] to buffer N
4979 * :[N]sbuffer [N] to buffer N
4980 */
4981 static void
4982ex_buffer(eap)
4983 exarg_T *eap;
4984{
4985 if (*eap->arg)
4986 eap->errmsg = e_trailing;
4987 else
4988 {
4989 if (eap->addr_count == 0) /* default is current buffer */
4990 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4991 else
4992 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4993 }
4994}
4995
4996/*
4997 * :[N]bmodified [N] to next mod. buffer
4998 * :[N]sbmodified [N] to next mod. buffer
4999 */
5000 static void
5001ex_bmodified(eap)
5002 exarg_T *eap;
5003{
5004 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5005}
5006
5007/*
5008 * :[N]bnext [N] to next buffer
5009 * :[N]sbnext [N] split and to next buffer
5010 */
5011 static void
5012ex_bnext(eap)
5013 exarg_T *eap;
5014{
5015 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5016}
5017
5018/*
5019 * :[N]bNext [N] to previous buffer
5020 * :[N]bprevious [N] to previous buffer
5021 * :[N]sbNext [N] split and to previous buffer
5022 * :[N]sbprevious [N] split and to previous buffer
5023 */
5024 static void
5025ex_bprevious(eap)
5026 exarg_T *eap;
5027{
5028 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5029}
5030
5031/*
5032 * :brewind to first buffer
5033 * :bfirst to first buffer
5034 * :sbrewind split and to first buffer
5035 * :sbfirst split and to first buffer
5036 */
5037 static void
5038ex_brewind(eap)
5039 exarg_T *eap;
5040{
5041 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5042}
5043
5044/*
5045 * :blast to last buffer
5046 * :sblast split and to last buffer
5047 */
5048 static void
5049ex_blast(eap)
5050 exarg_T *eap;
5051{
5052 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5053}
5054#endif
5055
5056 int
5057ends_excmd(c)
5058 int c;
5059{
5060 return (c == NUL || c == '|' || c == '"' || c == '\n');
5061}
5062
5063#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5064 || defined(PROTO)
5065/*
5066 * Return the next command, after the first '|' or '\n'.
5067 * Return NULL if not found.
5068 */
5069 char_u *
5070find_nextcmd(p)
5071 char_u *p;
5072{
5073 while (*p != '|' && *p != '\n')
5074 {
5075 if (*p == NUL)
5076 return NULL;
5077 ++p;
5078 }
5079 return (p + 1);
5080}
5081#endif
5082
5083/*
5084 * Check if *p is a separator between Ex commands.
5085 * Return NULL if it isn't, (p + 1) if it is.
5086 */
5087 char_u *
5088check_nextcmd(p)
5089 char_u *p;
5090{
5091 p = skipwhite(p);
5092 if (*p == '|' || *p == '\n')
5093 return (p + 1);
5094 else
5095 return NULL;
5096}
5097
5098/*
5099 * - if there are more files to edit
5100 * - and this is the last window
5101 * - and forceit not used
5102 * - and not repeated twice on a row
5103 * return FAIL and give error message if 'message' TRUE
5104 * return OK otherwise
5105 */
5106 static int
5107check_more(message, forceit)
5108 int message; /* when FALSE check only, no messages */
5109 int forceit;
5110{
5111 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5112
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005113 if (!forceit && only_one_window()
5114 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 if (message)
5117 {
5118#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5119 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5120 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005121 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005124 vim_strncpy(buff,
5125 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005126 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005128 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 _("%d more files to edit. Quit anyway?"), n);
5130 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5131 return OK;
5132 return FAIL;
5133 }
5134#endif
5135 if (n == 1)
5136 EMSG(_("E173: 1 more file to edit"));
5137 else
5138 EMSGN(_("E173: %ld more files to edit"), n);
5139 quitmore = 2; /* next try to quit is allowed */
5140 }
5141 return FAIL;
5142 }
5143 return OK;
5144}
5145
5146#ifdef FEAT_CMDL_COMPL
5147/*
5148 * Function given to ExpandGeneric() to obtain the list of command names.
5149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 char_u *
5151get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005152 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 int idx;
5154{
5155 if (idx >= (int)CMD_SIZE)
5156# ifdef FEAT_USR_CMDS
5157 return get_user_command_name(idx);
5158# else
5159 return NULL;
5160# endif
5161 return cmdnames[idx].cmd_name;
5162}
5163#endif
5164
5165#if defined(FEAT_USR_CMDS) || defined(PROTO)
5166static 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));
5167static void uc_list __ARGS((char_u *name, size_t name_len));
5168static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5169static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5170static 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));
5171
5172 static int
5173uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5174 char_u *name;
5175 size_t name_len;
5176 char_u *rep;
5177 long argt;
5178 long def;
5179 int flags;
5180 int compl;
5181 char_u *compl_arg;
5182 int force;
5183{
5184 ucmd_T *cmd = NULL;
5185 char_u *p;
5186 int i;
5187 int cmp = 1;
5188 char_u *rep_buf = NULL;
5189 garray_T *gap;
5190
Bram Moolenaar9c102382006-05-03 21:26:49 +00005191 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (rep_buf == NULL)
5193 {
5194 /* Can't replace termcodes - try using the string as is */
5195 rep_buf = vim_strsave(rep);
5196
5197 /* Give up if out of memory */
5198 if (rep_buf == NULL)
5199 return FAIL;
5200 }
5201
5202 /* get address of growarray: global or in curbuf */
5203 if (flags & UC_BUFFER)
5204 {
5205 gap = &curbuf->b_ucmds;
5206 if (gap->ga_itemsize == 0)
5207 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5208 }
5209 else
5210 gap = &ucmds;
5211
5212 /* Search for the command in the already defined commands. */
5213 for (i = 0; i < gap->ga_len; ++i)
5214 {
5215 size_t len;
5216
5217 cmd = USER_CMD_GA(gap, i);
5218 len = STRLEN(cmd->uc_name);
5219 cmp = STRNCMP(name, cmd->uc_name, name_len);
5220 if (cmp == 0)
5221 {
5222 if (name_len < len)
5223 cmp = -1;
5224 else if (name_len > len)
5225 cmp = 1;
5226 }
5227
5228 if (cmp == 0)
5229 {
5230 if (!force)
5231 {
5232 EMSG(_("E174: Command already exists: add ! to replace it"));
5233 goto fail;
5234 }
5235
5236 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005237 cmd->uc_rep = NULL;
5238#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5239 vim_free(cmd->uc_compl_arg);
5240 cmd->uc_compl_arg = NULL;
5241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 break;
5243 }
5244
5245 /* Stop as soon as we pass the name to add */
5246 if (cmp < 0)
5247 break;
5248 }
5249
5250 /* Extend the array unless we're replacing an existing command */
5251 if (cmp != 0)
5252 {
5253 if (ga_grow(gap, 1) != OK)
5254 goto fail;
5255 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5256 goto fail;
5257
5258 cmd = USER_CMD_GA(gap, i);
5259 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5260
5261 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263 cmd->uc_name = p;
5264 }
5265
5266 cmd->uc_rep = rep_buf;
5267 cmd->uc_argt = argt;
5268 cmd->uc_def = def;
5269 cmd->uc_compl = compl;
5270#ifdef FEAT_EVAL
5271 cmd->uc_scriptID = current_SID;
5272# ifdef FEAT_CMDL_COMPL
5273 cmd->uc_compl_arg = compl_arg;
5274# endif
5275#endif
5276
5277 return OK;
5278
5279fail:
5280 vim_free(rep_buf);
5281#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5282 vim_free(compl_arg);
5283#endif
5284 return FAIL;
5285}
5286
5287/*
5288 * List of names for completion for ":command" with the EXPAND_ flag.
5289 * Must be alphabetical for completion.
5290 */
5291static struct
5292{
5293 int expand;
5294 char *name;
5295} command_complete[] =
5296{
5297 {EXPAND_AUGROUP, "augroup"},
5298 {EXPAND_BUFFERS, "buffer"},
5299 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005300#if defined(FEAT_CSCOPE)
5301 {EXPAND_CSCOPE, "cscope"},
5302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5304 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005305 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306#endif
5307 {EXPAND_DIRECTORIES, "dir"},
5308 {EXPAND_ENV_VARS, "environment"},
5309 {EXPAND_EVENTS, "event"},
5310 {EXPAND_EXPRESSION, "expression"},
5311 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005312 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {EXPAND_FUNCTIONS, "function"},
5314 {EXPAND_HELP, "help"},
5315 {EXPAND_HIGHLIGHT, "highlight"},
5316 {EXPAND_MAPPINGS, "mapping"},
5317 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005318 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005320 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005321#if defined(FEAT_SIGNS)
5322 {EXPAND_SIGN, "sign"},
5323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {EXPAND_TAGS, "tag"},
5325 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5326 {EXPAND_USER_VARS, "var"},
5327 {0, NULL}
5328};
5329
5330 static void
5331uc_list(name, name_len)
5332 char_u *name;
5333 size_t name_len;
5334{
5335 int i, j;
5336 int found = FALSE;
5337 ucmd_T *cmd;
5338 int len;
5339 long a;
5340 garray_T *gap;
5341
5342 gap = &curbuf->b_ucmds;
5343 for (;;)
5344 {
5345 for (i = 0; i < gap->ga_len; ++i)
5346 {
5347 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005348 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350 /* Skip commands which don't match the requested prefix */
5351 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5352 continue;
5353
5354 /* Put out the title first time */
5355 if (!found)
5356 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5357 found = TRUE;
5358 msg_putchar('\n');
5359 if (got_int)
5360 break;
5361
5362 /* Special cases */
5363 msg_putchar(a & BANG ? '!' : ' ');
5364 msg_putchar(a & REGSTR ? '"' : ' ');
5365 msg_putchar(gap != &ucmds ? 'b' : ' ');
5366 msg_putchar(' ');
5367
5368 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5369 len = (int)STRLEN(cmd->uc_name) + 4;
5370
5371 do {
5372 msg_putchar(' ');
5373 ++len;
5374 } while (len < 16);
5375
5376 len = 0;
5377
5378 /* Arguments */
5379 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5380 {
5381 case 0: IObuff[len++] = '0'; break;
5382 case (EXTRA): IObuff[len++] = '*'; break;
5383 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5384 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5385 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5386 }
5387
5388 do {
5389 IObuff[len++] = ' ';
5390 } while (len < 5);
5391
5392 /* Range */
5393 if (a & (RANGE|COUNT))
5394 {
5395 if (a & COUNT)
5396 {
5397 /* -count=N */
5398 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5399 len += (int)STRLEN(IObuff + len);
5400 }
5401 else if (a & DFLALL)
5402 IObuff[len++] = '%';
5403 else if (cmd->uc_def >= 0)
5404 {
5405 /* -range=N */
5406 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5407 len += (int)STRLEN(IObuff + len);
5408 }
5409 else
5410 IObuff[len++] = '.';
5411 }
5412
5413 do {
5414 IObuff[len++] = ' ';
5415 } while (len < 11);
5416
5417 /* Completion */
5418 for (j = 0; command_complete[j].expand != 0; ++j)
5419 if (command_complete[j].expand == cmd->uc_compl)
5420 {
5421 STRCPY(IObuff + len, command_complete[j].name);
5422 len += (int)STRLEN(IObuff + len);
5423 break;
5424 }
5425
5426 do {
5427 IObuff[len++] = ' ';
5428 } while (len < 21);
5429
5430 IObuff[len] = '\0';
5431 msg_outtrans(IObuff);
5432
5433 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005434#ifdef FEAT_EVAL
5435 if (p_verbose > 0)
5436 last_set_msg(cmd->uc_scriptID);
5437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 out_flush();
5439 ui_breakcheck();
5440 if (got_int)
5441 break;
5442 }
5443 if (gap == &ucmds || i < gap->ga_len)
5444 break;
5445 gap = &ucmds;
5446 }
5447
5448 if (!found)
5449 MSG(_("No user-defined commands found"));
5450}
5451
5452 static char_u *
5453uc_fun_cmd()
5454{
5455 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5456 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5457 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5458 0xb9, 0x7f, 0};
5459 int i;
5460
5461 for (i = 0; fcmd[i]; ++i)
5462 IObuff[i] = fcmd[i] - 0x40;
5463 IObuff[i] = 0;
5464 return IObuff;
5465}
5466
5467 static int
5468uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5469 char_u *attr;
5470 size_t len;
5471 long *argt;
5472 long *def;
5473 int *flags;
5474 int *compl;
5475 char_u **compl_arg;
5476{
5477 char_u *p;
5478
5479 if (len == 0)
5480 {
5481 EMSG(_("E175: No attribute specified"));
5482 return FAIL;
5483 }
5484
5485 /* First, try the simple attributes (no arguments) */
5486 if (STRNICMP(attr, "bang", len) == 0)
5487 *argt |= BANG;
5488 else if (STRNICMP(attr, "buffer", len) == 0)
5489 *flags |= UC_BUFFER;
5490 else if (STRNICMP(attr, "register", len) == 0)
5491 *argt |= REGSTR;
5492 else if (STRNICMP(attr, "bar", len) == 0)
5493 *argt |= TRLBAR;
5494 else
5495 {
5496 int i;
5497 char_u *val = NULL;
5498 size_t vallen = 0;
5499 size_t attrlen = len;
5500
5501 /* Look for the attribute name - which is the part before any '=' */
5502 for (i = 0; i < (int)len; ++i)
5503 {
5504 if (attr[i] == '=')
5505 {
5506 val = &attr[i + 1];
5507 vallen = len - i - 1;
5508 attrlen = i;
5509 break;
5510 }
5511 }
5512
5513 if (STRNICMP(attr, "nargs", attrlen) == 0)
5514 {
5515 if (vallen == 1)
5516 {
5517 if (*val == '0')
5518 /* Do nothing - this is the default */;
5519 else if (*val == '1')
5520 *argt |= (EXTRA | NOSPC | NEEDARG);
5521 else if (*val == '*')
5522 *argt |= EXTRA;
5523 else if (*val == '?')
5524 *argt |= (EXTRA | NOSPC);
5525 else if (*val == '+')
5526 *argt |= (EXTRA | NEEDARG);
5527 else
5528 goto wrong_nargs;
5529 }
5530 else
5531 {
5532wrong_nargs:
5533 EMSG(_("E176: Invalid number of arguments"));
5534 return FAIL;
5535 }
5536 }
5537 else if (STRNICMP(attr, "range", attrlen) == 0)
5538 {
5539 *argt |= RANGE;
5540 if (vallen == 1 && *val == '%')
5541 *argt |= DFLALL;
5542 else if (val != NULL)
5543 {
5544 p = val;
5545 if (*def >= 0)
5546 {
5547two_count:
5548 EMSG(_("E177: Count cannot be specified twice"));
5549 return FAIL;
5550 }
5551
5552 *def = getdigits(&p);
5553 *argt |= (ZEROR | NOTADR);
5554
5555 if (p != val + vallen || vallen == 0)
5556 {
5557invalid_count:
5558 EMSG(_("E178: Invalid default value for count"));
5559 return FAIL;
5560 }
5561 }
5562 }
5563 else if (STRNICMP(attr, "count", attrlen) == 0)
5564 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005565 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 if (val != NULL)
5568 {
5569 p = val;
5570 if (*def >= 0)
5571 goto two_count;
5572
5573 *def = getdigits(&p);
5574
5575 if (p != val + vallen)
5576 goto invalid_count;
5577 }
5578
5579 if (*def < 0)
5580 *def = 0;
5581 }
5582 else if (STRNICMP(attr, "complete", attrlen) == 0)
5583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (val == NULL)
5585 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005586 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 return FAIL;
5588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005590 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5591 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594 else
5595 {
5596 char_u ch = attr[len];
5597 attr[len] = '\0';
5598 EMSG2(_("E181: Invalid attribute: %s"), attr);
5599 attr[len] = ch;
5600 return FAIL;
5601 }
5602 }
5603
5604 return OK;
5605}
5606
Bram Moolenaara850a712009-01-28 14:42:59 +00005607/*
5608 * ":command ..."
5609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 static void
5611ex_command(eap)
5612 exarg_T *eap;
5613{
5614 char_u *name;
5615 char_u *end;
5616 char_u *p;
5617 long argt = 0;
5618 long def = -1;
5619 int flags = 0;
5620 int compl = EXPAND_NOTHING;
5621 char_u *compl_arg = NULL;
5622 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005623 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 p = eap->arg;
5626
5627 /* Check for attributes */
5628 while (*p == '-')
5629 {
5630 ++p;
5631 end = skiptowhite(p);
5632 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5633 == FAIL)
5634 return;
5635 p = skipwhite(end);
5636 }
5637
5638 /* Get the name (if any) and skip to the following argument */
5639 name = p;
5640 if (ASCII_ISALPHA(*p))
5641 while (ASCII_ISALNUM(*p))
5642 ++p;
5643 if (!ends_excmd(*p) && !vim_iswhite(*p))
5644 {
5645 EMSG(_("E182: Invalid command name"));
5646 return;
5647 }
5648 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005649 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 /* If there is nothing after the name, and no attributes were specified,
5652 * we are listing commands
5653 */
5654 p = skipwhite(end);
5655 if (!has_attr && ends_excmd(*p))
5656 {
5657 uc_list(name, end - name);
5658 }
5659 else if (!ASCII_ISUPPER(*name))
5660 {
5661 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5662 return;
5663 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005664 else if ((name_len == 1 && *name == 'X')
5665 || (name_len <= 4
5666 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5667 {
5668 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5669 return;
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else
5672 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5673 eap->forceit);
5674}
5675
5676/*
5677 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Clear all user commands, global and for current buffer.
5679 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005680 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005682 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 uc_clear(&ucmds);
5685 uc_clear(&curbuf->b_ucmds);
5686}
5687
5688/*
5689 * Clear all user commands for "gap".
5690 */
5691 void
5692uc_clear(gap)
5693 garray_T *gap;
5694{
5695 int i;
5696 ucmd_T *cmd;
5697
5698 for (i = 0; i < gap->ga_len; ++i)
5699 {
5700 cmd = USER_CMD_GA(gap, i);
5701 vim_free(cmd->uc_name);
5702 vim_free(cmd->uc_rep);
5703# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5704 vim_free(cmd->uc_compl_arg);
5705# endif
5706 }
5707 ga_clear(gap);
5708}
5709
5710 static void
5711ex_delcommand(eap)
5712 exarg_T *eap;
5713{
5714 int i = 0;
5715 ucmd_T *cmd = NULL;
5716 int cmp = -1;
5717 garray_T *gap;
5718
5719 gap = &curbuf->b_ucmds;
5720 for (;;)
5721 {
5722 for (i = 0; i < gap->ga_len; ++i)
5723 {
5724 cmd = USER_CMD_GA(gap, i);
5725 cmp = STRCMP(eap->arg, cmd->uc_name);
5726 if (cmp <= 0)
5727 break;
5728 }
5729 if (gap == &ucmds || cmp == 0)
5730 break;
5731 gap = &ucmds;
5732 }
5733
5734 if (cmp != 0)
5735 {
5736 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5737 return;
5738 }
5739
5740 vim_free(cmd->uc_name);
5741 vim_free(cmd->uc_rep);
5742# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5743 vim_free(cmd->uc_compl_arg);
5744# endif
5745
5746 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 if (i < gap->ga_len)
5749 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5750}
5751
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005752/*
5753 * split and quote args for <f-args>
5754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 static char_u *
5756uc_split_args(arg, lenp)
5757 char_u *arg;
5758 size_t *lenp;
5759{
5760 char_u *buf;
5761 char_u *p;
5762 char_u *q;
5763 int len;
5764
5765 /* Precalculate length */
5766 p = arg;
5767 len = 2; /* Initial and final quotes */
5768
5769 while (*p)
5770 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005771 if (p[0] == '\\' && p[1] == '\\')
5772 {
5773 len += 2;
5774 p += 2;
5775 }
5776 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 len += 1;
5779 p += 2;
5780 }
5781 else if (*p == '\\' || *p == '"')
5782 {
5783 len += 2;
5784 p += 1;
5785 }
5786 else if (vim_iswhite(*p))
5787 {
5788 p = skipwhite(p);
5789 if (*p == NUL)
5790 break;
5791 len += 3; /* "," */
5792 }
5793 else
5794 {
5795 ++len;
5796 ++p;
5797 }
5798 }
5799
5800 buf = alloc(len + 1);
5801 if (buf == NULL)
5802 {
5803 *lenp = 0;
5804 return buf;
5805 }
5806
5807 p = arg;
5808 q = buf;
5809 *q++ = '"';
5810 while (*p)
5811 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005812 if (p[0] == '\\' && p[1] == '\\')
5813 {
5814 *q++ = '\\';
5815 *q++ = '\\';
5816 p += 2;
5817 }
5818 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
5820 *q++ = p[1];
5821 p += 2;
5822 }
5823 else if (*p == '\\' || *p == '"')
5824 {
5825 *q++ = '\\';
5826 *q++ = *p++;
5827 }
5828 else if (vim_iswhite(*p))
5829 {
5830 p = skipwhite(p);
5831 if (*p == NUL)
5832 break;
5833 *q++ = '"';
5834 *q++ = ',';
5835 *q++ = '"';
5836 }
5837 else
5838 {
5839 *q++ = *p++;
5840 }
5841 }
5842 *q++ = '"';
5843 *q = 0;
5844
5845 *lenp = len;
5846 return buf;
5847}
5848
5849/*
5850 * Check for a <> code in a user command.
5851 * "code" points to the '<'. "len" the length of the <> (inclusive).
5852 * "buf" is where the result is to be added.
5853 * "split_buf" points to a buffer used for splitting, caller should free it.
5854 * "split_len" is the length of what "split_buf" contains.
5855 * Returns the length of the replacement, which has been added to "buf".
5856 * Returns -1 if there was no match, and only the "<" has been copied.
5857 */
5858 static size_t
5859uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5860 char_u *code;
5861 size_t len;
5862 char_u *buf;
5863 ucmd_T *cmd; /* the user command we're expanding */
5864 exarg_T *eap; /* ex arguments */
5865 char_u **split_buf;
5866 size_t *split_len;
5867{
5868 size_t result = 0;
5869 char_u *p = code + 1;
5870 size_t l = len - 2;
5871 int quote = 0;
5872 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5873 ct_LT, ct_NONE } type = ct_NONE;
5874
5875 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5876 {
5877 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5878 p += 2;
5879 l -= 2;
5880 }
5881
Bram Moolenaar371d5402006-03-20 21:47:49 +00005882 ++l;
5883 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005885 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005887 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005889 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005891 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005893 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005895 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005897 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 type = ct_REGISTER;
5899
5900 switch (type)
5901 {
5902 case ct_ARGS:
5903 /* Simple case first */
5904 if (*eap->arg == NUL)
5905 {
5906 if (quote == 1)
5907 {
5908 result = 2;
5909 if (buf != NULL)
5910 STRCPY(buf, "''");
5911 }
5912 else
5913 result = 0;
5914 break;
5915 }
5916
5917 /* When specified there is a single argument don't split it.
5918 * Works for ":Cmd %" when % is "a b c". */
5919 if ((eap->argt & NOSPC) && quote == 2)
5920 quote = 1;
5921
5922 switch (quote)
5923 {
5924 case 0: /* No quoting, no splitting */
5925 result = STRLEN(eap->arg);
5926 if (buf != NULL)
5927 STRCPY(buf, eap->arg);
5928 break;
5929 case 1: /* Quote, but don't split */
5930 result = STRLEN(eap->arg) + 2;
5931 for (p = eap->arg; *p; ++p)
5932 {
5933 if (*p == '\\' || *p == '"')
5934 ++result;
5935 }
5936
5937 if (buf != NULL)
5938 {
5939 *buf++ = '"';
5940 for (p = eap->arg; *p; ++p)
5941 {
5942 if (*p == '\\' || *p == '"')
5943 *buf++ = '\\';
5944 *buf++ = *p;
5945 }
5946 *buf = '"';
5947 }
5948
5949 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005950 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 /* This is hard, so only do it once, and cache the result */
5952 if (*split_buf == NULL)
5953 *split_buf = uc_split_args(eap->arg, split_len);
5954
5955 result = *split_len;
5956 if (buf != NULL && result != 0)
5957 STRCPY(buf, *split_buf);
5958
5959 break;
5960 }
5961 break;
5962
5963 case ct_BANG:
5964 result = eap->forceit ? 1 : 0;
5965 if (quote)
5966 result += 2;
5967 if (buf != NULL)
5968 {
5969 if (quote)
5970 *buf++ = '"';
5971 if (eap->forceit)
5972 *buf++ = '!';
5973 if (quote)
5974 *buf = '"';
5975 }
5976 break;
5977
5978 case ct_LINE1:
5979 case ct_LINE2:
5980 case ct_COUNT:
5981 {
5982 char num_buf[20];
5983 long num = (type == ct_LINE1) ? eap->line1 :
5984 (type == ct_LINE2) ? eap->line2 :
5985 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5986 size_t num_len;
5987
5988 sprintf(num_buf, "%ld", num);
5989 num_len = STRLEN(num_buf);
5990 result = num_len;
5991
5992 if (quote)
5993 result += 2;
5994
5995 if (buf != NULL)
5996 {
5997 if (quote)
5998 *buf++ = '"';
5999 STRCPY(buf, num_buf);
6000 buf += num_len;
6001 if (quote)
6002 *buf = '"';
6003 }
6004
6005 break;
6006 }
6007
6008 case ct_REGISTER:
6009 result = eap->regname ? 1 : 0;
6010 if (quote)
6011 result += 2;
6012 if (buf != NULL)
6013 {
6014 if (quote)
6015 *buf++ = '\'';
6016 if (eap->regname)
6017 *buf++ = eap->regname;
6018 if (quote)
6019 *buf = '\'';
6020 }
6021 break;
6022
6023 case ct_LT:
6024 result = 1;
6025 if (buf != NULL)
6026 *buf = '<';
6027 break;
6028
6029 default:
6030 /* Not recognized: just copy the '<' and return -1. */
6031 result = (size_t)-1;
6032 if (buf != NULL)
6033 *buf = '<';
6034 break;
6035 }
6036
6037 return result;
6038}
6039
6040 static void
6041do_ucmd(eap)
6042 exarg_T *eap;
6043{
6044 char_u *buf;
6045 char_u *p;
6046 char_u *q;
6047
6048 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006049 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006050 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 size_t len, totlen;
6052
6053 size_t split_len = 0;
6054 char_u *split_buf = NULL;
6055 ucmd_T *cmd;
6056#ifdef FEAT_EVAL
6057 scid_T save_current_SID = current_SID;
6058#endif
6059
6060 if (eap->cmdidx == CMD_USER)
6061 cmd = USER_CMD(eap->useridx);
6062 else
6063 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6064
6065 /*
6066 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006067 * First round: "buf" is NULL, compute length, allocate "buf".
6068 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 */
6070 buf = NULL;
6071 for (;;)
6072 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006073 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006074 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006076
6077 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006079 start = vim_strchr(p, '<');
6080 if (start != NULL)
6081 end = vim_strchr(start + 1, '>');
6082 if (buf != NULL)
6083 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006084 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6085 ;
6086 if (*ksp == K_SPECIAL
6087 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006088 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6089# ifdef FEAT_GUI
6090 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6091# endif
6092 ))
6093 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006094 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006095 * KS_SPECIAL KE_FILLER, like for mappings, but
6096 * do_cmdline() doesn't handle that, so convert it back.
6097 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6098 len = ksp - p;
6099 if (len > 0)
6100 {
6101 mch_memmove(q, p, len);
6102 q += len;
6103 }
6104 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6105 p = ksp + 3;
6106 continue;
6107 }
6108 }
6109
6110 /* break if there no <item> is found */
6111 if (start == NULL || end == NULL)
6112 break;
6113
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 /* Include the '>' */
6115 ++end;
6116
6117 /* Take everything up to the '<' */
6118 len = start - p;
6119 if (buf == NULL)
6120 totlen += len;
6121 else
6122 {
6123 mch_memmove(q, p, len);
6124 q += len;
6125 }
6126
6127 len = uc_check_code(start, end - start, q, cmd, eap,
6128 &split_buf, &split_len);
6129 if (len == (size_t)-1)
6130 {
6131 /* no match, continue after '<' */
6132 p = start + 1;
6133 len = 1;
6134 }
6135 else
6136 p = end;
6137 if (buf == NULL)
6138 totlen += len;
6139 else
6140 q += len;
6141 }
6142 if (buf != NULL) /* second time here, finished */
6143 {
6144 STRCPY(q, p);
6145 break;
6146 }
6147
6148 totlen += STRLEN(p); /* Add on the trailing characters */
6149 buf = alloc((unsigned)(totlen + 1));
6150 if (buf == NULL)
6151 {
6152 vim_free(split_buf);
6153 return;
6154 }
6155 }
6156
6157#ifdef FEAT_EVAL
6158 current_SID = cmd->uc_scriptID;
6159#endif
6160 (void)do_cmdline(buf, eap->getline, eap->cookie,
6161 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6162#ifdef FEAT_EVAL
6163 current_SID = save_current_SID;
6164#endif
6165 vim_free(buf);
6166 vim_free(split_buf);
6167}
6168
6169# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6170 static char_u *
6171get_user_command_name(idx)
6172 int idx;
6173{
6174 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6175}
6176
6177/*
6178 * Function given to ExpandGeneric() to obtain the list of user command names.
6179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 char_u *
6181get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006182 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 int idx;
6184{
6185 if (idx < curbuf->b_ucmds.ga_len)
6186 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6187 idx -= curbuf->b_ucmds.ga_len;
6188 if (idx < ucmds.ga_len)
6189 return USER_CMD(idx)->uc_name;
6190 return NULL;
6191}
6192
6193/*
6194 * Function given to ExpandGeneric() to obtain the list of user command
6195 * attributes.
6196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 char_u *
6198get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006199 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int idx;
6201{
6202 static char *user_cmd_flags[] =
6203 {"bang", "bar", "buffer", "complete", "count",
6204 "nargs", "range", "register"};
6205
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006206 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 return NULL;
6208 return (char_u *)user_cmd_flags[idx];
6209}
6210
6211/*
6212 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 char_u *
6215get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006216 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 int idx;
6218{
6219 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6220
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006221 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 return NULL;
6223 return (char_u *)user_cmd_nargs[idx];
6224}
6225
6226/*
6227 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 char_u *
6230get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006231 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 int idx;
6233{
6234 return (char_u *)command_complete[idx].name;
6235}
6236# endif /* FEAT_CMDL_COMPL */
6237
6238#endif /* FEAT_USR_CMDS */
6239
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006240#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6241/*
6242 * Parse a completion argument "value[vallen]".
6243 * The detected completion goes in "*complp", argument type in "*argt".
6244 * When there is an argument, for function and user defined completion, it's
6245 * copied to allocated memory and stored in "*compl_arg".
6246 * Returns FAIL if something is wrong.
6247 */
6248 int
6249parse_compl_arg(value, vallen, complp, argt, compl_arg)
6250 char_u *value;
6251 int vallen;
6252 int *complp;
6253 long *argt;
6254 char_u **compl_arg;
6255{
6256 char_u *arg = NULL;
6257 size_t arglen = 0;
6258 int i;
6259 int valend = vallen;
6260
6261 /* Look for any argument part - which is the part after any ',' */
6262 for (i = 0; i < vallen; ++i)
6263 {
6264 if (value[i] == ',')
6265 {
6266 arg = &value[i + 1];
6267 arglen = vallen - i - 1;
6268 valend = i;
6269 break;
6270 }
6271 }
6272
6273 for (i = 0; command_complete[i].expand != 0; ++i)
6274 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006275 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006276 && STRNCMP(value, command_complete[i].name, valend) == 0)
6277 {
6278 *complp = command_complete[i].expand;
6279 if (command_complete[i].expand == EXPAND_BUFFERS)
6280 *argt |= BUFNAME;
6281 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6282 || command_complete[i].expand == EXPAND_FILES)
6283 *argt |= XFILE;
6284 break;
6285 }
6286 }
6287
6288 if (command_complete[i].expand == 0)
6289 {
6290 EMSG2(_("E180: Invalid complete value: %s"), value);
6291 return FAIL;
6292 }
6293
6294# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6295 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6296 && arg != NULL)
6297# else
6298 if (arg != NULL)
6299# endif
6300 {
6301 EMSG(_("E468: Completion argument only allowed for custom completion"));
6302 return FAIL;
6303 }
6304
6305# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6306 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6307 && arg == NULL)
6308 {
6309 EMSG(_("E467: Custom completion requires a function argument"));
6310 return FAIL;
6311 }
6312
6313 if (arg != NULL)
6314 *compl_arg = vim_strnsave(arg, (int)arglen);
6315# endif
6316 return OK;
6317}
6318#endif
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 static void
6321ex_colorscheme(eap)
6322 exarg_T *eap;
6323{
Bram Moolenaare6850792010-05-14 15:28:44 +02006324 if (*eap->arg == NUL)
6325 {
6326#ifdef FEAT_EVAL
6327 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6328 char_u *p = NULL;
6329
6330 if (expr != NULL)
6331 {
6332 ++emsg_off;
6333 p = eval_to_string(expr, NULL, FALSE);
6334 --emsg_off;
6335 vim_free(expr);
6336 }
6337 if (p != NULL)
6338 {
6339 MSG(p);
6340 vim_free(p);
6341 }
6342 else
6343 MSG("default");
6344#else
6345 MSG(_("unknown"));
6346#endif
6347 }
6348 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6350}
6351
6352 static void
6353ex_highlight(eap)
6354 exarg_T *eap;
6355{
6356 if (*eap->arg == NUL && eap->cmd[2] == '!')
6357 MSG(_("Greetings, Vim user!"));
6358 do_highlight(eap->arg, eap->forceit, FALSE);
6359}
6360
6361
6362/*
6363 * Call this function if we thought we were going to exit, but we won't
6364 * (because of an error). May need to restore the terminal mode.
6365 */
6366 void
6367not_exiting()
6368{
6369 exiting = FALSE;
6370 settmode(TMODE_RAW);
6371}
6372
6373/*
6374 * ":quit": quit current window, quit Vim if closed the last window.
6375 */
6376 static void
6377ex_quit(eap)
6378 exarg_T *eap;
6379{
6380#ifdef FEAT_CMDWIN
6381 if (cmdwin_type != 0)
6382 {
6383 cmdwin_result = Ctrl_C;
6384 return;
6385 }
6386#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006387 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006388 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006389 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006390 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006391 return;
6392 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393#ifdef FEAT_AUTOCMD
6394 if (curbuf_locked())
6395 return;
6396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
6398#ifdef FEAT_NETBEANS_INTG
6399 netbeansForcedQuit = eap->forceit;
6400#endif
6401
6402 /*
6403 * If there are more files or windows we won't exit.
6404 */
6405 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6406 exiting = TRUE;
6407 if ((!P_HID(curbuf)
6408 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6409 || check_more(TRUE, eap->forceit) == FAIL
6410 || (only_one_window() && check_changed_any(eap->forceit)))
6411 {
6412 not_exiting();
6413 }
6414 else
6415 {
6416#ifdef FEAT_WINDOWS
6417 if (only_one_window()) /* quit last window */
6418#endif
6419 getout(0);
6420#ifdef FEAT_WINDOWS
6421# ifdef FEAT_GUI
6422 need_mouse_correct = TRUE;
6423# endif
6424 /* close window; may free buffer */
6425 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6426#endif
6427 }
6428}
6429
6430/*
6431 * ":cquit".
6432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 static void
6434ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006435 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 getout(1); /* this does not always pass on the exit code to the Manx
6438 compiler. why? */
6439}
6440
6441/*
6442 * ":qall": try to quit all windows
6443 */
6444 static void
6445ex_quit_all(eap)
6446 exarg_T *eap;
6447{
6448# ifdef FEAT_CMDWIN
6449 if (cmdwin_type != 0)
6450 {
6451 if (eap->forceit)
6452 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6453 else
6454 cmdwin_result = K_XF2;
6455 return;
6456 }
6457# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006458
6459 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006460 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006461 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006462 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006463 return;
6464 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006465#ifdef FEAT_AUTOCMD
6466 if (curbuf_locked())
6467 return;
6468#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 exiting = TRUE;
6471 if (eap->forceit || !check_changed_any(FALSE))
6472 getout(0);
6473 not_exiting();
6474}
6475
Bram Moolenaard9967712006-03-11 21:18:15 +00006476#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477/*
6478 * ":close": close current window, unless it is the last one
6479 */
6480 static void
6481ex_close(eap)
6482 exarg_T *eap;
6483{
6484# ifdef FEAT_CMDWIN
6485 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006486 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 else
6488# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006489 if (!text_locked()
6490#ifdef FEAT_AUTOCMD
6491 && !curbuf_locked()
6492#endif
6493 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006494 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495}
6496
Bram Moolenaard9967712006-03-11 21:18:15 +00006497# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006498/*
6499 * ":pclose": Close any preview window.
6500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006502ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504{
6505 win_T *win;
6506
6507 for (win = firstwin; win != NULL; win = win->w_next)
6508 if (win->w_p_pvw)
6509 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006510 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511 break;
6512 }
6513}
Bram Moolenaard9967712006-03-11 21:18:15 +00006514# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515
Bram Moolenaarf740b292006-02-16 22:11:02 +00006516/*
6517 * Close window "win" and take care of handling closing the last window for a
6518 * modified buffer.
6519 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006521ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 int need_hide;
6527 buf_T *buf = win->w_buffer;
6528
6529 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006532# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if ((p_confirm || cmdmod.confirm) && p_write)
6534 {
6535 dialog_changed(buf, FALSE);
6536 if (buf_valid(buf) && bufIsChanged(buf))
6537 return;
6538 need_hide = FALSE;
6539 }
6540 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
6543 EMSG(_(e_nowrtmsg));
6544 return;
6545 }
6546 }
6547
Bram Moolenaard9967712006-03-11 21:18:15 +00006548# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006550# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006553 if (tp == NULL)
6554 win_close(win, !need_hide && !P_HID(buf));
6555 else
6556 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557}
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006560 * ":tabclose": close current tab page, unless it is the last one.
6561 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 */
6563 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006564ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 exarg_T *eap;
6566{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567 tabpage_T *tp;
6568
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006569# ifdef FEAT_CMDWIN
6570 if (cmdwin_type != 0)
6571 cmdwin_result = K_IGNORE;
6572 else
6573# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006575 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006578 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006579 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006580 tp = find_tabpage((int)eap->line2);
6581 if (tp == NULL)
6582 {
6583 beep_flush();
6584 return;
6585 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006586 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006587 {
6588 tabpage_close_other(tp, eap->forceit);
6589 return;
6590 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006591 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006592 if (!text_locked()
6593#ifdef FEAT_AUTOCMD
6594 && !curbuf_locked()
6595#endif
6596 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006597 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006599}
6600
6601/*
6602 * ":tabonly": close all tab pages except the current one
6603 */
6604 static void
6605ex_tabonly(eap)
6606 exarg_T *eap;
6607{
6608 tabpage_T *tp;
6609 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006610
6611# ifdef FEAT_CMDWIN
6612 if (cmdwin_type != 0)
6613 cmdwin_result = K_IGNORE;
6614 else
6615# endif
6616 if (first_tabpage->tp_next == NULL)
6617 MSG(_("Already only one tab page"));
6618 else
6619 {
6620 /* Repeat this up to a 1000 times, because autocommands may mess
6621 * up the lists. */
6622 for (done = 0; done < 1000; ++done)
6623 {
6624 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6625 if (tp->tp_topframe != topframe)
6626 {
6627 tabpage_close_other(tp, eap->forceit);
6628 /* if we failed to close it quit */
6629 if (valid_tabpage(tp))
6630 done = 1000;
6631 /* start over, "tp" is now invalid */
6632 break;
6633 }
6634 if (first_tabpage->tp_next == NULL)
6635 break;
6636 }
6637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
6640/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006641 * Close the current tab page.
6642 */
6643 void
6644tabpage_close(forceit)
6645 int forceit;
6646{
6647 /* First close all the windows but the current one. If that worked then
6648 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006649 if (lastwin != firstwin)
6650 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006651 if (lastwin == firstwin)
6652 ex_win_close(forceit, curwin, NULL);
6653# ifdef FEAT_GUI
6654 need_mouse_correct = TRUE;
6655# endif
6656}
6657
6658/*
6659 * Close tab page "tp", which is not the current tab page.
6660 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006661 * Also takes care of the tab pages line disappearing when closing the
6662 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006663 */
6664 void
6665tabpage_close_other(tp, forceit)
6666 tabpage_T *tp;
6667 int forceit;
6668{
6669 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006670 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006671 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006672
6673 /* Limit to 1000 windows, autocommands may add a window while we close
6674 * one. OK, so I'm paranoid... */
6675 while (++done < 1000)
6676 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006677 wp = tp->tp_firstwin;
6678 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006679
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006680 /* Autocommands may delete the tab page under our fingers and we may
6681 * fail to close a window with a modified buffer. */
6682 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006683 break;
6684 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006685
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006686 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006687 if (h != tabline_height())
6688 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006689}
6690
6691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 * ":only".
6693 */
6694 static void
6695ex_only(eap)
6696 exarg_T *eap;
6697{
6698# ifdef FEAT_GUI
6699 need_mouse_correct = TRUE;
6700# endif
6701 close_others(TRUE, eap->forceit);
6702}
6703
6704/*
6705 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006706 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006708 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709ex_all(eap)
6710 exarg_T *eap;
6711{
6712 if (eap->addr_count == 0)
6713 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006714 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715}
6716#endif /* FEAT_WINDOWS */
6717
6718 static void
6719ex_hide(eap)
6720 exarg_T *eap;
6721{
6722 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6723 eap->errmsg = e_invarg;
6724 else
6725 {
6726 /* ":hide" or ":hide | cmd": hide current window */
6727 eap->nextcmd = check_nextcmd(eap->arg);
6728#ifdef FEAT_WINDOWS
6729 if (!eap->skip)
6730 {
6731# ifdef FEAT_GUI
6732 need_mouse_correct = TRUE;
6733# endif
6734 win_close(curwin, FALSE); /* don't free buffer */
6735 }
6736#endif
6737 }
6738}
6739
6740/*
6741 * ":stop" and ":suspend": Suspend Vim.
6742 */
6743 static void
6744ex_stop(eap)
6745 exarg_T *eap;
6746{
6747 /*
6748 * Disallow suspending for "rvim".
6749 */
6750 if (!check_restricted()
6751#ifdef WIN3264
6752 /*
6753 * Check if external commands are allowed now.
6754 */
6755 && can_end_termcap_mode(TRUE)
6756#endif
6757 )
6758 {
6759 if (!eap->forceit)
6760 autowrite_all();
6761 windgoto((int)Rows - 1, 0);
6762 out_char('\n');
6763 out_flush();
6764 stoptermcap();
6765 out_flush(); /* needed for SUN to restore xterm buffer */
6766#ifdef FEAT_TITLE
6767 mch_restore_title(3); /* restore window titles */
6768#endif
6769 ui_suspend(); /* call machine specific function */
6770#ifdef FEAT_TITLE
6771 maketitle();
6772 resettitle(); /* force updating the title */
6773#endif
6774 starttermcap();
6775 scroll_start(); /* scroll screen before redrawing */
6776 redraw_later_clear();
6777 shell_resized(); /* may have resized window */
6778 }
6779}
6780
6781/*
6782 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6783 */
6784 static void
6785ex_exit(eap)
6786 exarg_T *eap;
6787{
6788#ifdef FEAT_CMDWIN
6789 if (cmdwin_type != 0)
6790 {
6791 cmdwin_result = Ctrl_C;
6792 return;
6793 }
6794#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006795 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006796 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006797 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006798 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006799 return;
6800 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006801#ifdef FEAT_AUTOCMD
6802 if (curbuf_locked())
6803 return;
6804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 /*
6807 * if more files or windows we won't exit
6808 */
6809 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6810 exiting = TRUE;
6811 if ( ((eap->cmdidx == CMD_wq
6812 || curbufIsChanged())
6813 && do_write(eap) == FAIL)
6814 || check_more(TRUE, eap->forceit) == FAIL
6815 || (only_one_window() && check_changed_any(eap->forceit)))
6816 {
6817 not_exiting();
6818 }
6819 else
6820 {
6821#ifdef FEAT_WINDOWS
6822 if (only_one_window()) /* quit last window, exit Vim */
6823#endif
6824 getout(0);
6825#ifdef FEAT_WINDOWS
6826# ifdef FEAT_GUI
6827 need_mouse_correct = TRUE;
6828# endif
6829 /* quit current window, may free buffer */
6830 win_close(curwin, !P_HID(curwin->w_buffer));
6831#endif
6832 }
6833}
6834
6835/*
6836 * ":print", ":list", ":number".
6837 */
6838 static void
6839ex_print(eap)
6840 exarg_T *eap;
6841{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006842 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6843 EMSG(_(e_emptybuf));
6844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006846 for ( ;!got_int; ui_breakcheck())
6847 {
6848 print_line(eap->line1,
6849 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6850 || (eap->flags & EXFLAG_NR)),
6851 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6852 if (++eap->line1 > eap->line2)
6853 break;
6854 out_flush(); /* show one line at a time */
6855 }
6856 setpcmark();
6857 /* put cursor at last line */
6858 curwin->w_cursor.lnum = eap->line2;
6859 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
6861
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863}
6864
6865#ifdef FEAT_BYTEOFF
6866 static void
6867ex_goto(eap)
6868 exarg_T *eap;
6869{
6870 goto_byte(eap->line2);
6871}
6872#endif
6873
6874/*
6875 * ":shell".
6876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 static void
6878ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006879 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
6881 do_shell(NULL, 0);
6882}
6883
6884#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6885 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006886 || defined(FEAT_GUI_MSWIN) \
6887 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 || defined(PROTO)
6889
6890/*
6891 * Handle a file drop. The code is here because a drop is *nearly* like an
6892 * :args command, but not quite (we have a list of exact filenames, so we
6893 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6894 * code is very similar to :args and hence needs access to a lot of the static
6895 * functions in this file.
6896 *
6897 * The list should be allocated using alloc(), as should each item in the
6898 * list. This function takes over responsibility for freeing the list.
6899 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006900 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6902 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6903 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6904 * file functionality is (currently) not in EMX this is not presently a
6905 * problem.
6906 */
6907 void
6908handle_drop(filec, filev, split)
6909 int filec; /* the number of files dropped */
6910 char_u **filev; /* the list of files dropped */
6911 int split; /* force splitting the window */
6912{
6913 exarg_T ea;
6914 int save_msg_scroll = msg_scroll;
6915
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006916 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006917 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006919#ifdef FEAT_AUTOCMD
6920 if (curbuf_locked())
6921 return;
6922#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006923 /* When the screen is being updated we should not change buffers and
6924 * windows structures, it may cause freed memory to be used. */
6925 if (updating_screen)
6926 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
6928 /* Check whether the current buffer is changed. If so, we will need
6929 * to split the current window or data could be lost.
6930 * We don't need to check if the 'hidden' option is set, as in this
6931 * case the buffer won't be lost.
6932 */
6933 if (!P_HID(curbuf) && !split)
6934 {
6935 ++emsg_off;
6936 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6937 --emsg_off;
6938 }
6939 if (split)
6940 {
6941# ifdef FEAT_WINDOWS
6942 if (win_split(0, 0) == FAIL)
6943 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006944 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 /* When splitting the window, create a new alist. Otherwise the
6947 * existing one is overwritten. */
6948 alist_unlink(curwin->w_alist);
6949 alist_new();
6950# else
6951 return; /* can't split, always fail */
6952# endif
6953 }
6954
6955 /*
6956 * Set up the new argument list.
6957 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006958 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
6960 /*
6961 * Move to the first file.
6962 */
6963 /* Fake up a minimal "next" command for do_argfile() */
6964 vim_memset(&ea, 0, sizeof(ea));
6965 ea.cmd = (char_u *)"next";
6966 do_argfile(&ea, 0);
6967
6968 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6969 * mode that is not needed here. */
6970 need_start_insertmode = FALSE;
6971
6972 /* Restore msg_scroll, otherwise a following command may cause scrolling
6973 * unexpectedly. The screen will be redrawn by the caller, thus
6974 * msg_scroll being set by displaying a message is irrelevant. */
6975 msg_scroll = save_msg_scroll;
6976}
6977#endif
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979/*
6980 * Clear an argument list: free all file names and reset it to zero entries.
6981 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006982 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983alist_clear(al)
6984 alist_T *al;
6985{
6986 while (--al->al_ga.ga_len >= 0)
6987 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6988 ga_clear(&al->al_ga);
6989}
6990
6991/*
6992 * Init an argument list.
6993 */
6994 void
6995alist_init(al)
6996 alist_T *al;
6997{
6998 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6999}
7000
7001#if defined(FEAT_WINDOWS) || defined(PROTO)
7002
7003/*
7004 * Remove a reference from an argument list.
7005 * Ignored when the argument list is the global one.
7006 * If the argument list is no longer used by any window, free it.
7007 */
7008 void
7009alist_unlink(al)
7010 alist_T *al;
7011{
7012 if (al != &global_alist && --al->al_refcount <= 0)
7013 {
7014 alist_clear(al);
7015 vim_free(al);
7016 }
7017}
7018
7019# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7020/*
7021 * Create a new argument list and use it for the current window.
7022 */
7023 void
7024alist_new()
7025{
7026 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7027 if (curwin->w_alist == NULL)
7028 {
7029 curwin->w_alist = &global_alist;
7030 ++global_alist.al_refcount;
7031 }
7032 else
7033 {
7034 curwin->w_alist->al_refcount = 1;
7035 alist_init(curwin->w_alist);
7036 }
7037}
7038# endif
7039#endif
7040
7041#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7042/*
7043 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007044 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7045 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 */
7047 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007048alist_expand(fnum_list, fnum_len)
7049 int *fnum_list;
7050 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007053 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 char_u **new_arg_files;
7055 int new_arg_file_count;
7056 char_u *save_p_su = p_su;
7057 int i;
7058
7059 /* Don't use 'suffixes' here. This should work like the shell did the
7060 * expansion. Also, the vimrc file isn't read yet, thus the user
7061 * can't set the options. */
7062 p_su = empty_option;
7063 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7064 if (old_arg_files != NULL)
7065 {
7066 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007067 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7068 old_arg_count = GARGCOUNT;
7069 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 &new_arg_file_count, &new_arg_files,
7071 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7072 && new_arg_file_count > 0)
7073 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007074 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7075 TRUE, fnum_list, fnum_len);
7076 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079 p_su = save_p_su;
7080}
7081#endif
7082
7083/*
7084 * Set the argument list for the current window.
7085 * Takes over the allocated files[] and the allocated fnames in it.
7086 */
7087 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007088alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 alist_T *al;
7090 int count;
7091 char_u **files;
7092 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007093 int *fnum_list;
7094 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 int i;
7097
7098 alist_clear(al);
7099 if (ga_grow(&al->al_ga, count) == OK)
7100 {
7101 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007102 {
7103 if (got_int)
7104 {
7105 /* When adding many buffers this can take a long time. Allow
7106 * interrupting here. */
7107 while (i < count)
7108 vim_free(files[i++]);
7109 break;
7110 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007111
7112 /* May set buffer name of a buffer previously used for the
7113 * argument list, so that it's re-used by alist_add. */
7114 if (fnum_list != NULL && i < fnum_len)
7115 buf_set_name(fnum_list[i], files[i]);
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007118 ui_breakcheck();
7119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 vim_free(files);
7121 }
7122 else
7123 FreeWild(count, files);
7124#ifdef FEAT_WINDOWS
7125 if (al == &global_alist)
7126#endif
7127 arg_had_last = FALSE;
7128}
7129
7130/*
7131 * Add file "fname" to argument list "al".
7132 * "fname" must have been allocated and "al" must have been checked for room.
7133 */
7134 void
7135alist_add(al, fname, set_fnum)
7136 alist_T *al;
7137 char_u *fname;
7138 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7139{
7140 if (fname == NULL) /* don't add NULL file names */
7141 return;
7142#ifdef BACKSLASH_IN_FILENAME
7143 slash_adjust(fname);
7144#endif
7145 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7146 if (set_fnum > 0)
7147 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7148 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7149 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150}
7151
7152#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7153/*
7154 * Adjust slashes in file names. Called after 'shellslash' was set.
7155 */
7156 void
7157alist_slash_adjust()
7158{
7159 int i;
7160# ifdef FEAT_WINDOWS
7161 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007162 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163# endif
7164
7165 for (i = 0; i < GARGCOUNT; ++i)
7166 if (GARGLIST[i].ae_fname != NULL)
7167 slash_adjust(GARGLIST[i].ae_fname);
7168# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007169 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 if (wp->w_alist != &global_alist)
7171 for (i = 0; i < WARGCOUNT(wp); ++i)
7172 if (WARGLIST(wp)[i].ae_fname != NULL)
7173 slash_adjust(WARGLIST(wp)[i].ae_fname);
7174# endif
7175}
7176#endif
7177
7178/*
7179 * ":preserve".
7180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 static void
7182ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007183 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007185 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 ml_preserve(curbuf, TRUE);
7187}
7188
7189/*
7190 * ":recover".
7191 */
7192 static void
7193ex_recover(eap)
7194 exarg_T *eap;
7195{
7196 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7197 recoverymode = TRUE;
7198 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7199 && (*eap->arg == NUL
7200 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7201 ml_recover();
7202 recoverymode = FALSE;
7203}
7204
7205/*
7206 * Command modifier used in a wrong way.
7207 */
7208 static void
7209ex_wrongmodifier(eap)
7210 exarg_T *eap;
7211{
7212 eap->errmsg = e_invcmd;
7213}
7214
7215#ifdef FEAT_WINDOWS
7216/*
7217 * :sview [+command] file split window with new file, read-only
7218 * :split [[+command] file] split window with current or new file
7219 * :vsplit [[+command] file] split window vertically with current or new file
7220 * :new [[+command] file] split window with no or new file
7221 * :vnew [[+command] file] split vertically window with no or new file
7222 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007223 *
7224 * :tabedit open new Tab page with empty window
7225 * :tabedit [+command] file open new Tab page and edit "file"
7226 * :tabnew [[+command] file] just like :tabedit
7227 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 */
7229 void
7230ex_splitview(eap)
7231 exarg_T *eap;
7232{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007233 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007234# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007236# endif
7237# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007241# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7243 {
7244 ex_ni(eap);
7245 return;
7246 }
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# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007253# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007255 * quickfix windows. But it's OK when doing ":tab split". */
7256 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
7258 if (eap->cmdidx == CMD_split)
7259 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007260# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 if (eap->cmdidx == CMD_vsplit)
7262 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007267# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007268 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7271 FNAME_MESS, TRUE, curbuf->b_ffname);
7272 if (fname == NULL)
7273 goto theend;
7274 eap->arg = fname;
7275 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007276# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007280# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007282# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 && eap->cmdidx != CMD_new)
7286 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007287# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007288 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007289# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007290 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007291# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007292 au_has_group((char_u *)"FileExplorer"))
7293 {
7294 /* No browsing supported but we do have the file explorer:
7295 * Edit the directory. */
7296 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7297 eap->arg = (char_u *)".";
7298 }
7299 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007300# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007301 {
7302 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007304 if (fname == NULL)
7305 goto theend;
7306 eap->arg = fname;
7307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 }
7309 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007312 /*
7313 * Either open new tab page or split the window.
7314 */
7315 if (eap->cmdidx == CMD_tabedit
7316 || eap->cmdidx == CMD_tabfind
7317 || eap->cmdidx == CMD_tabnew)
7318 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007319 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7320 : eap->addr_count == 0 ? 0
7321 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007322 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007323 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007324
7325 /* set the alternate buffer for the window we came from */
7326 if (curwin != old_curwin
7327 && win_valid(old_curwin)
7328 && old_curwin->w_buffer != curbuf
7329 && !cmdmod.keepalt)
7330 old_curwin->w_alt_fnum = curbuf->b_fnum;
7331 }
7332 }
7333 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7335 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007336# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 /* Reset 'scrollbind' when editing another file, but keep it when
7338 * doing ":split" without arguments. */
7339 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007340# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007344 {
7345 RESET_BINDING(curwin);
7346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 else
7348 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 do_exedit(eap, old_curwin);
7351 }
7352
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007353# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007357# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358theend:
7359 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007362
7363/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007364 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007365 */
7366 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007367tabpage_new()
7368{
7369 exarg_T ea;
7370
7371 vim_memset(&ea, 0, sizeof(ea));
7372 ea.cmdidx = CMD_tabnew;
7373 ea.cmd = (char_u *)"tabn";
7374 ea.arg = (char_u *)"";
7375 ex_splitview(&ea);
7376}
7377
7378/*
7379 * :tabnext command
7380 */
7381 static void
7382ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383 exarg_T *eap;
7384{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007385 switch (eap->cmdidx)
7386 {
7387 case CMD_tabfirst:
7388 case CMD_tabrewind:
7389 goto_tabpage(1);
7390 break;
7391 case CMD_tablast:
7392 goto_tabpage(9999);
7393 break;
7394 case CMD_tabprevious:
7395 case CMD_tabNext:
7396 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7397 break;
7398 default: /* CMD_tabnext */
7399 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7400 break;
7401 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007402}
7403
7404/*
7405 * :tabmove command
7406 */
7407 static void
7408ex_tabmove(eap)
7409 exarg_T *eap;
7410{
7411 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412}
7413
7414/*
7415 * :tabs command: List tabs and their contents.
7416 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417 static void
7418ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007419 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420{
7421 tabpage_T *tp;
7422 win_T *wp;
7423 int tabcount = 1;
7424
7425 msg_start();
7426 msg_scroll = TRUE;
7427 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7428 {
7429 msg_putchar('\n');
7430 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7431 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7432 out_flush(); /* output one line at a time */
7433 ui_breakcheck();
7434
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007435 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007436 wp = firstwin;
7437 else
7438 wp = tp->tp_firstwin;
7439 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7440 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007441 msg_putchar('\n');
7442 msg_putchar(wp == curwin ? '>' : ' ');
7443 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007444 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7445 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446 if (buf_spname(wp->w_buffer) != NULL)
7447 STRCPY(IObuff, buf_spname(wp->w_buffer));
7448 else
7449 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7450 IObuff, IOSIZE, TRUE);
7451 msg_outtrans(IObuff);
7452 out_flush(); /* output one line at a time */
7453 ui_breakcheck();
7454 }
7455 }
7456}
7457
7458#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460/*
7461 * ":mode": Set screen mode.
7462 * If no argument given, just get the screen size and redraw.
7463 */
7464 static void
7465ex_mode(eap)
7466 exarg_T *eap;
7467{
7468 if (*eap->arg == NUL)
7469 shell_resized();
7470 else
7471 mch_screenmode(eap->arg);
7472}
7473
7474#ifdef FEAT_WINDOWS
7475/*
7476 * ":resize".
7477 * set, increment or decrement current window height
7478 */
7479 static void
7480ex_resize(eap)
7481 exarg_T *eap;
7482{
7483 int n;
7484 win_T *wp = curwin;
7485
7486 if (eap->addr_count > 0)
7487 {
7488 n = eap->line2;
7489 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7490 ;
7491 }
7492
7493#ifdef FEAT_GUI
7494 need_mouse_correct = TRUE;
7495#endif
7496 n = atol((char *)eap->arg);
7497#ifdef FEAT_VERTSPLIT
7498 if (cmdmod.split & WSP_VERT)
7499 {
7500 if (*eap->arg == '-' || *eap->arg == '+')
7501 n += W_WIDTH(curwin);
7502 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7503 n = 9999;
7504 win_setwidth_win((int)n, wp);
7505 }
7506 else
7507#endif
7508 {
7509 if (*eap->arg == '-' || *eap->arg == '+')
7510 n += curwin->w_height;
7511 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7512 n = 9999;
7513 win_setheight_win((int)n, wp);
7514 }
7515}
7516#endif
7517
7518/*
7519 * ":find [+command] <file>" command.
7520 */
7521 static void
7522ex_find(eap)
7523 exarg_T *eap;
7524{
7525#ifdef FEAT_SEARCHPATH
7526 char_u *fname;
7527 int count;
7528
7529 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7530 TRUE, curbuf->b_ffname);
7531 if (eap->addr_count > 0)
7532 {
7533 /* Repeat finding the file "count" times. This matters when it
7534 * appears several times in the path. */
7535 count = eap->line2;
7536 while (fname != NULL && --count > 0)
7537 {
7538 vim_free(fname);
7539 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7540 FALSE, curbuf->b_ffname);
7541 }
7542 }
7543
7544 if (fname != NULL)
7545 {
7546 eap->arg = fname;
7547#endif
7548 do_exedit(eap, NULL);
7549#ifdef FEAT_SEARCHPATH
7550 vim_free(fname);
7551 }
7552#endif
7553}
7554
7555/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007556 * ":open" simulation: for now just work like ":visual".
7557 */
7558 static void
7559ex_open(eap)
7560 exarg_T *eap;
7561{
7562 regmatch_T regmatch;
7563 char_u *p;
7564
7565 curwin->w_cursor.lnum = eap->line2;
7566 beginline(BL_SOL | BL_FIX);
7567 if (*eap->arg == '/')
7568 {
7569 /* ":open /pattern/": put cursor in column found with pattern */
7570 ++eap->arg;
7571 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7572 *p = NUL;
7573 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7574 if (regmatch.regprog != NULL)
7575 {
7576 regmatch.rm_ic = p_ic;
7577 p = ml_get_curline();
7578 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007579 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007580 else
7581 EMSG(_(e_nomatch));
7582 vim_free(regmatch.regprog);
7583 }
7584 /* Move to the NUL, ignore any other arguments. */
7585 eap->arg += STRLEN(eap->arg);
7586 }
7587 check_cursor();
7588
7589 eap->cmdidx = CMD_visual;
7590 do_exedit(eap, NULL);
7591}
7592
7593/*
7594 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 */
7596 static void
7597ex_edit(eap)
7598 exarg_T *eap;
7599{
7600 do_exedit(eap, NULL);
7601}
7602
7603/*
7604 * ":edit <file>" command and alikes.
7605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 void
7607do_exedit(eap, old_curwin)
7608 exarg_T *eap;
7609 win_T *old_curwin; /* curwin before doing a split or NULL */
7610{
7611 int n;
7612#ifdef FEAT_WINDOWS
7613 int need_hide;
7614#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007615 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
7617 /*
7618 * ":vi" command ends Ex mode.
7619 */
7620 if (exmode_active && (eap->cmdidx == CMD_visual
7621 || eap->cmdidx == CMD_view))
7622 {
7623 exmode_active = FALSE;
7624 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 {
7626 /* Special case: ":global/pat/visual\NLvi-commands" */
7627 if (global_busy)
7628 {
7629 int rd = RedrawingDisabled;
7630 int nwr = no_wait_return;
7631 int ms = msg_scroll;
7632#ifdef FEAT_GUI
7633 int he = hold_gui_events;
7634#endif
7635
7636 if (eap->nextcmd != NULL)
7637 {
7638 stuffReadbuff(eap->nextcmd);
7639 eap->nextcmd = NULL;
7640 }
7641
7642 if (exmode_was != EXMODE_VIM)
7643 settmode(TMODE_RAW);
7644 RedrawingDisabled = 0;
7645 no_wait_return = 0;
7646 need_wait_return = FALSE;
7647 msg_scroll = 0;
7648#ifdef FEAT_GUI
7649 hold_gui_events = 0;
7650#endif
7651 must_redraw = CLEAR;
7652
7653 main_loop(FALSE, TRUE);
7654
7655 RedrawingDisabled = rd;
7656 no_wait_return = nwr;
7657 msg_scroll = ms;
7658#ifdef FEAT_GUI
7659 hold_gui_events = he;
7660#endif
7661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665
7666 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007667 || eap->cmdidx == CMD_tabnew
7668 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#ifdef FEAT_VERTSPLIT
7670 || eap->cmdidx == CMD_vnew
7671#endif
7672 ) && *eap->arg == NUL)
7673 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007674 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 setpcmark();
7676 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007677 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7678 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 else if ((eap->cmdidx != CMD_split
7681#ifdef FEAT_VERTSPLIT
7682 && eap->cmdidx != CMD_vsplit
7683#endif
7684 )
7685 || *eap->arg != NUL
7686#ifdef FEAT_BROWSE
7687 || cmdmod.browse
7688#endif
7689 )
7690 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007691#ifdef FEAT_AUTOCMD
7692 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7693 * can bring us here, others are stopped earlier. */
7694 if (*eap->arg != NUL && curbuf_locked())
7695 return;
7696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 n = readonlymode;
7698 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7699 readonlymode = TRUE;
7700 else if (eap->cmdidx == CMD_enew)
7701 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7702 empty buffer */
7703 setpcmark();
7704 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7705 NULL, eap,
7706 /* ":edit" goes to first line if Vi compatible */
7707 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7708 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7709 ? ECMD_ONE : eap->do_ecmd_lnum,
7710 (P_HID(curbuf) ? ECMD_HIDE : 0)
7711 + (eap->forceit ? ECMD_FORCEIT : 0)
7712#ifdef FEAT_LISTCMDS
7713 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7714#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007715 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
7717 /* Editing the file failed. If the window was split, close it. */
7718#ifdef FEAT_WINDOWS
7719 if (old_curwin != NULL)
7720 {
7721 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7722 if (!need_hide || P_HID(curbuf))
7723 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007724# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7725 cleanup_T cs;
7726
7727 /* Reset the error/interrupt/exception state here so that
7728 * aborting() returns FALSE when closing a window. */
7729 enter_cleanup(&cs);
7730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731# ifdef FEAT_GUI
7732 need_mouse_correct = TRUE;
7733# endif
7734 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007735
7736# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7737 /* Restore the error/interrupt/exception state if not
7738 * discarded by a new aborting error, interrupt, or
7739 * uncaught exception. */
7740 leave_cleanup(&cs);
7741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743 }
7744#endif
7745 }
7746 else if (readonlymode && curbuf->b_nwindows == 1)
7747 {
7748 /* When editing an already visited buffer, 'readonly' won't be set
7749 * but the previous value is kept. With ":view" and ":sview" we
7750 * want the file to be readonly, except when another window is
7751 * editing the same buffer. */
7752 curbuf->b_p_ro = TRUE;
7753 }
7754 readonlymode = n;
7755 }
7756 else
7757 {
7758 if (eap->do_ecmd_cmd != NULL)
7759 do_cmdline_cmd(eap->do_ecmd_cmd);
7760#ifdef FEAT_TITLE
7761 n = curwin->w_arg_idx_invalid;
7762#endif
7763 check_arg_idx(curwin);
7764#ifdef FEAT_TITLE
7765 if (n != curwin->w_arg_idx_invalid)
7766 maketitle();
7767#endif
7768 }
7769
7770#ifdef FEAT_WINDOWS
7771 /*
7772 * if ":split file" worked, set alternate file name in old window to new
7773 * file
7774 */
7775 if (old_curwin != NULL
7776 && *eap->arg != NUL
7777 && curwin != old_curwin
7778 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007779 && old_curwin->w_buffer != curbuf
7780 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 old_curwin->w_alt_fnum = curbuf->b_fnum;
7782#endif
7783
7784 ex_no_reprint = TRUE;
7785}
7786
7787#ifndef FEAT_GUI
7788/*
7789 * ":gui" and ":gvim" when there is no GUI.
7790 */
7791 static void
7792ex_nogui(eap)
7793 exarg_T *eap;
7794{
7795 eap->errmsg = e_nogvim;
7796}
7797#endif
7798
7799#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7800 static void
7801ex_tearoff(eap)
7802 exarg_T *eap;
7803{
7804 gui_make_tearoff(eap->arg);
7805}
7806#endif
7807
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007808#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 static void
7810ex_popup(eap)
7811 exarg_T *eap;
7812{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007813 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815#endif
7816
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 static void
7818ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007819 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7822 MSG(_("No swap file"));
7823 else
7824 msg(curbuf->b_ml.ml_mfp->mf_fname);
7825}
7826
7827/*
7828 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7829 * offset.
7830 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 static void
7833ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007834 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836#ifdef FEAT_SCROLLBIND
7837 win_T *wp;
7838 long topline;
7839 long y;
7840 linenr_T old_linenr = curwin->w_cursor.lnum;
7841
7842 setpcmark();
7843
7844 /*
7845 * determine max topline
7846 */
7847 if (curwin->w_p_scb)
7848 {
7849 topline = curwin->w_topline;
7850 for (wp = firstwin; wp; wp = wp->w_next)
7851 {
7852 if (wp->w_p_scb && wp->w_buffer)
7853 {
7854 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7855 if (topline > y)
7856 topline = y;
7857 }
7858 }
7859 if (topline < 1)
7860 topline = 1;
7861 }
7862 else
7863 {
7864 topline = 1;
7865 }
7866
7867
7868 /*
7869 * set all scrollbind windows to the same topline
7870 */
7871 wp = curwin;
7872 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7873 {
7874 if (curwin->w_p_scb)
7875 {
7876 y = topline - curwin->w_topline;
7877 if (y > 0)
7878 scrollup(y, TRUE);
7879 else
7880 scrolldown(-y, TRUE);
7881 curwin->w_scbind_pos = topline;
7882 redraw_later(VALID);
7883 cursor_correct();
7884#ifdef FEAT_WINDOWS
7885 curwin->w_redr_status = TRUE;
7886#endif
7887 }
7888 }
7889 curwin = wp;
7890 if (curwin->w_p_scb)
7891 {
7892 did_syncbind = TRUE;
7893 checkpcmark();
7894 if (old_linenr != curwin->w_cursor.lnum)
7895 {
7896 char_u ctrl_o[2];
7897
7898 ctrl_o[0] = Ctrl_O;
7899 ctrl_o[1] = 0;
7900 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7901 }
7902 }
7903#endif
7904}
7905
7906
7907 static void
7908ex_read(eap)
7909 exarg_T *eap;
7910{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007911 int i;
7912 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7913 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914
7915 if (eap->usefilter) /* :r!cmd */
7916 do_bang(1, eap, FALSE, FALSE, TRUE);
7917 else
7918 {
7919 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7920 return;
7921
7922#ifdef FEAT_BROWSE
7923 if (cmdmod.browse)
7924 {
7925 char_u *browseFile;
7926
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007927 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 NULL, NULL, NULL, curbuf);
7929 if (browseFile != NULL)
7930 {
7931 i = readfile(browseFile, NULL,
7932 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7933 vim_free(browseFile);
7934 }
7935 else
7936 i = OK;
7937 }
7938 else
7939#endif
7940 if (*eap->arg == NUL)
7941 {
7942 if (check_fname() == FAIL) /* check for no file name */
7943 return;
7944 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7945 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7946 }
7947 else
7948 {
7949 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7950 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7951 i = readfile(eap->arg, NULL,
7952 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7953
7954 }
7955 if (i == FAIL)
7956 {
7957#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7958 if (!aborting())
7959#endif
7960 EMSG2(_(e_notopen), eap->arg);
7961 }
7962 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007963 {
7964 if (empty && exmode_active)
7965 {
7966 /* Delete the empty line that remains. Historically ex does
7967 * this but vi doesn't. */
7968 if (eap->line2 == 0)
7969 lnum = curbuf->b_ml.ml_line_count;
7970 else
7971 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007972 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007973 {
7974 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007975 if (curwin->w_cursor.lnum > 1
7976 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007977 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007978 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007979 }
7980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984}
7985
Bram Moolenaarea408852005-06-25 22:49:46 +00007986static char_u *prev_dir = NULL;
7987
7988#if defined(EXITFREE) || defined(PROTO)
7989 void
7990free_cd_dir()
7991{
7992 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007993 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007994
7995 vim_free(globaldir);
7996 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007997}
7998#endif
7999
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001/*
8002 * ":cd", ":lcd", ":chdir" and ":lchdir".
8003 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008004 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005ex_cd(eap)
8006 exarg_T *eap;
8007{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 char_u *new_dir;
8009 char_u *tofree;
8010
8011 new_dir = eap->arg;
8012#if !defined(UNIX) && !defined(VMS)
8013 /* for non-UNIX ":cd" means: print current directory */
8014 if (*new_dir == NUL)
8015 ex_pwd(NULL);
8016 else
8017#endif
8018 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008019#ifdef FEAT_AUTOCMD
8020 if (allbuf_locked())
8021 return;
8022#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008023 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8024 && !eap->forceit)
8025 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008026 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008027 return;
8028 }
8029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 /* ":cd -": Change to previous directory */
8031 if (STRCMP(new_dir, "-") == 0)
8032 {
8033 if (prev_dir == NULL)
8034 {
8035 EMSG(_("E186: No previous directory"));
8036 return;
8037 }
8038 new_dir = prev_dir;
8039 }
8040
8041 /* Save current directory for next ":cd -" */
8042 tofree = prev_dir;
8043 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8044 prev_dir = vim_strsave(NameBuff);
8045 else
8046 prev_dir = NULL;
8047
8048#if defined(UNIX) || defined(VMS)
8049 /* for UNIX ":cd" means: go to home directory */
8050 if (*new_dir == NUL)
8051 {
8052 /* use NameBuff for home directory name */
8053# ifdef VMS
8054 char_u *p;
8055
8056 p = mch_getenv((char_u *)"SYS$LOGIN");
8057 if (p == NULL || *p == NUL) /* empty is the same as not set */
8058 NameBuff[0] = NUL;
8059 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008060 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061# else
8062 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8063# endif
8064 new_dir = NameBuff;
8065 }
8066#endif
8067 if (new_dir == NULL || vim_chdir(new_dir))
8068 EMSG(_(e_failed));
8069 else
8070 {
8071 vim_free(curwin->w_localdir);
8072 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8073 {
8074 /* If still in global directory, need to remember current
8075 * directory as global directory. */
8076 if (globaldir == NULL && prev_dir != NULL)
8077 globaldir = vim_strsave(prev_dir);
8078 /* Remember this local directory for the window. */
8079 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8080 curwin->w_localdir = vim_strsave(NameBuff);
8081 }
8082 else
8083 {
8084 /* We are now in the global directory, no need to remember its
8085 * name. */
8086 vim_free(globaldir);
8087 globaldir = NULL;
8088 curwin->w_localdir = NULL;
8089 }
8090
8091 shorten_fnames(TRUE);
8092
8093 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008094 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 ex_pwd(eap);
8096 }
8097 vim_free(tofree);
8098 }
8099}
8100
8101/*
8102 * ":pwd".
8103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 static void
8105ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008106 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8109 {
8110#ifdef BACKSLASH_IN_FILENAME
8111 slash_adjust(NameBuff);
8112#endif
8113 msg(NameBuff);
8114 }
8115 else
8116 EMSG(_("E187: Unknown"));
8117}
8118
8119/*
8120 * ":=".
8121 */
8122 static void
8123ex_equal(eap)
8124 exarg_T *eap;
8125{
8126 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008127 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128}
8129
8130 static void
8131ex_sleep(eap)
8132 exarg_T *eap;
8133{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008134 int n;
8135 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
8137 if (cursor_valid())
8138 {
8139 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8140 if (n >= 0)
8141 windgoto((int)n, curwin->w_wcol);
8142 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008143
8144 len = eap->line2;
8145 switch (*eap->arg)
8146 {
8147 case 'm': break;
8148 case NUL: len *= 1000L; break;
8149 default: EMSG2(_(e_invarg2), eap->arg); return;
8150 }
8151 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152}
8153
8154/*
8155 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8156 */
8157 void
8158do_sleep(msec)
8159 long msec;
8160{
8161 long done;
8162
8163 cursor_on();
8164 out_flush();
8165 for (done = 0; !got_int && done < msec; done += 1000L)
8166 {
8167 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8168 ui_breakcheck();
8169 }
8170}
8171
8172 static void
8173do_exmap(eap, isabbrev)
8174 exarg_T *eap;
8175 int isabbrev;
8176{
8177 int mode;
8178 char_u *cmdp;
8179
8180 cmdp = eap->cmd;
8181 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8182
8183 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8184 eap->arg, mode, isabbrev))
8185 {
8186 case 1: EMSG(_(e_invarg));
8187 break;
8188 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8189 break;
8190 }
8191}
8192
8193/*
8194 * ":winsize" command (obsolete).
8195 */
8196 static void
8197ex_winsize(eap)
8198 exarg_T *eap;
8199{
8200 int w, h;
8201 char_u *arg = eap->arg;
8202 char_u *p;
8203
8204 w = getdigits(&arg);
8205 arg = skipwhite(arg);
8206 p = arg;
8207 h = getdigits(&arg);
8208 if (*p != NUL && *arg == NUL)
8209 set_shellsize(w, h, TRUE);
8210 else
8211 EMSG(_("E465: :winsize requires two number arguments"));
8212}
8213
8214#ifdef FEAT_WINDOWS
8215 static void
8216ex_wincmd(eap)
8217 exarg_T *eap;
8218{
8219 int xchar = NUL;
8220 char_u *p;
8221
8222 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8223 {
8224 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8225 if (eap->arg[1] == NUL)
8226 {
8227 EMSG(_(e_invarg));
8228 return;
8229 }
8230 xchar = eap->arg[1];
8231 p = eap->arg + 2;
8232 }
8233 else
8234 p = eap->arg + 1;
8235
8236 eap->nextcmd = check_nextcmd(p);
8237 p = skipwhite(p);
8238 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8239 EMSG(_(e_invarg));
8240 else
8241 {
8242 /* Pass flags on for ":vertical wincmd ]". */
8243 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008244 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8246 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008247 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
8249}
8250#endif
8251
Bram Moolenaar843ee412004-06-30 16:16:41 +00008252#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253/*
8254 * ":winpos".
8255 */
8256 static void
8257ex_winpos(eap)
8258 exarg_T *eap;
8259{
8260 int x, y;
8261 char_u *arg = eap->arg;
8262 char_u *p;
8263
8264 if (*arg == NUL)
8265 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008266# if defined(FEAT_GUI) || defined(MSWIN)
8267# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008269# else
8270 if (mch_get_winpos(&x, &y) != FAIL)
8271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8274 msg(IObuff);
8275 }
8276 else
8277# endif
8278 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8279 }
8280 else
8281 {
8282 x = getdigits(&arg);
8283 arg = skipwhite(arg);
8284 p = arg;
8285 y = getdigits(&arg);
8286 if (*p == NUL || *arg != NUL)
8287 {
8288 EMSG(_("E466: :winpos requires two number arguments"));
8289 return;
8290 }
8291# ifdef FEAT_GUI
8292 if (gui.in_use)
8293 gui_mch_set_winpos(x, y);
8294 else if (gui.starting)
8295 {
8296 /* Remember the coordinates for when the window is opened. */
8297 gui_win_x = x;
8298 gui_win_y = y;
8299 }
8300# ifdef HAVE_TGETENT
8301 else
8302# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008303# else
8304# ifdef MSWIN
8305 mch_set_winpos(x, y);
8306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307# endif
8308# ifdef HAVE_TGETENT
8309 if (*T_CWP)
8310 term_set_winpos(x, y);
8311# endif
8312 }
8313}
8314#endif
8315
8316/*
8317 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8318 */
8319 static void
8320ex_operators(eap)
8321 exarg_T *eap;
8322{
8323 oparg_T oa;
8324
8325 clear_oparg(&oa);
8326 oa.regname = eap->regname;
8327 oa.start.lnum = eap->line1;
8328 oa.end.lnum = eap->line2;
8329 oa.line_count = eap->line2 - eap->line1 + 1;
8330 oa.motion_type = MLINE;
8331#ifdef FEAT_VIRTUALEDIT
8332 virtual_op = FALSE;
8333#endif
8334 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8335 {
8336 setpcmark();
8337 curwin->w_cursor.lnum = eap->line1;
8338 beginline(BL_SOL | BL_FIX);
8339 }
8340
8341 switch (eap->cmdidx)
8342 {
8343 case CMD_delete:
8344 oa.op_type = OP_DELETE;
8345 op_delete(&oa);
8346 break;
8347
8348 case CMD_yank:
8349 oa.op_type = OP_YANK;
8350 (void)op_yank(&oa, FALSE, TRUE);
8351 break;
8352
8353 default: /* CMD_rshift or CMD_lshift */
8354 if ((eap->cmdidx == CMD_rshift)
8355#ifdef FEAT_RIGHTLEFT
8356 ^ curwin->w_p_rl
8357#endif
8358 )
8359 oa.op_type = OP_RSHIFT;
8360 else
8361 oa.op_type = OP_LSHIFT;
8362 op_shift(&oa, FALSE, eap->amount);
8363 break;
8364 }
8365#ifdef FEAT_VIRTUALEDIT
8366 virtual_op = MAYBE;
8367#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008368 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369}
8370
8371/*
8372 * ":put".
8373 */
8374 static void
8375ex_put(eap)
8376 exarg_T *eap;
8377{
8378 /* ":0put" works like ":1put!". */
8379 if (eap->line2 == 0)
8380 {
8381 eap->line2 = 1;
8382 eap->forceit = TRUE;
8383 }
8384 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008385 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8386 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387}
8388
8389/*
8390 * Handle ":copy" and ":move".
8391 */
8392 static void
8393ex_copymove(eap)
8394 exarg_T *eap;
8395{
8396 long n;
8397
8398 n = get_address(&eap->arg, FALSE, FALSE);
8399 if (eap->arg == NULL) /* error detected */
8400 {
8401 eap->nextcmd = NULL;
8402 return;
8403 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008404 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
8406 /*
8407 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8408 */
8409 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8410 {
8411 EMSG(_(e_invaddr));
8412 return;
8413 }
8414
8415 if (eap->cmdidx == CMD_move)
8416 {
8417 if (do_move(eap->line1, eap->line2, n) == FAIL)
8418 return;
8419 }
8420 else
8421 ex_copy(eap->line1, eap->line2, n);
8422 u_clearline();
8423 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008424 ex_may_print(eap);
8425}
8426
8427/*
8428 * Print the current line if flags were given to the Ex command.
8429 */
8430 static void
8431ex_may_print(eap)
8432 exarg_T *eap;
8433{
8434 if (eap->flags != 0)
8435 {
8436 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8437 (eap->flags & EXFLAG_LIST));
8438 ex_no_reprint = TRUE;
8439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440}
8441
8442/*
8443 * ":smagic" and ":snomagic".
8444 */
8445 static void
8446ex_submagic(eap)
8447 exarg_T *eap;
8448{
8449 int magic_save = p_magic;
8450
8451 p_magic = (eap->cmdidx == CMD_smagic);
8452 do_sub(eap);
8453 p_magic = magic_save;
8454}
8455
8456/*
8457 * ":join".
8458 */
8459 static void
8460ex_join(eap)
8461 exarg_T *eap;
8462{
8463 curwin->w_cursor.lnum = eap->line1;
8464 if (eap->line1 == eap->line2)
8465 {
8466 if (eap->addr_count >= 2) /* :2,2join does nothing */
8467 return;
8468 if (eap->line2 == curbuf->b_ml.ml_line_count)
8469 {
8470 beep_flush();
8471 return;
8472 }
8473 ++eap->line2;
8474 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008475 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008477 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478}
8479
8480/*
8481 * ":[addr]@r" or ":[addr]*r": execute register
8482 */
8483 static void
8484ex_at(eap)
8485 exarg_T *eap;
8486{
8487 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008488 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 curwin->w_cursor.lnum = eap->line2;
8491
8492#ifdef USE_ON_FLY_SCROLL
8493 dont_scroll = TRUE; /* disallow scrolling here */
8494#endif
8495
8496 /* get the register name. No name means to use the previous one */
8497 c = *eap->arg;
8498 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8499 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008500 /* Put the register in the typeahead buffer with the "silent" flag. */
8501 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8502 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 else
8507 {
8508 int save_efr = exec_from_reg;
8509
8510 exec_from_reg = TRUE;
8511
8512 /*
8513 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008514 * Continue until the stuff buffer is empty and all added characters
8515 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008517 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8519
8520 exec_from_reg = save_efr;
8521 }
8522}
8523
8524/*
8525 * ":!".
8526 */
8527 static void
8528ex_bang(eap)
8529 exarg_T *eap;
8530{
8531 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8532}
8533
8534/*
8535 * ":undo".
8536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 static void
8538ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008539 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008541 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008542 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008543 else
8544 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545}
8546
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008547#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008548 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008549ex_wundo(eap)
8550 exarg_T *eap;
8551{
8552 char_u hash[UNDO_HASH_SIZE];
8553
8554 u_compute_hash(hash);
8555 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8556}
8557
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008558 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008559ex_rundo(eap)
8560 exarg_T *eap;
8561{
8562 char_u hash[UNDO_HASH_SIZE];
8563
8564 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008565 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008566}
8567#endif
8568
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569/*
8570 * ":redo".
8571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 static void
8573ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008574 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 u_redo(1);
8577}
8578
8579/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008580 * ":earlier" and ":later".
8581 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008582 static void
8583ex_later(eap)
8584 exarg_T *eap;
8585{
8586 long count = 0;
8587 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008588 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008589 char_u *p = eap->arg;
8590
8591 if (*p == NUL)
8592 count = 1;
8593 else if (isdigit(*p))
8594 {
8595 count = getdigits(&p);
8596 switch (*p)
8597 {
8598 case 's': ++p; sec = TRUE; break;
8599 case 'm': ++p; sec = TRUE; count *= 60; break;
8600 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008601 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8602 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008603 }
8604 }
8605
8606 if (*p != NUL)
8607 EMSG2(_(e_invarg2), eap->arg);
8608 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008609 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8610 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008611}
8612
8613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 * ":redir": start/stop redirection.
8615 */
8616 static void
8617ex_redir(eap)
8618 exarg_T *eap;
8619{
8620 char *mode;
8621 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008622 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624 if (STRICMP(eap->arg, "END") == 0)
8625 close_redir();
8626 else
8627 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008628 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008630 ++arg;
8631 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008633 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 mode = "a";
8635 }
8636 else
8637 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008638 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640 close_redir();
8641
8642 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008643 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 if (fname == NULL)
8645 return;
8646#ifdef FEAT_BROWSE
8647 if (cmdmod.browse)
8648 {
8649 char_u *browseFile;
8650
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008651 browseFile = do_browse(BROWSE_SAVE,
8652 (char_u *)_("Save Redirection"),
8653 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 if (browseFile == NULL)
8655 return; /* operation cancelled */
8656 vim_free(fname);
8657 fname = browseFile;
8658 eap->forceit = TRUE; /* since dialog already asked */
8659 }
8660#endif
8661
8662 redir_fd = open_exfile(fname, eap->forceit, mode);
8663 vim_free(fname);
8664 }
8665#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008666 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {
8668 /* redirect to a register a-z (resp. A-Z for appending) */
8669 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008670 ++arg;
8671 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008673 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008674 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008676 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008678 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008679 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008680 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008683 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008684 if (*arg == '>')
8685 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008686 /* Make register empty when not using @A-@Z and the
8687 * command is valid. */
8688 if (*arg == NUL && !isupper(redir_reg))
8689 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008691 }
8692 if (*arg != NUL)
8693 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008695 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008696 }
8697 }
8698 else if (*arg == '=' && arg[1] == '>')
8699 {
8700 int append;
8701
8702 /* redirect to a variable */
8703 close_redir();
8704 arg += 2;
8705
8706 if (*arg == '>')
8707 {
8708 ++arg;
8709 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 }
8711 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008712 append = FALSE;
8713
8714 if (var_redir_start(skipwhite(arg), append) == OK)
8715 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 }
8717#endif
8718
8719 /* TODO: redirect to a buffer */
8720
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008722 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008724
8725 /* Make sure redirection is not off. Can happen for cmdline completion
8726 * that indirectly invokes a command to catch its output. */
8727 if (redir_fd != NULL
8728#ifdef FEAT_EVAL
8729 || redir_reg || redir_vname
8730#endif
8731 )
8732 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733}
8734
8735/*
8736 * ":redraw": force redraw
8737 */
8738 static void
8739ex_redraw(eap)
8740 exarg_T *eap;
8741{
8742 int r = RedrawingDisabled;
8743 int p = p_lz;
8744
8745 RedrawingDisabled = 0;
8746 p_lz = FALSE;
8747 update_topline();
8748 update_screen(eap->forceit ? CLEAR :
8749#ifdef FEAT_VISUAL
8750 VIsual_active ? INVERTED :
8751#endif
8752 0);
8753#ifdef FEAT_TITLE
8754 if (need_maketitle)
8755 maketitle();
8756#endif
8757 RedrawingDisabled = r;
8758 p_lz = p;
8759
8760 /* Reset msg_didout, so that a message that's there is overwritten. */
8761 msg_didout = FALSE;
8762 msg_col = 0;
8763
8764 out_flush();
8765}
8766
8767/*
8768 * ":redrawstatus": force redraw of status line(s)
8769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 static void
8771ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008772 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774#if defined(FEAT_WINDOWS)
8775 int r = RedrawingDisabled;
8776 int p = p_lz;
8777
8778 RedrawingDisabled = 0;
8779 p_lz = FALSE;
8780 if (eap->forceit)
8781 status_redraw_all();
8782 else
8783 status_redraw_curbuf();
8784 update_screen(
8785# ifdef FEAT_VISUAL
8786 VIsual_active ? INVERTED :
8787# endif
8788 0);
8789 RedrawingDisabled = r;
8790 p_lz = p;
8791 out_flush();
8792#endif
8793}
8794
8795 static void
8796close_redir()
8797{
8798 if (redir_fd != NULL)
8799 {
8800 fclose(redir_fd);
8801 redir_fd = NULL;
8802 }
8803#ifdef FEAT_EVAL
8804 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008805 if (redir_vname)
8806 {
8807 var_redir_stop();
8808 redir_vname = 0;
8809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810#endif
8811}
8812
8813#if defined(FEAT_SESSION) && defined(USE_CRNL)
8814# define MKSESSION_NL
8815static int mksession_nl = FALSE; /* use NL only in put_eol() */
8816#endif
8817
8818/*
8819 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8820 */
8821 static void
8822ex_mkrc(eap)
8823 exarg_T *eap;
8824{
8825 FILE *fd;
8826 int failed = FALSE;
8827 char_u *fname;
8828#ifdef FEAT_BROWSE
8829 char_u *browseFile = NULL;
8830#endif
8831#ifdef FEAT_SESSION
8832 int view_session = FALSE;
8833 int using_vdir = FALSE; /* using 'viewdir'? */
8834 char_u *viewFile = NULL;
8835 unsigned *flagp;
8836#endif
8837
8838 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8839 {
8840#ifdef FEAT_SESSION
8841 view_session = TRUE;
8842#else
8843 ex_ni(eap);
8844 return;
8845#endif
8846 }
8847
8848#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008849 /* Use the short file name until ":lcd" is used. We also don't use the
8850 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008851 did_lcd = FALSE;
8852
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8854 if (eap->cmdidx == CMD_mkview
8855 && (*eap->arg == NUL
8856 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8857 {
8858 eap->forceit = TRUE;
8859 fname = get_view_file(*eap->arg);
8860 if (fname == NULL)
8861 return;
8862 viewFile = fname;
8863 using_vdir = TRUE;
8864 }
8865 else
8866#endif
8867 if (*eap->arg != NUL)
8868 fname = eap->arg;
8869 else if (eap->cmdidx == CMD_mkvimrc)
8870 fname = (char_u *)VIMRC_FILE;
8871#ifdef FEAT_SESSION
8872 else if (eap->cmdidx == CMD_mksession)
8873 fname = (char_u *)SESSION_FILE;
8874#endif
8875 else
8876 fname = (char_u *)EXRC_FILE;
8877
8878#ifdef FEAT_BROWSE
8879 if (cmdmod.browse)
8880 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008881 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882# ifdef FEAT_SESSION
8883 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8884 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8885# endif
8886 (char_u *)_("Save Setup"),
8887 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8888 if (browseFile == NULL)
8889 goto theend;
8890 fname = browseFile;
8891 eap->forceit = TRUE; /* since dialog already asked */
8892 }
8893#endif
8894
8895#if defined(FEAT_SESSION) && defined(vim_mkdir)
8896 /* When using 'viewdir' may have to create the directory. */
8897 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008898 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899#endif
8900
8901 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8902 if (fd != NULL)
8903 {
8904#ifdef FEAT_SESSION
8905 if (eap->cmdidx == CMD_mkview)
8906 flagp = &vop_flags;
8907 else
8908 flagp = &ssop_flags;
8909#endif
8910
8911#ifdef MKSESSION_NL
8912 /* "unix" in 'sessionoptions': use NL line separator */
8913 if (view_session && (*flagp & SSOP_UNIX))
8914 mksession_nl = TRUE;
8915#endif
8916
8917 /* Write the version command for :mkvimrc */
8918 if (eap->cmdidx == CMD_mkvimrc)
8919 (void)put_line(fd, "version 6.0");
8920
8921#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008922 if (eap->cmdidx == CMD_mksession)
8923 {
8924 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8925 failed = TRUE;
8926 }
8927
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (eap->cmdidx != CMD_mkview)
8929#endif
8930 {
8931 /* Write setting 'compatible' first, because it has side effects.
8932 * For that same reason only do it when needed. */
8933 if (p_cp)
8934 (void)put_line(fd, "if !&cp | set cp | endif");
8935 else
8936 (void)put_line(fd, "if &cp | set nocp | endif");
8937 }
8938
8939#ifdef FEAT_SESSION
8940 if (!view_session
8941 || (eap->cmdidx == CMD_mksession
8942 && (*flagp & SSOP_OPTIONS)))
8943#endif
8944 failed |= (makemap(fd, NULL) == FAIL
8945 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8946
8947#ifdef FEAT_SESSION
8948 if (!failed && view_session)
8949 {
8950 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8951 failed = TRUE;
8952 if (eap->cmdidx == CMD_mksession)
8953 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008954 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
Bram Moolenaard9462e32011-04-11 21:35:11 +02008956 dirnow = alloc(MAXPATHL);
8957 if (dirnow == NULL)
8958 failed = TRUE;
8959 else
8960 {
8961 /*
8962 * Change to session file's dir.
8963 */
8964 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008966 *dirnow = NUL;
8967 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8968 {
8969 if (vim_chdirfile(fname) == OK)
8970 shorten_fnames(TRUE);
8971 }
8972 else if (*dirnow != NUL
8973 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8974 {
8975 if (mch_chdir((char *)globaldir) == 0)
8976 shorten_fnames(TRUE);
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaard9462e32011-04-11 21:35:11 +02008979 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
Bram Moolenaard9462e32011-04-11 21:35:11 +02008981 /* restore original dir */
8982 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008984 {
8985 if (mch_chdir((char *)dirnow) != 0)
8986 EMSG(_(e_prev_dir));
8987 shorten_fnames(TRUE);
8988 }
8989 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 }
8991 }
8992 else
8993 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008994 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8995 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8998 == FAIL)
8999 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009000 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9001 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009002 if (eap->cmdidx == CMD_mksession)
9003 {
9004 if (put_line(fd, "unlet SessionLoad") == FAIL)
9005 failed = TRUE;
9006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 }
9008#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009009 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9010 failed = TRUE;
9011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 failed |= fclose(fd);
9013
9014 if (failed)
9015 EMSG(_(e_write));
9016#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9017 else if (eap->cmdidx == CMD_mksession)
9018 {
9019 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009020 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
Bram Moolenaard9462e32011-04-11 21:35:11 +02009022 tbuf = alloc(MAXPATHL);
9023 if (tbuf != NULL)
9024 {
9025 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9026 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9027 vim_free(tbuf);
9028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 }
9030#endif
9031#ifdef MKSESSION_NL
9032 mksession_nl = FALSE;
9033#endif
9034 }
9035
9036#ifdef FEAT_BROWSE
9037theend:
9038 vim_free(browseFile);
9039#endif
9040#ifdef FEAT_SESSION
9041 vim_free(viewFile);
9042#endif
9043}
9044
Bram Moolenaardf177f62005-02-22 08:39:57 +00009045#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9046 || defined(PROTO)
9047 int
9048vim_mkdir_emsg(name, prot)
9049 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009050 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009051{
9052 if (vim_mkdir(name, prot) != 0)
9053 {
9054 EMSG2(_("E739: Cannot create directory: %s"), name);
9055 return FAIL;
9056 }
9057 return OK;
9058}
9059#endif
9060
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061/*
9062 * Open a file for writing for an Ex command, with some checks.
9063 * Return file descriptor, or NULL on failure.
9064 */
9065 FILE *
9066open_exfile(fname, forceit, mode)
9067 char_u *fname;
9068 int forceit;
9069 char *mode; /* "w" for create new file or "a" for append */
9070{
9071 FILE *fd;
9072
9073#ifdef UNIX
9074 /* with Unix it is possible to open a directory */
9075 if (mch_isdir(fname))
9076 {
9077 EMSG2(_(e_isadir2), fname);
9078 return NULL;
9079 }
9080#endif
9081 if (!forceit && *mode != 'a' && vim_fexists(fname))
9082 {
9083 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9084 return NULL;
9085 }
9086
9087 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9088 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9089
9090 return fd;
9091}
9092
9093/*
9094 * ":mark" and ":k".
9095 */
9096 static void
9097ex_mark(eap)
9098 exarg_T *eap;
9099{
9100 pos_T pos;
9101
9102 if (*eap->arg == NUL) /* No argument? */
9103 EMSG(_(e_argreq));
9104 else if (eap->arg[1] != NUL) /* more than one character? */
9105 EMSG(_(e_trailing));
9106 else
9107 {
9108 pos = curwin->w_cursor; /* save curwin->w_cursor */
9109 curwin->w_cursor.lnum = eap->line2;
9110 beginline(BL_WHITE | BL_FIX);
9111 if (setmark(*eap->arg) == FAIL) /* set mark */
9112 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9113 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9114 }
9115}
9116
9117/*
9118 * Update w_topline, w_leftcol and the cursor position.
9119 */
9120 void
9121update_topline_cursor()
9122{
9123 check_cursor(); /* put cursor on valid line */
9124 update_topline();
9125 if (!curwin->w_p_wrap)
9126 validate_cursor();
9127 update_curswant();
9128}
9129
9130#ifdef FEAT_EX_EXTRA
9131/*
9132 * ":normal[!] {commands}": Execute normal mode commands.
9133 */
9134 static void
9135ex_normal(eap)
9136 exarg_T *eap;
9137{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 int save_msg_scroll = msg_scroll;
9139 int save_restart_edit = restart_edit;
9140 int save_msg_didout = msg_didout;
9141 int save_State = State;
9142 tasave_T tabuf;
9143 int save_insertmode = p_im;
9144 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009145 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146#ifdef FEAT_MBYTE
9147 char_u *arg = NULL;
9148 int l;
9149 char_u *p;
9150#endif
9151
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009152 if (ex_normal_lock > 0)
9153 {
9154 EMSG(_(e_secure));
9155 return;
9156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 if (ex_normal_busy >= p_mmd)
9158 {
9159 EMSG(_("E192: Recursive use of :normal too deep"));
9160 return;
9161 }
9162 ++ex_normal_busy;
9163
9164 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9165 restart_edit = 0; /* don't go to Insert mode */
9166 p_im = FALSE; /* don't use 'insertmode' */
9167
9168#ifdef FEAT_MBYTE
9169 /*
9170 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9171 * this for the K_SPECIAL leading byte, otherwise special keys will not
9172 * work.
9173 */
9174 if (has_mbyte)
9175 {
9176 int len = 0;
9177
9178 /* Count the number of characters to be escaped. */
9179 for (p = eap->arg; *p != NUL; ++p)
9180 {
9181# ifdef FEAT_GUI
9182 if (*p == CSI) /* leadbyte CSI */
9183 len += 2;
9184# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009185 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9187# ifdef FEAT_GUI
9188 || *p == CSI
9189# endif
9190 )
9191 len += 2;
9192 }
9193 if (len > 0)
9194 {
9195 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9196 if (arg != NULL)
9197 {
9198 len = 0;
9199 for (p = eap->arg; *p != NUL; ++p)
9200 {
9201 arg[len++] = *p;
9202# ifdef FEAT_GUI
9203 if (*p == CSI)
9204 {
9205 arg[len++] = KS_EXTRA;
9206 arg[len++] = (int)KE_CSI;
9207 }
9208# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009209 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 {
9211 arg[len++] = *++p;
9212 if (*p == K_SPECIAL)
9213 {
9214 arg[len++] = KS_SPECIAL;
9215 arg[len++] = KE_FILLER;
9216 }
9217# ifdef FEAT_GUI
9218 else if (*p == CSI)
9219 {
9220 arg[len++] = KS_EXTRA;
9221 arg[len++] = (int)KE_CSI;
9222 }
9223# endif
9224 }
9225 arg[len] = NUL;
9226 }
9227 }
9228 }
9229 }
9230#endif
9231
9232 /*
9233 * Save the current typeahead. This is required to allow using ":normal"
9234 * from an event handler and makes sure we don't hang when the argument
9235 * ends with half a command.
9236 */
9237 save_typeahead(&tabuf);
9238 if (tabuf.typebuf_valid)
9239 {
9240 /*
9241 * Repeat the :normal command for each line in the range. When no
9242 * range given, execute it just once, without positioning the cursor
9243 * first.
9244 */
9245 do
9246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 if (eap->addr_count != 0)
9248 {
9249 curwin->w_cursor.lnum = eap->line1++;
9250 curwin->w_cursor.col = 0;
9251 }
9252
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009253 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#ifdef FEAT_MBYTE
9255 arg != NULL ? arg :
9256#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009257 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 }
9259 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9260 }
9261
9262 /* Might not return to the main loop when in an event handler. */
9263 update_topline_cursor();
9264
9265 /* Restore the previous typeahead. */
9266 restore_typeahead(&tabuf);
9267
9268 --ex_normal_busy;
9269 msg_scroll = save_msg_scroll;
9270 restart_edit = save_restart_edit;
9271 p_im = save_insertmode;
9272 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009273 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9275
9276 /* Restore the state (needed when called from a function executed for
9277 * 'indentexpr'). */
9278 State = save_State;
9279#ifdef FEAT_MBYTE
9280 vim_free(arg);
9281#endif
9282}
9283
9284/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009285 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 */
9287 static void
9288ex_startinsert(eap)
9289 exarg_T *eap;
9290{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009291 if (eap->forceit)
9292 {
9293 coladvance((colnr_T)MAXCOL);
9294 curwin->w_curswant = MAXCOL;
9295 curwin->w_set_curswant = FALSE;
9296 }
9297
Bram Moolenaara40c5002005-01-09 21:16:21 +00009298 /* Ignore the command when already in Insert mode. Inserting an
9299 * expression register that invokes a function can do this. */
9300 if (State & INSERT)
9301 return;
9302
Bram Moolenaar64969662005-12-14 21:59:55 +00009303 if (eap->cmdidx == CMD_startinsert)
9304 restart_edit = 'a';
9305 else if (eap->cmdidx == CMD_startreplace)
9306 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009308 restart_edit = 'V';
9309
9310 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009312 if (eap->cmdidx == CMD_startinsert)
9313 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 curwin->w_curswant = 0; /* avoid MAXCOL */
9315 }
9316}
9317
9318/*
9319 * ":stopinsert"
9320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 static void
9322ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009323 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 restart_edit = 0;
9326 stop_insert_mode = TRUE;
9327}
9328#endif
9329
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009330#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9331/*
9332 * Execute normal mode command "cmd".
9333 * "remap" can be REMAP_NONE or REMAP_YES.
9334 */
9335 void
9336exec_normal_cmd(cmd, remap, silent)
9337 char_u *cmd;
9338 int remap;
9339 int silent;
9340{
9341 oparg_T oa;
9342
9343 /*
9344 * Stuff the argument into the typeahead buffer.
9345 * Execute normal_cmd() until there is no typeahead left.
9346 */
9347 clear_oparg(&oa);
9348 finish_op = FALSE;
9349 ins_typebuf(cmd, remap, 0, TRUE, silent);
9350 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9351 && !got_int)
9352 {
9353 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009354 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009355 }
9356}
9357#endif
9358
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359#ifdef FEAT_FIND_ID
9360 static void
9361ex_checkpath(eap)
9362 exarg_T *eap;
9363{
9364 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9365 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9366 (linenr_T)1, (linenr_T)MAXLNUM);
9367}
9368
9369#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9370/*
9371 * ":psearch"
9372 */
9373 static void
9374ex_psearch(eap)
9375 exarg_T *eap;
9376{
9377 g_do_tagpreview = p_pvh;
9378 ex_findpat(eap);
9379 g_do_tagpreview = 0;
9380}
9381#endif
9382
9383 static void
9384ex_findpat(eap)
9385 exarg_T *eap;
9386{
9387 int whole = TRUE;
9388 long n;
9389 char_u *p;
9390 int action;
9391
9392 switch (cmdnames[eap->cmdidx].cmd_name[2])
9393 {
9394 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9395 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9396 action = ACTION_GOTO;
9397 else
9398 action = ACTION_SHOW;
9399 break;
9400 case 'i': /* ":ilist" and ":dlist" */
9401 action = ACTION_SHOW_ALL;
9402 break;
9403 case 'u': /* ":ijump" and ":djump" */
9404 action = ACTION_GOTO;
9405 break;
9406 default: /* ":isplit" and ":dsplit" */
9407 action = ACTION_SPLIT;
9408 break;
9409 }
9410
9411 n = 1;
9412 if (vim_isdigit(*eap->arg)) /* get count */
9413 {
9414 n = getdigits(&eap->arg);
9415 eap->arg = skipwhite(eap->arg);
9416 }
9417 if (*eap->arg == '/') /* Match regexp, not just whole words */
9418 {
9419 whole = FALSE;
9420 ++eap->arg;
9421 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9422 if (*p)
9423 {
9424 *p++ = NUL;
9425 p = skipwhite(p);
9426
9427 /* Check for trailing illegal characters */
9428 if (!ends_excmd(*p))
9429 eap->errmsg = e_trailing;
9430 else
9431 eap->nextcmd = check_nextcmd(p);
9432 }
9433 }
9434 if (!eap->skip)
9435 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9436 whole, !eap->forceit,
9437 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9438 n, action, eap->line1, eap->line2);
9439}
9440#endif
9441
9442#ifdef FEAT_WINDOWS
9443
9444# ifdef FEAT_QUICKFIX
9445/*
9446 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9447 */
9448 static void
9449ex_ptag(eap)
9450 exarg_T *eap;
9451{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009452 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9454}
9455
9456/*
9457 * ":pedit"
9458 */
9459 static void
9460ex_pedit(eap)
9461 exarg_T *eap;
9462{
9463 win_T *curwin_save = curwin;
9464
9465 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009466 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 keep_help_flag = curwin_save->w_buffer->b_help;
9468 do_exedit(eap, NULL);
9469 keep_help_flag = FALSE;
9470 if (curwin != curwin_save && win_valid(curwin_save))
9471 {
9472 /* Return cursor to where we were */
9473 validate_cursor();
9474 redraw_later(VALID);
9475 win_enter(curwin_save, TRUE);
9476 }
9477 g_do_tagpreview = 0;
9478}
9479# endif
9480
9481/*
9482 * ":stag", ":stselect" and ":stjump".
9483 */
9484 static void
9485ex_stag(eap)
9486 exarg_T *eap;
9487{
9488 postponed_split = -1;
9489 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009490 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9492 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009493 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495#endif
9496
9497/*
9498 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9499 */
9500 static void
9501ex_tag(eap)
9502 exarg_T *eap;
9503{
9504 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9505}
9506
9507 static void
9508ex_tag_cmd(eap, name)
9509 exarg_T *eap;
9510 char_u *name;
9511{
9512 int cmd;
9513
9514 switch (name[1])
9515 {
9516 case 'j': cmd = DT_JUMP; /* ":tjump" */
9517 break;
9518 case 's': cmd = DT_SELECT; /* ":tselect" */
9519 break;
9520 case 'p': cmd = DT_PREV; /* ":tprevious" */
9521 break;
9522 case 'N': cmd = DT_PREV; /* ":tNext" */
9523 break;
9524 case 'n': cmd = DT_NEXT; /* ":tnext" */
9525 break;
9526 case 'o': cmd = DT_POP; /* ":pop" */
9527 break;
9528 case 'f': /* ":tfirst" */
9529 case 'r': cmd = DT_FIRST; /* ":trewind" */
9530 break;
9531 case 'l': cmd = DT_LAST; /* ":tlast" */
9532 break;
9533 default: /* ":tag" */
9534#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009535 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 {
9537 do_cstag(eap);
9538 return;
9539 }
9540#endif
9541 cmd = DT_TAG;
9542 break;
9543 }
9544
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009545 if (name[0] == 'l')
9546 {
9547#ifndef FEAT_QUICKFIX
9548 ex_ni(eap);
9549 return;
9550#else
9551 cmd = DT_LTAG;
9552#endif
9553 }
9554
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9556 eap->forceit, TRUE);
9557}
9558
9559/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009560 * Check "str" for starting with a special cmdline variable.
9561 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9562 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9563 */
9564 int
9565find_cmdline_var(src, usedlen)
9566 char_u *src;
9567 int *usedlen;
9568{
9569 int len;
9570 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009571 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009572 "%",
9573#define SPEC_PERC 0
9574 "#",
9575#define SPEC_HASH 1
9576 "<cword>", /* cursor word */
9577#define SPEC_CWORD 2
9578 "<cWORD>", /* cursor WORD */
9579#define SPEC_CCWORD 3
9580 "<cfile>", /* cursor path name */
9581#define SPEC_CFILE 4
9582 "<sfile>", /* ":so" file name */
9583#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009584 "<slnum>", /* ":so" file line number */
9585#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009586#ifdef FEAT_AUTOCMD
9587 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009588# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009589 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009590# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009591 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009592# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009593#endif
9594#ifdef FEAT_CLIENTSERVER
9595 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009596# ifdef FEAT_AUTOCMD
9597# define SPEC_CLIENT 10
9598# else
9599# define SPEC_CLIENT 7
9600# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009601#endif
9602 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009603
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009604 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009605 {
9606 len = (int)STRLEN(spec_str[i]);
9607 if (STRNCMP(src, spec_str[i], len) == 0)
9608 {
9609 *usedlen = len;
9610 return i;
9611 }
9612 }
9613 return -1;
9614}
9615
9616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 * Evaluate cmdline variables.
9618 *
9619 * change '%' to curbuf->b_ffname
9620 * '#' to curwin->w_altfile
9621 * '<cword>' to word under the cursor
9622 * '<cWORD>' to WORD under the cursor
9623 * '<cfile>' to path name under the cursor
9624 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009625 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 * '<afile>' to file name for autocommand
9627 * '<abuf>' to buffer number for autocommand
9628 * '<amatch>' to matching name for autocommand
9629 *
9630 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9631 * "" for error without a message) and NULL is returned.
9632 * Returns an allocated string if a valid match was found.
9633 * Returns NULL if no match was found. "usedlen" then still contains the
9634 * number of characters to skip.
9635 */
9636 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009637eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009639 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 int *usedlen; /* characters after src that are used */
9641 linenr_T *lnump; /* line number for :e command, or NULL */
9642 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009643 int *escaped; /* return value has escaped white space (can
9644 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 int i;
9647 char_u *s;
9648 char_u *result;
9649 char_u *resultbuf = NULL;
9650 int resultlen;
9651 buf_T *buf;
9652 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9653 int spec_idx;
9654#ifdef FEAT_MODIFY_FNAME
9655 int skip_mod = FALSE;
9656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
9659 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009660 if (escaped != NULL)
9661 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662
9663 /*
9664 * Check if there is something to do.
9665 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009666 spec_idx = find_cmdline_var(src, usedlen);
9667 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 {
9669 *usedlen = 1;
9670 return NULL;
9671 }
9672
9673 /*
9674 * Skip when preceded with a backslash "\%" and "\#".
9675 * Note: In "\\%" the % is also not recognized!
9676 */
9677 if (src > srcstart && src[-1] == '\\')
9678 {
9679 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009680 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 return NULL;
9682 }
9683
9684 /*
9685 * word or WORD under cursor
9686 */
9687 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9688 {
9689 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9690 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9691 if (resultlen == 0)
9692 {
9693 *errormsg = (char_u *)"";
9694 return NULL;
9695 }
9696 }
9697
9698 /*
9699 * '#': Alternate file name
9700 * '%': Current file name
9701 * File name under the cursor
9702 * File name for autocommand
9703 * and following modifiers
9704 */
9705 else
9706 {
9707 switch (spec_idx)
9708 {
9709 case SPEC_PERC: /* '%': current file */
9710 if (curbuf->b_fname == NULL)
9711 {
9712 result = (char_u *)"";
9713 valid = 0; /* Must have ":p:h" to be valid */
9714 }
9715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 break;
9718
9719 case SPEC_HASH: /* '#' or "#99": alternate file */
9720 if (src[1] == '#') /* "##": the argument list */
9721 {
9722 result = arg_all();
9723 resultbuf = result;
9724 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009725 if (escaped != NULL)
9726 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727#ifdef FEAT_MODIFY_FNAME
9728 skip_mod = TRUE;
9729#endif
9730 break;
9731 }
9732 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009733 if (*s == '<') /* "#<99" uses v:oldfiles */
9734 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 i = (int)getdigits(&s);
9736 *usedlen = (int)(s - src); /* length of what we expand */
9737
Bram Moolenaard812df62008-11-09 12:46:09 +00009738 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009740 if (*usedlen < 2)
9741 {
9742 /* Should we give an error message for #<text? */
9743 *usedlen = 1;
9744 return NULL;
9745 }
9746#ifdef FEAT_EVAL
9747 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9748 (long)i);
9749 if (result == NULL)
9750 {
9751 *errormsg = (char_u *)"";
9752 return NULL;
9753 }
9754#else
9755 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 }
9759 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009760 {
9761 buf = buflist_findnr(i);
9762 if (buf == NULL)
9763 {
9764 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9765 return NULL;
9766 }
9767 if (lnump != NULL)
9768 *lnump = ECMD_LAST;
9769 if (buf->b_fname == NULL)
9770 {
9771 result = (char_u *)"";
9772 valid = 0; /* Must have ":p:h" to be valid */
9773 }
9774 else
9775 result = buf->b_fname;
9776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 break;
9778
9779#ifdef FEAT_SEARCHPATH
9780 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009781 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 if (result == NULL)
9783 {
9784 *errormsg = (char_u *)"";
9785 return NULL;
9786 }
9787 resultbuf = result; /* remember allocated string */
9788 break;
9789#endif
9790
9791#ifdef FEAT_AUTOCMD
9792 case SPEC_AFILE: /* file name for autocommand */
9793 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009794 if (result != NULL && !autocmd_fname_full)
9795 {
9796 /* Still need to turn the fname into a full path. It is
9797 * postponed to avoid a delay when <afile> is not used. */
9798 autocmd_fname_full = TRUE;
9799 result = FullName_save(autocmd_fname, FALSE);
9800 vim_free(autocmd_fname);
9801 autocmd_fname = result;
9802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 if (result == NULL)
9804 {
9805 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9806 return NULL;
9807 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009808 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 break;
9810
9811 case SPEC_ABUF: /* buffer number for autocommand */
9812 if (autocmd_bufnr <= 0)
9813 {
9814 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9815 return NULL;
9816 }
9817 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9818 result = strbuf;
9819 break;
9820
9821 case SPEC_AMATCH: /* match name for autocommand */
9822 result = autocmd_match;
9823 if (result == NULL)
9824 {
9825 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9826 return NULL;
9827 }
9828 break;
9829
9830#endif
9831 case SPEC_SFILE: /* file name for ":so" command */
9832 result = sourcing_name;
9833 if (result == NULL)
9834 {
9835 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9836 return NULL;
9837 }
9838 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009839 case SPEC_SLNUM: /* line in file for ":so" command */
9840 if (sourcing_name == NULL || sourcing_lnum == 0)
9841 {
9842 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9843 return NULL;
9844 }
9845 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9846 result = strbuf;
9847 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#if defined(FEAT_CLIENTSERVER)
9849 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009850 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9851 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 result = strbuf;
9853 break;
9854#endif
9855 }
9856
9857 resultlen = (int)STRLEN(result); /* length of new string */
9858 if (src[*usedlen] == '<') /* remove the file name extension */
9859 {
9860 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 resultlen = (int)(s - result);
9863 }
9864#ifdef FEAT_MODIFY_FNAME
9865 else if (!skip_mod)
9866 {
9867 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9868 &resultlen);
9869 if (result == NULL)
9870 {
9871 *errormsg = (char_u *)"";
9872 return NULL;
9873 }
9874 }
9875#endif
9876 }
9877
9878 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9879 {
9880 if (valid != VALID_HEAD + VALID_PATH)
9881 /* xgettext:no-c-format */
9882 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9883 else
9884 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9885 result = NULL;
9886 }
9887 else
9888 result = vim_strnsave(result, resultlen);
9889 vim_free(resultbuf);
9890 return result;
9891}
9892
9893/*
9894 * Concatenate all files in the argument list, separated by spaces, and return
9895 * it in one allocated string.
9896 * Spaces and backslashes in the file names are escaped with a backslash.
9897 * Returns NULL when out of memory.
9898 */
9899 static char_u *
9900arg_all()
9901{
9902 int len;
9903 int idx;
9904 char_u *retval = NULL;
9905 char_u *p;
9906
9907 /*
9908 * Do this loop two times:
9909 * first time: compute the total length
9910 * second time: concatenate the names
9911 */
9912 for (;;)
9913 {
9914 len = 0;
9915 for (idx = 0; idx < ARGCOUNT; ++idx)
9916 {
9917 p = alist_name(&ARGLIST[idx]);
9918 if (p != NULL)
9919 {
9920 if (len > 0)
9921 {
9922 /* insert a space in between names */
9923 if (retval != NULL)
9924 retval[len] = ' ';
9925 ++len;
9926 }
9927 for ( ; *p != NUL; ++p)
9928 {
9929 if (*p == ' ' || *p == '\\')
9930 {
9931 /* insert a backslash */
9932 if (retval != NULL)
9933 retval[len] = '\\';
9934 ++len;
9935 }
9936 if (retval != NULL)
9937 retval[len] = *p;
9938 ++len;
9939 }
9940 }
9941 }
9942
9943 /* second time: break here */
9944 if (retval != NULL)
9945 {
9946 retval[len] = NUL;
9947 break;
9948 }
9949
9950 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009951 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (retval == NULL)
9953 break;
9954 }
9955
9956 return retval;
9957}
9958
9959#if defined(FEAT_AUTOCMD) || defined(PROTO)
9960/*
9961 * Expand the <sfile> string in "arg".
9962 *
9963 * Returns an allocated string, or NULL for any error.
9964 */
9965 char_u *
9966expand_sfile(arg)
9967 char_u *arg;
9968{
9969 char_u *errormsg;
9970 int len;
9971 char_u *result;
9972 char_u *newres;
9973 char_u *repl;
9974 int srclen;
9975 char_u *p;
9976
9977 result = vim_strsave(arg);
9978 if (result == NULL)
9979 return NULL;
9980
9981 for (p = result; *p; )
9982 {
9983 if (STRNCMP(p, "<sfile>", 7) != 0)
9984 ++p;
9985 else
9986 {
9987 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009988 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 if (errormsg != NULL)
9990 {
9991 if (*errormsg)
9992 emsg(errormsg);
9993 vim_free(result);
9994 return NULL;
9995 }
9996 if (repl == NULL) /* no match (cannot happen) */
9997 {
9998 p += srclen;
9999 continue;
10000 }
10001 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10002 newres = alloc(len);
10003 if (newres == NULL)
10004 {
10005 vim_free(repl);
10006 vim_free(result);
10007 return NULL;
10008 }
10009 mch_memmove(newres, result, (size_t)(p - result));
10010 STRCPY(newres + (p - result), repl);
10011 len = (int)STRLEN(newres);
10012 STRCAT(newres, p + srclen);
10013 vim_free(repl);
10014 vim_free(result);
10015 result = newres;
10016 p = newres + len; /* continue after the match */
10017 }
10018 }
10019
10020 return result;
10021}
10022#endif
10023
10024#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010025static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10026 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10028static frame_T *ses_skipframe __ARGS((frame_T *fr));
10029static int ses_do_frame __ARGS((frame_T *fr));
10030static int ses_do_win __ARGS((win_T *wp));
10031static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10032static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10033static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10034
10035/*
10036 * Write openfile commands for the current buffers to an .exrc file.
10037 * Return FAIL on error, OK otherwise.
10038 */
10039 static int
10040makeopens(fd, dirnow)
10041 FILE *fd;
10042 char_u *dirnow; /* Current directory name */
10043{
10044 buf_T *buf;
10045 int only_save_windows = TRUE;
10046 int nr;
10047 int cnr = 1;
10048 int restore_size = TRUE;
10049 win_T *wp;
10050 char_u *sname;
10051 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010052 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010053 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010054 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010055 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010056 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
10058 if (ssop_flags & SSOP_BUFFERS)
10059 only_save_windows = FALSE; /* Save ALL buffers */
10060
10061 /*
10062 * Begin by setting the this_session variable, and then other
10063 * sessionable variables.
10064 */
10065#ifdef FEAT_EVAL
10066 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10067 return FAIL;
10068 if (ssop_flags & SSOP_GLOBALS)
10069 if (store_session_globals(fd) == FAIL)
10070 return FAIL;
10071#endif
10072
10073 /*
10074 * Close all windows but one.
10075 */
10076 if (put_line(fd, "silent only") == FAIL)
10077 return FAIL;
10078
10079 /*
10080 * Now a :cd command to the session directory or the current directory
10081 */
10082 if (ssop_flags & SSOP_SESDIR)
10083 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010084 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10085 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 return FAIL;
10087 }
10088 else if (ssop_flags & SSOP_CURDIR)
10089 {
10090 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10091 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010092 || fputs("cd ", fd) < 0
10093 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10094 || put_eol(fd) == FAIL)
10095 {
10096 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 vim_free(sname);
10100 }
10101
10102 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010103 * If there is an empty, unnamed buffer we will wipe it out later.
10104 * Remember the buffer number.
10105 */
10106 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10107 return FAIL;
10108 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10109 return FAIL;
10110 if (put_line(fd, "endif") == FAIL)
10111 return FAIL;
10112
10113 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 * Now save the current files, current buffer first.
10115 */
10116 if (put_line(fd, "set shortmess=aoO") == FAIL)
10117 return FAIL;
10118
10119 /* Now put the other buffers into the buffer list */
10120 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10121 {
10122 if (!(only_save_windows && buf->b_nwindows == 0)
10123 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10124 && buf->b_fname != NULL
10125 && buf->b_p_bl)
10126 {
10127 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10128 : buf->b_wininfo->wi_fpos.lnum) < 0
10129 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10130 return FAIL;
10131 }
10132 }
10133
10134 /* the global argument list */
10135 if (ses_arglist(fd, "args", &global_alist.al_ga,
10136 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10137 return FAIL;
10138
10139 if (ssop_flags & SSOP_RESIZE)
10140 {
10141 /* Note: after the restore we still check it worked!*/
10142 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10143 || put_eol(fd) == FAIL)
10144 return FAIL;
10145 }
10146
10147#ifdef FEAT_GUI
10148 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10149 {
10150 int x, y;
10151
10152 if (gui_mch_get_winpos(&x, &y) == OK)
10153 {
10154 /* Note: after the restore we still check it worked!*/
10155 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10156 return FAIL;
10157 }
10158 }
10159#endif
10160
10161 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010162 * May repeat putting Windows for each tab, when "tabpages" is in
10163 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010164 * Don't use goto_tabpage(), it may change directory and trigger
10165 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010167 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010168 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010169 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010171 int need_tabnew = FALSE;
10172
Bram Moolenaar18144c82006-04-12 21:52:12 +000010173 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010175 tabpage_T *tp = find_tabpage(tabnr);
10176
10177 if (tp == NULL)
10178 break; /* done all tab pages */
10179 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010180 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010181 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010182 tab_topframe = topframe;
10183 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010184 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010185 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010186 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010187 tab_topframe = tp->tp_topframe;
10188 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010189 if (tabnr > 1)
10190 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaar18144c82006-04-12 21:52:12 +000010193 /*
10194 * Before creating the window layout, try loading one file. If this
10195 * is aborted we don't end up with a number of useless windows.
10196 * This may have side effects! (e.g., compressed or network file).
10197 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010198 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010199 {
10200 if (ses_do_win(wp)
10201 && wp->w_buffer->b_ffname != NULL
10202 && !wp->w_buffer->b_help
10203#ifdef FEAT_QUICKFIX
10204 && !bt_nofile(wp->w_buffer)
10205#endif
10206 )
10207 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010208 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010209 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10210 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010211 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010212 if (!wp->w_arg_idx_invalid)
10213 edited_win = wp;
10214 break;
10215 }
10216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010218 /* If no file got edited create an empty tab page. */
10219 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10220 return FAIL;
10221
Bram Moolenaar18144c82006-04-12 21:52:12 +000010222 /*
10223 * Save current window layout.
10224 */
10225 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010227 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010229 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10230 return FAIL;
10231 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10232 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
Bram Moolenaar18144c82006-04-12 21:52:12 +000010234 /*
10235 * Check if window sizes can be restored (no windows omitted).
10236 * Remember the window number of the current window after restoring.
10237 */
10238 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010239 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010240 {
10241 if (ses_do_win(wp))
10242 ++nr;
10243 else
10244 restore_size = FALSE;
10245 if (curwin == wp)
10246 cnr = nr;
10247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248
Bram Moolenaar18144c82006-04-12 21:52:12 +000010249 /* Go to the first window. */
10250 if (put_line(fd, "wincmd t") == FAIL)
10251 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010252
Bram Moolenaar18144c82006-04-12 21:52:12 +000010253 /*
10254 * If more than one window, see if sizes can be restored.
10255 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10256 * resized when moving between windows.
10257 * Do this before restoring the view, so that the topline and the
10258 * cursor can be set. This is done again below.
10259 */
10260 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10261 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010262 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264
Bram Moolenaar18144c82006-04-12 21:52:12 +000010265 /*
10266 * Restore the view of the window (options, file, cursor, etc.).
10267 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010268 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010269 {
10270 if (!ses_do_win(wp))
10271 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010272 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10273 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010274 return FAIL;
10275 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10276 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010277 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010278 }
10279
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010280 /* The argument index in the first tab page is zero, need to set it in
10281 * each window. For further tab pages it's the window where we do
10282 * "tabedit". */
10283 cur_arg_idx = next_arg_idx;
10284
Bram Moolenaar18144c82006-04-12 21:52:12 +000010285 /*
10286 * Restore cursor to the current window if it's not the first one.
10287 */
10288 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10289 || put_eol(fd) == FAIL))
10290 return FAIL;
10291
10292 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010293 * Restore window sizes again after jumping around in windows, because
10294 * the current window has a minimum size while others may not.
10295 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010296 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010297 return FAIL;
10298
Bram Moolenaar18144c82006-04-12 21:52:12 +000010299 /* Don't continue in another tab page when doing only the current one
10300 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010301 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010302 break;
10303 }
10304
10305 if (ssop_flags & SSOP_TABPAGES)
10306 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010307 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10308 || put_eol(fd) == FAIL)
10309 return FAIL;
10310 }
10311
Bram Moolenaar9c102382006-05-03 21:26:49 +000010312 /*
10313 * Wipe out an empty unnamed buffer we started in.
10314 */
10315 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10316 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010317 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010318 return FAIL;
10319 if (put_line(fd, "endif") == FAIL)
10320 return FAIL;
10321 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10322 return FAIL;
10323
10324 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10325 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10326 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328
10329 /*
10330 * Lastly, execute the x.vim file if it exists.
10331 */
10332 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10333 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010334 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 || put_line(fd, "endif") == FAIL)
10336 return FAIL;
10337
10338 return OK;
10339}
10340
10341 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010342ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 FILE *fd;
10344 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010345 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346{
10347 int n = 0;
10348 win_T *wp;
10349
10350 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10351 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010352 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 {
10354 if (!ses_do_win(wp))
10355 continue;
10356 ++n;
10357
10358 /* restore height when not full height */
10359 if (wp->w_height + wp->w_status_height < topframe->fr_height
10360 && (fprintf(fd,
10361 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10362 n, (long)wp->w_height, Rows / 2, Rows) < 0
10363 || put_eol(fd) == FAIL))
10364 return FAIL;
10365
10366 /* restore width when not full width */
10367 if (wp->w_width < Columns && (fprintf(fd,
10368 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10369 n, (long)wp->w_width, Columns / 2, Columns) < 0
10370 || put_eol(fd) == FAIL))
10371 return FAIL;
10372 }
10373 }
10374 else
10375 {
10376 /* Just equalise window sizes */
10377 if (put_line(fd, "wincmd =") == FAIL)
10378 return FAIL;
10379 }
10380 return OK;
10381}
10382
10383/*
10384 * Write commands to "fd" to recursively create windows for frame "fr",
10385 * horizontally and vertically split.
10386 * After the commands the last window in the frame is the current window.
10387 * Returns FAIL when writing the commands to "fd" fails.
10388 */
10389 static int
10390ses_win_rec(fd, fr)
10391 FILE *fd;
10392 frame_T *fr;
10393{
10394 frame_T *frc;
10395 int count = 0;
10396
10397 if (fr->fr_layout != FR_LEAF)
10398 {
10399 /* Find first frame that's not skipped and then create a window for
10400 * each following one (first frame is already there). */
10401 frc = ses_skipframe(fr->fr_child);
10402 if (frc != NULL)
10403 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10404 {
10405 /* Make window as big as possible so that we have lots of room
10406 * to split. */
10407 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10408 || put_line(fd, fr->fr_layout == FR_COL
10409 ? "split" : "vsplit") == FAIL)
10410 return FAIL;
10411 ++count;
10412 }
10413
10414 /* Go back to the first window. */
10415 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10416 ? "%dwincmd k" : "%dwincmd h", count) < 0
10417 || put_eol(fd) == FAIL))
10418 return FAIL;
10419
10420 /* Recursively create frames/windows in each window of this column or
10421 * row. */
10422 frc = ses_skipframe(fr->fr_child);
10423 while (frc != NULL)
10424 {
10425 ses_win_rec(fd, frc);
10426 frc = ses_skipframe(frc->fr_next);
10427 /* Go to next window. */
10428 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10429 return FAIL;
10430 }
10431 }
10432 return OK;
10433}
10434
10435/*
10436 * Skip frames that don't contain windows we want to save in the Session.
10437 * Returns NULL when there none.
10438 */
10439 static frame_T *
10440ses_skipframe(fr)
10441 frame_T *fr;
10442{
10443 frame_T *frc;
10444
10445 for (frc = fr; frc != NULL; frc = frc->fr_next)
10446 if (ses_do_frame(frc))
10447 break;
10448 return frc;
10449}
10450
10451/*
10452 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10453 * the Session.
10454 */
10455 static int
10456ses_do_frame(fr)
10457 frame_T *fr;
10458{
10459 frame_T *frc;
10460
10461 if (fr->fr_layout == FR_LEAF)
10462 return ses_do_win(fr->fr_win);
10463 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10464 if (ses_do_frame(frc))
10465 return TRUE;
10466 return FALSE;
10467}
10468
10469/*
10470 * Return non-zero if window "wp" is to be stored in the Session.
10471 */
10472 static int
10473ses_do_win(wp)
10474 win_T *wp;
10475{
10476 if (wp->w_buffer->b_fname == NULL
10477#ifdef FEAT_QUICKFIX
10478 /* When 'buftype' is "nofile" can't restore the window contents. */
10479 || bt_nofile(wp->w_buffer)
10480#endif
10481 )
10482 return (ssop_flags & SSOP_BLANK);
10483 if (wp->w_buffer->b_help)
10484 return (ssop_flags & SSOP_HELP);
10485 return TRUE;
10486}
10487
10488/*
10489 * Write commands to "fd" to restore the view of a window.
10490 * Caller must make sure 'scrolloff' is zero.
10491 */
10492 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010493put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 FILE *fd;
10495 win_T *wp;
10496 int add_edit; /* add ":edit" command to view */
10497 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010498 int current_arg_idx; /* current argument index of the window, use
10499 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 win_T *save_curwin;
10502 int f;
10503 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010504 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505
10506 /* Always restore cursor position for ":mksession". For ":mkview" only
10507 * when 'viewoptions' contains "cursor". */
10508 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10509
10510 /*
10511 * Local argument list.
10512 */
10513 if (wp->w_alist == &global_alist)
10514 {
10515 if (put_line(fd, "argglobal") == FAIL)
10516 return FAIL;
10517 }
10518 else
10519 {
10520 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10521 flagp == &vop_flags
10522 || !(*flagp & SSOP_CURDIR)
10523 || wp->w_localdir != NULL, flagp) == FAIL)
10524 return FAIL;
10525 }
10526
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010527 /* Only when part of a session: restore the argument index. Some
10528 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010529 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010530 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010532 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 || put_eol(fd) == FAIL)
10534 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010535 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 }
10537
10538 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010539 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 {
10541 /*
10542 * Load the file.
10543 */
10544 if (wp->w_buffer->b_ffname != NULL
10545#ifdef FEAT_QUICKFIX
10546 && !bt_nofile(wp->w_buffer)
10547#endif
10548 )
10549 {
10550 /*
10551 * Editing a file in this buffer: use ":edit file".
10552 * This may have side effects! (e.g., compressed or network file).
10553 */
10554 if (fputs("edit ", fd) < 0
10555 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10556 return FAIL;
10557 }
10558 else
10559 {
10560 /* No file in this buffer, just make it empty. */
10561 if (put_line(fd, "enew") == FAIL)
10562 return FAIL;
10563#ifdef FEAT_QUICKFIX
10564 if (wp->w_buffer->b_ffname != NULL)
10565 {
10566 /* The buffer does have a name, but it's not a file name. */
10567 if (fputs("file ", fd) < 0
10568 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10569 return FAIL;
10570 }
10571#endif
10572 do_cursor = FALSE;
10573 }
10574 }
10575
10576 /*
10577 * Local mappings and abbreviations.
10578 */
10579 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10580 && makemap(fd, wp->w_buffer) == FAIL)
10581 return FAIL;
10582
10583 /*
10584 * Local options. Need to go to the window temporarily.
10585 * Store only local values when using ":mkview" and when ":mksession" is
10586 * used and 'sessionoptions' doesn't include "options".
10587 * Some folding options are always stored when "folds" is included,
10588 * otherwise the folds would not be restored correctly.
10589 */
10590 save_curwin = curwin;
10591 curwin = wp;
10592 curbuf = curwin->w_buffer;
10593 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10594 f = makeset(fd, OPT_LOCAL,
10595 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10596#ifdef FEAT_FOLDING
10597 else if (*flagp & SSOP_FOLDS)
10598 f = makefoldset(fd);
10599#endif
10600 else
10601 f = OK;
10602 curwin = save_curwin;
10603 curbuf = curwin->w_buffer;
10604 if (f == FAIL)
10605 return FAIL;
10606
10607#ifdef FEAT_FOLDING
10608 /*
10609 * Save Folds when 'buftype' is empty and for help files.
10610 */
10611 if ((*flagp & SSOP_FOLDS)
10612 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010613# ifdef FEAT_QUICKFIX
10614 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10615# endif
10616 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 {
10618 if (put_folds(fd, wp) == FAIL)
10619 return FAIL;
10620 }
10621#endif
10622
10623 /*
10624 * Set the cursor after creating folds, since that moves the cursor.
10625 */
10626 if (do_cursor)
10627 {
10628
10629 /* Restore the cursor line in the file and relatively in the
10630 * window. Don't use "G", it changes the jumplist. */
10631 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10632 (long)wp->w_cursor.lnum,
10633 (long)(wp->w_cursor.lnum - wp->w_topline),
10634 (long)wp->w_height / 2, (long)wp->w_height) < 0
10635 || put_eol(fd) == FAIL
10636 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10637 || put_line(fd, "exe s:l") == FAIL
10638 || put_line(fd, "normal! zt") == FAIL
10639 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10640 || put_eol(fd) == FAIL)
10641 return FAIL;
10642 /* Restore the cursor column and left offset when not wrapping. */
10643 if (wp->w_cursor.col == 0)
10644 {
10645 if (put_line(fd, "normal! 0") == FAIL)
10646 return FAIL;
10647 }
10648 else
10649 {
10650 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10651 {
10652 if (fprintf(fd,
10653 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10654 (long)wp->w_cursor.col,
10655 (long)(wp->w_cursor.col - wp->w_leftcol),
10656 (long)wp->w_width / 2, (long)wp->w_width) < 0
10657 || put_eol(fd) == FAIL
10658 || put_line(fd, "if s:c > 0") == FAIL
10659 || fprintf(fd,
10660 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10661 (long)wp->w_cursor.col) < 0
10662 || put_eol(fd) == FAIL
10663 || put_line(fd, "else") == FAIL
10664 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10665 || put_eol(fd) == FAIL
10666 || put_line(fd, "endif") == FAIL)
10667 return FAIL;
10668 }
10669 else
10670 {
10671 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10672 || put_eol(fd) == FAIL)
10673 return FAIL;
10674 }
10675 }
10676 }
10677
10678 /*
10679 * Local directory.
10680 */
10681 if (wp->w_localdir != NULL)
10682 {
10683 if (fputs("lcd ", fd) < 0
10684 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10685 || put_eol(fd) == FAIL)
10686 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010687 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 }
10689
10690 return OK;
10691}
10692
10693/*
10694 * Write an argument list to the session file.
10695 * Returns FAIL if writing fails.
10696 */
10697 static int
10698ses_arglist(fd, cmd, gap, fullname, flagp)
10699 FILE *fd;
10700 char *cmd;
10701 garray_T *gap;
10702 int fullname; /* TRUE: use full path name */
10703 unsigned *flagp;
10704{
10705 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010706 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 char_u *s;
10708
10709 if (gap->ga_len == 0)
10710 return put_line(fd, "silent! argdel *");
10711 if (fputs(cmd, fd) < 0)
10712 return FAIL;
10713 for (i = 0; i < gap->ga_len; ++i)
10714 {
10715 /* NULL file names are skipped (only happens when out of memory). */
10716 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10717 if (s != NULL)
10718 {
10719 if (fullname)
10720 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010721 buf = alloc(MAXPATHL);
10722 if (buf != NULL)
10723 {
10724 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10725 s = buf;
10726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 }
10728 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010729 {
10730 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010732 }
10733 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 }
10735 }
10736 return put_eol(fd);
10737}
10738
10739/*
10740 * Write a buffer name to the session file.
10741 * Also ends the line.
10742 * Returns FAIL if writing fails.
10743 */
10744 static int
10745ses_fname(fd, buf, flagp)
10746 FILE *fd;
10747 buf_T *buf;
10748 unsigned *flagp;
10749{
10750 char_u *name;
10751
10752 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010753 * the session file will be sourced.
10754 * Don't do this for ":mkview", we don't know the current directory.
10755 * Don't do this after ":lcd", we don't keep track of what the current
10756 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (buf->b_sfname != NULL
10758 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010759 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010760#ifdef FEAT_AUTOCHDIR
10761 && !p_acd
10762#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010763 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 name = buf->b_sfname;
10765 else
10766 name = buf->b_ffname;
10767 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10768 return FAIL;
10769 return OK;
10770}
10771
10772/*
10773 * Write a file name to the session file.
10774 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10775 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010776 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 */
10778 static int
10779ses_put_fname(fd, name, flagp)
10780 FILE *fd;
10781 char_u *name;
10782 unsigned *flagp;
10783{
10784 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010785 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787
10788 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010789 if (sname == NULL)
10790 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010792 if (*flagp & SSOP_SLASH)
10793 {
10794 /* change all backslashes to forward slashes */
10795 for (p = sname; *p != NUL; mb_ptr_adv(p))
10796 if (*p == '\\')
10797 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010799
10800 /* escapse special characters */
10801 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010803 if (p == NULL)
10804 return FAIL;
10805
10806 /* write the result */
10807 if (fputs((char *)p, fd) < 0)
10808 retval = FAIL;
10809
10810 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 return retval;
10812}
10813
10814/*
10815 * ":loadview [nr]"
10816 */
10817 static void
10818ex_loadview(eap)
10819 exarg_T *eap;
10820{
10821 char_u *fname;
10822
10823 fname = get_view_file(*eap->arg);
10824 if (fname != NULL)
10825 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010826 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 vim_free(fname);
10828 }
10829}
10830
10831/*
10832 * Get the name of the view file for the current buffer.
10833 */
10834 static char_u *
10835get_view_file(c)
10836 int c;
10837{
10838 int len = 0;
10839 char_u *p, *s;
10840 char_u *retval;
10841 char_u *sname;
10842
10843 if (curbuf->b_ffname == NULL)
10844 {
10845 EMSG(_(e_noname));
10846 return NULL;
10847 }
10848 sname = home_replace_save(NULL, curbuf->b_ffname);
10849 if (sname == NULL)
10850 return NULL;
10851
10852 /*
10853 * We want a file name without separators, because we're not going to make
10854 * a directory.
10855 * "normal" path separator -> "=+"
10856 * "=" -> "=="
10857 * ":" path separator -> "=-"
10858 */
10859 for (p = sname; *p; ++p)
10860 if (*p == '=' || vim_ispathsep(*p))
10861 ++len;
10862 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10863 if (retval != NULL)
10864 {
10865 STRCPY(retval, p_vdir);
10866 add_pathsep(retval);
10867 s = retval + STRLEN(retval);
10868 for (p = sname; *p; ++p)
10869 {
10870 if (*p == '=')
10871 {
10872 *s++ = '=';
10873 *s++ = '=';
10874 }
10875 else if (vim_ispathsep(*p))
10876 {
10877 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010878#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (*p == ':')
10880 *s++ = '-';
10881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010883 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
10885 else
10886 *s++ = *p;
10887 }
10888 *s++ = '=';
10889 *s++ = c;
10890 STRCPY(s, ".vim");
10891 }
10892
10893 vim_free(sname);
10894 return retval;
10895}
10896
10897#endif /* FEAT_SESSION */
10898
10899/*
10900 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10901 * Return FAIL for a write error.
10902 */
10903 int
10904put_eol(fd)
10905 FILE *fd;
10906{
10907 if (
10908#ifdef USE_CRNL
10909 (
10910# ifdef MKSESSION_NL
10911 !mksession_nl &&
10912# endif
10913 (putc('\r', fd) < 0)) ||
10914#endif
10915 (putc('\n', fd) < 0))
10916 return FAIL;
10917 return OK;
10918}
10919
10920/*
10921 * Write a line to "fd".
10922 * Return FAIL for a write error.
10923 */
10924 int
10925put_line(fd, s)
10926 FILE *fd;
10927 char *s;
10928{
10929 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10930 return FAIL;
10931 return OK;
10932}
10933
10934#ifdef FEAT_VIMINFO
10935/*
10936 * ":rviminfo" and ":wviminfo".
10937 */
10938 static void
10939ex_viminfo(eap)
10940 exarg_T *eap;
10941{
10942 char_u *save_viminfo;
10943
10944 save_viminfo = p_viminfo;
10945 if (*p_viminfo == NUL)
10946 p_viminfo = (char_u *)"'100";
10947 if (eap->cmdidx == CMD_rviminfo)
10948 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010949 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10950 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 EMSG(_("E195: Cannot open viminfo file for reading"));
10952 }
10953 else
10954 write_viminfo(eap->arg, eap->forceit);
10955 p_viminfo = save_viminfo;
10956}
10957#endif
10958
10959#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010960/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010961 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010962 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 void
10965dialog_msg(buff, format, fname)
10966 char_u *buff;
10967 char *format;
10968 char_u *fname;
10969{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 if (fname == NULL)
10971 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010972 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973}
10974#endif
10975
10976/*
10977 * ":behave {mswin,xterm}"
10978 */
10979 static void
10980ex_behave(eap)
10981 exarg_T *eap;
10982{
10983 if (STRCMP(eap->arg, "mswin") == 0)
10984 {
10985 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10986 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10987 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10988 set_option_value((char_u *)"keymodel", 0L,
10989 (char_u *)"startsel,stopsel", 0);
10990 }
10991 else if (STRCMP(eap->arg, "xterm") == 0)
10992 {
10993 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10994 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10995 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10996 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10997 }
10998 else
10999 EMSG2(_(e_invarg2), eap->arg);
11000}
11001
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011002#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11003/*
11004 * Function given to ExpandGeneric() to obtain the possible arguments of the
11005 * ":behave {mswin,xterm}" command.
11006 */
11007 char_u *
11008get_behave_arg(xp, idx)
11009 expand_T *xp UNUSED;
11010 int idx;
11011{
11012 if (idx == 0)
11013 return (char_u *)"mswin";
11014 if (idx == 1)
11015 return (char_u *)"xterm";
11016 return NULL;
11017}
11018#endif
11019
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020#ifdef FEAT_AUTOCMD
11021static int filetype_detect = FALSE;
11022static int filetype_plugin = FALSE;
11023static int filetype_indent = FALSE;
11024
11025/*
11026 * ":filetype [plugin] [indent] {on,off,detect}"
11027 * on: Load the filetype.vim file to install autocommands for file types.
11028 * off: Load the ftoff.vim file to remove all autocommands for file types.
11029 * plugin on: load filetype.vim and ftplugin.vim
11030 * plugin off: load ftplugof.vim
11031 * indent on: load filetype.vim and indent.vim
11032 * indent off: load indoff.vim
11033 */
11034 static void
11035ex_filetype(eap)
11036 exarg_T *eap;
11037{
11038 char_u *arg = eap->arg;
11039 int plugin = FALSE;
11040 int indent = FALSE;
11041
11042 if (*eap->arg == NUL)
11043 {
11044 /* Print current status. */
11045 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11046 filetype_detect ? "ON" : "OFF",
11047 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11048 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11049 return;
11050 }
11051
11052 /* Accept "plugin" and "indent" in any order. */
11053 for (;;)
11054 {
11055 if (STRNCMP(arg, "plugin", 6) == 0)
11056 {
11057 plugin = TRUE;
11058 arg = skipwhite(arg + 6);
11059 continue;
11060 }
11061 if (STRNCMP(arg, "indent", 6) == 0)
11062 {
11063 indent = TRUE;
11064 arg = skipwhite(arg + 6);
11065 continue;
11066 }
11067 break;
11068 }
11069 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11070 {
11071 if (*arg == 'o' || !filetype_detect)
11072 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011073 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 filetype_detect = TRUE;
11075 if (plugin)
11076 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011077 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 filetype_plugin = TRUE;
11079 }
11080 if (indent)
11081 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011082 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 filetype_indent = TRUE;
11084 }
11085 }
11086 if (*arg == 'd')
11087 {
11088 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011089 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 }
11091 }
11092 else if (STRCMP(arg, "off") == 0)
11093 {
11094 if (plugin || indent)
11095 {
11096 if (plugin)
11097 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011098 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 filetype_plugin = FALSE;
11100 }
11101 if (indent)
11102 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011103 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 filetype_indent = FALSE;
11105 }
11106 }
11107 else
11108 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011109 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 filetype_detect = FALSE;
11111 }
11112 }
11113 else
11114 EMSG2(_(e_invarg2), arg);
11115}
11116
11117/*
11118 * ":setfiletype {name}"
11119 */
11120 static void
11121ex_setfiletype(eap)
11122 exarg_T *eap;
11123{
11124 if (!did_filetype)
11125 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11126}
11127#endif
11128
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 static void
11130ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011131 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132{
11133#ifdef FEAT_DIGRAPHS
11134 if (*eap->arg != NUL)
11135 putdigraph(eap->arg);
11136 else
11137 listdigraphs();
11138#else
11139 EMSG(_("E196: No digraphs in this version"));
11140#endif
11141}
11142
11143 static void
11144ex_set(eap)
11145 exarg_T *eap;
11146{
11147 int flags = 0;
11148
11149 if (eap->cmdidx == CMD_setlocal)
11150 flags = OPT_LOCAL;
11151 else if (eap->cmdidx == CMD_setglobal)
11152 flags = OPT_GLOBAL;
11153#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11154 if (cmdmod.browse && flags == 0)
11155 ex_options(eap);
11156 else
11157#endif
11158 (void)do_set(eap->arg, flags);
11159}
11160
11161#ifdef FEAT_SEARCH_EXTRA
11162/*
11163 * ":nohlsearch"
11164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 static void
11166ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011167 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
11169 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011170 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171}
11172
11173/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011174 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 * Sets nextcmd to the start of the next command, if any. Also called when
11176 * skipping commands to find the next command.
11177 */
11178 static void
11179ex_match(eap)
11180 exarg_T *eap;
11181{
11182 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011183 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 char_u *end;
11185 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011186 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011187
11188 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011189 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011190 else
11191 {
11192 EMSG(e_invcmd);
11193 return;
11194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
11196 /* First clear any old pattern. */
11197 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011198 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
11200 if (ends_excmd(*eap->arg))
11201 end = eap->arg;
11202 else if ((STRNICMP(eap->arg, "none", 4) == 0
11203 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11204 end = eap->arg + 4;
11205 else
11206 {
11207 p = skiptowhite(eap->arg);
11208 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011209 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 p = skipwhite(p);
11211 if (*p == NUL)
11212 {
11213 /* There must be two arguments. */
11214 EMSG2(_(e_invarg2), eap->arg);
11215 return;
11216 }
11217 end = skip_regexp(p + 1, *p, TRUE, NULL);
11218 if (!eap->skip)
11219 {
11220 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11221 {
11222 eap->errmsg = e_trailing;
11223 return;
11224 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011225 if (*end != *p)
11226 {
11227 EMSG2(_(e_invarg2), p);
11228 return;
11229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
11231 c = *end;
11232 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011233 match_add(curwin, g, p + 1, 10, id);
11234 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011235 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 }
11237 }
11238 eap->nextcmd = find_nextcmd(end);
11239}
11240#endif
11241
11242#ifdef FEAT_CRYPT
11243/*
11244 * ":X": Get crypt key
11245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 static void
11247ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011248 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011250 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011251 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253#endif
11254
11255#ifdef FEAT_FOLDING
11256 static void
11257ex_fold(eap)
11258 exarg_T *eap;
11259{
11260 if (foldManualAllowed(TRUE))
11261 foldCreate(eap->line1, eap->line2);
11262}
11263
11264 static void
11265ex_foldopen(eap)
11266 exarg_T *eap;
11267{
11268 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11269 eap->forceit, FALSE);
11270}
11271
11272 static void
11273ex_folddo(eap)
11274 exarg_T *eap;
11275{
11276 linenr_T lnum;
11277
11278 /* First set the marks for all lines closed/open. */
11279 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11280 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11281 ml_setmarked(lnum);
11282
11283 /* Execute the command on the marked lines. */
11284 global_exe(eap->arg);
11285 ml_clearmarked(); /* clear rest of the marks */
11286}
11287#endif