blob: 602bbfab457bf04d4ba09fe8735a351ebe5f670c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200132#if !defined(FEAT_PERL) \
133 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
134 || !defined(FEAT_TCL) \
135 || !defined(FEAT_RUBY) \
136 || !defined(FEAT_LUA) \
137 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000138# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void ex_script_ni __ARGS((exarg_T *eap));
140#endif
141static char_u *invalid_range __ARGS((exarg_T *eap));
142static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000143#ifdef FEAT_QUICKFIX
144static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
147static void ex_highlight __ARGS((exarg_T *eap));
148static void ex_colorscheme __ARGS((exarg_T *eap));
149static void ex_quit __ARGS((exarg_T *eap));
150static void ex_cquit __ARGS((exarg_T *eap));
151static void ex_quit_all __ARGS((exarg_T *eap));
152#ifdef FEAT_WINDOWS
153static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000154static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_resize __ARGS((exarg_T *eap));
157static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000159static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000160static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#else
164# define ex_close ex_ni
165# define ex_only ex_ni
166# define ex_all ex_ni
167# define ex_resize ex_ni
168# define ex_splitview ex_ni
169# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000170# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000172# define ex_tabs ex_ni
173# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000174# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
177static void ex_pclose __ARGS((exarg_T *eap));
178static void ex_ptag __ARGS((exarg_T *eap));
179static void ex_pedit __ARGS((exarg_T *eap));
180#else
181# define ex_pclose ex_ni
182# define ex_ptag ex_ni
183# define ex_pedit ex_ni
184#endif
185static void ex_hide __ARGS((exarg_T *eap));
186static void ex_stop __ARGS((exarg_T *eap));
187static void ex_exit __ARGS((exarg_T *eap));
188static void ex_print __ARGS((exarg_T *eap));
189#ifdef FEAT_BYTEOFF
190static void ex_goto __ARGS((exarg_T *eap));
191#else
192# define ex_goto ex_ni
193#endif
194static void ex_shell __ARGS((exarg_T *eap));
195static void ex_preserve __ARGS((exarg_T *eap));
196static void ex_recover __ARGS((exarg_T *eap));
197#ifndef FEAT_LISTCMDS
198# define ex_argedit ex_ni
199# define ex_argadd ex_ni
200# define ex_argdelete ex_ni
201# define ex_listdo ex_ni
202#endif
203static void ex_mode __ARGS((exarg_T *eap));
204static void ex_wrongmodifier __ARGS((exarg_T *eap));
205static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000206static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static void ex_edit __ARGS((exarg_T *eap));
208#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
209# define ex_drop ex_ni
210#endif
211#ifndef FEAT_GUI
212# define ex_gui ex_nogui
213static void ex_nogui __ARGS((exarg_T *eap));
214#endif
215#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
216static void ex_tearoff __ARGS((exarg_T *eap));
217#else
218# define ex_tearoff ex_ni
219#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000220#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221static void ex_popup __ARGS((exarg_T *eap));
222#else
223# define ex_popup ex_ni
224#endif
225#ifndef FEAT_GUI_MSWIN
226# define ex_simalt ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define gui_mch_find_dialog ex_ni
230# define gui_mch_replace_dialog ex_ni
231#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000232#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define ex_helpfind ex_ni
234#endif
235#ifndef FEAT_CSCOPE
236# define do_cscope ex_ni
237# define do_scscope ex_ni
238# define do_cstag ex_ni
239#endif
240#ifndef FEAT_SYN_HL
241# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200242# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#endif
244#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000245# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000247# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000248# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000249# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200251#ifndef FEAT_PERSISTENT_UNDO
252# define ex_rundo ex_ni
253# define ex_wundo ex_ni
254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200255#ifndef FEAT_LUA
256# define ex_lua ex_script_ni
257# define ex_luado ex_ni
258# define ex_luafile ex_ni
259#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000260#ifndef FEAT_MZSCHEME
261# define ex_mzscheme ex_script_ni
262# define ex_mzfile ex_ni
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_PERL
265# define ex_perl ex_script_ni
266# define ex_perldo ex_ni
267#endif
268#ifndef FEAT_PYTHON
269# define ex_python ex_script_ni
270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274# define ex_py3file ex_ni
275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#ifndef FEAT_TCL
277# define ex_tcl ex_script_ni
278# define ex_tcldo ex_ni
279# define ex_tclfile ex_ni
280#endif
281#ifndef FEAT_RUBY
282# define ex_ruby ex_script_ni
283# define ex_rubydo ex_ni
284# define ex_rubyfile ex_ni
285#endif
286#ifndef FEAT_SNIFF
287# define ex_sniff ex_ni
288#endif
289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
292static void ex_swapname __ARGS((exarg_T *eap));
293static void ex_syncbind __ARGS((exarg_T *eap));
294static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static void ex_pwd __ARGS((exarg_T *eap));
296static void ex_equal __ARGS((exarg_T *eap));
297static void ex_sleep __ARGS((exarg_T *eap));
298static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
299static void ex_winsize __ARGS((exarg_T *eap));
300#ifdef FEAT_WINDOWS
301static void ex_wincmd __ARGS((exarg_T *eap));
302#else
303# define ex_wincmd ex_ni
304#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000305#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_winpos __ARGS((exarg_T *eap));
307#else
308# define ex_winpos ex_ni
309#endif
310static void ex_operators __ARGS((exarg_T *eap));
311static void ex_put __ARGS((exarg_T *eap));
312static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000313static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static void ex_submagic __ARGS((exarg_T *eap));
315static void ex_join __ARGS((exarg_T *eap));
316static void ex_at __ARGS((exarg_T *eap));
317static void ex_bang __ARGS((exarg_T *eap));
318static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200319#ifdef FEAT_PERSISTENT_UNDO
320static void ex_wundo __ARGS((exarg_T *eap));
321static void ex_rundo __ARGS((exarg_T *eap));
322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000324static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static void ex_redir __ARGS((exarg_T *eap));
326static void ex_redraw __ARGS((exarg_T *eap));
327static void ex_redrawstatus __ARGS((exarg_T *eap));
328static void close_redir __ARGS((void));
329static void ex_mkrc __ARGS((exarg_T *eap));
330static void ex_mark __ARGS((exarg_T *eap));
331#ifdef FEAT_USR_CMDS
332static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000333static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#endif
335#ifdef FEAT_EX_EXTRA
336static void ex_normal __ARGS((exarg_T *eap));
337static void ex_startinsert __ARGS((exarg_T *eap));
338static void ex_stopinsert __ARGS((exarg_T *eap));
339#else
340# define ex_normal ex_ni
341# define ex_align ex_ni
342# define ex_retab ex_ni
343# define ex_startinsert ex_ni
344# define ex_stopinsert ex_ni
345# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000346# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
348#ifdef FEAT_FIND_ID
349static void ex_checkpath __ARGS((exarg_T *eap));
350static void ex_findpat __ARGS((exarg_T *eap));
351#else
352# define ex_findpat ex_ni
353# define ex_checkpath ex_ni
354#endif
355#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
356static void ex_psearch __ARGS((exarg_T *eap));
357#else
358# define ex_psearch ex_ni
359#endif
360static void ex_tag __ARGS((exarg_T *eap));
361static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
362#ifndef FEAT_EVAL
363# define ex_scriptnames ex_ni
364# define ex_finish ex_ni
365# define ex_echo ex_ni
366# define ex_echohl ex_ni
367# define ex_execute ex_ni
368# define ex_call ex_ni
369# define ex_if ex_ni
370# define ex_endif ex_ni
371# define ex_else ex_ni
372# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000373# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000377# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378# define ex_throw ex_ni
379# define ex_try ex_ni
380# define ex_catch ex_ni
381# define ex_finally ex_ni
382# define ex_endtry ex_ni
383# define ex_endfunction ex_ni
384# define ex_let ex_ni
385# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000386# define ex_lockvar ex_ni
387# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_function ex_ni
389# define ex_delfunction ex_ni
390# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000391# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393static char_u *arg_all __ARGS((void));
394#ifdef FEAT_SESSION
395static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000396static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void ex_loadview __ARGS((exarg_T *eap));
398static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000399static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#else
401# define ex_loadview ex_ni
402#endif
403#ifndef FEAT_EVAL
404# define ex_compiler ex_ni
405#endif
406#ifdef FEAT_VIMINFO
407static void ex_viminfo __ARGS((exarg_T *eap));
408#else
409# define ex_viminfo ex_ni
410#endif
411static void ex_behave __ARGS((exarg_T *eap));
412#ifdef FEAT_AUTOCMD
413static void ex_filetype __ARGS((exarg_T *eap));
414static void ex_setfiletype __ARGS((exarg_T *eap));
415#else
416# define ex_filetype ex_ni
417# define ex_setfiletype ex_ni
418#endif
419#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000420# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421# define ex_diffpatch ex_ni
422# define ex_diffgetput ex_ni
423# define ex_diffsplit ex_ni
424# define ex_diffthis ex_ni
425# define ex_diffupdate ex_ni
426#endif
427static void ex_digraphs __ARGS((exarg_T *eap));
428static void ex_set __ARGS((exarg_T *eap));
429#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
430# define ex_options ex_ni
431#endif
432#ifdef FEAT_SEARCH_EXTRA
433static void ex_nohlsearch __ARGS((exarg_T *eap));
434static void ex_match __ARGS((exarg_T *eap));
435#else
436# define ex_nohlsearch ex_ni
437# define ex_match ex_ni
438#endif
439#ifdef FEAT_CRYPT
440static void ex_X __ARGS((exarg_T *eap));
441#else
442# define ex_X ex_ni
443#endif
444#ifdef FEAT_FOLDING
445static void ex_fold __ARGS((exarg_T *eap));
446static void ex_foldopen __ARGS((exarg_T *eap));
447static void ex_folddo __ARGS((exarg_T *eap));
448#else
449# define ex_fold ex_ni
450# define ex_foldopen ex_ni
451# define ex_folddo ex_ni
452#endif
453#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
454 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
455# define ex_language ex_ni
456#endif
457#ifndef FEAT_SIGNS
458# define ex_sign ex_ni
459#endif
460#ifndef FEAT_SUN_WORKSHOP
461# define ex_wsverb ex_ni
462#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
469#ifndef FEAT_EVAL
470# define ex_debug ex_ni
471# define ex_breakadd ex_ni
472# define ex_debuggreedy ex_ni
473# define ex_breakdel ex_ni
474# define ex_breaklist ex_ni
475#endif
476
477#ifndef FEAT_CMDHIST
478# define ex_history ex_ni
479#endif
480#ifndef FEAT_JUMPLIST
481# define ex_jumps ex_ni
482# define ex_changes ex_ni
483#endif
484
Bram Moolenaar05159a02005-02-26 23:04:13 +0000485#ifndef FEAT_PROFILE
486# define ex_profile ex_ni
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Declare cmdnames[].
491 */
492#define DO_DECLARE_EXCMD
493#include "ex_cmds.h"
494
495/*
496 * Table used to quickly search for a command, based on its first character.
497 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000498static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 CMD_append,
501 CMD_buffer,
502 CMD_change,
503 CMD_delete,
504 CMD_edit,
505 CMD_file,
506 CMD_global,
507 CMD_help,
508 CMD_insert,
509 CMD_join,
510 CMD_k,
511 CMD_list,
512 CMD_move,
513 CMD_next,
514 CMD_open,
515 CMD_print,
516 CMD_quit,
517 CMD_read,
518 CMD_substitute,
519 CMD_t,
520 CMD_undo,
521 CMD_vglobal,
522 CMD_write,
523 CMD_xit,
524 CMD_yank,
525 CMD_z,
526 CMD_bang
527};
528
529static char_u dollar_command[2] = {'$', 0};
530
531
532#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534typedef struct
535{
536 char_u *line; /* command line */
537 linenr_T lnum; /* sourcing_lnum of the line */
538} wcmd_T;
539
540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 garray_T *lines_gap; /* growarray with line info */
548 int current_line; /* last read line from growarray */
549 int repeating; /* TRUE when looping a second time */
550 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
551 char_u *(*getline) __ARGS((int, void *, int));
552 void *cookie;
553};
554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
556static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000558
559/* Struct to save a few things while debugging. Used in do_cmdline() only. */
560struct dbg_stuff
561{
562 int trylevel;
563 int force_abort;
564 except_T *caught_stack;
565 char_u *vv_exception;
566 char_u *vv_throwpoint;
567 int did_emsg;
568 int got_int;
569 int did_throw;
570 int need_rethrow;
571 int check_cstack;
572 except_T *current_exception;
573};
574
575static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577
578 static void
579save_dbg_stuff(dsp)
580 struct dbg_stuff *dsp;
581{
582 dsp->trylevel = trylevel; trylevel = 0;
583 dsp->force_abort = force_abort; force_abort = FALSE;
584 dsp->caught_stack = caught_stack; caught_stack = NULL;
585 dsp->vv_exception = v_exception(NULL);
586 dsp->vv_throwpoint = v_throwpoint(NULL);
587
588 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
589 dsp->did_emsg = did_emsg; did_emsg = FALSE;
590 dsp->got_int = got_int; got_int = FALSE;
591 dsp->did_throw = did_throw; did_throw = FALSE;
592 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
593 dsp->check_cstack = check_cstack; check_cstack = FALSE;
594 dsp->current_exception = current_exception; current_exception = NULL;
595}
596
597 static void
598restore_dbg_stuff(dsp)
599 struct dbg_stuff *dsp;
600{
601 suppress_errthrow = FALSE;
602 trylevel = dsp->trylevel;
603 force_abort = dsp->force_abort;
604 caught_stack = dsp->caught_stack;
605 (void)v_exception(dsp->vv_exception);
606 (void)v_throwpoint(dsp->vv_throwpoint);
607 did_emsg = dsp->did_emsg;
608 got_int = dsp->got_int;
609 did_throw = dsp->did_throw;
610 need_rethrow = dsp->need_rethrow;
611 check_cstack = dsp->check_cstack;
612 current_exception = dsp->current_exception;
613}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615
616
617/*
618 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
619 * command is given.
620 */
621 void
622do_exmode(improved)
623 int improved; /* TRUE for "improved Ex" mode */
624{
625 int save_msg_scroll;
626 int prev_msg_row;
627 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 int changedtick;
629
630 if (improved)
631 exmode_active = EXMODE_VIM;
632 else
633 exmode_active = EXMODE_NORMAL;
634 State = NORMAL;
635
636 /* When using ":global /pat/ visual" and then "Q" we return to continue
637 * the :global command. */
638 if (global_busy)
639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 save_msg_scroll = msg_scroll;
642 ++RedrawingDisabled; /* don't redisplay the window */
643 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_GUI
645 /* Ignore scrollbar and mouse events in Ex mode */
646 ++hold_gui_events;
647#endif
648#ifdef FEAT_SNIFF
649 want_sniff_request = 0; /* No K_SNIFF wanted */
650#endif
651
652 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
653 while (exmode_active)
654 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000655#ifdef FEAT_EX_EXTRA
656 /* Check for a ":normal" command and no more characters left. */
657 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
658 {
659 exmode_active = FALSE;
660 break;
661 }
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 msg_scroll = TRUE;
664 need_wait_return = FALSE;
665 ex_pressedreturn = FALSE;
666 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 prev_msg_row = msg_row;
669 prev_line = curwin->w_cursor.lnum;
670#ifdef FEAT_SNIFF
671 ProcessSniffRequests();
672#endif
673 if (improved)
674 {
675 cmdline_row = msg_row;
676 do_cmdline(NULL, getexline, NULL, 0);
677 }
678 else
679 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
680 lines_left = Rows - 1;
681
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if ((prev_line != curwin->w_cursor.lnum
683 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (curbuf->b_ml.ml_flags & ML_EMPTY)
686 EMSG(_(e_emptybuf));
687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (ex_pressedreturn)
690 {
691 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000692 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 msg_row = prev_msg_row;
694 if (prev_msg_row == Rows - 1)
695 msg_row--;
696 }
697 msg_col = 0;
698 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
699 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
703 {
704 if (curbuf->b_ml.ml_flags & ML_EMPTY)
705 EMSG(_(e_emptybuf));
706 else
707 EMSG(_("E501: At end-of-file"));
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
711#ifdef FEAT_GUI
712 --hold_gui_events;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --RedrawingDisabled;
715 --no_wait_return;
716 update_screen(CLEAR);
717 need_wait_return = FALSE;
718 msg_scroll = save_msg_scroll;
719}
720
721/*
722 * Execute a simple command line. Used for translated commands like "*".
723 */
724 int
725do_cmdline_cmd(cmd)
726 char_u *cmd;
727{
728 return do_cmdline(cmd, NULL, NULL,
729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
730}
731
732/*
733 * do_cmdline(): execute one Ex command line
734 *
735 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * 2. Split up in parts separated with '|'.
738 *
739 * This function can be called recursively!
740 *
741 * flags:
742 * DOCMD_VERBOSE - The command will be included in the error message.
743 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * DOCMD_KEYTYPED - Don't reset KeyTyped.
746 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
747 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
748 *
749 * return FAIL if cmdline could not be executed, OK otherwise
750 */
751 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 char_u *(*fgetline) __ARGS((int, void *, int));
755 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 static int recursive = 0; /* recursive depth */
762 int msg_didout_before_start = 0;
763 int count = 0; /* line number count */
764 int did_inc = FALSE; /* incremented RedrawingDisabled */
765 int retval = OK;
766#ifdef FEAT_EVAL
767 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 int current_line = 0; /* active line in lines_ga */
770 char_u *fname = NULL; /* function or script name */
771 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
772 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000773 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int initial_trylevel;
775 struct msglist **saved_msg_list = NULL;
776 struct msglist *private_msg_list;
777
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 char_u *(*cmd_getline) __ARGS((int, void *, int));
780 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# define cmd_cookie cookie
787#endif
788 static int call_depth = 0; /* recursiveness */
789
790#ifdef FEAT_EVAL
791 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
792 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000793 * This ensures that the do_errthrow() call in do_one_cmd() does not
794 * combine the messages stored by an earlier invocation of do_one_cmd()
795 * with the command name of the later one. This would happen when
796 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 saved_msg_list = msg_list;
798 msg_list = &private_msg_list;
799 private_msg_list = NULL;
800#endif
801
802 /* It's possible to create an endless loop with ":execute", catch that
803 * here. The value of 200 allows nested function calls, ":source", etc. */
804 if (call_depth == 200)
805 {
806 EMSG(_("E169: Command too recursive"));
807#ifdef FEAT_EVAL
808 /* When converting to an exception, we do not include the command name
809 * since this is not an error of the specific command. */
810 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
811 msg_list = saved_msg_list;
812#endif
813 return FAIL;
814 }
815 ++call_depth;
816
817#ifdef FEAT_EVAL
818 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 cstack.cs_trylevel = 0;
821 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
824
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++ex_nesting_level;
831
832 /* Get the function or script name and the address where the next breakpoint
833 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = func_name(real_cookie);
837 breakpoint = func_breakpoint(real_cookie);
838 dbg_tick = func_dbg_tick(real_cookie);
839 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
842 fname = sourcing_name;
843 breakpoint = source_breakpoint(real_cookie);
844 dbg_tick = source_dbg_tick(real_cookie);
845 }
846
847 /*
848 * Initialize "force_abort" and "suppress_errthrow" at the top level.
849 */
850 if (!recursive)
851 {
852 force_abort = FALSE;
853 suppress_errthrow = FALSE;
854 }
855
856 /*
857 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000858 * exception handling (used when debugging). Otherwise clear it to avoid
859 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000861 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000862 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000863 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200864 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 initial_trylevel = trylevel;
867
868 /*
869 * "did_throw" will be set to TRUE when an exception is being thrown.
870 */
871 did_throw = FALSE;
872#endif
873 /*
874 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * If force_abort is set, we cancel everything.
877 */
878 did_emsg = FALSE;
879
880 /*
881 * KeyTyped is only set when calling vgetc(). Reset it here when not
882 * calling vgetc() (sourced command lines).
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (!(flags & DOCMD_KEYTYPED)
885 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 KeyTyped = FALSE;
887
888 /*
889 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000890 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * - for multiple commands on one line, separated with '|'
892 * - when repeating until there are no more lines (for ":source")
893 */
894 next_cmdline = cmdline;
895 do
896 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100898 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899#endif
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (next_cmdline == NULL
903#ifdef FEAT_EVAL
904 && !force_abort
905 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 )
909 did_emsg = FALSE;
910
911 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * 3. If a line is given: Make a copy, so we can mess with it.
915 */
916
917#ifdef FEAT_EVAL
918 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 /* Each '|' separated command is stored separately in lines_ga, to
922 * be able to jump to it. Don't use next_cmdline now. */
923 vim_free(cmdline_copy);
924 cmdline_copy = NULL;
925
926 /* Check if a function has returned or, unless it has an unclosed
927 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000931 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 func_line_end(real_cookie);
933# endif
934 if (func_has_ended(real_cookie))
935 {
936 retval = FAIL;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 script_line_end();
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
952
953 /* If breakpoints have been added/deleted need to check for it. */
954 if (breakpoint != NULL && dbg_tick != NULL
955 && *dbg_tick != debug_tick)
956 {
957 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
962
963 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
964 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
965
966 /* Did we encounter a breakpoint? */
967 if (breakpoint != NULL && *breakpoint != 0
968 && *breakpoint <= sourcing_lnum)
969 {
970 dbg_breakpoint(fname, sourcing_lnum);
971 /* Find next breakpoint. */
972 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 fname, sourcing_lnum);
975 *dbg_tick = debug_tick;
976 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000978 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000979 {
980 if (getline_is_func)
981 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 script_line_start();
984 }
985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * below, so that it stores lines in or reads them from
993 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 * while/for loop. */
995 cmd_getline = get_loop_line;
996 cmd_cookie = (void *)&cmd_loop_cookie;
997 cmd_loop_cookie.lines_gap = &lines_ga;
998 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 cmd_loop_cookie.cookie = cookie;
1001 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 else
1004 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 cmd_cookie = cookie;
1007 }
1008#endif
1009
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (next_cmdline == NULL)
1012 {
1013 /*
1014 * Need to set msg_didout for the first line after an ":if",
1015 * otherwise the ":if" will be overwritten.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_EVAL
1021 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1022#else
1023 0
1024#endif
1025 )) == NULL)
1026 {
1027 /* Don't call wait_return for aborted command line. The NULL
1028 * returned for the end of a sourced file or executed function
1029 * doesn't do this. */
1030 if (KeyTyped && !(flags & DOCMD_REPEAT))
1031 need_wait_return = FALSE;
1032 retval = FAIL;
1033 break;
1034 }
1035 used_getline = TRUE;
1036
1037 /*
1038 * Keep the first typed line. Clear it when more lines are typed.
1039 */
1040 if (flags & DOCMD_KEEPLINE)
1041 {
1042 vim_free(repeat_cmdline);
1043 if (count == 0)
1044 repeat_cmdline = vim_strsave(next_cmdline);
1045 else
1046 repeat_cmdline = NULL;
1047 }
1048 }
1049
1050 /* 3. Make a copy of the command so we can mess with it. */
1051 else if (cmdline_copy == NULL)
1052 {
1053 next_cmdline = vim_strsave(next_cmdline);
1054 if (next_cmdline == NULL)
1055 {
1056 EMSG(_(e_outofmem));
1057 retval = FAIL;
1058 break;
1059 }
1060 }
1061 cmdline_copy = next_cmdline;
1062
1063#ifdef FEAT_EVAL
1064 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 * Save the current line when inside a ":while" or ":for", and when
1066 * the command looks like a ":while" or ":for", because we may need it
1067 * later. When there is a '|' and another command, it is stored
1068 * separately, because we need to be able to jump back to it from an
1069 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (current_line == lines_ga.ga_len
1072 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 retval = FAIL;
1077 break;
1078 }
1079 }
1080 did_endif = FALSE;
1081#endif
1082
1083 if (count++ == 0)
1084 {
1085 /*
1086 * All output from the commands is put below each other, without
1087 * waiting for a return. Don't do this when executing commands
1088 * from a script or when being called recursive (e.g. for ":e
1089 * +command file").
1090 */
1091 if (!(flags & DOCMD_NOWAIT) && !recursive)
1092 {
1093 msg_didout_before_start = msg_didout;
1094 msg_didany = FALSE; /* no output yet */
1095 msg_start();
1096 msg_scroll = TRUE; /* put messages below each other */
1097 ++no_wait_return; /* dont wait for return until finished */
1098 ++RedrawingDisabled;
1099 did_inc = TRUE;
1100 }
1101 }
1102
1103 if (p_verbose >= 15 && sourcing_name != NULL)
1104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001106 verbose_enter_scroll();
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 smsg((char_u *)_("line %ld: %s"),
1109 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 if (msg_silent == 0)
1111 msg_puts((char_u *)"\n"); /* don't overwrite this */
1112
1113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 --no_wait_return;
1115 }
1116
1117 /*
1118 * 2. Execute one '|' separated command.
1119 * do_one_cmd() will return NULL if there is no trailing '|'.
1120 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1121 */
1122 ++recursive;
1123 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1124#ifdef FEAT_EVAL
1125 &cstack,
1126#endif
1127 cmd_getline, cmd_cookie);
1128 --recursive;
1129
1130#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cmd_cookie == (void *)&cmd_loop_cookie)
1132 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 if (next_cmdline == NULL)
1138 {
1139 vim_free(cmdline_copy);
1140 cmdline_copy = NULL;
1141#ifdef FEAT_CMDHIST
1142 /*
1143 * If the command was typed, remember it for the ':' register.
1144 * Do this AFTER executing the command to make :@: work.
1145 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001146 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 && new_last_cmdline != NULL)
1148 {
1149 vim_free(last_cmdline);
1150 last_cmdline = new_last_cmdline;
1151 new_last_cmdline = NULL;
1152 }
1153#endif
1154 }
1155 else
1156 {
1157 /* need to copy the command after the '|' to cmdline_copy, for the
1158 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001159 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 next_cmdline = cmdline_copy;
1161 }
1162
1163
1164#ifdef FEAT_EVAL
1165 /* reset did_emsg for a function that is not aborted by an error */
1166 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 && !func_has_abort(real_cookie))
1169 did_emsg = FALSE;
1170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 ++current_line;
1174
1175 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 * An ":endwhile", ":endfor" and ":continue" is handled here.
1177 * If we were executing commands, jump back to the ":while" or
1178 * ":for".
1179 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* Jump back to the matching ":while" or ":for". Be careful
1186 * not to use a cs_line[] from an entry that isn't a ":while"
1187 * or ":for": It would make "current_line" invalid and can
1188 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (!did_emsg && !got_int && !did_throw
1190 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 && (cstack.cs_flags[cstack.cs_idx]
1192 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && cstack.cs_line[cstack.cs_idx] >= 0
1194 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1195 {
1196 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 /* remember we jumped there */
1198 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 line_breakcheck(); /* check if CTRL-C typed */
1200
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* Check for the next breakpoint at or after the ":while"
1202 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (breakpoint != NULL)
1204 {
1205 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 fname,
1208 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1209 *dbg_tick = debug_tick;
1210 }
1211 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1217 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220
1221 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1228 }
1229 }
1230
1231 /*
1232 * When not inside any ":while" loop, clear remembered lines.
1233 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (lines_ga.ga_len > 0)
1237 {
1238 sourcing_lnum =
1239 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1240 free_cmdlines(&lines_ga);
1241 }
1242 current_line = 0;
1243 }
1244
1245 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1247 * being restored at the ":endtry". Reset them here and set the
1248 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1249 * This includes the case where a missing ":endif", ":endwhile" or
1250 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 cstack.cs_lflags &= ~CSL_HAD_FINA;
1255 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1256 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 did_throw ? (void *)current_exception : NULL);
1258 did_emsg = got_int = did_throw = FALSE;
1259 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1260 }
1261
1262 /* Update global "trylevel" for recursive calls to do_cmdline() from
1263 * within this loop. */
1264 trylevel = initial_trylevel + cstack.cs_trylevel;
1265
1266 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001267 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * files) is aborted because of an error, an interrupt, or an uncaught
1269 * exception, cancel everything. If it is left normally, reset
1270 * force_abort to get the non-EH compatible abortion behavior for
1271 * the rest of the script.
1272 */
1273 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1274 force_abort = FALSE;
1275
1276 /* Convert an interrupt to an exception if appropriate. */
1277 (void)do_intthrow(&cstack);
1278#endif /* FEAT_EVAL */
1279
1280 }
1281 /*
1282 * Continue executing command lines when:
1283 * - no CTRL-C typed, no aborting error, no exception thrown or try
1284 * conditionals need to be checked for executing finally clauses or
1285 * catching an interrupt exception
1286 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * looping for ":source" command or function call.
1289 */
1290 while (!((got_int
1291#ifdef FEAT_EVAL
1292 || (did_emsg && force_abort) || did_throw
1293#endif
1294 )
1295#ifdef FEAT_EVAL
1296 && cstack.cs_trylevel == 0
1297#endif
1298 )
1299 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001300 && (getline_equal(fgetline, cookie, getexmodeline)
1301 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (next_cmdline != NULL
1303#ifdef FEAT_EVAL
1304 || cstack.cs_idx >= 0
1305#endif
1306 || (flags & DOCMD_REPEAT)));
1307
1308 vim_free(cmdline_copy);
1309#ifdef FEAT_EVAL
1310 free_cmdlines(&lines_ga);
1311 ga_clear(&lines_ga);
1312
1313 if (cstack.cs_idx >= 0)
1314 {
1315 /*
1316 * If a sourced file or executed function ran to its end, report the
1317 * unclosed conditional.
1318 */
1319 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 && ((getline_equal(fgetline, cookie, getsourceline)
1321 && !source_finished(fgetline, cookie))
1322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && !func_has_ended(real_cookie))))
1324 {
1325 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1326 EMSG(_(e_endtry));
1327 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1328 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1330 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 EMSG(_(e_endif));
1333 }
1334
1335 /*
1336 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1337 * ":endtry" in a sourced file or executed function. If the try
1338 * conditional is in its finally clause, ignore anything pending.
1339 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001340 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 {
1344 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1345
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001346 if (idx >= 0)
1347 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1349 &cstack.cs_looplevel);
1350 }
1351 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 trylevel = initial_trylevel;
1353 }
1354
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001355 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1356 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001358 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ? (char_u *)"endfunction" : (char_u *)NULL);
1360
1361 if (trylevel == 0)
1362 {
1363 /*
1364 * When an exception is being thrown out of the outermost try
1365 * conditional, discard the uncaught exception, disable the conversion
1366 * of interrupts or errors to exceptions, and ensure that no more
1367 * commands are executed.
1368 */
1369 if (did_throw)
1370 {
1371 void *p = NULL;
1372 char_u *saved_sourcing_name;
1373 int saved_sourcing_lnum;
1374 struct msglist *messages = NULL, *next;
1375
1376 /*
1377 * If the uncaught exception is a user exception, report it as an
1378 * error. If it is an error exception, display the saved error
1379 * message now. For an interrupt exception, do nothing; the
1380 * interrupt message is given elsewhere.
1381 */
1382 switch (current_exception->type)
1383 {
1384 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001385 vim_snprintf((char *)IObuff, IOSIZE,
1386 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 current_exception->value);
1388 p = vim_strsave(IObuff);
1389 break;
1390 case ET_ERROR:
1391 messages = current_exception->messages;
1392 current_exception->messages = NULL;
1393 break;
1394 case ET_INTERRUPT:
1395 break;
1396 default:
1397 p = vim_strsave((char_u *)_(e_internal));
1398 }
1399
1400 saved_sourcing_name = sourcing_name;
1401 saved_sourcing_lnum = sourcing_lnum;
1402 sourcing_name = current_exception->throw_name;
1403 sourcing_lnum = current_exception->throw_lnum;
1404 current_exception->throw_name = NULL;
1405
1406 discard_current_exception(); /* uses IObuff if 'verbose' */
1407 suppress_errthrow = TRUE;
1408 force_abort = TRUE;
1409
1410 if (messages != NULL)
1411 {
1412 do
1413 {
1414 next = messages->next;
1415 emsg(messages->msg);
1416 vim_free(messages->msg);
1417 vim_free(messages);
1418 messages = next;
1419 }
1420 while (messages != NULL);
1421 }
1422 else if (p != NULL)
1423 {
1424 emsg(p);
1425 vim_free(p);
1426 }
1427 vim_free(sourcing_name);
1428 sourcing_name = saved_sourcing_name;
1429 sourcing_lnum = saved_sourcing_lnum;
1430 }
1431
1432 /*
1433 * On an interrupt or an aborting error not converted to an exception,
1434 * disable the conversion of errors to exceptions. (Interrupts are not
1435 * converted any more, here.) This enables also the interrupt message
1436 * when force_abort is set and did_emsg unset in case of an interrupt
1437 * from a finally clause after an error.
1438 */
1439 else if (got_int || (did_emsg && force_abort))
1440 suppress_errthrow = TRUE;
1441 }
1442
1443 /*
1444 * The current cstack will be freed when do_cmdline() returns. An uncaught
1445 * exception will have to be rethrown in the previous cstack. If a function
1446 * has just returned or a script file was just finished and the previous
1447 * cstack belongs to the same function or, respectively, script file, it
1448 * will have to be checked for finally clauses to be executed due to the
1449 * ":return" or ":finish". This is done in do_one_cmd().
1450 */
1451 if (did_throw)
1452 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001453 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001455 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 && ex_nesting_level > func_level(real_cookie) + 1))
1457 {
1458 if (!did_throw)
1459 check_cstack = TRUE;
1460 }
1461 else
1462 {
1463 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 --ex_nesting_level;
1466 /*
1467 * Go to debug mode when returning from a function in which we are
1468 * single-stepping.
1469 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 if ((getline_equal(fgetline, cookie, getsourceline)
1471 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ? (char_u *)_("End of sourced file")
1475 : (char_u *)_("End of function"));
1476 }
1477
1478 /*
1479 * Restore the exception environment (done after returning from the
1480 * debugger).
1481 */
1482 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001483 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 msg_list = saved_msg_list;
1486#endif /* FEAT_EVAL */
1487
1488 /*
1489 * If there was too much output to fit on the command line, ask the user to
1490 * hit return before redrawing the screen. With the ":global" command we do
1491 * this only once after the command is finished.
1492 */
1493 if (did_inc)
1494 {
1495 --RedrawingDisabled;
1496 --no_wait_return;
1497 msg_scroll = FALSE;
1498
1499 /*
1500 * When just finished an ":if"-":else" which was typed, no need to
1501 * wait for hit-return. Also for an error situation.
1502 */
1503 if (retval == FAIL
1504#ifdef FEAT_EVAL
1505 || (did_endif && KeyTyped && !did_emsg)
1506#endif
1507 )
1508 {
1509 need_wait_return = FALSE;
1510 msg_didany = FALSE; /* don't wait when restarting edit */
1511 }
1512 else if (need_wait_return)
1513 {
1514 /*
1515 * The msg_start() above clears msg_didout. The wait_return we do
1516 * here should not overwrite the command that may be shown before
1517 * doing that.
1518 */
1519 msg_didout |= msg_didout_before_start;
1520 wait_return(FALSE);
1521 }
1522 }
1523
1524#ifndef FEAT_EVAL
1525 /*
1526 * Reset if_level, in case a sourced script file contains more ":if" than
1527 * ":endif" (could be ":if x | foo | endif").
1528 */
1529 if_level = 0;
1530#endif
1531
1532 --call_depth;
1533 return retval;
1534}
1535
1536#ifdef FEAT_EVAL
1537/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int c;
1543 void *cookie;
1544 int indent;
1545{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 wcmd_T *wp;
1548 char_u *line;
1549
1550 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1551 {
1552 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001553 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (cp->getline == NULL)
1557 line = getcmdline(c, 0L, indent);
1558 else
1559 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001560 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 ++cp->current_line;
1562
1563 return line;
1564 }
1565
1566 KeyTyped = FALSE;
1567 ++cp->current_line;
1568 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1569 sourcing_lnum = wp->lnum;
1570 return vim_strsave(wp->line);
1571}
1572
1573/*
1574 * Store a line in "gap" so that a ":while" loop can execute it again.
1575 */
1576 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 garray_T *gap;
1579 char_u *line;
1580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
1593free_cmdlines(gap)
1594 garray_T *gap;
1595{
1596 while (gap->ga_len > 0)
1597 {
1598 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1599 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601}
1602#endif
1603
1604/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001605 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1606 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609getline_equal(fgetline, cookie, func)
1610 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001611 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 char_u *(*func) __ARGS((int, void *, int));
1613{
1614#ifdef FEAT_EVAL
1615 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001619 * function that's originally used to obtain the lines. This may be
1620 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 cp = (struct loop_cookie *)cookie;
1623 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 gp = cp->getline;
1626 cp = cp->cookie;
1627 }
1628 return gp == func;
1629#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#endif
1632}
1633
1634#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1635/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * getline function. Otherwise return "cookie".
1638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001641 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644# ifdef FEAT_EVAL
1645 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001646 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar89d40322006-08-29 15:30:07 +00001648 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001649 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001652 cp = (struct loop_cookie *)cookie;
1653 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655 gp = cp->getline;
1656 cp = cp->cookie;
1657 }
1658 return cp;
1659# else
1660 return cookie;
1661# endif
1662}
1663#endif
1664
1665/*
1666 * Execute one Ex command.
1667 *
1668 * If 'sourcing' is TRUE, the command will be included in the error message.
1669 *
1670 * 1. skip comment lines and leading space
1671 * 2. handle command modifiers
1672 * 3. parse range
1673 * 4. parse command
1674 * 5. parse arguments
1675 * 6. switch on command name
1676 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
1679 * This function may be called recursively!
1680 */
1681#if (_MSC_VER == 1200)
1682/*
Bram Moolenaared203462004-06-16 11:19:22 +00001683 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001685 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687 static char_u *
1688do_one_cmd(cmdlinep, sourcing,
1689#ifdef FEAT_EVAL
1690 cstack,
1691#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001692 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 char_u **cmdlinep;
1694 int sourcing;
1695#ifdef FEAT_EVAL
1696 struct condstack *cstack;
1697#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001698 char_u *(*fgetline) __ARGS((int, void *, int));
1699 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 char_u *p;
1702 linenr_T lnum;
1703 long n;
1704 char_u *errormsg = NULL; /* error message */
1705 exarg_T ea; /* Ex command arguments */
1706 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001707 int save_msg_scroll = msg_scroll;
1708 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001710#ifdef HAVE_SANDBOX
1711 int did_sandbox = FALSE;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 cmdmod_T save_cmdmod;
1714 int ni; /* set when Not Implemented */
1715
1716 vim_memset(&ea, 0, sizeof(ea));
1717 ea.line1 = 1;
1718 ea.line2 = 1;
1719#ifdef FEAT_EVAL
1720 ++ex_nesting_level;
1721#endif
1722
1723 /* when not editing the last file :q has to be typed twice */
1724 if (quitmore
1725#ifdef FEAT_EVAL
1726 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001727 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
1729 )
1730 --quitmore;
1731
1732 /*
1733 * Reset browse, confirm, etc.. They are restored when returning, for
1734 * recursive calls.
1735 */
1736 save_cmdmod = cmdmod;
1737 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1738
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001739 /* "#!anything" is handled like a comment. */
1740 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1741 goto doend;
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /*
1744 * Repeat until no more command modifiers are found.
1745 */
1746 ea.cmd = *cmdlinep;
1747 for (;;)
1748 {
1749/*
1750 * 1. skip comment lines and leading white space and colons
1751 */
1752 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1753 ++ea.cmd;
1754
1755 /* in ex mode, an empty line works like :+ */
1756 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 && (getline_equal(fgetline, cookie, getexmodeline)
1758 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001759 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
1761 ea.cmd = (char_u *)"+";
1762 ex_pressedreturn = TRUE;
1763 }
1764
1765 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001766 if (*ea.cmd == '"')
1767 goto doend;
1768 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001769 {
1770 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774/*
1775 * 2. handle command modifiers.
1776 */
1777 p = ea.cmd;
1778 if (VIM_ISDIGIT(*ea.cmd))
1779 p = skipwhite(skipdigits(ea.cmd));
1780 switch (*p)
1781 {
1782 /* When adding an entry, also modify cmd_exists(). */
1783 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1784 break;
1785#ifdef FEAT_WINDOWS
1786 cmdmod.split |= WSP_ABOVE;
1787#endif
1788 continue;
1789
1790 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1791 {
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_BELOW;
1794#endif
1795 continue;
1796 }
1797 if (checkforcmd(&ea.cmd, "browse", 3))
1798 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001799#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 cmdmod.browse = TRUE;
1801#endif
1802 continue;
1803 }
1804 if (!checkforcmd(&ea.cmd, "botright", 2))
1805 break;
1806#ifdef FEAT_WINDOWS
1807 cmdmod.split |= WSP_BOT;
1808#endif
1809 continue;
1810
1811 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1812 break;
1813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1814 cmdmod.confirm = TRUE;
1815#endif
1816 continue;
1817
1818 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1819 {
1820 cmdmod.keepmarks = TRUE;
1821 continue;
1822 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001823 if (checkforcmd(&ea.cmd, "keepalt", 5))
1824 {
1825 cmdmod.keepalt = TRUE;
1826 continue;
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1829 break;
1830 cmdmod.keepjumps = TRUE;
1831 continue;
1832
1833 /* ":hide" and ":hide | cmd" are not modifiers */
1834 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1835 || *p == NUL || ends_excmd(*p))
1836 break;
1837 ea.cmd = p;
1838 cmdmod.hide = TRUE;
1839 continue;
1840
1841 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1842 {
1843 cmdmod.lockmarks = TRUE;
1844 continue;
1845 }
1846
1847 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1848 break;
1849#ifdef FEAT_WINDOWS
1850 cmdmod.split |= WSP_ABOVE;
1851#endif
1852 continue;
1853
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001854 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1855 break;
1856#ifdef FEAT_AUTOCMD
1857 if (cmdmod.save_ei == NULL)
1858 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001859 /* Set 'eventignore' to "all". Restore the
1860 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001861 cmdmod.save_ei = vim_strsave(p_ei);
1862 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001863 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001864 }
1865#endif
1866 continue;
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1869 break;
1870#ifdef FEAT_WINDOWS
1871 cmdmod.split |= WSP_BELOW;
1872#endif
1873 continue;
1874
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001875 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1876 {
1877#ifdef HAVE_SANDBOX
1878 if (!did_sandbox)
1879 ++sandbox;
1880 did_sandbox = TRUE;
1881#endif
1882 continue;
1883 }
1884 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001886 if (save_msg_silent == -1)
1887 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1890 {
1891 /* ":silent!", but not "silent !cmd" */
1892 ea.cmd = skipwhite(ea.cmd + 1);
1893 ++emsg_silent;
1894 ++did_esilent;
1895 }
1896 continue;
1897
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001898 case 't': if (checkforcmd(&p, "tab", 3))
1899 {
1900#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001901 if (vim_isdigit(*ea.cmd))
1902 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1903 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001904 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001905 ea.cmd = p;
1906#endif
1907 continue;
1908 }
1909 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 break;
1911#ifdef FEAT_WINDOWS
1912 cmdmod.split |= WSP_TOP;
1913#endif
1914 continue;
1915
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001916 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1917 break;
1918 if (save_msg_silent == -1)
1919 save_msg_silent = msg_silent;
1920 msg_silent = 0;
1921 continue;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1924 {
1925#ifdef FEAT_VERTSPLIT
1926 cmdmod.split |= WSP_VERT;
1927#endif
1928 continue;
1929 }
1930 if (!checkforcmd(&p, "verbose", 4))
1931 break;
1932 if (verbose_save < 0)
1933 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001934 if (vim_isdigit(*ea.cmd))
1935 p_verbose = atoi((char *)ea.cmd);
1936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 p_verbose = 1;
1938 ea.cmd = p;
1939 continue;
1940 }
1941 break;
1942 }
1943
1944#ifdef FEAT_EVAL
1945 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1946 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1947#else
1948 ea.skip = (if_level > 0);
1949#endif
1950
1951#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001952# ifdef FEAT_PROFILE
1953 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001954 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001955 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001956 if (getline_equal(fgetline, cookie, get_func_line))
1957 func_line_exec(getline_cookie(fgetline, cookie));
1958 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 script_line_exec();
1960 }
1961#endif
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* May go to debug mode. If this happens and the ">quit" debug command is
1964 * used, throw an interrupt exception and skip the next command. */
1965 dbg_check_breakpoint(&ea);
1966 if (!ea.skip && got_int)
1967 {
1968 ea.skip = TRUE;
1969 (void)do_intthrow(cstack);
1970 }
1971#endif
1972
1973/*
1974 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1975 *
1976 * where 'addr' is:
1977 *
1978 * % (entire file)
1979 * $ [+-NUM]
1980 * 'x [+-NUM] (where x denotes a currently defined mark)
1981 * . [+-NUM]
1982 * [+-NUM]..
1983 * NUM
1984 *
1985 * The ea.cmd pointer is updated to point to the first character following the
1986 * range spec. If an initial address is found, but no second, the upper bound
1987 * is equal to the lower.
1988 */
1989
1990 /* repeat for all ',' or ';' separated addresses */
1991 for (;;)
1992 {
1993 ea.line1 = ea.line2;
1994 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1995 ea.cmd = skipwhite(ea.cmd);
1996 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1997 if (ea.cmd == NULL) /* error detected */
1998 goto doend;
1999 if (lnum == MAXLNUM)
2000 {
2001 if (*ea.cmd == '%') /* '%' - all lines */
2002 {
2003 ++ea.cmd;
2004 ea.line1 = 1;
2005 ea.line2 = curbuf->b_ml.ml_line_count;
2006 ++ea.addr_count;
2007 }
2008 /* '*' - visual area */
2009 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2010 {
2011 pos_T *fp;
2012
2013 ++ea.cmd;
2014 if (!ea.skip)
2015 {
2016 fp = getmark('<', FALSE);
2017 if (check_mark(fp) == FAIL)
2018 goto doend;
2019 ea.line1 = fp->lnum;
2020 fp = getmark('>', FALSE);
2021 if (check_mark(fp) == FAIL)
2022 goto doend;
2023 ea.line2 = fp->lnum;
2024 ++ea.addr_count;
2025 }
2026 }
2027 }
2028 else
2029 ea.line2 = lnum;
2030 ea.addr_count++;
2031
2032 if (*ea.cmd == ';')
2033 {
2034 if (!ea.skip)
2035 curwin->w_cursor.lnum = ea.line2;
2036 }
2037 else if (*ea.cmd != ',')
2038 break;
2039 ++ea.cmd;
2040 }
2041
2042 /* One address given: set start and end lines */
2043 if (ea.addr_count == 1)
2044 {
2045 ea.line1 = ea.line2;
2046 /* ... but only implicit: really no address given */
2047 if (lnum == MAXLNUM)
2048 ea.addr_count = 0;
2049 }
2050
2051 /* Don't leave the cursor on an illegal line (caused by ';') */
2052 check_cursor_lnum();
2053
2054/*
2055 * 4. parse command
2056 */
2057
2058 /*
2059 * Skip ':' and any white space
2060 */
2061 ea.cmd = skipwhite(ea.cmd);
2062 while (*ea.cmd == ':')
2063 ea.cmd = skipwhite(ea.cmd + 1);
2064
2065 /*
2066 * If we got a line, but no command, then go to the line.
2067 * If we find a '|' or '\n' we set ea.nextcmd.
2068 */
2069 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2070 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2071 {
2072 /*
2073 * strange vi behaviour:
2074 * ":3" jumps to line 3
2075 * ":3|..." prints line 3
2076 * ":|" prints current line
2077 */
2078 if (ea.skip) /* skip this if inside :if */
2079 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002080 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 ea.cmdidx = CMD_print;
2083 ea.argt = RANGE+COUNT+TRLBAR;
2084 if ((errormsg = invalid_range(&ea)) == NULL)
2085 {
2086 correct_range(&ea);
2087 ex_print(&ea);
2088 }
2089 }
2090 else if (ea.addr_count != 0)
2091 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002092 if (ea.line2 > curbuf->b_ml.ml_line_count)
2093 {
2094 /* With '-' in 'cpoptions' a line number past the file is an
2095 * error, otherwise put it at the end of the file. */
2096 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2097 ea.line2 = -1;
2098 else
2099 ea.line2 = curbuf->b_ml.ml_line_count;
2100 }
2101
2102 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002103 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 else
2105 {
2106 if (ea.line2 == 0)
2107 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
2109 curwin->w_cursor.lnum = ea.line2;
2110 beginline(BL_SOL | BL_FIX);
2111 }
2112 }
2113 goto doend;
2114 }
2115
2116 /* Find the command and let "p" point to after it. */
2117 p = find_command(&ea, NULL);
2118
2119#ifdef FEAT_USR_CMDS
2120 if (p == NULL)
2121 {
2122 if (!ea.skip)
2123 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2124 goto doend;
2125 }
2126 /* Check for wrong commands. */
2127 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2128 {
2129 errormsg = uc_fun_cmd();
2130 goto doend;
2131 }
2132#endif
2133 if (ea.cmdidx == CMD_SIZE)
2134 {
2135 if (!ea.skip)
2136 {
2137 STRCPY(IObuff, _("E492: Not an editor command"));
2138 if (!sourcing)
2139 {
2140 STRCAT(IObuff, ": ");
2141 STRNCAT(IObuff, *cmdlinep, 40);
2142 }
2143 errormsg = IObuff;
2144 }
2145 goto doend;
2146 }
2147
2148 ni = (
2149#ifdef FEAT_USR_CMDS
2150 !USER_CMDIDX(ea.cmdidx) &&
2151#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002152 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002153#ifdef HAVE_EX_SCRIPT_NI
2154 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2155#endif
2156 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158#ifndef FEAT_EVAL
2159 /*
2160 * When the expression evaluation is disabled, recognize the ":if" and
2161 * ":endif" commands and ignore everything in between it.
2162 */
2163 if (ea.cmdidx == CMD_if)
2164 ++if_level;
2165 if (if_level)
2166 {
2167 if (ea.cmdidx == CMD_endif)
2168 --if_level;
2169 goto doend;
2170 }
2171
2172#endif
2173
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002174 /* forced commands */
2175 if (*p == '!' && ea.cmdidx != CMD_substitute
2176 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 ++p;
2179 ea.forceit = TRUE;
2180 }
2181 else
2182 ea.forceit = FALSE;
2183
2184/*
2185 * 5. parse arguments
2186 */
2187#ifdef FEAT_USR_CMDS
2188 if (!USER_CMDIDX(ea.cmdidx))
2189#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002190 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 if (!ea.skip)
2193 {
2194#ifdef HAVE_SANDBOX
2195 if (sandbox != 0 && !(ea.argt & SBOXOK))
2196 {
2197 /* Command not allowed in sandbox. */
2198 errormsg = (char_u *)_(e_sandbox);
2199 goto doend;
2200 }
2201#endif
2202 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2203 {
2204 /* Command not allowed in non-'modifiable' buffer */
2205 errormsg = (char_u *)_(e_modifiable);
2206 goto doend;
2207 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002208
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002209 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# ifdef FEAT_USR_CMDS
2211 && !USER_CMDIDX(ea.cmdidx)
2212# endif
2213 )
2214 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002215 /* Command not allowed when editing the command line. */
2216#ifdef FEAT_CMDWIN
2217 if (cmdwin_type != 0)
2218 errormsg = (char_u *)_(e_cmdwin);
2219 else
2220#endif
2221 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto doend;
2223 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002225 /* Disallow editing another buffer when "curbuf_lock" is set.
2226 * Do allow ":edit" (check for argument later).
2227 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002228 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002229 && ea.cmdidx != CMD_edit
2230 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231# ifdef FEAT_USR_CMDS
2232 && !USER_CMDIDX(ea.cmdidx)
2233# endif
2234 && curbuf_locked())
2235 goto doend;
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2239 {
2240 /* no range allowed */
2241 errormsg = (char_u *)_(e_norange);
2242 goto doend;
2243 }
2244 }
2245
2246 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2247 {
2248 errormsg = (char_u *)_(e_nobang);
2249 goto doend;
2250 }
2251
2252 /*
2253 * Don't complain about the range if it is not used
2254 * (could happen if line_count is accidentally set to 0).
2255 */
2256 if (!ea.skip && !ni)
2257 {
2258 /*
2259 * If the range is backwards, ask for confirmation and, if given, swap
2260 * ea.line1 & ea.line2 so it's forwards again.
2261 * When global command is busy, don't ask, will fail below.
2262 */
2263 if (!global_busy && ea.line1 > ea.line2)
2264 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002265 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002267 if (sourcing || exmode_active)
2268 {
2269 errormsg = (char_u *)_("E493: Backwards range given");
2270 goto doend;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (ask_yesno((char_u *)
2273 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002274 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 lnum = ea.line1;
2277 ea.line1 = ea.line2;
2278 ea.line2 = lnum;
2279 }
2280 if ((errormsg = invalid_range(&ea)) != NULL)
2281 goto doend;
2282 }
2283
2284 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2285 ea.line2 = 1;
2286
2287 correct_range(&ea);
2288
2289#ifdef FEAT_FOLDING
2290 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2291 {
2292 /* Put the first line at the start of a closed fold, put the last line
2293 * at the end of a closed fold. */
2294 (void)hasFolding(ea.line1, &ea.line1, NULL);
2295 (void)hasFolding(ea.line2, NULL, &ea.line2);
2296 }
2297#endif
2298
2299#ifdef FEAT_QUICKFIX
2300 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002301 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * option here, so things like % get expanded.
2303 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002304 p = replace_makeprg(&ea, p, cmdlinep);
2305 if (p == NULL)
2306 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308
2309 /*
2310 * Skip to start of argument.
2311 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2312 */
2313 if (ea.cmdidx == CMD_bang)
2314 ea.arg = p;
2315 else
2316 ea.arg = skipwhite(p);
2317
2318 /*
2319 * Check for "++opt=val" argument.
2320 * Must be first, allow ":w ++enc=utf8 !cmd"
2321 */
2322 if (ea.argt & ARGOPT)
2323 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2324 if (getargopt(&ea) == FAIL && !ni)
2325 {
2326 errormsg = (char_u *)_(e_invarg);
2327 goto doend;
2328 }
2329
2330 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2331 {
2332 if (*ea.arg == '>') /* append */
2333 {
2334 if (*++ea.arg != '>') /* typed wrong */
2335 {
2336 errormsg = (char_u *)_("E494: Use w or w>>");
2337 goto doend;
2338 }
2339 ea.arg = skipwhite(ea.arg + 1);
2340 ea.append = TRUE;
2341 }
2342 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2343 {
2344 ++ea.arg;
2345 ea.usefilter = TRUE;
2346 }
2347 }
2348
2349 if (ea.cmdidx == CMD_read)
2350 {
2351 if (ea.forceit)
2352 {
2353 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2354 ea.forceit = FALSE;
2355 }
2356 else if (*ea.arg == '!') /* :r !filter */
2357 {
2358 ++ea.arg;
2359 ea.usefilter = TRUE;
2360 }
2361 }
2362
2363 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2364 {
2365 ea.amount = 1;
2366 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2367 {
2368 ++ea.arg;
2369 ++ea.amount;
2370 }
2371 ea.arg = skipwhite(ea.arg);
2372 }
2373
2374 /*
2375 * Check for "+command" argument, before checking for next command.
2376 * Don't do this for ":read !cmd" and ":write !cmd".
2377 */
2378 if ((ea.argt & EDITCMD) && !ea.usefilter)
2379 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2380
2381 /*
2382 * Check for '|' to separate commands and '"' to start comments.
2383 * Don't do this for ":read !cmd" and ":write !cmd".
2384 */
2385 if ((ea.argt & TRLBAR) && !ea.usefilter)
2386 separate_nextcmd(&ea);
2387
2388 /*
2389 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2391 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 else if (ea.cmdidx == CMD_bang
2394 || ea.cmdidx == CMD_global
2395 || ea.cmdidx == CMD_vglobal
2396 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 for (p = ea.arg; *p; ++p)
2399 {
2400 /* Remove one backslash before a newline, so that it's possible to
2401 * pass a newline to the shell and also a newline that is preceded
2402 * with a backslash. This makes it impossible to end a shell
2403 * command in a backslash, but that doesn't appear useful.
2404 * Halving the number of backslashes is incompatible with previous
2405 * versions. */
2406 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002407 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 else if (*p == '\n')
2409 {
2410 ea.nextcmd = p + 1;
2411 *p = NUL;
2412 break;
2413 }
2414 }
2415 }
2416
2417 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2418 {
2419 ea.line1 = 1;
2420 ea.line2 = curbuf->b_ml.ml_line_count;
2421 }
2422
2423 /* accept numbered register only when no count allowed (:put) */
2424 if ( (ea.argt & REGSTR)
2425 && *ea.arg != NUL
2426#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* Do not allow register = for user commands */
2428 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
2430 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2431 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002432#ifndef FEAT_CLIPBOARD
2433 /* check these explicitly for a more specific error message */
2434 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002436 errormsg = (char_u *)_(e_invalidreg);
2437 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002440 if (
2441#ifdef FEAT_USR_CMDS
2442 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2443 && USER_CMDIDX(ea.cmdidx)))
2444#else
2445 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2446#endif
2447 )
2448 {
2449 ea.regname = *ea.arg++;
2450#ifdef FEAT_EVAL
2451 /* for '=' register: accept the rest of the line as an expression */
2452 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2453 {
2454 set_expr_line(vim_strsave(ea.arg));
2455 ea.arg += STRLEN(ea.arg);
2456 }
2457#endif
2458 ea.arg = skipwhite(ea.arg);
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461
2462 /*
2463 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2464 * count, it's a buffer name.
2465 */
2466 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2467 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2468 || vim_iswhite(*p)))
2469 {
2470 n = getdigits(&ea.arg);
2471 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002472 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 errormsg = (char_u *)_(e_zerocount);
2475 goto doend;
2476 }
2477 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2478 {
2479 ea.line2 = n;
2480 if (ea.addr_count == 0)
2481 ea.addr_count = 1;
2482 }
2483 else
2484 {
2485 ea.line1 = ea.line2;
2486 ea.line2 += n - 1;
2487 ++ea.addr_count;
2488 /*
2489 * Be vi compatible: no error message for out of range.
2490 */
2491 if (ea.line2 > curbuf->b_ml.ml_line_count)
2492 ea.line2 = curbuf->b_ml.ml_line_count;
2493 }
2494 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002495
2496 /*
2497 * Check for flags: 'l', 'p' and '#'.
2498 */
2499 if (ea.argt & EXFLAGS)
2500 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002502 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002503 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 errormsg = (char_u *)_(e_trailing);
2506 goto doend;
2507 }
2508
2509 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2510 {
2511 errormsg = (char_u *)_(e_argreq);
2512 goto doend;
2513 }
2514
2515#ifdef FEAT_EVAL
2516 /*
2517 * Skip the command when it's not going to be executed.
2518 * The commands like :if, :endif, etc. always need to be executed.
2519 * Also make an exception for commands that handle a trailing command
2520 * themselves.
2521 */
2522 if (ea.skip)
2523 {
2524 switch (ea.cmdidx)
2525 {
2526 /* commands that need evaluation */
2527 case CMD_while:
2528 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002529 case CMD_for:
2530 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 case CMD_if:
2532 case CMD_elseif:
2533 case CMD_else:
2534 case CMD_endif:
2535 case CMD_try:
2536 case CMD_catch:
2537 case CMD_finally:
2538 case CMD_endtry:
2539 case CMD_function:
2540 break;
2541
2542 /* Commands that handle '|' themselves. Check: A command should
2543 * either have the TRLBAR flag, appear in this list or appear in
2544 * the list at ":help :bar". */
2545 case CMD_aboveleft:
2546 case CMD_and:
2547 case CMD_belowright:
2548 case CMD_botright:
2549 case CMD_browse:
2550 case CMD_call:
2551 case CMD_confirm:
2552 case CMD_delfunction:
2553 case CMD_djump:
2554 case CMD_dlist:
2555 case CMD_dsearch:
2556 case CMD_dsplit:
2557 case CMD_echo:
2558 case CMD_echoerr:
2559 case CMD_echomsg:
2560 case CMD_echon:
2561 case CMD_execute:
2562 case CMD_help:
2563 case CMD_hide:
2564 case CMD_ijump:
2565 case CMD_ilist:
2566 case CMD_isearch:
2567 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002568 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 case CMD_keepjumps:
2570 case CMD_keepmarks:
2571 case CMD_leftabove:
2572 case CMD_let:
2573 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002574 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002576 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 case CMD_perl:
2578 case CMD_psearch:
2579 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002580 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002581 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_return:
2583 case CMD_rightbelow:
2584 case CMD_ruby:
2585 case CMD_silent:
2586 case CMD_smagic:
2587 case CMD_snomagic:
2588 case CMD_substitute:
2589 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002590 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 case CMD_tcl:
2592 case CMD_throw:
2593 case CMD_tilde:
2594 case CMD_topleft:
2595 case CMD_unlet:
2596 case CMD_verbose:
2597 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002598 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600
2601 default: goto doend;
2602 }
2603 }
2604#endif
2605
2606 if (ea.argt & XFILE)
2607 {
2608 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2609 goto doend;
2610 }
2611
2612#ifdef FEAT_LISTCMDS
2613 /*
2614 * Accept buffer name. Cannot be used at the same time with a buffer
2615 * number. Don't do this for a user command.
2616 */
2617 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2618# ifdef FEAT_USR_CMDS
2619 && !USER_CMDIDX(ea.cmdidx)
2620# endif
2621 )
2622 {
2623 /*
2624 * :bdelete, :bwipeout and :bunload take several arguments, separated
2625 * by spaces: find next space (skipping over escaped characters).
2626 * The others take one argument: ignore trailing spaces.
2627 */
2628 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2629 || ea.cmdidx == CMD_bunload)
2630 p = skiptowhite_esc(ea.arg);
2631 else
2632 {
2633 p = ea.arg + STRLEN(ea.arg);
2634 while (p > ea.arg && vim_iswhite(p[-1]))
2635 --p;
2636 }
2637 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2638 if (ea.line2 < 0) /* failed */
2639 goto doend;
2640 ea.addr_count = 1;
2641 ea.arg = skipwhite(p);
2642 }
2643#endif
2644
2645/*
2646 * 6. switch on command name
2647 *
2648 * The "ea" structure holds the arguments that can be used.
2649 */
2650 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 ea.cookie = cookie;
2653#ifdef FEAT_EVAL
2654 ea.cstack = cstack;
2655#endif
2656
2657#ifdef FEAT_USR_CMDS
2658 if (USER_CMDIDX(ea.cmdidx))
2659 {
2660 /*
2661 * Execute a user-defined command.
2662 */
2663 do_ucmd(&ea);
2664 }
2665 else
2666#endif
2667 {
2668 /*
2669 * Call the function to execute the command.
2670 */
2671 ea.errmsg = NULL;
2672 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2673 if (ea.errmsg != NULL)
2674 errormsg = (char_u *)_(ea.errmsg);
2675 }
2676
2677#ifdef FEAT_EVAL
2678 /*
2679 * If the command just executed called do_cmdline(), any throw or ":return"
2680 * or ":finish" encountered there must also check the cstack of the still
2681 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2682 * exception, or reanimate a returned function or finished script file and
2683 * return or finish it again.
2684 */
2685 if (need_rethrow)
2686 do_throw(cstack);
2687 else if (check_cstack)
2688 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002691 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 && current_func_returned())
2693 do_return(&ea, TRUE, FALSE, NULL);
2694 }
2695 need_rethrow = check_cstack = FALSE;
2696#endif
2697
2698doend:
2699 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2700 curwin->w_cursor.lnum = 1;
2701
2702 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2703 {
2704 if (sourcing)
2705 {
2706 if (errormsg != IObuff)
2707 {
2708 STRCPY(IObuff, errormsg);
2709 errormsg = IObuff;
2710 }
2711 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002712 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 emsg(errormsg);
2715 }
2716#ifdef FEAT_EVAL
2717 do_errthrow(cstack,
2718 (ea.cmdidx != CMD_SIZE
2719# ifdef FEAT_USR_CMDS
2720 && !USER_CMDIDX(ea.cmdidx)
2721# endif
2722 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2723#endif
2724
2725 if (verbose_save >= 0)
2726 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002727#ifdef FEAT_AUTOCMD
2728 if (cmdmod.save_ei != NULL)
2729 {
2730 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002731 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2732 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002733 free_string_option(cmdmod.save_ei);
2734 }
2735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 cmdmod = save_cmdmod;
2738
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002739 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 /* messages could be enabled for a serious error, need to check if the
2742 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002743 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002744 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 emsg_silent -= did_esilent;
2746 if (emsg_silent < 0)
2747 emsg_silent = 0;
2748 /* Restore msg_scroll, it's set by file I/O commands, even when no
2749 * message is actually displayed. */
2750 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002751
2752 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2753 * somewhere in the line. Put it back in the first column. */
2754 if (redirecting())
2755 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002758#ifdef HAVE_SANDBOX
2759 if (did_sandbox)
2760 --sandbox;
2761#endif
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2764 ea.nextcmd = NULL;
2765
2766#ifdef FEAT_EVAL
2767 --ex_nesting_level;
2768#endif
2769
2770 return ea.nextcmd;
2771}
2772#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002773 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#endif
2775
2776/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002777 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 * If there is a match advance "pp" to the argument and return TRUE.
2779 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002780 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002782 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 char *cmd; /* name of command */
2784 int len; /* required length */
2785{
2786 int i;
2787
2788 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002789 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 break;
2791 if (i >= len && !isalpha((*pp)[i]))
2792 {
2793 *pp = skipwhite(*pp + i);
2794 return TRUE;
2795 }
2796 return FALSE;
2797}
2798
2799/*
2800 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002801 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002803 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 * Returns NULL for an ambiguous user command.
2805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 static char_u *
2807find_command(eap, full)
2808 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002809 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 int len;
2812 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002813 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 /*
2816 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002817 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 * - the 'k' command can directly be followed by any character.
2819 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2820 * but :sre[wind] is another command, as are :scrip[tnames],
2821 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002822 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 p = eap->cmd;
2825 if (*p == 'k')
2826 {
2827 eap->cmdidx = CMD_k;
2828 ++p;
2829 }
2830 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002831 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2832 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 || p[1] == 'g'
2834 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2835 || p[1] == 'I'
2836 || (p[1] == 'r' && p[2] != 'e')))
2837 {
2838 eap->cmdidx = CMD_substitute;
2839 ++p;
2840 }
2841 else
2842 {
2843 while (ASCII_ISALPHA(*p))
2844 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002845 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002846 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002847 while (ASCII_ISALNUM(*p))
2848 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 /* check for non-alpha command */
2851 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2852 ++p;
2853 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002854 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2855 {
2856 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2857 * :delete with the 'l' flag. Same for 'p'. */
2858 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002859 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002860 break;
2861 if (i == len - 1)
2862 {
2863 --len;
2864 if (p[-1] == 'l')
2865 eap->flags |= EXFLAG_LIST;
2866 else
2867 eap->flags |= EXFLAG_PRINT;
2868 }
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 if (ASCII_ISLOWER(*eap->cmd))
2872 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2873 else
2874 eap->cmdidx = cmdidxs[26];
2875
2876 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2877 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2878 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2879 (size_t)len) == 0)
2880 {
2881#ifdef FEAT_EVAL
2882 if (full != NULL
2883 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2884 *full = TRUE;
2885#endif
2886 break;
2887 }
2888
2889#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002890 /* Look for a user defined command as a last resort. Let ":Print" be
2891 * overruled by a user defined command. */
2892 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2893 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002895 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 while (ASCII_ISALNUM(*p))
2897 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002898 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002901 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 eap->cmdidx = CMD_SIZE;
2903 }
2904
2905 return p;
2906}
2907
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002908#ifdef FEAT_USR_CMDS
2909/*
2910 * Search for a user command that matches "eap->cmd".
2911 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2912 * Return a pointer to just after the command.
2913 * Return NULL if there is no matching command.
2914 */
2915 static char_u *
2916find_ucmd(eap, p, full, xp, compl)
2917 exarg_T *eap;
2918 char_u *p; /* end of the command (possibly including count) */
2919 int *full; /* set to TRUE for a full match */
2920 expand_T *xp; /* used for completion, NULL otherwise */
2921 int *compl; /* completion flags or NULL */
2922{
2923 int len = (int)(p - eap->cmd);
2924 int j, k, matchlen = 0;
2925 ucmd_T *uc;
2926 int found = FALSE;
2927 int possible = FALSE;
2928 char_u *cp, *np; /* Point into typed cmd and test name */
2929 garray_T *gap;
2930 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2931 only full match global is accepted. */
2932
2933 /*
2934 * Look for buffer-local user commands first, then global ones.
2935 */
2936 gap = &curbuf->b_ucmds;
2937 for (;;)
2938 {
2939 for (j = 0; j < gap->ga_len; ++j)
2940 {
2941 uc = USER_CMD_GA(gap, j);
2942 cp = eap->cmd;
2943 np = uc->uc_name;
2944 k = 0;
2945 while (k < len && *np != NUL && *cp++ == *np++)
2946 k++;
2947 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2948 {
2949 /* If finding a second match, the command is ambiguous. But
2950 * not if a buffer-local command wasn't a full match and a
2951 * global command is a full match. */
2952 if (k == len && found && *np != NUL)
2953 {
2954 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002956 amb_local = TRUE;
2957 }
2958
2959 if (!found || (k == len && *np == NUL))
2960 {
2961 /* If we matched up to a digit, then there could
2962 * be another command including the digit that we
2963 * should use instead.
2964 */
2965 if (k == len)
2966 found = TRUE;
2967 else
2968 possible = TRUE;
2969
2970 if (gap == &ucmds)
2971 eap->cmdidx = CMD_USER;
2972 else
2973 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002974 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002975 eap->useridx = j;
2976
2977# ifdef FEAT_CMDL_COMPL
2978 if (compl != NULL)
2979 *compl = uc->uc_compl;
2980# ifdef FEAT_EVAL
2981 if (xp != NULL)
2982 {
2983 xp->xp_arg = uc->uc_compl_arg;
2984 xp->xp_scriptID = uc->uc_scriptID;
2985 }
2986# endif
2987# endif
2988 /* Do not search for further abbreviations
2989 * if this is an exact match. */
2990 matchlen = k;
2991 if (k == len && *np == NUL)
2992 {
2993 if (full != NULL)
2994 *full = TRUE;
2995 amb_local = FALSE;
2996 break;
2997 }
2998 }
2999 }
3000 }
3001
3002 /* Stop if we found a full match or searched all. */
3003 if (j < gap->ga_len || gap == &ucmds)
3004 break;
3005 gap = &ucmds;
3006 }
3007
3008 /* Only found ambiguous matches. */
3009 if (amb_local)
3010 {
3011 if (xp != NULL)
3012 xp->xp_context = EXPAND_UNSUCCESSFUL;
3013 return NULL;
3014 }
3015
3016 /* The match we found may be followed immediately by a number. Move "p"
3017 * back to point to it. */
3018 if (found || possible)
3019 return p + (matchlen - len);
3020 return p;
3021}
3022#endif
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003025static struct cmdmod
3026{
3027 char *name;
3028 int minlen;
3029 int has_count; /* :123verbose :3tab */
3030} cmdmods[] = {
3031 {"aboveleft", 3, FALSE},
3032 {"belowright", 3, FALSE},
3033 {"botright", 2, FALSE},
3034 {"browse", 3, FALSE},
3035 {"confirm", 4, FALSE},
3036 {"hide", 3, FALSE},
3037 {"keepalt", 5, FALSE},
3038 {"keepjumps", 5, FALSE},
3039 {"keepmarks", 3, FALSE},
3040 {"leftabove", 5, FALSE},
3041 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003042 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003043 {"rightbelow", 6, FALSE},
3044 {"sandbox", 3, FALSE},
3045 {"silent", 3, FALSE},
3046 {"tab", 3, TRUE},
3047 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003048 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003049 {"verbose", 4, TRUE},
3050 {"vertical", 4, FALSE},
3051};
3052
3053/*
3054 * Return length of a command modifier (including optional count).
3055 * Return zero when it's not a modifier.
3056 */
3057 int
3058modifier_len(cmd)
3059 char_u *cmd;
3060{
3061 int i, j;
3062 char_u *p = cmd;
3063
3064 if (VIM_ISDIGIT(*cmd))
3065 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003066 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003067 {
3068 for (j = 0; p[j] != NUL; ++j)
3069 if (p[j] != cmdmods[i].name[j])
3070 break;
3071 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3072 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003073 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003074 }
3075 return 0;
3076}
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078/*
3079 * Return > 0 if an Ex command "name" exists.
3080 * Return 2 if there is an exact match.
3081 * Return 3 if there is an ambiguous match.
3082 */
3083 int
3084cmd_exists(name)
3085 char_u *name;
3086{
3087 exarg_T ea;
3088 int full = FALSE;
3089 int i;
3090 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003091 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003094 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 for (j = 0; name[j] != NUL; ++j)
3097 if (name[j] != cmdmods[i].name[j])
3098 break;
3099 if (name[j] == NUL && j >= cmdmods[i].minlen)
3100 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3101 }
3102
Bram Moolenaara9587612006-05-04 21:47:50 +00003103 /* Check built-in commands and user defined commands.
3104 * For ":2match" and ":3match" we need to skip the number. */
3105 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003107 p = find_command(&ea, &full);
3108 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003110 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3111 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003112 if (*skipwhite(p) != NUL)
3113 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3115}
3116#endif
3117
3118/*
3119 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3120 * we don't need/want deleted. Maybe this could be done better if we didn't
3121 * repeat all this stuff. The only problem is that they may not stay
3122 * perfectly compatible with each other, but then the command line syntax
3123 * probably won't change that much -- webb.
3124 */
3125 char_u *
3126set_one_cmd_context(xp, buff)
3127 expand_T *xp;
3128 char_u *buff; /* buffer for command string */
3129{
3130 char_u *p;
3131 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 int len = 0;
3133 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3135 int compl = EXPAND_NOTHING;
3136#endif
3137#ifdef FEAT_CMDL_COMPL
3138 int delim;
3139#endif
3140 int forceit = FALSE;
3141 int usefilter = FALSE; /* filter instead of file name */
3142
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003143 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 xp->xp_pattern = buff;
3145 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148/*
3149 * 2. skip comment lines and leading space, colons or bars
3150 */
3151 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3152 ;
3153 xp->xp_pattern = cmd;
3154
3155 if (*cmd == NUL)
3156 return NULL;
3157 if (*cmd == '"') /* ignore comment lines */
3158 {
3159 xp->xp_context = EXPAND_NOTHING;
3160 return NULL;
3161 }
3162
3163/*
3164 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3165 */
3166 cmd = skip_range(cmd, &xp->xp_context);
3167
3168/*
3169 * 4. parse command
3170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 xp->xp_pattern = cmd;
3172 if (*cmd == NUL)
3173 return NULL;
3174 if (*cmd == '"')
3175 {
3176 xp->xp_context = EXPAND_NOTHING;
3177 return NULL;
3178 }
3179
3180 if (*cmd == '|' || *cmd == '\n')
3181 return cmd + 1; /* There's another command */
3182
3183 /*
3184 * Isolate the command and search for it in the command table.
3185 * Exceptions:
3186 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003187 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3189 */
3190 if (*cmd == 'k' && cmd[1] != 'e')
3191 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 p = cmd + 1;
3194 }
3195 else
3196 {
3197 p = cmd;
3198 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3199 ++p;
3200 /* check for non-alpha command */
3201 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3202 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
3207 xp->xp_context = EXPAND_UNSUCCESSFUL;
3208 return NULL;
3209 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003211 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3212 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3213 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 break;
3215
3216#ifdef FEAT_USR_CMDS
3217 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3219 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#endif
3221 }
3222
3223 /*
3224 * If the cursor is touching the command, and it ends in an alpha-numeric
3225 * character, complete the command name.
3226 */
3227 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3228 return NULL;
3229
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3233 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 p = cmd + 1;
3236 }
3237#ifdef FEAT_USR_CMDS
3238 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3239 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 ea.cmd = cmd;
3241 p = find_ucmd(&ea, p, NULL, xp,
3242# if defined(FEAT_CMDL_COMPL)
3243 &compl
3244# else
3245 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003248 if (p == NULL)
3249 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251#endif
3252 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
3255 /* Not still touching the command and it was an illegal one */
3256 xp->xp_context = EXPAND_UNSUCCESSFUL;
3257 return NULL;
3258 }
3259
3260 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3261
3262 if (*p == '!') /* forced commands */
3263 {
3264 forceit = TRUE;
3265 ++p;
3266 }
3267
3268/*
3269 * 5. parse arguments
3270 */
3271#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003274 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 arg = skipwhite(p);
3277
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 if (*arg == '>') /* append */
3281 {
3282 if (*++arg == '>')
3283 ++arg;
3284 arg = skipwhite(arg);
3285 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003286 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 ++arg;
3289 usefilter = TRUE;
3290 }
3291 }
3292
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
3295 usefilter = forceit; /* :r! filter if forced */
3296 if (*arg == '!') /* :r !filter */
3297 {
3298 ++arg;
3299 usefilter = TRUE;
3300 }
3301 }
3302
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 while (*arg == *cmd) /* allow any number of '>' or '<' */
3306 ++arg;
3307 arg = skipwhite(arg);
3308 }
3309
3310 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 /* Check if we're in the +command */
3314 p = arg + 1;
3315 arg = skip_cmd_arg(arg, FALSE);
3316
3317 /* Still touching the command after '+'? */
3318 if (*arg == NUL)
3319 return p;
3320
3321 /* Skip space(s) after +command to get to the real argument */
3322 arg = skipwhite(arg);
3323 }
3324
3325 /*
3326 * Check for '|' to separate commands and '"' to start comments.
3327 * Don't do this for ":read !cmd" and ":write !cmd".
3328 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
3331 p = arg;
3332 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 p += 2;
3335 while (*p)
3336 {
3337 if (*p == Ctrl_V)
3338 {
3339 if (p[1] != NUL)
3340 ++p;
3341 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 || *p == '|' || *p == '\n')
3344 {
3345 if (*(p - 1) != '\\')
3346 {
3347 if (*p == '|' || *p == '\n')
3348 return p + 1;
3349 return NULL; /* It's a comment */
3350 }
3351 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003352 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 }
3355
3356 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003357 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 vim_strchr((char_u *)"|\"", *arg) == NULL)
3359 return NULL;
3360
3361 /* Find start of last argument (argument just before cursor): */
3362 p = buff + STRLEN(buff);
3363 while (p != arg && *p != ' ' && *p != TAB)
3364 p--;
3365 if (*p == ' ' || *p == TAB)
3366 p++;
3367 xp->xp_pattern = p;
3368
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003371 int c;
3372 int in_quote = FALSE;
3373 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 /*
3376 * Allow spaces within back-quotes to count as part of the argument
3377 * being expanded.
3378 */
3379 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003380 p = xp->xp_pattern;
3381 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003383#ifdef FEAT_MBYTE
3384 if (has_mbyte)
3385 c = mb_ptr2char(p);
3386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003388 c = *p;
3389 if (c == '\\' && p[1] != NUL)
3390 ++p;
3391 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 if (!in_quote)
3394 {
3395 xp->xp_pattern = p;
3396 bow = p + 1;
3397 }
3398 in_quote = !in_quote;
3399 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003400 /* An argument can contain just about everything, except
3401 * characters that end the command and white space. */
3402 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003403#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003404 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003405#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003406 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003407 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003408 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003409 while (*p != NUL)
3410 {
3411#ifdef FEAT_MBYTE
3412 if (has_mbyte)
3413 c = mb_ptr2char(p);
3414 else
3415#endif
3416 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003417 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003418 break;
3419#ifdef FEAT_MBYTE
3420 if (has_mbyte)
3421 len = (*mb_ptr2len)(p);
3422 else
3423#endif
3424 len = 1;
3425 mb_ptr_adv(p);
3426 }
3427 if (in_quote)
3428 bow = p;
3429 else
3430 xp->xp_pattern = p;
3431 p -= len;
3432 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003433 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435
3436 /*
3437 * If we are still inside the quotes, and we passed a space, just
3438 * expand from there.
3439 */
3440 if (bow != NULL && in_quote)
3441 xp->xp_pattern = bow;
3442 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003443
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003444 /* For a shell command more chars need to be escaped. */
3445 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003446 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003447#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003448 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003450 /* When still after the command name expand executables. */
3451 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003452 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 /* Check for environment variable */
3456 if (*xp->xp_pattern == '$'
3457#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3458 || *xp->xp_pattern == '%'
3459#endif
3460 )
3461 {
3462 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3463 if (!vim_isIDc(*p))
3464 break;
3465 if (*p == NUL)
3466 {
3467 xp->xp_context = EXPAND_ENV_VARS;
3468 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003469#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3470 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003471 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003472 compl = EXPAND_ENV_VARS;
3473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 }
3476 }
3477
3478/*
3479 * 6. switch on command name
3480 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003481 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003483 case CMD_find:
3484 case CMD_sfind:
3485 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003486 if (xp->xp_context == EXPAND_FILES)
3487 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 case CMD_cd:
3490 case CMD_chdir:
3491 case CMD_lcd:
3492 case CMD_lchdir:
3493 if (xp->xp_context == EXPAND_FILES)
3494 xp->xp_context = EXPAND_DIRECTORIES;
3495 break;
3496 case CMD_help:
3497 xp->xp_context = EXPAND_HELP;
3498 xp->xp_pattern = arg;
3499 break;
3500
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003501 /* Command modifiers: return the argument.
3502 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003504 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 case CMD_belowright:
3506 case CMD_botright:
3507 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003508 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003510 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 case CMD_folddoclosed:
3512 case CMD_folddoopen:
3513 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003514 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 case CMD_keepjumps:
3516 case CMD_keepmarks:
3517 case CMD_leftabove:
3518 case CMD_lockmarks:
3519 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003520 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003522 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 case CMD_topleft:
3524 case CMD_verbose:
3525 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003526 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 return arg;
3528
Bram Moolenaar4f688582007-07-24 12:34:30 +00003529#ifdef FEAT_CMDL_COMPL
3530# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 case CMD_match:
3532 if (*arg == NUL || !ends_excmd(*arg))
3533 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003534 /* also complete "None" */
3535 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 arg = skipwhite(skiptowhite(arg));
3537 if (*arg != NUL)
3538 {
3539 xp->xp_context = EXPAND_NOTHING;
3540 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3541 }
3542 }
3543 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546/*
3547 * All completion for the +cmdline_compl feature goes here.
3548 */
3549
3550# ifdef FEAT_USR_CMDS
3551 case CMD_command:
3552 /* Check for attributes */
3553 while (*arg == '-')
3554 {
3555 arg++; /* Skip "-" */
3556 p = skiptowhite(arg);
3557 if (*p == NUL)
3558 {
3559 /* Cursor is still in the attribute */
3560 p = vim_strchr(arg, '=');
3561 if (p == NULL)
3562 {
3563 /* No "=", so complete attribute names */
3564 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3565 xp->xp_pattern = arg;
3566 return NULL;
3567 }
3568
3569 /* For the -complete and -nargs attributes, we complete
3570 * their arguments as well.
3571 */
3572 if (STRNICMP(arg, "complete", p - arg) == 0)
3573 {
3574 xp->xp_context = EXPAND_USER_COMPLETE;
3575 xp->xp_pattern = p + 1;
3576 return NULL;
3577 }
3578 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3579 {
3580 xp->xp_context = EXPAND_USER_NARGS;
3581 xp->xp_pattern = p + 1;
3582 return NULL;
3583 }
3584 return NULL;
3585 }
3586 arg = skipwhite(p);
3587 }
3588
3589 /* After the attributes comes the new command name */
3590 p = skiptowhite(arg);
3591 if (*p == NUL)
3592 {
3593 xp->xp_context = EXPAND_USER_COMMANDS;
3594 xp->xp_pattern = arg;
3595 break;
3596 }
3597
3598 /* And finally comes a normal command */
3599 return skipwhite(p);
3600
3601 case CMD_delcommand:
3602 xp->xp_context = EXPAND_USER_COMMANDS;
3603 xp->xp_pattern = arg;
3604 break;
3605# endif
3606
3607 case CMD_global:
3608 case CMD_vglobal:
3609 delim = *arg; /* get the delimiter */
3610 if (delim)
3611 ++arg; /* skip delimiter if there is one */
3612
3613 while (arg[0] != NUL && arg[0] != delim)
3614 {
3615 if (arg[0] == '\\' && arg[1] != NUL)
3616 ++arg;
3617 ++arg;
3618 }
3619 if (arg[0] != NUL)
3620 return arg + 1;
3621 break;
3622 case CMD_and:
3623 case CMD_substitute:
3624 delim = *arg;
3625 if (delim)
3626 {
3627 /* skip "from" part */
3628 ++arg;
3629 arg = skip_regexp(arg, delim, p_magic, NULL);
3630 }
3631 /* skip "to" part */
3632 while (arg[0] != NUL && arg[0] != delim)
3633 {
3634 if (arg[0] == '\\' && arg[1] != NUL)
3635 ++arg;
3636 ++arg;
3637 }
3638 if (arg[0] != NUL) /* skip delimiter */
3639 ++arg;
3640 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3641 ++arg;
3642 if (arg[0] != NUL)
3643 return arg;
3644 break;
3645 case CMD_isearch:
3646 case CMD_dsearch:
3647 case CMD_ilist:
3648 case CMD_dlist:
3649 case CMD_ijump:
3650 case CMD_psearch:
3651 case CMD_djump:
3652 case CMD_isplit:
3653 case CMD_dsplit:
3654 arg = skipwhite(skipdigits(arg)); /* skip count */
3655 if (*arg == '/') /* Match regexp, not just whole words */
3656 {
3657 for (++arg; *arg && *arg != '/'; arg++)
3658 if (*arg == '\\' && arg[1] != NUL)
3659 arg++;
3660 if (*arg)
3661 {
3662 arg = skipwhite(arg + 1);
3663
3664 /* Check for trailing illegal characters */
3665 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3666 xp->xp_context = EXPAND_NOTHING;
3667 else
3668 return arg;
3669 }
3670 }
3671 break;
3672#ifdef FEAT_AUTOCMD
3673 case CMD_autocmd:
3674 return set_context_in_autocmd(xp, arg, FALSE);
3675
3676 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003677 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 return set_context_in_autocmd(xp, arg, TRUE);
3679#endif
3680 case CMD_set:
3681 set_context_in_set_cmd(xp, arg, 0);
3682 break;
3683 case CMD_setglobal:
3684 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3685 break;
3686 case CMD_setlocal:
3687 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3688 break;
3689 case CMD_tag:
3690 case CMD_stag:
3691 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003692 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 case CMD_tselect:
3694 case CMD_stselect:
3695 case CMD_ptselect:
3696 case CMD_tjump:
3697 case CMD_stjump:
3698 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003699 if (*p_wop != NUL)
3700 xp->xp_context = EXPAND_TAGS_LISTFILES;
3701 else
3702 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 xp->xp_pattern = arg;
3704 break;
3705 case CMD_augroup:
3706 xp->xp_context = EXPAND_AUGROUP;
3707 xp->xp_pattern = arg;
3708 break;
3709#ifdef FEAT_SYN_HL
3710 case CMD_syntax:
3711 set_context_in_syntax_cmd(xp, arg);
3712 break;
3713#endif
3714#ifdef FEAT_EVAL
3715 case CMD_let:
3716 case CMD_if:
3717 case CMD_elseif:
3718 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003719 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 case CMD_echo:
3721 case CMD_echon:
3722 case CMD_execute:
3723 case CMD_echomsg:
3724 case CMD_echoerr:
3725 case CMD_call:
3726 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003727 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 break;
3729
3730 case CMD_unlet:
3731 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3732 arg = xp->xp_pattern + 1;
3733 xp->xp_context = EXPAND_USER_VARS;
3734 xp->xp_pattern = arg;
3735 break;
3736
3737 case CMD_function:
3738 case CMD_delfunction:
3739 xp->xp_context = EXPAND_USER_FUNC;
3740 xp->xp_pattern = arg;
3741 break;
3742
3743 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003744 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 break;
3746#endif
3747 case CMD_highlight:
3748 set_context_in_highlight_cmd(xp, arg);
3749 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003750#ifdef FEAT_CSCOPE
3751 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003752 case CMD_lcscope:
3753 case CMD_scscope:
3754 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003755 break;
3756#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003757#ifdef FEAT_SIGNS
3758 case CMD_sign:
3759 set_context_in_sign_cmd(xp, arg);
3760 break;
3761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#ifdef FEAT_LISTCMDS
3763 case CMD_bdelete:
3764 case CMD_bwipeout:
3765 case CMD_bunload:
3766 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3767 arg = xp->xp_pattern + 1;
3768 /*FALLTHROUGH*/
3769 case CMD_buffer:
3770 case CMD_sbuffer:
3771 case CMD_checktime:
3772 xp->xp_context = EXPAND_BUFFERS;
3773 xp->xp_pattern = arg;
3774 break;
3775#endif
3776#ifdef FEAT_USR_CMDS
3777 case CMD_USER:
3778 case CMD_USER_BUF:
3779 if (compl != EXPAND_NOTHING)
3780 {
3781 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003782 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
3784# ifdef FEAT_MENU
3785 if (compl == EXPAND_MENUS)
3786 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3787# endif
3788 if (compl == EXPAND_COMMANDS)
3789 return arg;
3790 if (compl == EXPAND_MAPPINGS)
3791 return set_context_in_map_cmd(xp, (char_u *)"map",
3792 arg, forceit, FALSE, FALSE, CMD_map);
3793 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3794 arg = xp->xp_pattern + 1;
3795 xp->xp_pattern = arg;
3796 }
3797 xp->xp_context = compl;
3798 }
3799 break;
3800#endif
3801 case CMD_map: case CMD_noremap:
3802 case CMD_nmap: case CMD_nnoremap:
3803 case CMD_vmap: case CMD_vnoremap:
3804 case CMD_omap: case CMD_onoremap:
3805 case CMD_imap: case CMD_inoremap:
3806 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003807 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003809 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 case CMD_unmap:
3811 case CMD_nunmap:
3812 case CMD_vunmap:
3813 case CMD_ounmap:
3814 case CMD_iunmap:
3815 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003816 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003818 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case CMD_abbreviate: case CMD_noreabbrev:
3820 case CMD_cabbrev: case CMD_cnoreabbrev:
3821 case CMD_iabbrev: case CMD_inoreabbrev:
3822 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003823 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_unabbreviate:
3825 case CMD_cunabbrev:
3826 case CMD_iunabbrev:
3827 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003828 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#ifdef FEAT_MENU
3830 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3831 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3832 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3833 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3834 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3835 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3836 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3837 case CMD_tmenu: case CMD_tunmenu:
3838 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3839 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3840#endif
3841
3842 case CMD_colorscheme:
3843 xp->xp_context = EXPAND_COLORS;
3844 xp->xp_pattern = arg;
3845 break;
3846
3847 case CMD_compiler:
3848 xp->xp_context = EXPAND_COMPILER;
3849 xp->xp_pattern = arg;
3850 break;
3851
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003852 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003853 xp->xp_context = EXPAND_OWNSYNTAX;
3854 xp->xp_pattern = arg;
3855 break;
3856
3857 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003858 xp->xp_context = EXPAND_FILETYPE;
3859 xp->xp_pattern = arg;
3860 break;
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3863 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3864 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003865 p = skiptowhite(arg);
3866 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868 xp->xp_context = EXPAND_LANGUAGE;
3869 xp->xp_pattern = arg;
3870 }
3871 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003872 {
3873 if ( STRNCMP(arg, "messages", p - arg) == 0
3874 || STRNCMP(arg, "ctype", p - arg) == 0
3875 || STRNCMP(arg, "time", p - arg) == 0)
3876 {
3877 xp->xp_context = EXPAND_LOCALES;
3878 xp->xp_pattern = skipwhite(p);
3879 }
3880 else
3881 xp->xp_context = EXPAND_NOTHING;
3882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 break;
3884#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003885#if defined(FEAT_PROFILE)
3886 case CMD_profile:
3887 set_context_in_profile_cmd(xp, arg);
3888 break;
3889#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003890 case CMD_behave:
3891 xp->xp_context = EXPAND_BEHAVE;
3892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
3894#endif /* FEAT_CMDL_COMPL */
3895
3896 default:
3897 break;
3898 }
3899 return NULL;
3900}
3901
3902/*
3903 * skip a range specifier of the form: addr [,addr] [;addr] ..
3904 *
3905 * Backslashed delimiters after / or ? will be skipped, and commands will
3906 * not be expanded between /'s and ?'s or after "'".
3907 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003908 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 * Returns the "cmd" pointer advanced to beyond the range.
3910 */
3911 char_u *
3912skip_range(cmd, ctx)
3913 char_u *cmd;
3914 int *ctx; /* pointer to xp_context or NULL */
3915{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003916 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaardf177f62005-02-22 08:39:57 +00003918 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920 if (*cmd == '\'')
3921 {
3922 if (*++cmd == NUL && ctx != NULL)
3923 *ctx = EXPAND_NOTHING;
3924 }
3925 else if (*cmd == '/' || *cmd == '?')
3926 {
3927 delim = *cmd++;
3928 while (*cmd != NUL && *cmd != delim)
3929 if (*cmd++ == '\\' && *cmd != NUL)
3930 ++cmd;
3931 if (*cmd == NUL && ctx != NULL)
3932 *ctx = EXPAND_NOTHING;
3933 }
3934 if (*cmd != NUL)
3935 ++cmd;
3936 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003937
3938 /* Skip ":" and white space. */
3939 while (*cmd == ':')
3940 cmd = skipwhite(cmd + 1);
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 return cmd;
3943}
3944
3945/*
3946 * get a single EX address
3947 *
3948 * Set ptr to the next character after the part that was interpreted.
3949 * Set ptr to NULL when an error is encountered.
3950 *
3951 * Return MAXLNUM when no Ex address was found.
3952 */
3953 static linenr_T
3954get_address(ptr, skip, to_other_file)
3955 char_u **ptr;
3956 int skip; /* only skip the address, don't use it */
3957 int to_other_file; /* flag: may jump to other file */
3958{
3959 int c;
3960 int i;
3961 long n;
3962 char_u *cmd;
3963 pos_T pos;
3964 pos_T *fp;
3965 linenr_T lnum;
3966
3967 cmd = skipwhite(*ptr);
3968 lnum = MAXLNUM;
3969 do
3970 {
3971 switch (*cmd)
3972 {
3973 case '.': /* '.' - Cursor position */
3974 ++cmd;
3975 lnum = curwin->w_cursor.lnum;
3976 break;
3977
3978 case '$': /* '$' - last line */
3979 ++cmd;
3980 lnum = curbuf->b_ml.ml_line_count;
3981 break;
3982
3983 case '\'': /* ''' - mark */
3984 if (*++cmd == NUL)
3985 {
3986 cmd = NULL;
3987 goto error;
3988 }
3989 if (skip)
3990 ++cmd;
3991 else
3992 {
3993 /* Only accept a mark in another file when it is
3994 * used by itself: ":'M". */
3995 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3996 ++cmd;
3997 if (fp == (pos_T *)-1)
3998 /* Jumped to another file. */
3999 lnum = curwin->w_cursor.lnum;
4000 else
4001 {
4002 if (check_mark(fp) == FAIL)
4003 {
4004 cmd = NULL;
4005 goto error;
4006 }
4007 lnum = fp->lnum;
4008 }
4009 }
4010 break;
4011
4012 case '/':
4013 case '?': /* '/' or '?' - search */
4014 c = *cmd++;
4015 if (skip) /* skip "/pat/" */
4016 {
4017 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4018 if (*cmd == c)
4019 ++cmd;
4020 }
4021 else
4022 {
4023 pos = curwin->w_cursor; /* save curwin->w_cursor */
4024 /*
4025 * When '/' or '?' follows another address, start
4026 * from there.
4027 */
4028 if (lnum != MAXLNUM)
4029 curwin->w_cursor.lnum = lnum;
4030 /*
4031 * Start a forward search at the end of the line.
4032 * Start a backward search at the start of the line.
4033 * This makes sure we never match in the current
4034 * line, and can match anywhere in the
4035 * next/previous line.
4036 */
4037 if (c == '/')
4038 curwin->w_cursor.col = MAXCOL;
4039 else
4040 curwin->w_cursor.col = 0;
4041 searchcmdlen = 0;
4042 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004043 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
4045 curwin->w_cursor = pos;
4046 cmd = NULL;
4047 goto error;
4048 }
4049 lnum = curwin->w_cursor.lnum;
4050 curwin->w_cursor = pos;
4051 /* adjust command string pointer */
4052 cmd += searchcmdlen;
4053 }
4054 break;
4055
4056 case '\\': /* "\?", "\/" or "\&", repeat search */
4057 ++cmd;
4058 if (*cmd == '&')
4059 i = RE_SUBST;
4060 else if (*cmd == '?' || *cmd == '/')
4061 i = RE_SEARCH;
4062 else
4063 {
4064 EMSG(_(e_backslash));
4065 cmd = NULL;
4066 goto error;
4067 }
4068
4069 if (!skip)
4070 {
4071 /*
4072 * When search follows another address, start from
4073 * there.
4074 */
4075 if (lnum != MAXLNUM)
4076 pos.lnum = lnum;
4077 else
4078 pos.lnum = curwin->w_cursor.lnum;
4079
4080 /*
4081 * Start the search just like for the above
4082 * do_search().
4083 */
4084 if (*cmd != '?')
4085 pos.col = MAXCOL;
4086 else
4087 pos.col = 0;
4088 if (searchit(curwin, curbuf, &pos,
4089 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004090 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004091 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 lnum = pos.lnum;
4093 else
4094 {
4095 cmd = NULL;
4096 goto error;
4097 }
4098 }
4099 ++cmd;
4100 break;
4101
4102 default:
4103 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4104 lnum = getdigits(&cmd);
4105 }
4106
4107 for (;;)
4108 {
4109 cmd = skipwhite(cmd);
4110 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4111 break;
4112
4113 if (lnum == MAXLNUM)
4114 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4115 if (VIM_ISDIGIT(*cmd))
4116 i = '+'; /* "number" is same as "+number" */
4117 else
4118 i = *cmd++;
4119 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4120 n = 1;
4121 else
4122 n = getdigits(&cmd);
4123 if (i == '-')
4124 lnum -= n;
4125 else
4126 lnum += n;
4127 }
4128 } while (*cmd == '/' || *cmd == '?');
4129
4130error:
4131 *ptr = cmd;
4132 return lnum;
4133}
4134
4135/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004136 * Get flags from an Ex command argument.
4137 */
4138 static void
4139get_flags(eap)
4140 exarg_T *eap;
4141{
4142 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4143 {
4144 if (*eap->arg == 'l')
4145 eap->flags |= EXFLAG_LIST;
4146 else if (*eap->arg == 'p')
4147 eap->flags |= EXFLAG_PRINT;
4148 else
4149 eap->flags |= EXFLAG_NR;
4150 eap->arg = skipwhite(eap->arg + 1);
4151 }
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Function called for command which is Not Implemented. NI!
4156 */
4157 void
4158ex_ni(eap)
4159 exarg_T *eap;
4160{
4161 if (!eap->skip)
4162 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4163}
4164
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004165#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166/*
4167 * Function called for script command which is Not Implemented. NI!
4168 * Skips over ":perl <<EOF" constructs.
4169 */
4170 static void
4171ex_script_ni(eap)
4172 exarg_T *eap;
4173{
4174 if (!eap->skip)
4175 ex_ni(eap);
4176 else
4177 vim_free(script_get(eap, eap->arg));
4178}
4179#endif
4180
4181/*
4182 * Check range in Ex command for validity.
4183 * Return NULL when valid, error message when invalid.
4184 */
4185 static char_u *
4186invalid_range(eap)
4187 exarg_T *eap;
4188{
4189 if ( eap->line1 < 0
4190 || eap->line2 < 0
4191 || eap->line1 > eap->line2
4192 || ((eap->argt & RANGE)
4193 && !(eap->argt & NOTADR)
4194 && eap->line2 > curbuf->b_ml.ml_line_count
4195#ifdef FEAT_DIFF
4196 + (eap->cmdidx == CMD_diffget)
4197#endif
4198 ))
4199 return (char_u *)_(e_invrange);
4200 return NULL;
4201}
4202
4203/*
4204 * Correct the range for zero line number, if required.
4205 */
4206 static void
4207correct_range(eap)
4208 exarg_T *eap;
4209{
4210 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4211 {
4212 if (eap->line1 == 0)
4213 eap->line1 = 1;
4214 if (eap->line2 == 0)
4215 eap->line2 = 1;
4216 }
4217}
4218
Bram Moolenaar748bf032005-02-02 23:04:36 +00004219#ifdef FEAT_QUICKFIX
4220static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4221
4222/*
4223 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4224 * pattern. Otherwise return eap->arg.
4225 */
4226 static char_u *
4227skip_grep_pat(eap)
4228 exarg_T *eap;
4229{
4230 char_u *p = eap->arg;
4231
Bram Moolenaara37420f2006-02-04 22:37:47 +00004232 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4233 || eap->cmdidx == CMD_vimgrepadd
4234 || eap->cmdidx == CMD_lvimgrepadd
4235 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004236 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004238 if (p == NULL)
4239 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004240 }
4241 return p;
4242}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004243
4244/*
4245 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4246 * in the command line, so that things like % get expanded.
4247 */
4248 static char_u *
4249replace_makeprg(eap, p, cmdlinep)
4250 exarg_T *eap;
4251 char_u *p;
4252 char_u **cmdlinep;
4253{
4254 char_u *new_cmdline;
4255 char_u *program;
4256 char_u *pos;
4257 char_u *ptr;
4258 int len;
4259 int i;
4260
4261 /*
4262 * Don't do it when ":vimgrep" is used for ":grep".
4263 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004264 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4265 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4266 || eap->cmdidx == CMD_grepadd
4267 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004268 && !grep_internal(eap->cmdidx))
4269 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004270 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4271 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004272 {
4273 if (*curbuf->b_p_gp == NUL)
4274 program = p_gp;
4275 else
4276 program = curbuf->b_p_gp;
4277 }
4278 else
4279 {
4280 if (*curbuf->b_p_mp == NUL)
4281 program = p_mp;
4282 else
4283 program = curbuf->b_p_mp;
4284 }
4285
4286 p = skipwhite(p);
4287
4288 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4289 {
4290 /* replace $* by given arguments */
4291 i = 1;
4292 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4293 ++i;
4294 len = (int)STRLEN(p);
4295 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4296 if (new_cmdline == NULL)
4297 return NULL; /* out of memory */
4298 ptr = new_cmdline;
4299 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4300 {
4301 i = (int)(pos - program);
4302 STRNCPY(ptr, program, i);
4303 STRCPY(ptr += i, p);
4304 ptr += len;
4305 program = pos + 2;
4306 }
4307 STRCPY(ptr, program);
4308 }
4309 else
4310 {
4311 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4312 if (new_cmdline == NULL)
4313 return NULL; /* out of memory */
4314 STRCPY(new_cmdline, program);
4315 STRCAT(new_cmdline, " ");
4316 STRCAT(new_cmdline, p);
4317 }
4318 msg_make(p);
4319
4320 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4321 vim_free(*cmdlinep);
4322 *cmdlinep = new_cmdline;
4323 p = new_cmdline;
4324 }
4325 return p;
4326}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004327#endif
4328
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329/*
4330 * Expand file name in Ex command argument.
4331 * Return FAIL for failure, OK otherwise.
4332 */
4333 int
4334expand_filename(eap, cmdlinep, errormsgp)
4335 exarg_T *eap;
4336 char_u **cmdlinep;
4337 char_u **errormsgp;
4338{
4339 int has_wildcards; /* need to expand wildcards */
4340 char_u *repl;
4341 int srclen;
4342 char_u *p;
4343 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004344 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar748bf032005-02-02 23:04:36 +00004346#ifdef FEAT_QUICKFIX
4347 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4348 p = skip_grep_pat(eap);
4349#else
4350 p = eap->arg;
4351#endif
4352
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 /*
4354 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4355 * the file name contains a wildcard it should not cause expanding.
4356 * (it will be expanded anyway if there is a wildcard before replacing).
4357 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004358 has_wildcards = mch_has_wildcard(p);
4359 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004361#ifdef FEAT_EVAL
4362 /* Skip over `=expr`, wildcards in it are not expanded. */
4363 if (p[0] == '`' && p[1] == '=')
4364 {
4365 p += 2;
4366 (void)skip_expr(&p);
4367 if (*p == '`')
4368 ++p;
4369 continue;
4370 }
4371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 /*
4373 * Quick check if this cannot be the start of a special string.
4374 * Also removes backslash before '%', '#' and '<'.
4375 */
4376 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4377 {
4378 ++p;
4379 continue;
4380 }
4381
4382 /*
4383 * Try to find a match at this position.
4384 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004385 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4386 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (*errormsgp != NULL) /* error detected */
4388 return FAIL;
4389 if (repl == NULL) /* no match found */
4390 {
4391 p += srclen;
4392 continue;
4393 }
4394
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004395 /* Wildcards won't be expanded below, the replacement is taken
4396 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4397 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4398 {
4399 char_u *l = repl;
4400
4401 repl = expand_env_save(repl);
4402 vim_free(l);
4403 }
4404
Bram Moolenaar63b92542007-03-27 14:57:09 +00004405 /* Need to escape white space et al. with a backslash.
4406 * Don't do this for:
4407 * - replacement that already has been escaped: "##"
4408 * - shell commands (may have to use quotes instead).
4409 * - non-unix systems when there is a single argument (spaces don't
4410 * separate arguments then).
4411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004413 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 && eap->cmdidx != CMD_bang
4415 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004416 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004418 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004420 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421#ifndef UNIX
4422 && !(eap->argt & NOSPC)
4423#endif
4424 )
4425 {
4426 char_u *l;
4427#ifdef BACKSLASH_IN_FILENAME
4428 /* Don't escape a backslash here, because rem_backslash() doesn't
4429 * remove it later. */
4430 static char_u *nobslash = (char_u *)" \t\"|";
4431# define ESCAPE_CHARS nobslash
4432#else
4433# define ESCAPE_CHARS escape_chars
4434#endif
4435
4436 for (l = repl; *l; ++l)
4437 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4438 {
4439 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4440 if (l != NULL)
4441 {
4442 vim_free(repl);
4443 repl = l;
4444 }
4445 break;
4446 }
4447 }
4448
4449 /* For a shell command a '!' must be escaped. */
4450 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004451 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 char_u *l;
4454
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004455 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if (l != NULL)
4457 {
4458 vim_free(repl);
4459 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004460 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (strstr((char *)p_sh, "sh") != NULL)
4462 {
4463 l = vim_strsave_escaped(repl, (char_u *)"!");
4464 if (l != NULL)
4465 {
4466 vim_free(repl);
4467 repl = l;
4468 }
4469 }
4470 }
4471 }
4472
4473 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4474 vim_free(repl);
4475 if (p == NULL)
4476 return FAIL;
4477 }
4478
4479 /*
4480 * One file argument: Expand wildcards.
4481 * Don't do this with ":r !command" or ":w !command".
4482 */
4483 if ((eap->argt & NOSPC) && !eap->usefilter)
4484 {
4485 /*
4486 * May do this twice:
4487 * 1. Replace environment variables.
4488 * 2. Replace any other wildcards, remove backslashes.
4489 */
4490 for (n = 1; n <= 2; ++n)
4491 {
4492 if (n == 2)
4493 {
4494#ifdef UNIX
4495 /*
4496 * Only for Unix we check for more than one file name.
4497 * For other systems spaces are considered to be part
4498 * of the file name.
4499 * Only check here if there is no wildcard, otherwise
4500 * ExpandOne() will check for errors. This allows
4501 * ":e `ls ve*.c`" on Unix.
4502 */
4503 if (!has_wildcards)
4504 for (p = eap->arg; *p; ++p)
4505 {
4506 /* skip escaped characters */
4507 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4508 ++p;
4509 else if (vim_iswhite(*p))
4510 {
4511 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4512 return FAIL;
4513 }
4514 }
4515#endif
4516
4517 /*
4518 * Halve the number of backslashes (this is Vi compatible).
4519 * For Unix and OS/2, when wildcards are expanded, this is
4520 * done by ExpandOne() below.
4521 */
4522#if defined(UNIX) || defined(OS2)
4523 if (!has_wildcards)
4524#endif
4525 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
4528 if (has_wildcards)
4529 {
4530 if (n == 1)
4531 {
4532 /*
4533 * First loop: May expand environment variables. This
4534 * can be done much faster with expand_env() than with
4535 * something else (e.g., calling a shell).
4536 * After expanding environment variables, check again
4537 * if there are still wildcards present.
4538 */
4539 if (vim_strchr(eap->arg, '$') != NULL
4540 || vim_strchr(eap->arg, '~') != NULL)
4541 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004542 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004543 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 has_wildcards = mch_has_wildcard(NameBuff);
4545 p = NameBuff;
4546 }
4547 else
4548 p = NULL;
4549 }
4550 else /* n == 2 */
4551 {
4552 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004553 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 ExpandInit(&xpc);
4556 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004557 if (p_wic)
4558 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004560 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (p == NULL)
4562 return FAIL;
4563 }
4564 if (p != NULL)
4565 {
4566 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4567 p, cmdlinep);
4568 if (n == 2) /* p came from ExpandOne() */
4569 vim_free(p);
4570 }
4571 }
4572 }
4573 }
4574 return OK;
4575}
4576
4577/*
4578 * Replace part of the command line, keeping eap->cmd, eap->arg and
4579 * eap->nextcmd correct.
4580 * "src" points to the part that is to be replaced, of length "srclen".
4581 * "repl" is the replacement string.
4582 * Returns a pointer to the character after the replaced string.
4583 * Returns NULL for failure.
4584 */
4585 static char_u *
4586repl_cmdline(eap, src, srclen, repl, cmdlinep)
4587 exarg_T *eap;
4588 char_u *src;
4589 int srclen;
4590 char_u *repl;
4591 char_u **cmdlinep;
4592{
4593 int len;
4594 int i;
4595 char_u *new_cmdline;
4596
4597 /*
4598 * The new command line is build in new_cmdline[].
4599 * First allocate it.
4600 * Careful: a "+cmd" argument may have been NUL terminated.
4601 */
4602 len = (int)STRLEN(repl);
4603 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004604 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4606 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4607 return NULL; /* out of memory! */
4608
4609 /*
4610 * Copy the stuff before the expanded part.
4611 * Copy the expanded stuff.
4612 * Copy what came after the expanded part.
4613 * Copy the next commands, if there are any.
4614 */
4615 i = (int)(src - *cmdlinep); /* length of part before match */
4616 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 mch_memmove(new_cmdline + i, repl, (size_t)len);
4619 i += len; /* remember the end of the string */
4620 STRCPY(new_cmdline + i, src + srclen);
4621 src = new_cmdline + i; /* remember where to continue */
4622
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004623 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
4625 i = (int)STRLEN(new_cmdline) + 1;
4626 STRCPY(new_cmdline + i, eap->nextcmd);
4627 eap->nextcmd = new_cmdline + i;
4628 }
4629 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4630 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4631 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4632 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4633 vim_free(*cmdlinep);
4634 *cmdlinep = new_cmdline;
4635
4636 return src;
4637}
4638
4639/*
4640 * Check for '|' to separate commands and '"' to start comments.
4641 */
4642 void
4643separate_nextcmd(eap)
4644 exarg_T *eap;
4645{
4646 char_u *p;
4647
Bram Moolenaar86b68352004-12-27 21:59:20 +00004648#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004649 p = skip_grep_pat(eap);
4650#else
4651 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004652#endif
4653
4654 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
4656 if (*p == Ctrl_V)
4657 {
4658 if (eap->argt & (USECTRLV | XFILE))
4659 ++p; /* skip CTRL-V and next char */
4660 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004661 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004662 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (*p == NUL) /* stop at NUL after CTRL-V */
4664 break;
4665 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004666
4667#ifdef FEAT_EVAL
4668 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004670 {
4671 p += 2;
4672 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004673 }
4674#endif
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 /* Check for '"': start of comment or '|': next command */
4677 /* :@" and :*" do not start a comment!
4678 * :redir @" doesn't either. */
4679 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4680 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4681 || p != eap->arg)
4682 && (eap->cmdidx != CMD_redir
4683 || p != eap->arg + 1 || p[-1] != '@'))
4684 || *p == '|' || *p == '\n')
4685 {
4686 /*
4687 * We remove the '\' before the '|', unless USECTRLV is used
4688 * AND 'b' is present in 'cpoptions'.
4689 */
4690 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4691 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4692 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004693 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 --p;
4695 }
4696 else
4697 {
4698 eap->nextcmd = check_nextcmd(p);
4699 *p = NUL;
4700 break;
4701 }
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4706 del_trailing_spaces(eap->arg);
4707}
4708
4709/*
4710 * get + command from ex argument
4711 */
4712 static char_u *
4713getargcmd(argp)
4714 char_u **argp;
4715{
4716 char_u *arg = *argp;
4717 char_u *command = NULL;
4718
4719 if (*arg == '+') /* +[command] */
4720 {
4721 ++arg;
4722 if (vim_isspace(*arg))
4723 command = dollar_command;
4724 else
4725 {
4726 command = arg;
4727 arg = skip_cmd_arg(command, TRUE);
4728 if (*arg != NUL)
4729 *arg++ = NUL; /* terminate command with NUL */
4730 }
4731
4732 arg = skipwhite(arg); /* skip over spaces */
4733 *argp = arg;
4734 }
4735 return command;
4736}
4737
4738/*
4739 * Find end of "+command" argument. Skip over "\ " and "\\".
4740 */
4741 static char_u *
4742skip_cmd_arg(p, rembs)
4743 char_u *p;
4744 int rembs; /* TRUE to halve the number of backslashes */
4745{
4746 while (*p && !vim_isspace(*p))
4747 {
4748 if (*p == '\\' && p[1] != NUL)
4749 {
4750 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004751 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 else
4753 ++p;
4754 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004755 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 return p;
4758}
4759
4760/*
4761 * Get "++opt=arg" argument.
4762 * Return FAIL or OK.
4763 */
4764 static int
4765getargopt(eap)
4766 exarg_T *eap;
4767{
4768 char_u *arg = eap->arg + 2;
4769 int *pp = NULL;
4770#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004771 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 char_u *p;
4773#endif
4774
4775 /* ":edit ++[no]bin[ary] file" */
4776 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4777 {
4778 if (*arg == 'n')
4779 {
4780 arg += 2;
4781 eap->force_bin = FORCE_NOBIN;
4782 }
4783 else
4784 eap->force_bin = FORCE_BIN;
4785 if (!checkforcmd(&arg, "binary", 3))
4786 return FAIL;
4787 eap->arg = skipwhite(arg);
4788 return OK;
4789 }
4790
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004791 /* ":read ++edit file" */
4792 if (STRNCMP(arg, "edit", 4) == 0)
4793 {
4794 eap->read_edit = TRUE;
4795 eap->arg = skipwhite(arg + 4);
4796 return OK;
4797 }
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (STRNCMP(arg, "ff", 2) == 0)
4800 {
4801 arg += 2;
4802 pp = &eap->force_ff;
4803 }
4804 else if (STRNCMP(arg, "fileformat", 10) == 0)
4805 {
4806 arg += 10;
4807 pp = &eap->force_ff;
4808 }
4809#ifdef FEAT_MBYTE
4810 else if (STRNCMP(arg, "enc", 3) == 0)
4811 {
4812 arg += 3;
4813 pp = &eap->force_enc;
4814 }
4815 else if (STRNCMP(arg, "encoding", 8) == 0)
4816 {
4817 arg += 8;
4818 pp = &eap->force_enc;
4819 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004820 else if (STRNCMP(arg, "bad", 3) == 0)
4821 {
4822 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004823 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#endif
4826
4827 if (pp == NULL || *arg != '=')
4828 return FAIL;
4829
4830 ++arg;
4831 *pp = (int)(arg - eap->cmd);
4832 arg = skip_cmd_arg(arg, FALSE);
4833 eap->arg = skipwhite(arg);
4834 *arg = NUL;
4835
4836#ifdef FEAT_MBYTE
4837 if (pp == &eap->force_ff)
4838 {
4839#endif
4840 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4841 return FAIL;
4842#ifdef FEAT_MBYTE
4843 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004844 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 /* Make 'fileencoding' lower case. */
4847 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4848 *p = TOLOWER_ASC(*p);
4849 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004850 else
4851 {
4852 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4853 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004854 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004855 if (STRICMP(p, "keep") == 0)
4856 eap->bad_char = BAD_KEEP;
4857 else if (STRICMP(p, "drop") == 0)
4858 eap->bad_char = BAD_DROP;
4859 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4860 eap->bad_char = *p;
4861 else
4862 return FAIL;
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
4865
4866 return OK;
4867}
4868
4869/*
4870 * ":abbreviate" and friends.
4871 */
4872 static void
4873ex_abbreviate(eap)
4874 exarg_T *eap;
4875{
4876 do_exmap(eap, TRUE); /* almost the same as mapping */
4877}
4878
4879/*
4880 * ":map" and friends.
4881 */
4882 static void
4883ex_map(eap)
4884 exarg_T *eap;
4885{
4886 /*
4887 * If we are sourcing .exrc or .vimrc in current directory we
4888 * print the mappings for security reasons.
4889 */
4890 if (secure)
4891 {
4892 secure = 2;
4893 msg_outtrans(eap->cmd);
4894 msg_putchar('\n');
4895 }
4896 do_exmap(eap, FALSE);
4897}
4898
4899/*
4900 * ":unmap" and friends.
4901 */
4902 static void
4903ex_unmap(eap)
4904 exarg_T *eap;
4905{
4906 do_exmap(eap, FALSE);
4907}
4908
4909/*
4910 * ":mapclear" and friends.
4911 */
4912 static void
4913ex_mapclear(eap)
4914 exarg_T *eap;
4915{
4916 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4917}
4918
4919/*
4920 * ":abclear" and friends.
4921 */
4922 static void
4923ex_abclear(eap)
4924 exarg_T *eap;
4925{
4926 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4927}
4928
4929#ifdef FEAT_AUTOCMD
4930 static void
4931ex_autocmd(eap)
4932 exarg_T *eap;
4933{
4934 /*
4935 * Disallow auto commands from .exrc and .vimrc in current
4936 * directory for security reasons.
4937 */
4938 if (secure)
4939 {
4940 secure = 2;
4941 eap->errmsg = e_curdir;
4942 }
4943 else if (eap->cmdidx == CMD_autocmd)
4944 do_autocmd(eap->arg, eap->forceit);
4945 else
4946 do_augroup(eap->arg, eap->forceit);
4947}
4948
4949/*
4950 * ":doautocmd": Apply the automatic commands to the current buffer.
4951 */
4952 static void
4953ex_doautocmd(eap)
4954 exarg_T *eap;
4955{
4956 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004957 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958}
4959#endif
4960
4961#ifdef FEAT_LISTCMDS
4962/*
4963 * :[N]bunload[!] [N] [bufname] unload buffer
4964 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4965 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4966 */
4967 static void
4968ex_bunload(eap)
4969 exarg_T *eap;
4970{
4971 eap->errmsg = do_bufdel(
4972 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4973 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4974 : DOBUF_UNLOAD, eap->arg,
4975 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4976}
4977
4978/*
4979 * :[N]buffer [N] to buffer N
4980 * :[N]sbuffer [N] to buffer N
4981 */
4982 static void
4983ex_buffer(eap)
4984 exarg_T *eap;
4985{
4986 if (*eap->arg)
4987 eap->errmsg = e_trailing;
4988 else
4989 {
4990 if (eap->addr_count == 0) /* default is current buffer */
4991 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4992 else
4993 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4994 }
4995}
4996
4997/*
4998 * :[N]bmodified [N] to next mod. buffer
4999 * :[N]sbmodified [N] to next mod. buffer
5000 */
5001 static void
5002ex_bmodified(eap)
5003 exarg_T *eap;
5004{
5005 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5006}
5007
5008/*
5009 * :[N]bnext [N] to next buffer
5010 * :[N]sbnext [N] split and to next buffer
5011 */
5012 static void
5013ex_bnext(eap)
5014 exarg_T *eap;
5015{
5016 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5017}
5018
5019/*
5020 * :[N]bNext [N] to previous buffer
5021 * :[N]bprevious [N] to previous buffer
5022 * :[N]sbNext [N] split and to previous buffer
5023 * :[N]sbprevious [N] split and to previous buffer
5024 */
5025 static void
5026ex_bprevious(eap)
5027 exarg_T *eap;
5028{
5029 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5030}
5031
5032/*
5033 * :brewind to first buffer
5034 * :bfirst to first buffer
5035 * :sbrewind split and to first buffer
5036 * :sbfirst split and to first buffer
5037 */
5038 static void
5039ex_brewind(eap)
5040 exarg_T *eap;
5041{
5042 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5043}
5044
5045/*
5046 * :blast to last buffer
5047 * :sblast split and to last buffer
5048 */
5049 static void
5050ex_blast(eap)
5051 exarg_T *eap;
5052{
5053 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5054}
5055#endif
5056
5057 int
5058ends_excmd(c)
5059 int c;
5060{
5061 return (c == NUL || c == '|' || c == '"' || c == '\n');
5062}
5063
5064#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5065 || defined(PROTO)
5066/*
5067 * Return the next command, after the first '|' or '\n'.
5068 * Return NULL if not found.
5069 */
5070 char_u *
5071find_nextcmd(p)
5072 char_u *p;
5073{
5074 while (*p != '|' && *p != '\n')
5075 {
5076 if (*p == NUL)
5077 return NULL;
5078 ++p;
5079 }
5080 return (p + 1);
5081}
5082#endif
5083
5084/*
5085 * Check if *p is a separator between Ex commands.
5086 * Return NULL if it isn't, (p + 1) if it is.
5087 */
5088 char_u *
5089check_nextcmd(p)
5090 char_u *p;
5091{
5092 p = skipwhite(p);
5093 if (*p == '|' || *p == '\n')
5094 return (p + 1);
5095 else
5096 return NULL;
5097}
5098
5099/*
5100 * - if there are more files to edit
5101 * - and this is the last window
5102 * - and forceit not used
5103 * - and not repeated twice on a row
5104 * return FAIL and give error message if 'message' TRUE
5105 * return OK otherwise
5106 */
5107 static int
5108check_more(message, forceit)
5109 int message; /* when FALSE check only, no messages */
5110 int forceit;
5111{
5112 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5113
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005114 if (!forceit && only_one_window()
5115 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 if (message)
5118 {
5119#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5120 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5121 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005122 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005125 vim_strncpy(buff,
5126 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005127 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005129 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 _("%d more files to edit. Quit anyway?"), n);
5131 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5132 return OK;
5133 return FAIL;
5134 }
5135#endif
5136 if (n == 1)
5137 EMSG(_("E173: 1 more file to edit"));
5138 else
5139 EMSGN(_("E173: %ld more files to edit"), n);
5140 quitmore = 2; /* next try to quit is allowed */
5141 }
5142 return FAIL;
5143 }
5144 return OK;
5145}
5146
5147#ifdef FEAT_CMDL_COMPL
5148/*
5149 * Function given to ExpandGeneric() to obtain the list of command names.
5150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 char_u *
5152get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005153 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 int idx;
5155{
5156 if (idx >= (int)CMD_SIZE)
5157# ifdef FEAT_USR_CMDS
5158 return get_user_command_name(idx);
5159# else
5160 return NULL;
5161# endif
5162 return cmdnames[idx].cmd_name;
5163}
5164#endif
5165
5166#if defined(FEAT_USR_CMDS) || defined(PROTO)
5167static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5168static void uc_list __ARGS((char_u *name, size_t name_len));
5169static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5170static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5171static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len));
5172
5173 static int
5174uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5175 char_u *name;
5176 size_t name_len;
5177 char_u *rep;
5178 long argt;
5179 long def;
5180 int flags;
5181 int compl;
5182 char_u *compl_arg;
5183 int force;
5184{
5185 ucmd_T *cmd = NULL;
5186 char_u *p;
5187 int i;
5188 int cmp = 1;
5189 char_u *rep_buf = NULL;
5190 garray_T *gap;
5191
Bram Moolenaar9c102382006-05-03 21:26:49 +00005192 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (rep_buf == NULL)
5194 {
5195 /* Can't replace termcodes - try using the string as is */
5196 rep_buf = vim_strsave(rep);
5197
5198 /* Give up if out of memory */
5199 if (rep_buf == NULL)
5200 return FAIL;
5201 }
5202
5203 /* get address of growarray: global or in curbuf */
5204 if (flags & UC_BUFFER)
5205 {
5206 gap = &curbuf->b_ucmds;
5207 if (gap->ga_itemsize == 0)
5208 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5209 }
5210 else
5211 gap = &ucmds;
5212
5213 /* Search for the command in the already defined commands. */
5214 for (i = 0; i < gap->ga_len; ++i)
5215 {
5216 size_t len;
5217
5218 cmd = USER_CMD_GA(gap, i);
5219 len = STRLEN(cmd->uc_name);
5220 cmp = STRNCMP(name, cmd->uc_name, name_len);
5221 if (cmp == 0)
5222 {
5223 if (name_len < len)
5224 cmp = -1;
5225 else if (name_len > len)
5226 cmp = 1;
5227 }
5228
5229 if (cmp == 0)
5230 {
5231 if (!force)
5232 {
5233 EMSG(_("E174: Command already exists: add ! to replace it"));
5234 goto fail;
5235 }
5236
5237 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005238 cmd->uc_rep = NULL;
5239#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5240 vim_free(cmd->uc_compl_arg);
5241 cmd->uc_compl_arg = NULL;
5242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 break;
5244 }
5245
5246 /* Stop as soon as we pass the name to add */
5247 if (cmp < 0)
5248 break;
5249 }
5250
5251 /* Extend the array unless we're replacing an existing command */
5252 if (cmp != 0)
5253 {
5254 if (ga_grow(gap, 1) != OK)
5255 goto fail;
5256 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5257 goto fail;
5258
5259 cmd = USER_CMD_GA(gap, i);
5260 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5261
5262 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 cmd->uc_name = p;
5265 }
5266
5267 cmd->uc_rep = rep_buf;
5268 cmd->uc_argt = argt;
5269 cmd->uc_def = def;
5270 cmd->uc_compl = compl;
5271#ifdef FEAT_EVAL
5272 cmd->uc_scriptID = current_SID;
5273# ifdef FEAT_CMDL_COMPL
5274 cmd->uc_compl_arg = compl_arg;
5275# endif
5276#endif
5277
5278 return OK;
5279
5280fail:
5281 vim_free(rep_buf);
5282#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5283 vim_free(compl_arg);
5284#endif
5285 return FAIL;
5286}
5287
5288/*
5289 * List of names for completion for ":command" with the EXPAND_ flag.
5290 * Must be alphabetical for completion.
5291 */
5292static struct
5293{
5294 int expand;
5295 char *name;
5296} command_complete[] =
5297{
5298 {EXPAND_AUGROUP, "augroup"},
5299 {EXPAND_BUFFERS, "buffer"},
5300 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005301#if defined(FEAT_CSCOPE)
5302 {EXPAND_CSCOPE, "cscope"},
5303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5305 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005306 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#endif
5308 {EXPAND_DIRECTORIES, "dir"},
5309 {EXPAND_ENV_VARS, "environment"},
5310 {EXPAND_EVENTS, "event"},
5311 {EXPAND_EXPRESSION, "expression"},
5312 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005313 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {EXPAND_FUNCTIONS, "function"},
5315 {EXPAND_HELP, "help"},
5316 {EXPAND_HIGHLIGHT, "highlight"},
5317 {EXPAND_MAPPINGS, "mapping"},
5318 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005319 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005321 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005322#if defined(FEAT_SIGNS)
5323 {EXPAND_SIGN, "sign"},
5324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {EXPAND_TAGS, "tag"},
5326 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5327 {EXPAND_USER_VARS, "var"},
5328 {0, NULL}
5329};
5330
5331 static void
5332uc_list(name, name_len)
5333 char_u *name;
5334 size_t name_len;
5335{
5336 int i, j;
5337 int found = FALSE;
5338 ucmd_T *cmd;
5339 int len;
5340 long a;
5341 garray_T *gap;
5342
5343 gap = &curbuf->b_ucmds;
5344 for (;;)
5345 {
5346 for (i = 0; i < gap->ga_len; ++i)
5347 {
5348 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005349 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 /* Skip commands which don't match the requested prefix */
5352 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5353 continue;
5354
5355 /* Put out the title first time */
5356 if (!found)
5357 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5358 found = TRUE;
5359 msg_putchar('\n');
5360 if (got_int)
5361 break;
5362
5363 /* Special cases */
5364 msg_putchar(a & BANG ? '!' : ' ');
5365 msg_putchar(a & REGSTR ? '"' : ' ');
5366 msg_putchar(gap != &ucmds ? 'b' : ' ');
5367 msg_putchar(' ');
5368
5369 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5370 len = (int)STRLEN(cmd->uc_name) + 4;
5371
5372 do {
5373 msg_putchar(' ');
5374 ++len;
5375 } while (len < 16);
5376
5377 len = 0;
5378
5379 /* Arguments */
5380 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5381 {
5382 case 0: IObuff[len++] = '0'; break;
5383 case (EXTRA): IObuff[len++] = '*'; break;
5384 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5385 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5386 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5387 }
5388
5389 do {
5390 IObuff[len++] = ' ';
5391 } while (len < 5);
5392
5393 /* Range */
5394 if (a & (RANGE|COUNT))
5395 {
5396 if (a & COUNT)
5397 {
5398 /* -count=N */
5399 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5400 len += (int)STRLEN(IObuff + len);
5401 }
5402 else if (a & DFLALL)
5403 IObuff[len++] = '%';
5404 else if (cmd->uc_def >= 0)
5405 {
5406 /* -range=N */
5407 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5408 len += (int)STRLEN(IObuff + len);
5409 }
5410 else
5411 IObuff[len++] = '.';
5412 }
5413
5414 do {
5415 IObuff[len++] = ' ';
5416 } while (len < 11);
5417
5418 /* Completion */
5419 for (j = 0; command_complete[j].expand != 0; ++j)
5420 if (command_complete[j].expand == cmd->uc_compl)
5421 {
5422 STRCPY(IObuff + len, command_complete[j].name);
5423 len += (int)STRLEN(IObuff + len);
5424 break;
5425 }
5426
5427 do {
5428 IObuff[len++] = ' ';
5429 } while (len < 21);
5430
5431 IObuff[len] = '\0';
5432 msg_outtrans(IObuff);
5433
5434 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005435#ifdef FEAT_EVAL
5436 if (p_verbose > 0)
5437 last_set_msg(cmd->uc_scriptID);
5438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 out_flush();
5440 ui_breakcheck();
5441 if (got_int)
5442 break;
5443 }
5444 if (gap == &ucmds || i < gap->ga_len)
5445 break;
5446 gap = &ucmds;
5447 }
5448
5449 if (!found)
5450 MSG(_("No user-defined commands found"));
5451}
5452
5453 static char_u *
5454uc_fun_cmd()
5455{
5456 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5457 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5458 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5459 0xb9, 0x7f, 0};
5460 int i;
5461
5462 for (i = 0; fcmd[i]; ++i)
5463 IObuff[i] = fcmd[i] - 0x40;
5464 IObuff[i] = 0;
5465 return IObuff;
5466}
5467
5468 static int
5469uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5470 char_u *attr;
5471 size_t len;
5472 long *argt;
5473 long *def;
5474 int *flags;
5475 int *compl;
5476 char_u **compl_arg;
5477{
5478 char_u *p;
5479
5480 if (len == 0)
5481 {
5482 EMSG(_("E175: No attribute specified"));
5483 return FAIL;
5484 }
5485
5486 /* First, try the simple attributes (no arguments) */
5487 if (STRNICMP(attr, "bang", len) == 0)
5488 *argt |= BANG;
5489 else if (STRNICMP(attr, "buffer", len) == 0)
5490 *flags |= UC_BUFFER;
5491 else if (STRNICMP(attr, "register", len) == 0)
5492 *argt |= REGSTR;
5493 else if (STRNICMP(attr, "bar", len) == 0)
5494 *argt |= TRLBAR;
5495 else
5496 {
5497 int i;
5498 char_u *val = NULL;
5499 size_t vallen = 0;
5500 size_t attrlen = len;
5501
5502 /* Look for the attribute name - which is the part before any '=' */
5503 for (i = 0; i < (int)len; ++i)
5504 {
5505 if (attr[i] == '=')
5506 {
5507 val = &attr[i + 1];
5508 vallen = len - i - 1;
5509 attrlen = i;
5510 break;
5511 }
5512 }
5513
5514 if (STRNICMP(attr, "nargs", attrlen) == 0)
5515 {
5516 if (vallen == 1)
5517 {
5518 if (*val == '0')
5519 /* Do nothing - this is the default */;
5520 else if (*val == '1')
5521 *argt |= (EXTRA | NOSPC | NEEDARG);
5522 else if (*val == '*')
5523 *argt |= EXTRA;
5524 else if (*val == '?')
5525 *argt |= (EXTRA | NOSPC);
5526 else if (*val == '+')
5527 *argt |= (EXTRA | NEEDARG);
5528 else
5529 goto wrong_nargs;
5530 }
5531 else
5532 {
5533wrong_nargs:
5534 EMSG(_("E176: Invalid number of arguments"));
5535 return FAIL;
5536 }
5537 }
5538 else if (STRNICMP(attr, "range", attrlen) == 0)
5539 {
5540 *argt |= RANGE;
5541 if (vallen == 1 && *val == '%')
5542 *argt |= DFLALL;
5543 else if (val != NULL)
5544 {
5545 p = val;
5546 if (*def >= 0)
5547 {
5548two_count:
5549 EMSG(_("E177: Count cannot be specified twice"));
5550 return FAIL;
5551 }
5552
5553 *def = getdigits(&p);
5554 *argt |= (ZEROR | NOTADR);
5555
5556 if (p != val + vallen || vallen == 0)
5557 {
5558invalid_count:
5559 EMSG(_("E178: Invalid default value for count"));
5560 return FAIL;
5561 }
5562 }
5563 }
5564 else if (STRNICMP(attr, "count", attrlen) == 0)
5565 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005566 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
5568 if (val != NULL)
5569 {
5570 p = val;
5571 if (*def >= 0)
5572 goto two_count;
5573
5574 *def = getdigits(&p);
5575
5576 if (p != val + vallen)
5577 goto invalid_count;
5578 }
5579
5580 if (*def < 0)
5581 *def = 0;
5582 }
5583 else if (STRNICMP(attr, "complete", attrlen) == 0)
5584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 if (val == NULL)
5586 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005587 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 return FAIL;
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005591 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5592 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 else
5596 {
5597 char_u ch = attr[len];
5598 attr[len] = '\0';
5599 EMSG2(_("E181: Invalid attribute: %s"), attr);
5600 attr[len] = ch;
5601 return FAIL;
5602 }
5603 }
5604
5605 return OK;
5606}
5607
Bram Moolenaara850a712009-01-28 14:42:59 +00005608/*
5609 * ":command ..."
5610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 static void
5612ex_command(eap)
5613 exarg_T *eap;
5614{
5615 char_u *name;
5616 char_u *end;
5617 char_u *p;
5618 long argt = 0;
5619 long def = -1;
5620 int flags = 0;
5621 int compl = EXPAND_NOTHING;
5622 char_u *compl_arg = NULL;
5623 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005624 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
5626 p = eap->arg;
5627
5628 /* Check for attributes */
5629 while (*p == '-')
5630 {
5631 ++p;
5632 end = skiptowhite(p);
5633 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5634 == FAIL)
5635 return;
5636 p = skipwhite(end);
5637 }
5638
5639 /* Get the name (if any) and skip to the following argument */
5640 name = p;
5641 if (ASCII_ISALPHA(*p))
5642 while (ASCII_ISALNUM(*p))
5643 ++p;
5644 if (!ends_excmd(*p) && !vim_iswhite(*p))
5645 {
5646 EMSG(_("E182: Invalid command name"));
5647 return;
5648 }
5649 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005650 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652 /* If there is nothing after the name, and no attributes were specified,
5653 * we are listing commands
5654 */
5655 p = skipwhite(end);
5656 if (!has_attr && ends_excmd(*p))
5657 {
5658 uc_list(name, end - name);
5659 }
5660 else if (!ASCII_ISUPPER(*name))
5661 {
5662 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5663 return;
5664 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005665 else if ((name_len == 1 && *name == 'X')
5666 || (name_len <= 4
5667 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5668 {
5669 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5670 return;
5671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else
5673 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5674 eap->forceit);
5675}
5676
5677/*
5678 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * Clear all user commands, global and for current buffer.
5680 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005681 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005683 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 uc_clear(&ucmds);
5686 uc_clear(&curbuf->b_ucmds);
5687}
5688
5689/*
5690 * Clear all user commands for "gap".
5691 */
5692 void
5693uc_clear(gap)
5694 garray_T *gap;
5695{
5696 int i;
5697 ucmd_T *cmd;
5698
5699 for (i = 0; i < gap->ga_len; ++i)
5700 {
5701 cmd = USER_CMD_GA(gap, i);
5702 vim_free(cmd->uc_name);
5703 vim_free(cmd->uc_rep);
5704# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5705 vim_free(cmd->uc_compl_arg);
5706# endif
5707 }
5708 ga_clear(gap);
5709}
5710
5711 static void
5712ex_delcommand(eap)
5713 exarg_T *eap;
5714{
5715 int i = 0;
5716 ucmd_T *cmd = NULL;
5717 int cmp = -1;
5718 garray_T *gap;
5719
5720 gap = &curbuf->b_ucmds;
5721 for (;;)
5722 {
5723 for (i = 0; i < gap->ga_len; ++i)
5724 {
5725 cmd = USER_CMD_GA(gap, i);
5726 cmp = STRCMP(eap->arg, cmd->uc_name);
5727 if (cmp <= 0)
5728 break;
5729 }
5730 if (gap == &ucmds || cmp == 0)
5731 break;
5732 gap = &ucmds;
5733 }
5734
5735 if (cmp != 0)
5736 {
5737 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5738 return;
5739 }
5740
5741 vim_free(cmd->uc_name);
5742 vim_free(cmd->uc_rep);
5743# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5744 vim_free(cmd->uc_compl_arg);
5745# endif
5746
5747 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
5749 if (i < gap->ga_len)
5750 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5751}
5752
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005753/*
5754 * split and quote args for <f-args>
5755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 static char_u *
5757uc_split_args(arg, lenp)
5758 char_u *arg;
5759 size_t *lenp;
5760{
5761 char_u *buf;
5762 char_u *p;
5763 char_u *q;
5764 int len;
5765
5766 /* Precalculate length */
5767 p = arg;
5768 len = 2; /* Initial and final quotes */
5769
5770 while (*p)
5771 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005772 if (p[0] == '\\' && p[1] == '\\')
5773 {
5774 len += 2;
5775 p += 2;
5776 }
5777 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
5779 len += 1;
5780 p += 2;
5781 }
5782 else if (*p == '\\' || *p == '"')
5783 {
5784 len += 2;
5785 p += 1;
5786 }
5787 else if (vim_iswhite(*p))
5788 {
5789 p = skipwhite(p);
5790 if (*p == NUL)
5791 break;
5792 len += 3; /* "," */
5793 }
5794 else
5795 {
5796 ++len;
5797 ++p;
5798 }
5799 }
5800
5801 buf = alloc(len + 1);
5802 if (buf == NULL)
5803 {
5804 *lenp = 0;
5805 return buf;
5806 }
5807
5808 p = arg;
5809 q = buf;
5810 *q++ = '"';
5811 while (*p)
5812 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005813 if (p[0] == '\\' && p[1] == '\\')
5814 {
5815 *q++ = '\\';
5816 *q++ = '\\';
5817 p += 2;
5818 }
5819 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
5821 *q++ = p[1];
5822 p += 2;
5823 }
5824 else if (*p == '\\' || *p == '"')
5825 {
5826 *q++ = '\\';
5827 *q++ = *p++;
5828 }
5829 else if (vim_iswhite(*p))
5830 {
5831 p = skipwhite(p);
5832 if (*p == NUL)
5833 break;
5834 *q++ = '"';
5835 *q++ = ',';
5836 *q++ = '"';
5837 }
5838 else
5839 {
5840 *q++ = *p++;
5841 }
5842 }
5843 *q++ = '"';
5844 *q = 0;
5845
5846 *lenp = len;
5847 return buf;
5848}
5849
5850/*
5851 * Check for a <> code in a user command.
5852 * "code" points to the '<'. "len" the length of the <> (inclusive).
5853 * "buf" is where the result is to be added.
5854 * "split_buf" points to a buffer used for splitting, caller should free it.
5855 * "split_len" is the length of what "split_buf" contains.
5856 * Returns the length of the replacement, which has been added to "buf".
5857 * Returns -1 if there was no match, and only the "<" has been copied.
5858 */
5859 static size_t
5860uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5861 char_u *code;
5862 size_t len;
5863 char_u *buf;
5864 ucmd_T *cmd; /* the user command we're expanding */
5865 exarg_T *eap; /* ex arguments */
5866 char_u **split_buf;
5867 size_t *split_len;
5868{
5869 size_t result = 0;
5870 char_u *p = code + 1;
5871 size_t l = len - 2;
5872 int quote = 0;
5873 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5874 ct_LT, ct_NONE } type = ct_NONE;
5875
5876 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5877 {
5878 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5879 p += 2;
5880 l -= 2;
5881 }
5882
Bram Moolenaar371d5402006-03-20 21:47:49 +00005883 ++l;
5884 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005886 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005888 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005890 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005892 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005894 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005896 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005898 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 type = ct_REGISTER;
5900
5901 switch (type)
5902 {
5903 case ct_ARGS:
5904 /* Simple case first */
5905 if (*eap->arg == NUL)
5906 {
5907 if (quote == 1)
5908 {
5909 result = 2;
5910 if (buf != NULL)
5911 STRCPY(buf, "''");
5912 }
5913 else
5914 result = 0;
5915 break;
5916 }
5917
5918 /* When specified there is a single argument don't split it.
5919 * Works for ":Cmd %" when % is "a b c". */
5920 if ((eap->argt & NOSPC) && quote == 2)
5921 quote = 1;
5922
5923 switch (quote)
5924 {
5925 case 0: /* No quoting, no splitting */
5926 result = STRLEN(eap->arg);
5927 if (buf != NULL)
5928 STRCPY(buf, eap->arg);
5929 break;
5930 case 1: /* Quote, but don't split */
5931 result = STRLEN(eap->arg) + 2;
5932 for (p = eap->arg; *p; ++p)
5933 {
5934 if (*p == '\\' || *p == '"')
5935 ++result;
5936 }
5937
5938 if (buf != NULL)
5939 {
5940 *buf++ = '"';
5941 for (p = eap->arg; *p; ++p)
5942 {
5943 if (*p == '\\' || *p == '"')
5944 *buf++ = '\\';
5945 *buf++ = *p;
5946 }
5947 *buf = '"';
5948 }
5949
5950 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005951 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 /* This is hard, so only do it once, and cache the result */
5953 if (*split_buf == NULL)
5954 *split_buf = uc_split_args(eap->arg, split_len);
5955
5956 result = *split_len;
5957 if (buf != NULL && result != 0)
5958 STRCPY(buf, *split_buf);
5959
5960 break;
5961 }
5962 break;
5963
5964 case ct_BANG:
5965 result = eap->forceit ? 1 : 0;
5966 if (quote)
5967 result += 2;
5968 if (buf != NULL)
5969 {
5970 if (quote)
5971 *buf++ = '"';
5972 if (eap->forceit)
5973 *buf++ = '!';
5974 if (quote)
5975 *buf = '"';
5976 }
5977 break;
5978
5979 case ct_LINE1:
5980 case ct_LINE2:
5981 case ct_COUNT:
5982 {
5983 char num_buf[20];
5984 long num = (type == ct_LINE1) ? eap->line1 :
5985 (type == ct_LINE2) ? eap->line2 :
5986 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5987 size_t num_len;
5988
5989 sprintf(num_buf, "%ld", num);
5990 num_len = STRLEN(num_buf);
5991 result = num_len;
5992
5993 if (quote)
5994 result += 2;
5995
5996 if (buf != NULL)
5997 {
5998 if (quote)
5999 *buf++ = '"';
6000 STRCPY(buf, num_buf);
6001 buf += num_len;
6002 if (quote)
6003 *buf = '"';
6004 }
6005
6006 break;
6007 }
6008
6009 case ct_REGISTER:
6010 result = eap->regname ? 1 : 0;
6011 if (quote)
6012 result += 2;
6013 if (buf != NULL)
6014 {
6015 if (quote)
6016 *buf++ = '\'';
6017 if (eap->regname)
6018 *buf++ = eap->regname;
6019 if (quote)
6020 *buf = '\'';
6021 }
6022 break;
6023
6024 case ct_LT:
6025 result = 1;
6026 if (buf != NULL)
6027 *buf = '<';
6028 break;
6029
6030 default:
6031 /* Not recognized: just copy the '<' and return -1. */
6032 result = (size_t)-1;
6033 if (buf != NULL)
6034 *buf = '<';
6035 break;
6036 }
6037
6038 return result;
6039}
6040
6041 static void
6042do_ucmd(eap)
6043 exarg_T *eap;
6044{
6045 char_u *buf;
6046 char_u *p;
6047 char_u *q;
6048
6049 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006050 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006051 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 size_t len, totlen;
6053
6054 size_t split_len = 0;
6055 char_u *split_buf = NULL;
6056 ucmd_T *cmd;
6057#ifdef FEAT_EVAL
6058 scid_T save_current_SID = current_SID;
6059#endif
6060
6061 if (eap->cmdidx == CMD_USER)
6062 cmd = USER_CMD(eap->useridx);
6063 else
6064 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6065
6066 /*
6067 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006068 * First round: "buf" is NULL, compute length, allocate "buf".
6069 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 */
6071 buf = NULL;
6072 for (;;)
6073 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006074 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006075 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006077
6078 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006080 start = vim_strchr(p, '<');
6081 if (start != NULL)
6082 end = vim_strchr(start + 1, '>');
6083 if (buf != NULL)
6084 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006085 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6086 ;
6087 if (*ksp == K_SPECIAL
6088 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006089 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6090# ifdef FEAT_GUI
6091 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6092# endif
6093 ))
6094 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006095 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006096 * KS_SPECIAL KE_FILLER, like for mappings, but
6097 * do_cmdline() doesn't handle that, so convert it back.
6098 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6099 len = ksp - p;
6100 if (len > 0)
6101 {
6102 mch_memmove(q, p, len);
6103 q += len;
6104 }
6105 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6106 p = ksp + 3;
6107 continue;
6108 }
6109 }
6110
6111 /* break if there no <item> is found */
6112 if (start == NULL || end == NULL)
6113 break;
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* Include the '>' */
6116 ++end;
6117
6118 /* Take everything up to the '<' */
6119 len = start - p;
6120 if (buf == NULL)
6121 totlen += len;
6122 else
6123 {
6124 mch_memmove(q, p, len);
6125 q += len;
6126 }
6127
6128 len = uc_check_code(start, end - start, q, cmd, eap,
6129 &split_buf, &split_len);
6130 if (len == (size_t)-1)
6131 {
6132 /* no match, continue after '<' */
6133 p = start + 1;
6134 len = 1;
6135 }
6136 else
6137 p = end;
6138 if (buf == NULL)
6139 totlen += len;
6140 else
6141 q += len;
6142 }
6143 if (buf != NULL) /* second time here, finished */
6144 {
6145 STRCPY(q, p);
6146 break;
6147 }
6148
6149 totlen += STRLEN(p); /* Add on the trailing characters */
6150 buf = alloc((unsigned)(totlen + 1));
6151 if (buf == NULL)
6152 {
6153 vim_free(split_buf);
6154 return;
6155 }
6156 }
6157
6158#ifdef FEAT_EVAL
6159 current_SID = cmd->uc_scriptID;
6160#endif
6161 (void)do_cmdline(buf, eap->getline, eap->cookie,
6162 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6163#ifdef FEAT_EVAL
6164 current_SID = save_current_SID;
6165#endif
6166 vim_free(buf);
6167 vim_free(split_buf);
6168}
6169
6170# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6171 static char_u *
6172get_user_command_name(idx)
6173 int idx;
6174{
6175 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6176}
6177
6178/*
6179 * Function given to ExpandGeneric() to obtain the list of user command names.
6180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 char_u *
6182get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006183 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 int idx;
6185{
6186 if (idx < curbuf->b_ucmds.ga_len)
6187 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6188 idx -= curbuf->b_ucmds.ga_len;
6189 if (idx < ucmds.ga_len)
6190 return USER_CMD(idx)->uc_name;
6191 return NULL;
6192}
6193
6194/*
6195 * Function given to ExpandGeneric() to obtain the list of user command
6196 * attributes.
6197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 char_u *
6199get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006200 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 int idx;
6202{
6203 static char *user_cmd_flags[] =
6204 {"bang", "bar", "buffer", "complete", "count",
6205 "nargs", "range", "register"};
6206
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006207 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 return NULL;
6209 return (char_u *)user_cmd_flags[idx];
6210}
6211
6212/*
6213 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 char_u *
6216get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006217 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 int idx;
6219{
6220 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6221
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006222 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return NULL;
6224 return (char_u *)user_cmd_nargs[idx];
6225}
6226
6227/*
6228 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 char_u *
6231get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006232 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 int idx;
6234{
6235 return (char_u *)command_complete[idx].name;
6236}
6237# endif /* FEAT_CMDL_COMPL */
6238
6239#endif /* FEAT_USR_CMDS */
6240
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006241#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6242/*
6243 * Parse a completion argument "value[vallen]".
6244 * The detected completion goes in "*complp", argument type in "*argt".
6245 * When there is an argument, for function and user defined completion, it's
6246 * copied to allocated memory and stored in "*compl_arg".
6247 * Returns FAIL if something is wrong.
6248 */
6249 int
6250parse_compl_arg(value, vallen, complp, argt, compl_arg)
6251 char_u *value;
6252 int vallen;
6253 int *complp;
6254 long *argt;
6255 char_u **compl_arg;
6256{
6257 char_u *arg = NULL;
6258 size_t arglen = 0;
6259 int i;
6260 int valend = vallen;
6261
6262 /* Look for any argument part - which is the part after any ',' */
6263 for (i = 0; i < vallen; ++i)
6264 {
6265 if (value[i] == ',')
6266 {
6267 arg = &value[i + 1];
6268 arglen = vallen - i - 1;
6269 valend = i;
6270 break;
6271 }
6272 }
6273
6274 for (i = 0; command_complete[i].expand != 0; ++i)
6275 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006276 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006277 && STRNCMP(value, command_complete[i].name, valend) == 0)
6278 {
6279 *complp = command_complete[i].expand;
6280 if (command_complete[i].expand == EXPAND_BUFFERS)
6281 *argt |= BUFNAME;
6282 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6283 || command_complete[i].expand == EXPAND_FILES)
6284 *argt |= XFILE;
6285 break;
6286 }
6287 }
6288
6289 if (command_complete[i].expand == 0)
6290 {
6291 EMSG2(_("E180: Invalid complete value: %s"), value);
6292 return FAIL;
6293 }
6294
6295# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6296 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6297 && arg != NULL)
6298# else
6299 if (arg != NULL)
6300# endif
6301 {
6302 EMSG(_("E468: Completion argument only allowed for custom completion"));
6303 return FAIL;
6304 }
6305
6306# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6307 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6308 && arg == NULL)
6309 {
6310 EMSG(_("E467: Custom completion requires a function argument"));
6311 return FAIL;
6312 }
6313
6314 if (arg != NULL)
6315 *compl_arg = vim_strnsave(arg, (int)arglen);
6316# endif
6317 return OK;
6318}
6319#endif
6320
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 static void
6322ex_colorscheme(eap)
6323 exarg_T *eap;
6324{
Bram Moolenaare6850792010-05-14 15:28:44 +02006325 if (*eap->arg == NUL)
6326 {
6327#ifdef FEAT_EVAL
6328 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6329 char_u *p = NULL;
6330
6331 if (expr != NULL)
6332 {
6333 ++emsg_off;
6334 p = eval_to_string(expr, NULL, FALSE);
6335 --emsg_off;
6336 vim_free(expr);
6337 }
6338 if (p != NULL)
6339 {
6340 MSG(p);
6341 vim_free(p);
6342 }
6343 else
6344 MSG("default");
6345#else
6346 MSG(_("unknown"));
6347#endif
6348 }
6349 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6351}
6352
6353 static void
6354ex_highlight(eap)
6355 exarg_T *eap;
6356{
6357 if (*eap->arg == NUL && eap->cmd[2] == '!')
6358 MSG(_("Greetings, Vim user!"));
6359 do_highlight(eap->arg, eap->forceit, FALSE);
6360}
6361
6362
6363/*
6364 * Call this function if we thought we were going to exit, but we won't
6365 * (because of an error). May need to restore the terminal mode.
6366 */
6367 void
6368not_exiting()
6369{
6370 exiting = FALSE;
6371 settmode(TMODE_RAW);
6372}
6373
6374/*
6375 * ":quit": quit current window, quit Vim if closed the last window.
6376 */
6377 static void
6378ex_quit(eap)
6379 exarg_T *eap;
6380{
6381#ifdef FEAT_CMDWIN
6382 if (cmdwin_type != 0)
6383 {
6384 cmdwin_result = Ctrl_C;
6385 return;
6386 }
6387#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006388 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006389 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006390 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006391 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006392 return;
6393 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006394#ifdef FEAT_AUTOCMD
6395 if (curbuf_locked())
6396 return;
6397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399#ifdef FEAT_NETBEANS_INTG
6400 netbeansForcedQuit = eap->forceit;
6401#endif
6402
6403 /*
6404 * If there are more files or windows we won't exit.
6405 */
6406 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6407 exiting = TRUE;
6408 if ((!P_HID(curbuf)
6409 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6410 || check_more(TRUE, eap->forceit) == FAIL
6411 || (only_one_window() && check_changed_any(eap->forceit)))
6412 {
6413 not_exiting();
6414 }
6415 else
6416 {
6417#ifdef FEAT_WINDOWS
6418 if (only_one_window()) /* quit last window */
6419#endif
6420 getout(0);
6421#ifdef FEAT_WINDOWS
6422# ifdef FEAT_GUI
6423 need_mouse_correct = TRUE;
6424# endif
6425 /* close window; may free buffer */
6426 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6427#endif
6428 }
6429}
6430
6431/*
6432 * ":cquit".
6433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 static void
6435ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006436 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
6438 getout(1); /* this does not always pass on the exit code to the Manx
6439 compiler. why? */
6440}
6441
6442/*
6443 * ":qall": try to quit all windows
6444 */
6445 static void
6446ex_quit_all(eap)
6447 exarg_T *eap;
6448{
6449# ifdef FEAT_CMDWIN
6450 if (cmdwin_type != 0)
6451 {
6452 if (eap->forceit)
6453 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6454 else
6455 cmdwin_result = K_XF2;
6456 return;
6457 }
6458# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006459
6460 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006461 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006462 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006463 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006464 return;
6465 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466#ifdef FEAT_AUTOCMD
6467 if (curbuf_locked())
6468 return;
6469#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 exiting = TRUE;
6472 if (eap->forceit || !check_changed_any(FALSE))
6473 getout(0);
6474 not_exiting();
6475}
6476
Bram Moolenaard9967712006-03-11 21:18:15 +00006477#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478/*
6479 * ":close": close current window, unless it is the last one
6480 */
6481 static void
6482ex_close(eap)
6483 exarg_T *eap;
6484{
6485# ifdef FEAT_CMDWIN
6486 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006487 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 else
6489# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006490 if (!text_locked()
6491#ifdef FEAT_AUTOCMD
6492 && !curbuf_locked()
6493#endif
6494 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006495 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496}
6497
Bram Moolenaard9967712006-03-11 21:18:15 +00006498# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499/*
6500 * ":pclose": Close any preview window.
6501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006503ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505{
6506 win_T *win;
6507
6508 for (win = firstwin; win != NULL; win = win->w_next)
6509 if (win->w_p_pvw)
6510 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006511 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512 break;
6513 }
6514}
Bram Moolenaard9967712006-03-11 21:18:15 +00006515# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006516
Bram Moolenaarf740b292006-02-16 22:11:02 +00006517/*
6518 * Close window "win" and take care of handling closing the last window for a
6519 * modified buffer.
6520 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006521 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006522ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006525 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 int need_hide;
6528 buf_T *buf = win->w_buffer;
6529
6530 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006531 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006533# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if ((p_confirm || cmdmod.confirm) && p_write)
6535 {
6536 dialog_changed(buf, FALSE);
6537 if (buf_valid(buf) && bufIsChanged(buf))
6538 return;
6539 need_hide = FALSE;
6540 }
6541 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
6544 EMSG(_(e_nowrtmsg));
6545 return;
6546 }
6547 }
6548
Bram Moolenaard9967712006-03-11 21:18:15 +00006549# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006551# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006554 if (tp == NULL)
6555 win_close(win, !need_hide && !P_HID(buf));
6556 else
6557 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558}
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006561 * ":tabclose": close current tab page, unless it is the last one.
6562 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 */
6564 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 exarg_T *eap;
6567{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568 tabpage_T *tp;
6569
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570# ifdef FEAT_CMDWIN
6571 if (cmdwin_type != 0)
6572 cmdwin_result = K_IGNORE;
6573 else
6574# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006575 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006576 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006579 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006581 tp = find_tabpage((int)eap->line2);
6582 if (tp == NULL)
6583 {
6584 beep_flush();
6585 return;
6586 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006587 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006588 {
6589 tabpage_close_other(tp, eap->forceit);
6590 return;
6591 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006593 if (!text_locked()
6594#ifdef FEAT_AUTOCMD
6595 && !curbuf_locked()
6596#endif
6597 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006598 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006600}
6601
6602/*
6603 * ":tabonly": close all tab pages except the current one
6604 */
6605 static void
6606ex_tabonly(eap)
6607 exarg_T *eap;
6608{
6609 tabpage_T *tp;
6610 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006611
6612# ifdef FEAT_CMDWIN
6613 if (cmdwin_type != 0)
6614 cmdwin_result = K_IGNORE;
6615 else
6616# endif
6617 if (first_tabpage->tp_next == NULL)
6618 MSG(_("Already only one tab page"));
6619 else
6620 {
6621 /* Repeat this up to a 1000 times, because autocommands may mess
6622 * up the lists. */
6623 for (done = 0; done < 1000; ++done)
6624 {
6625 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6626 if (tp->tp_topframe != topframe)
6627 {
6628 tabpage_close_other(tp, eap->forceit);
6629 /* if we failed to close it quit */
6630 if (valid_tabpage(tp))
6631 done = 1000;
6632 /* start over, "tp" is now invalid */
6633 break;
6634 }
6635 if (first_tabpage->tp_next == NULL)
6636 break;
6637 }
6638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006642 * Close the current tab page.
6643 */
6644 void
6645tabpage_close(forceit)
6646 int forceit;
6647{
6648 /* First close all the windows but the current one. If that worked then
6649 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006650 if (lastwin != firstwin)
6651 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006652 if (lastwin == firstwin)
6653 ex_win_close(forceit, curwin, NULL);
6654# ifdef FEAT_GUI
6655 need_mouse_correct = TRUE;
6656# endif
6657}
6658
6659/*
6660 * Close tab page "tp", which is not the current tab page.
6661 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006662 * Also takes care of the tab pages line disappearing when closing the
6663 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006664 */
6665 void
6666tabpage_close_other(tp, forceit)
6667 tabpage_T *tp;
6668 int forceit;
6669{
6670 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006671 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006672 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006673
6674 /* Limit to 1000 windows, autocommands may add a window while we close
6675 * one. OK, so I'm paranoid... */
6676 while (++done < 1000)
6677 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006678 wp = tp->tp_firstwin;
6679 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006680
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006681 /* Autocommands may delete the tab page under our fingers and we may
6682 * fail to close a window with a modified buffer. */
6683 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006684 break;
6685 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006686
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006687 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006688 if (h != tabline_height())
6689 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006690}
6691
6692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 * ":only".
6694 */
6695 static void
6696ex_only(eap)
6697 exarg_T *eap;
6698{
6699# ifdef FEAT_GUI
6700 need_mouse_correct = TRUE;
6701# endif
6702 close_others(TRUE, eap->forceit);
6703}
6704
6705/*
6706 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006707 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006709 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710ex_all(eap)
6711 exarg_T *eap;
6712{
6713 if (eap->addr_count == 0)
6714 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006715 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716}
6717#endif /* FEAT_WINDOWS */
6718
6719 static void
6720ex_hide(eap)
6721 exarg_T *eap;
6722{
6723 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6724 eap->errmsg = e_invarg;
6725 else
6726 {
6727 /* ":hide" or ":hide | cmd": hide current window */
6728 eap->nextcmd = check_nextcmd(eap->arg);
6729#ifdef FEAT_WINDOWS
6730 if (!eap->skip)
6731 {
6732# ifdef FEAT_GUI
6733 need_mouse_correct = TRUE;
6734# endif
6735 win_close(curwin, FALSE); /* don't free buffer */
6736 }
6737#endif
6738 }
6739}
6740
6741/*
6742 * ":stop" and ":suspend": Suspend Vim.
6743 */
6744 static void
6745ex_stop(eap)
6746 exarg_T *eap;
6747{
6748 /*
6749 * Disallow suspending for "rvim".
6750 */
6751 if (!check_restricted()
6752#ifdef WIN3264
6753 /*
6754 * Check if external commands are allowed now.
6755 */
6756 && can_end_termcap_mode(TRUE)
6757#endif
6758 )
6759 {
6760 if (!eap->forceit)
6761 autowrite_all();
6762 windgoto((int)Rows - 1, 0);
6763 out_char('\n');
6764 out_flush();
6765 stoptermcap();
6766 out_flush(); /* needed for SUN to restore xterm buffer */
6767#ifdef FEAT_TITLE
6768 mch_restore_title(3); /* restore window titles */
6769#endif
6770 ui_suspend(); /* call machine specific function */
6771#ifdef FEAT_TITLE
6772 maketitle();
6773 resettitle(); /* force updating the title */
6774#endif
6775 starttermcap();
6776 scroll_start(); /* scroll screen before redrawing */
6777 redraw_later_clear();
6778 shell_resized(); /* may have resized window */
6779 }
6780}
6781
6782/*
6783 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6784 */
6785 static void
6786ex_exit(eap)
6787 exarg_T *eap;
6788{
6789#ifdef FEAT_CMDWIN
6790 if (cmdwin_type != 0)
6791 {
6792 cmdwin_result = Ctrl_C;
6793 return;
6794 }
6795#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006796 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006797 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006798 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006799 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006800 return;
6801 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006802#ifdef FEAT_AUTOCMD
6803 if (curbuf_locked())
6804 return;
6805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 /*
6808 * if more files or windows we won't exit
6809 */
6810 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6811 exiting = TRUE;
6812 if ( ((eap->cmdidx == CMD_wq
6813 || curbufIsChanged())
6814 && do_write(eap) == FAIL)
6815 || check_more(TRUE, eap->forceit) == FAIL
6816 || (only_one_window() && check_changed_any(eap->forceit)))
6817 {
6818 not_exiting();
6819 }
6820 else
6821 {
6822#ifdef FEAT_WINDOWS
6823 if (only_one_window()) /* quit last window, exit Vim */
6824#endif
6825 getout(0);
6826#ifdef FEAT_WINDOWS
6827# ifdef FEAT_GUI
6828 need_mouse_correct = TRUE;
6829# endif
6830 /* quit current window, may free buffer */
6831 win_close(curwin, !P_HID(curwin->w_buffer));
6832#endif
6833 }
6834}
6835
6836/*
6837 * ":print", ":list", ":number".
6838 */
6839 static void
6840ex_print(eap)
6841 exarg_T *eap;
6842{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006843 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6844 EMSG(_(e_emptybuf));
6845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006847 for ( ;!got_int; ui_breakcheck())
6848 {
6849 print_line(eap->line1,
6850 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6851 || (eap->flags & EXFLAG_NR)),
6852 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6853 if (++eap->line1 > eap->line2)
6854 break;
6855 out_flush(); /* show one line at a time */
6856 }
6857 setpcmark();
6858 /* put cursor at last line */
6859 curwin->w_cursor.lnum = eap->line2;
6860 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864}
6865
6866#ifdef FEAT_BYTEOFF
6867 static void
6868ex_goto(eap)
6869 exarg_T *eap;
6870{
6871 goto_byte(eap->line2);
6872}
6873#endif
6874
6875/*
6876 * ":shell".
6877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 static void
6879ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006880 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 do_shell(NULL, 0);
6883}
6884
6885#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6886 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006887 || defined(FEAT_GUI_MSWIN) \
6888 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 || defined(PROTO)
6890
6891/*
6892 * Handle a file drop. The code is here because a drop is *nearly* like an
6893 * :args command, but not quite (we have a list of exact filenames, so we
6894 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6895 * code is very similar to :args and hence needs access to a lot of the static
6896 * functions in this file.
6897 *
6898 * The list should be allocated using alloc(), as should each item in the
6899 * list. This function takes over responsibility for freeing the list.
6900 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006901 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6903 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6904 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6905 * file functionality is (currently) not in EMX this is not presently a
6906 * problem.
6907 */
6908 void
6909handle_drop(filec, filev, split)
6910 int filec; /* the number of files dropped */
6911 char_u **filev; /* the list of files dropped */
6912 int split; /* force splitting the window */
6913{
6914 exarg_T ea;
6915 int save_msg_scroll = msg_scroll;
6916
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006917 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006918 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920#ifdef FEAT_AUTOCMD
6921 if (curbuf_locked())
6922 return;
6923#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006924 /* When the screen is being updated we should not change buffers and
6925 * windows structures, it may cause freed memory to be used. */
6926 if (updating_screen)
6927 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
6929 /* Check whether the current buffer is changed. If so, we will need
6930 * to split the current window or data could be lost.
6931 * We don't need to check if the 'hidden' option is set, as in this
6932 * case the buffer won't be lost.
6933 */
6934 if (!P_HID(curbuf) && !split)
6935 {
6936 ++emsg_off;
6937 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6938 --emsg_off;
6939 }
6940 if (split)
6941 {
6942# ifdef FEAT_WINDOWS
6943 if (win_split(0, 0) == FAIL)
6944 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006945 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947 /* When splitting the window, create a new alist. Otherwise the
6948 * existing one is overwritten. */
6949 alist_unlink(curwin->w_alist);
6950 alist_new();
6951# else
6952 return; /* can't split, always fail */
6953# endif
6954 }
6955
6956 /*
6957 * Set up the new argument list.
6958 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006959 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 /*
6962 * Move to the first file.
6963 */
6964 /* Fake up a minimal "next" command for do_argfile() */
6965 vim_memset(&ea, 0, sizeof(ea));
6966 ea.cmd = (char_u *)"next";
6967 do_argfile(&ea, 0);
6968
6969 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6970 * mode that is not needed here. */
6971 need_start_insertmode = FALSE;
6972
6973 /* Restore msg_scroll, otherwise a following command may cause scrolling
6974 * unexpectedly. The screen will be redrawn by the caller, thus
6975 * msg_scroll being set by displaying a message is irrelevant. */
6976 msg_scroll = save_msg_scroll;
6977}
6978#endif
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980/*
6981 * Clear an argument list: free all file names and reset it to zero entries.
6982 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006983 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984alist_clear(al)
6985 alist_T *al;
6986{
6987 while (--al->al_ga.ga_len >= 0)
6988 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6989 ga_clear(&al->al_ga);
6990}
6991
6992/*
6993 * Init an argument list.
6994 */
6995 void
6996alist_init(al)
6997 alist_T *al;
6998{
6999 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7000}
7001
7002#if defined(FEAT_WINDOWS) || defined(PROTO)
7003
7004/*
7005 * Remove a reference from an argument list.
7006 * Ignored when the argument list is the global one.
7007 * If the argument list is no longer used by any window, free it.
7008 */
7009 void
7010alist_unlink(al)
7011 alist_T *al;
7012{
7013 if (al != &global_alist && --al->al_refcount <= 0)
7014 {
7015 alist_clear(al);
7016 vim_free(al);
7017 }
7018}
7019
7020# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7021/*
7022 * Create a new argument list and use it for the current window.
7023 */
7024 void
7025alist_new()
7026{
7027 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7028 if (curwin->w_alist == NULL)
7029 {
7030 curwin->w_alist = &global_alist;
7031 ++global_alist.al_refcount;
7032 }
7033 else
7034 {
7035 curwin->w_alist->al_refcount = 1;
7036 alist_init(curwin->w_alist);
7037 }
7038}
7039# endif
7040#endif
7041
7042#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7043/*
7044 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007045 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7046 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 */
7048 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007049alist_expand(fnum_list, fnum_len)
7050 int *fnum_list;
7051 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007054 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 char_u **new_arg_files;
7056 int new_arg_file_count;
7057 char_u *save_p_su = p_su;
7058 int i;
7059
7060 /* Don't use 'suffixes' here. This should work like the shell did the
7061 * expansion. Also, the vimrc file isn't read yet, thus the user
7062 * can't set the options. */
7063 p_su = empty_option;
7064 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7065 if (old_arg_files != NULL)
7066 {
7067 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007068 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7069 old_arg_count = GARGCOUNT;
7070 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 &new_arg_file_count, &new_arg_files,
7072 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7073 && new_arg_file_count > 0)
7074 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007075 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7076 TRUE, fnum_list, fnum_len);
7077 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
7080 p_su = save_p_su;
7081}
7082#endif
7083
7084/*
7085 * Set the argument list for the current window.
7086 * Takes over the allocated files[] and the allocated fnames in it.
7087 */
7088 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007089alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 alist_T *al;
7091 int count;
7092 char_u **files;
7093 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007094 int *fnum_list;
7095 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 int i;
7098
7099 alist_clear(al);
7100 if (ga_grow(&al->al_ga, count) == OK)
7101 {
7102 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007103 {
7104 if (got_int)
7105 {
7106 /* When adding many buffers this can take a long time. Allow
7107 * interrupting here. */
7108 while (i < count)
7109 vim_free(files[i++]);
7110 break;
7111 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007112
7113 /* May set buffer name of a buffer previously used for the
7114 * argument list, so that it's re-used by alist_add. */
7115 if (fnum_list != NULL && i < fnum_len)
7116 buf_set_name(fnum_list[i], files[i]);
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007119 ui_breakcheck();
7120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 vim_free(files);
7122 }
7123 else
7124 FreeWild(count, files);
7125#ifdef FEAT_WINDOWS
7126 if (al == &global_alist)
7127#endif
7128 arg_had_last = FALSE;
7129}
7130
7131/*
7132 * Add file "fname" to argument list "al".
7133 * "fname" must have been allocated and "al" must have been checked for room.
7134 */
7135 void
7136alist_add(al, fname, set_fnum)
7137 alist_T *al;
7138 char_u *fname;
7139 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7140{
7141 if (fname == NULL) /* don't add NULL file names */
7142 return;
7143#ifdef BACKSLASH_IN_FILENAME
7144 slash_adjust(fname);
7145#endif
7146 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7147 if (set_fnum > 0)
7148 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7149 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7150 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151}
7152
7153#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7154/*
7155 * Adjust slashes in file names. Called after 'shellslash' was set.
7156 */
7157 void
7158alist_slash_adjust()
7159{
7160 int i;
7161# ifdef FEAT_WINDOWS
7162 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007163 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164# endif
7165
7166 for (i = 0; i < GARGCOUNT; ++i)
7167 if (GARGLIST[i].ae_fname != NULL)
7168 slash_adjust(GARGLIST[i].ae_fname);
7169# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007170 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (wp->w_alist != &global_alist)
7172 for (i = 0; i < WARGCOUNT(wp); ++i)
7173 if (WARGLIST(wp)[i].ae_fname != NULL)
7174 slash_adjust(WARGLIST(wp)[i].ae_fname);
7175# endif
7176}
7177#endif
7178
7179/*
7180 * ":preserve".
7181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 static void
7183ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007184 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007186 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 ml_preserve(curbuf, TRUE);
7188}
7189
7190/*
7191 * ":recover".
7192 */
7193 static void
7194ex_recover(eap)
7195 exarg_T *eap;
7196{
7197 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7198 recoverymode = TRUE;
7199 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7200 && (*eap->arg == NUL
7201 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7202 ml_recover();
7203 recoverymode = FALSE;
7204}
7205
7206/*
7207 * Command modifier used in a wrong way.
7208 */
7209 static void
7210ex_wrongmodifier(eap)
7211 exarg_T *eap;
7212{
7213 eap->errmsg = e_invcmd;
7214}
7215
7216#ifdef FEAT_WINDOWS
7217/*
7218 * :sview [+command] file split window with new file, read-only
7219 * :split [[+command] file] split window with current or new file
7220 * :vsplit [[+command] file] split window vertically with current or new file
7221 * :new [[+command] file] split window with no or new file
7222 * :vnew [[+command] file] split vertically window with no or new file
7223 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007224 *
7225 * :tabedit open new Tab page with empty window
7226 * :tabedit [+command] file open new Tab page and edit "file"
7227 * :tabnew [[+command] file] just like :tabedit
7228 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 */
7230 void
7231ex_splitview(eap)
7232 exarg_T *eap;
7233{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007234 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007235# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007237# endif
7238# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007242# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7244 {
7245 ex_ni(eap);
7246 return;
7247 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007250# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007254# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007256 * quickfix windows. But it's OK when doing ":tab split". */
7257 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 if (eap->cmdidx == CMD_split)
7260 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (eap->cmdidx == CMD_vsplit)
7263 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007268# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007269 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {
7271 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7272 FNAME_MESS, TRUE, curbuf->b_ffname);
7273 if (fname == NULL)
7274 goto theend;
7275 eap->arg = fname;
7276 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007277# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007281# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007283# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 && eap->cmdidx != CMD_new)
7287 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007288# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007289 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007290# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007291 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007292# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007293 au_has_group((char_u *)"FileExplorer"))
7294 {
7295 /* No browsing supported but we do have the file explorer:
7296 * Edit the directory. */
7297 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7298 eap->arg = (char_u *)".";
7299 }
7300 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007301# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007302 {
7303 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007305 if (fname == NULL)
7306 goto theend;
7307 eap->arg = fname;
7308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 }
7310 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007313 /*
7314 * Either open new tab page or split the window.
7315 */
7316 if (eap->cmdidx == CMD_tabedit
7317 || eap->cmdidx == CMD_tabfind
7318 || eap->cmdidx == CMD_tabnew)
7319 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007320 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7321 : eap->addr_count == 0 ? 0
7322 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007323 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007324 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007325
7326 /* set the alternate buffer for the window we came from */
7327 if (curwin != old_curwin
7328 && win_valid(old_curwin)
7329 && old_curwin->w_buffer != curbuf
7330 && !cmdmod.keepalt)
7331 old_curwin->w_alt_fnum = curbuf->b_fnum;
7332 }
7333 }
7334 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7336 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007337# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 /* Reset 'scrollbind' when editing another file, but keep it when
7339 * doing ":split" without arguments. */
7340 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007341# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007345 {
7346 RESET_BINDING(curwin);
7347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 else
7349 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 do_exedit(eap, old_curwin);
7352 }
7353
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007358# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359theend:
7360 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363
7364/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007365 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366 */
7367 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007368tabpage_new()
7369{
7370 exarg_T ea;
7371
7372 vim_memset(&ea, 0, sizeof(ea));
7373 ea.cmdidx = CMD_tabnew;
7374 ea.cmd = (char_u *)"tabn";
7375 ea.arg = (char_u *)"";
7376 ex_splitview(&ea);
7377}
7378
7379/*
7380 * :tabnext command
7381 */
7382 static void
7383ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384 exarg_T *eap;
7385{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007386 switch (eap->cmdidx)
7387 {
7388 case CMD_tabfirst:
7389 case CMD_tabrewind:
7390 goto_tabpage(1);
7391 break;
7392 case CMD_tablast:
7393 goto_tabpage(9999);
7394 break;
7395 case CMD_tabprevious:
7396 case CMD_tabNext:
7397 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7398 break;
7399 default: /* CMD_tabnext */
7400 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7401 break;
7402 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007403}
7404
7405/*
7406 * :tabmove command
7407 */
7408 static void
7409ex_tabmove(eap)
7410 exarg_T *eap;
7411{
7412 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413}
7414
7415/*
7416 * :tabs command: List tabs and their contents.
7417 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418 static void
7419ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007420 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421{
7422 tabpage_T *tp;
7423 win_T *wp;
7424 int tabcount = 1;
7425
7426 msg_start();
7427 msg_scroll = TRUE;
7428 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7429 {
7430 msg_putchar('\n');
7431 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7432 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7433 out_flush(); /* output one line at a time */
7434 ui_breakcheck();
7435
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007436 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437 wp = firstwin;
7438 else
7439 wp = tp->tp_firstwin;
7440 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7441 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007442 msg_putchar('\n');
7443 msg_putchar(wp == curwin ? '>' : ' ');
7444 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007445 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7446 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447 if (buf_spname(wp->w_buffer) != NULL)
7448 STRCPY(IObuff, buf_spname(wp->w_buffer));
7449 else
7450 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7451 IObuff, IOSIZE, TRUE);
7452 msg_outtrans(IObuff);
7453 out_flush(); /* output one line at a time */
7454 ui_breakcheck();
7455 }
7456 }
7457}
7458
7459#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461/*
7462 * ":mode": Set screen mode.
7463 * If no argument given, just get the screen size and redraw.
7464 */
7465 static void
7466ex_mode(eap)
7467 exarg_T *eap;
7468{
7469 if (*eap->arg == NUL)
7470 shell_resized();
7471 else
7472 mch_screenmode(eap->arg);
7473}
7474
7475#ifdef FEAT_WINDOWS
7476/*
7477 * ":resize".
7478 * set, increment or decrement current window height
7479 */
7480 static void
7481ex_resize(eap)
7482 exarg_T *eap;
7483{
7484 int n;
7485 win_T *wp = curwin;
7486
7487 if (eap->addr_count > 0)
7488 {
7489 n = eap->line2;
7490 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7491 ;
7492 }
7493
7494#ifdef FEAT_GUI
7495 need_mouse_correct = TRUE;
7496#endif
7497 n = atol((char *)eap->arg);
7498#ifdef FEAT_VERTSPLIT
7499 if (cmdmod.split & WSP_VERT)
7500 {
7501 if (*eap->arg == '-' || *eap->arg == '+')
7502 n += W_WIDTH(curwin);
7503 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7504 n = 9999;
7505 win_setwidth_win((int)n, wp);
7506 }
7507 else
7508#endif
7509 {
7510 if (*eap->arg == '-' || *eap->arg == '+')
7511 n += curwin->w_height;
7512 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7513 n = 9999;
7514 win_setheight_win((int)n, wp);
7515 }
7516}
7517#endif
7518
7519/*
7520 * ":find [+command] <file>" command.
7521 */
7522 static void
7523ex_find(eap)
7524 exarg_T *eap;
7525{
7526#ifdef FEAT_SEARCHPATH
7527 char_u *fname;
7528 int count;
7529
7530 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7531 TRUE, curbuf->b_ffname);
7532 if (eap->addr_count > 0)
7533 {
7534 /* Repeat finding the file "count" times. This matters when it
7535 * appears several times in the path. */
7536 count = eap->line2;
7537 while (fname != NULL && --count > 0)
7538 {
7539 vim_free(fname);
7540 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7541 FALSE, curbuf->b_ffname);
7542 }
7543 }
7544
7545 if (fname != NULL)
7546 {
7547 eap->arg = fname;
7548#endif
7549 do_exedit(eap, NULL);
7550#ifdef FEAT_SEARCHPATH
7551 vim_free(fname);
7552 }
7553#endif
7554}
7555
7556/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007557 * ":open" simulation: for now just work like ":visual".
7558 */
7559 static void
7560ex_open(eap)
7561 exarg_T *eap;
7562{
7563 regmatch_T regmatch;
7564 char_u *p;
7565
7566 curwin->w_cursor.lnum = eap->line2;
7567 beginline(BL_SOL | BL_FIX);
7568 if (*eap->arg == '/')
7569 {
7570 /* ":open /pattern/": put cursor in column found with pattern */
7571 ++eap->arg;
7572 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7573 *p = NUL;
7574 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7575 if (regmatch.regprog != NULL)
7576 {
7577 regmatch.rm_ic = p_ic;
7578 p = ml_get_curline();
7579 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007581 else
7582 EMSG(_(e_nomatch));
7583 vim_free(regmatch.regprog);
7584 }
7585 /* Move to the NUL, ignore any other arguments. */
7586 eap->arg += STRLEN(eap->arg);
7587 }
7588 check_cursor();
7589
7590 eap->cmdidx = CMD_visual;
7591 do_exedit(eap, NULL);
7592}
7593
7594/*
7595 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 */
7597 static void
7598ex_edit(eap)
7599 exarg_T *eap;
7600{
7601 do_exedit(eap, NULL);
7602}
7603
7604/*
7605 * ":edit <file>" command and alikes.
7606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 void
7608do_exedit(eap, old_curwin)
7609 exarg_T *eap;
7610 win_T *old_curwin; /* curwin before doing a split or NULL */
7611{
7612 int n;
7613#ifdef FEAT_WINDOWS
7614 int need_hide;
7615#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007616 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
7618 /*
7619 * ":vi" command ends Ex mode.
7620 */
7621 if (exmode_active && (eap->cmdidx == CMD_visual
7622 || eap->cmdidx == CMD_view))
7623 {
7624 exmode_active = FALSE;
7625 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007626 {
7627 /* Special case: ":global/pat/visual\NLvi-commands" */
7628 if (global_busy)
7629 {
7630 int rd = RedrawingDisabled;
7631 int nwr = no_wait_return;
7632 int ms = msg_scroll;
7633#ifdef FEAT_GUI
7634 int he = hold_gui_events;
7635#endif
7636
7637 if (eap->nextcmd != NULL)
7638 {
7639 stuffReadbuff(eap->nextcmd);
7640 eap->nextcmd = NULL;
7641 }
7642
7643 if (exmode_was != EXMODE_VIM)
7644 settmode(TMODE_RAW);
7645 RedrawingDisabled = 0;
7646 no_wait_return = 0;
7647 need_wait_return = FALSE;
7648 msg_scroll = 0;
7649#ifdef FEAT_GUI
7650 hold_gui_events = 0;
7651#endif
7652 must_redraw = CLEAR;
7653
7654 main_loop(FALSE, TRUE);
7655
7656 RedrawingDisabled = rd;
7657 no_wait_return = nwr;
7658 msg_scroll = ms;
7659#ifdef FEAT_GUI
7660 hold_gui_events = he;
7661#endif
7662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666
7667 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007668 || eap->cmdidx == CMD_tabnew
7669 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670#ifdef FEAT_VERTSPLIT
7671 || eap->cmdidx == CMD_vnew
7672#endif
7673 ) && *eap->arg == NUL)
7674 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007675 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 setpcmark();
7677 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007678 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7679 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
7681 else if ((eap->cmdidx != CMD_split
7682#ifdef FEAT_VERTSPLIT
7683 && eap->cmdidx != CMD_vsplit
7684#endif
7685 )
7686 || *eap->arg != NUL
7687#ifdef FEAT_BROWSE
7688 || cmdmod.browse
7689#endif
7690 )
7691 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007692#ifdef FEAT_AUTOCMD
7693 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7694 * can bring us here, others are stopped earlier. */
7695 if (*eap->arg != NUL && curbuf_locked())
7696 return;
7697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 n = readonlymode;
7699 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7700 readonlymode = TRUE;
7701 else if (eap->cmdidx == CMD_enew)
7702 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7703 empty buffer */
7704 setpcmark();
7705 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7706 NULL, eap,
7707 /* ":edit" goes to first line if Vi compatible */
7708 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7709 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7710 ? ECMD_ONE : eap->do_ecmd_lnum,
7711 (P_HID(curbuf) ? ECMD_HIDE : 0)
7712 + (eap->forceit ? ECMD_FORCEIT : 0)
7713#ifdef FEAT_LISTCMDS
7714 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7715#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007716 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
7718 /* Editing the file failed. If the window was split, close it. */
7719#ifdef FEAT_WINDOWS
7720 if (old_curwin != NULL)
7721 {
7722 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7723 if (!need_hide || P_HID(curbuf))
7724 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007725# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7726 cleanup_T cs;
7727
7728 /* Reset the error/interrupt/exception state here so that
7729 * aborting() returns FALSE when closing a window. */
7730 enter_cleanup(&cs);
7731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732# ifdef FEAT_GUI
7733 need_mouse_correct = TRUE;
7734# endif
7735 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007736
7737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7738 /* Restore the error/interrupt/exception state if not
7739 * discarded by a new aborting error, interrupt, or
7740 * uncaught exception. */
7741 leave_cleanup(&cs);
7742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
7744 }
7745#endif
7746 }
7747 else if (readonlymode && curbuf->b_nwindows == 1)
7748 {
7749 /* When editing an already visited buffer, 'readonly' won't be set
7750 * but the previous value is kept. With ":view" and ":sview" we
7751 * want the file to be readonly, except when another window is
7752 * editing the same buffer. */
7753 curbuf->b_p_ro = TRUE;
7754 }
7755 readonlymode = n;
7756 }
7757 else
7758 {
7759 if (eap->do_ecmd_cmd != NULL)
7760 do_cmdline_cmd(eap->do_ecmd_cmd);
7761#ifdef FEAT_TITLE
7762 n = curwin->w_arg_idx_invalid;
7763#endif
7764 check_arg_idx(curwin);
7765#ifdef FEAT_TITLE
7766 if (n != curwin->w_arg_idx_invalid)
7767 maketitle();
7768#endif
7769 }
7770
7771#ifdef FEAT_WINDOWS
7772 /*
7773 * if ":split file" worked, set alternate file name in old window to new
7774 * file
7775 */
7776 if (old_curwin != NULL
7777 && *eap->arg != NUL
7778 && curwin != old_curwin
7779 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007780 && old_curwin->w_buffer != curbuf
7781 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 old_curwin->w_alt_fnum = curbuf->b_fnum;
7783#endif
7784
7785 ex_no_reprint = TRUE;
7786}
7787
7788#ifndef FEAT_GUI
7789/*
7790 * ":gui" and ":gvim" when there is no GUI.
7791 */
7792 static void
7793ex_nogui(eap)
7794 exarg_T *eap;
7795{
7796 eap->errmsg = e_nogvim;
7797}
7798#endif
7799
7800#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7801 static void
7802ex_tearoff(eap)
7803 exarg_T *eap;
7804{
7805 gui_make_tearoff(eap->arg);
7806}
7807#endif
7808
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007809#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 static void
7811ex_popup(eap)
7812 exarg_T *eap;
7813{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007814 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815}
7816#endif
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 static void
7819ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007820 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7823 MSG(_("No swap file"));
7824 else
7825 msg(curbuf->b_ml.ml_mfp->mf_fname);
7826}
7827
7828/*
7829 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7830 * offset.
7831 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 static void
7834ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007835 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
7837#ifdef FEAT_SCROLLBIND
7838 win_T *wp;
7839 long topline;
7840 long y;
7841 linenr_T old_linenr = curwin->w_cursor.lnum;
7842
7843 setpcmark();
7844
7845 /*
7846 * determine max topline
7847 */
7848 if (curwin->w_p_scb)
7849 {
7850 topline = curwin->w_topline;
7851 for (wp = firstwin; wp; wp = wp->w_next)
7852 {
7853 if (wp->w_p_scb && wp->w_buffer)
7854 {
7855 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7856 if (topline > y)
7857 topline = y;
7858 }
7859 }
7860 if (topline < 1)
7861 topline = 1;
7862 }
7863 else
7864 {
7865 topline = 1;
7866 }
7867
7868
7869 /*
7870 * set all scrollbind windows to the same topline
7871 */
7872 wp = curwin;
7873 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7874 {
7875 if (curwin->w_p_scb)
7876 {
7877 y = topline - curwin->w_topline;
7878 if (y > 0)
7879 scrollup(y, TRUE);
7880 else
7881 scrolldown(-y, TRUE);
7882 curwin->w_scbind_pos = topline;
7883 redraw_later(VALID);
7884 cursor_correct();
7885#ifdef FEAT_WINDOWS
7886 curwin->w_redr_status = TRUE;
7887#endif
7888 }
7889 }
7890 curwin = wp;
7891 if (curwin->w_p_scb)
7892 {
7893 did_syncbind = TRUE;
7894 checkpcmark();
7895 if (old_linenr != curwin->w_cursor.lnum)
7896 {
7897 char_u ctrl_o[2];
7898
7899 ctrl_o[0] = Ctrl_O;
7900 ctrl_o[1] = 0;
7901 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7902 }
7903 }
7904#endif
7905}
7906
7907
7908 static void
7909ex_read(eap)
7910 exarg_T *eap;
7911{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007912 int i;
7913 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7914 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915
7916 if (eap->usefilter) /* :r!cmd */
7917 do_bang(1, eap, FALSE, FALSE, TRUE);
7918 else
7919 {
7920 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7921 return;
7922
7923#ifdef FEAT_BROWSE
7924 if (cmdmod.browse)
7925 {
7926 char_u *browseFile;
7927
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007928 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 NULL, NULL, NULL, curbuf);
7930 if (browseFile != NULL)
7931 {
7932 i = readfile(browseFile, NULL,
7933 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7934 vim_free(browseFile);
7935 }
7936 else
7937 i = OK;
7938 }
7939 else
7940#endif
7941 if (*eap->arg == NUL)
7942 {
7943 if (check_fname() == FAIL) /* check for no file name */
7944 return;
7945 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7946 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7947 }
7948 else
7949 {
7950 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7951 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7952 i = readfile(eap->arg, NULL,
7953 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7954
7955 }
7956 if (i == FAIL)
7957 {
7958#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7959 if (!aborting())
7960#endif
7961 EMSG2(_(e_notopen), eap->arg);
7962 }
7963 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007964 {
7965 if (empty && exmode_active)
7966 {
7967 /* Delete the empty line that remains. Historically ex does
7968 * this but vi doesn't. */
7969 if (eap->line2 == 0)
7970 lnum = curbuf->b_ml.ml_line_count;
7971 else
7972 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007973 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007974 {
7975 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007976 if (curwin->w_cursor.lnum > 1
7977 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007978 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007979 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007980 }
7981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 }
7985}
7986
Bram Moolenaarea408852005-06-25 22:49:46 +00007987static char_u *prev_dir = NULL;
7988
7989#if defined(EXITFREE) || defined(PROTO)
7990 void
7991free_cd_dir()
7992{
7993 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007994 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007995
7996 vim_free(globaldir);
7997 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007998}
7999#endif
8000
8001
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002/*
8003 * ":cd", ":lcd", ":chdir" and ":lchdir".
8004 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008005 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006ex_cd(eap)
8007 exarg_T *eap;
8008{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 char_u *new_dir;
8010 char_u *tofree;
8011
8012 new_dir = eap->arg;
8013#if !defined(UNIX) && !defined(VMS)
8014 /* for non-UNIX ":cd" means: print current directory */
8015 if (*new_dir == NUL)
8016 ex_pwd(NULL);
8017 else
8018#endif
8019 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008020#ifdef FEAT_AUTOCMD
8021 if (allbuf_locked())
8022 return;
8023#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008024 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8025 && !eap->forceit)
8026 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008027 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008028 return;
8029 }
8030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 /* ":cd -": Change to previous directory */
8032 if (STRCMP(new_dir, "-") == 0)
8033 {
8034 if (prev_dir == NULL)
8035 {
8036 EMSG(_("E186: No previous directory"));
8037 return;
8038 }
8039 new_dir = prev_dir;
8040 }
8041
8042 /* Save current directory for next ":cd -" */
8043 tofree = prev_dir;
8044 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8045 prev_dir = vim_strsave(NameBuff);
8046 else
8047 prev_dir = NULL;
8048
8049#if defined(UNIX) || defined(VMS)
8050 /* for UNIX ":cd" means: go to home directory */
8051 if (*new_dir == NUL)
8052 {
8053 /* use NameBuff for home directory name */
8054# ifdef VMS
8055 char_u *p;
8056
8057 p = mch_getenv((char_u *)"SYS$LOGIN");
8058 if (p == NULL || *p == NUL) /* empty is the same as not set */
8059 NameBuff[0] = NUL;
8060 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008061 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062# else
8063 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8064# endif
8065 new_dir = NameBuff;
8066 }
8067#endif
8068 if (new_dir == NULL || vim_chdir(new_dir))
8069 EMSG(_(e_failed));
8070 else
8071 {
8072 vim_free(curwin->w_localdir);
8073 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8074 {
8075 /* If still in global directory, need to remember current
8076 * directory as global directory. */
8077 if (globaldir == NULL && prev_dir != NULL)
8078 globaldir = vim_strsave(prev_dir);
8079 /* Remember this local directory for the window. */
8080 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8081 curwin->w_localdir = vim_strsave(NameBuff);
8082 }
8083 else
8084 {
8085 /* We are now in the global directory, no need to remember its
8086 * name. */
8087 vim_free(globaldir);
8088 globaldir = NULL;
8089 curwin->w_localdir = NULL;
8090 }
8091
8092 shorten_fnames(TRUE);
8093
8094 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008095 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 ex_pwd(eap);
8097 }
8098 vim_free(tofree);
8099 }
8100}
8101
8102/*
8103 * ":pwd".
8104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 static void
8106ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008107 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8110 {
8111#ifdef BACKSLASH_IN_FILENAME
8112 slash_adjust(NameBuff);
8113#endif
8114 msg(NameBuff);
8115 }
8116 else
8117 EMSG(_("E187: Unknown"));
8118}
8119
8120/*
8121 * ":=".
8122 */
8123 static void
8124ex_equal(eap)
8125 exarg_T *eap;
8126{
8127 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008128 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129}
8130
8131 static void
8132ex_sleep(eap)
8133 exarg_T *eap;
8134{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008135 int n;
8136 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137
8138 if (cursor_valid())
8139 {
8140 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8141 if (n >= 0)
8142 windgoto((int)n, curwin->w_wcol);
8143 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008144
8145 len = eap->line2;
8146 switch (*eap->arg)
8147 {
8148 case 'm': break;
8149 case NUL: len *= 1000L; break;
8150 default: EMSG2(_(e_invarg2), eap->arg); return;
8151 }
8152 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
8155/*
8156 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8157 */
8158 void
8159do_sleep(msec)
8160 long msec;
8161{
8162 long done;
8163
8164 cursor_on();
8165 out_flush();
8166 for (done = 0; !got_int && done < msec; done += 1000L)
8167 {
8168 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8169 ui_breakcheck();
8170 }
8171}
8172
8173 static void
8174do_exmap(eap, isabbrev)
8175 exarg_T *eap;
8176 int isabbrev;
8177{
8178 int mode;
8179 char_u *cmdp;
8180
8181 cmdp = eap->cmd;
8182 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8183
8184 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8185 eap->arg, mode, isabbrev))
8186 {
8187 case 1: EMSG(_(e_invarg));
8188 break;
8189 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8190 break;
8191 }
8192}
8193
8194/*
8195 * ":winsize" command (obsolete).
8196 */
8197 static void
8198ex_winsize(eap)
8199 exarg_T *eap;
8200{
8201 int w, h;
8202 char_u *arg = eap->arg;
8203 char_u *p;
8204
8205 w = getdigits(&arg);
8206 arg = skipwhite(arg);
8207 p = arg;
8208 h = getdigits(&arg);
8209 if (*p != NUL && *arg == NUL)
8210 set_shellsize(w, h, TRUE);
8211 else
8212 EMSG(_("E465: :winsize requires two number arguments"));
8213}
8214
8215#ifdef FEAT_WINDOWS
8216 static void
8217ex_wincmd(eap)
8218 exarg_T *eap;
8219{
8220 int xchar = NUL;
8221 char_u *p;
8222
8223 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8224 {
8225 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8226 if (eap->arg[1] == NUL)
8227 {
8228 EMSG(_(e_invarg));
8229 return;
8230 }
8231 xchar = eap->arg[1];
8232 p = eap->arg + 2;
8233 }
8234 else
8235 p = eap->arg + 1;
8236
8237 eap->nextcmd = check_nextcmd(p);
8238 p = skipwhite(p);
8239 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8240 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008241 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {
8243 /* Pass flags on for ":vertical wincmd ]". */
8244 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008245 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8247 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008248 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 }
8250}
8251#endif
8252
Bram Moolenaar843ee412004-06-30 16:16:41 +00008253#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254/*
8255 * ":winpos".
8256 */
8257 static void
8258ex_winpos(eap)
8259 exarg_T *eap;
8260{
8261 int x, y;
8262 char_u *arg = eap->arg;
8263 char_u *p;
8264
8265 if (*arg == NUL)
8266 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008267# if defined(FEAT_GUI) || defined(MSWIN)
8268# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008270# else
8271 if (mch_get_winpos(&x, &y) != FAIL)
8272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {
8274 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8275 msg(IObuff);
8276 }
8277 else
8278# endif
8279 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8280 }
8281 else
8282 {
8283 x = getdigits(&arg);
8284 arg = skipwhite(arg);
8285 p = arg;
8286 y = getdigits(&arg);
8287 if (*p == NUL || *arg != NUL)
8288 {
8289 EMSG(_("E466: :winpos requires two number arguments"));
8290 return;
8291 }
8292# ifdef FEAT_GUI
8293 if (gui.in_use)
8294 gui_mch_set_winpos(x, y);
8295 else if (gui.starting)
8296 {
8297 /* Remember the coordinates for when the window is opened. */
8298 gui_win_x = x;
8299 gui_win_y = y;
8300 }
8301# ifdef HAVE_TGETENT
8302 else
8303# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008304# else
8305# ifdef MSWIN
8306 mch_set_winpos(x, y);
8307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308# endif
8309# ifdef HAVE_TGETENT
8310 if (*T_CWP)
8311 term_set_winpos(x, y);
8312# endif
8313 }
8314}
8315#endif
8316
8317/*
8318 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8319 */
8320 static void
8321ex_operators(eap)
8322 exarg_T *eap;
8323{
8324 oparg_T oa;
8325
8326 clear_oparg(&oa);
8327 oa.regname = eap->regname;
8328 oa.start.lnum = eap->line1;
8329 oa.end.lnum = eap->line2;
8330 oa.line_count = eap->line2 - eap->line1 + 1;
8331 oa.motion_type = MLINE;
8332#ifdef FEAT_VIRTUALEDIT
8333 virtual_op = FALSE;
8334#endif
8335 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8336 {
8337 setpcmark();
8338 curwin->w_cursor.lnum = eap->line1;
8339 beginline(BL_SOL | BL_FIX);
8340 }
8341
8342 switch (eap->cmdidx)
8343 {
8344 case CMD_delete:
8345 oa.op_type = OP_DELETE;
8346 op_delete(&oa);
8347 break;
8348
8349 case CMD_yank:
8350 oa.op_type = OP_YANK;
8351 (void)op_yank(&oa, FALSE, TRUE);
8352 break;
8353
8354 default: /* CMD_rshift or CMD_lshift */
8355 if ((eap->cmdidx == CMD_rshift)
8356#ifdef FEAT_RIGHTLEFT
8357 ^ curwin->w_p_rl
8358#endif
8359 )
8360 oa.op_type = OP_RSHIFT;
8361 else
8362 oa.op_type = OP_LSHIFT;
8363 op_shift(&oa, FALSE, eap->amount);
8364 break;
8365 }
8366#ifdef FEAT_VIRTUALEDIT
8367 virtual_op = MAYBE;
8368#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008369 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370}
8371
8372/*
8373 * ":put".
8374 */
8375 static void
8376ex_put(eap)
8377 exarg_T *eap;
8378{
8379 /* ":0put" works like ":1put!". */
8380 if (eap->line2 == 0)
8381 {
8382 eap->line2 = 1;
8383 eap->forceit = TRUE;
8384 }
8385 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008386 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8387 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388}
8389
8390/*
8391 * Handle ":copy" and ":move".
8392 */
8393 static void
8394ex_copymove(eap)
8395 exarg_T *eap;
8396{
8397 long n;
8398
8399 n = get_address(&eap->arg, FALSE, FALSE);
8400 if (eap->arg == NULL) /* error detected */
8401 {
8402 eap->nextcmd = NULL;
8403 return;
8404 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008405 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
8407 /*
8408 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8409 */
8410 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8411 {
8412 EMSG(_(e_invaddr));
8413 return;
8414 }
8415
8416 if (eap->cmdidx == CMD_move)
8417 {
8418 if (do_move(eap->line1, eap->line2, n) == FAIL)
8419 return;
8420 }
8421 else
8422 ex_copy(eap->line1, eap->line2, n);
8423 u_clearline();
8424 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008425 ex_may_print(eap);
8426}
8427
8428/*
8429 * Print the current line if flags were given to the Ex command.
8430 */
8431 static void
8432ex_may_print(eap)
8433 exarg_T *eap;
8434{
8435 if (eap->flags != 0)
8436 {
8437 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8438 (eap->flags & EXFLAG_LIST));
8439 ex_no_reprint = TRUE;
8440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441}
8442
8443/*
8444 * ":smagic" and ":snomagic".
8445 */
8446 static void
8447ex_submagic(eap)
8448 exarg_T *eap;
8449{
8450 int magic_save = p_magic;
8451
8452 p_magic = (eap->cmdidx == CMD_smagic);
8453 do_sub(eap);
8454 p_magic = magic_save;
8455}
8456
8457/*
8458 * ":join".
8459 */
8460 static void
8461ex_join(eap)
8462 exarg_T *eap;
8463{
8464 curwin->w_cursor.lnum = eap->line1;
8465 if (eap->line1 == eap->line2)
8466 {
8467 if (eap->addr_count >= 2) /* :2,2join does nothing */
8468 return;
8469 if (eap->line2 == curbuf->b_ml.ml_line_count)
8470 {
8471 beep_flush();
8472 return;
8473 }
8474 ++eap->line2;
8475 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008476 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008478 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479}
8480
8481/*
8482 * ":[addr]@r" or ":[addr]*r": execute register
8483 */
8484 static void
8485ex_at(eap)
8486 exarg_T *eap;
8487{
8488 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008489 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490
8491 curwin->w_cursor.lnum = eap->line2;
8492
8493#ifdef USE_ON_FLY_SCROLL
8494 dont_scroll = TRUE; /* disallow scrolling here */
8495#endif
8496
8497 /* get the register name. No name means to use the previous one */
8498 c = *eap->arg;
8499 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8500 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008501 /* Put the register in the typeahead buffer with the "silent" flag. */
8502 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8503 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 else
8508 {
8509 int save_efr = exec_from_reg;
8510
8511 exec_from_reg = TRUE;
8512
8513 /*
8514 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008515 * Continue until the stuff buffer is empty and all added characters
8516 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008518 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8520
8521 exec_from_reg = save_efr;
8522 }
8523}
8524
8525/*
8526 * ":!".
8527 */
8528 static void
8529ex_bang(eap)
8530 exarg_T *eap;
8531{
8532 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8533}
8534
8535/*
8536 * ":undo".
8537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 static void
8539ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008540 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008542 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008543 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008544 else
8545 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546}
8547
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008548#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008549 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008550ex_wundo(eap)
8551 exarg_T *eap;
8552{
8553 char_u hash[UNDO_HASH_SIZE];
8554
8555 u_compute_hash(hash);
8556 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8557}
8558
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008559 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008560ex_rundo(eap)
8561 exarg_T *eap;
8562{
8563 char_u hash[UNDO_HASH_SIZE];
8564
8565 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008566 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008567}
8568#endif
8569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570/*
8571 * ":redo".
8572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 static void
8574ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008575 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 u_redo(1);
8578}
8579
8580/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008581 * ":earlier" and ":later".
8582 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008583 static void
8584ex_later(eap)
8585 exarg_T *eap;
8586{
8587 long count = 0;
8588 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008589 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008590 char_u *p = eap->arg;
8591
8592 if (*p == NUL)
8593 count = 1;
8594 else if (isdigit(*p))
8595 {
8596 count = getdigits(&p);
8597 switch (*p)
8598 {
8599 case 's': ++p; sec = TRUE; break;
8600 case 'm': ++p; sec = TRUE; count *= 60; break;
8601 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008602 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8603 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008604 }
8605 }
8606
8607 if (*p != NUL)
8608 EMSG2(_(e_invarg2), eap->arg);
8609 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008610 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8611 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008612}
8613
8614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 * ":redir": start/stop redirection.
8616 */
8617 static void
8618ex_redir(eap)
8619 exarg_T *eap;
8620{
8621 char *mode;
8622 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008623 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625 if (STRICMP(eap->arg, "END") == 0)
8626 close_redir();
8627 else
8628 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008629 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008631 ++arg;
8632 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008634 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 mode = "a";
8636 }
8637 else
8638 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008639 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 close_redir();
8642
8643 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008644 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 if (fname == NULL)
8646 return;
8647#ifdef FEAT_BROWSE
8648 if (cmdmod.browse)
8649 {
8650 char_u *browseFile;
8651
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008652 browseFile = do_browse(BROWSE_SAVE,
8653 (char_u *)_("Save Redirection"),
8654 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 if (browseFile == NULL)
8656 return; /* operation cancelled */
8657 vim_free(fname);
8658 fname = browseFile;
8659 eap->forceit = TRUE; /* since dialog already asked */
8660 }
8661#endif
8662
8663 redir_fd = open_exfile(fname, eap->forceit, mode);
8664 vim_free(fname);
8665 }
8666#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008667 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
8669 /* redirect to a register a-z (resp. A-Z for appending) */
8670 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008671 ++arg;
8672 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008674 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008675 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008677 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008679 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008680 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008681 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008684 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008685 if (*arg == '>')
8686 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008687 /* Make register empty when not using @A-@Z and the
8688 * command is valid. */
8689 if (*arg == NUL && !isupper(redir_reg))
8690 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008692 }
8693 if (*arg != NUL)
8694 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008695 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008696 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697 }
8698 }
8699 else if (*arg == '=' && arg[1] == '>')
8700 {
8701 int append;
8702
8703 /* redirect to a variable */
8704 close_redir();
8705 arg += 2;
8706
8707 if (*arg == '>')
8708 {
8709 ++arg;
8710 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
8712 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008713 append = FALSE;
8714
8715 if (var_redir_start(skipwhite(arg), append) == OK)
8716 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 }
8718#endif
8719
8720 /* TODO: redirect to a buffer */
8721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008723 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008725
8726 /* Make sure redirection is not off. Can happen for cmdline completion
8727 * that indirectly invokes a command to catch its output. */
8728 if (redir_fd != NULL
8729#ifdef FEAT_EVAL
8730 || redir_reg || redir_vname
8731#endif
8732 )
8733 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734}
8735
8736/*
8737 * ":redraw": force redraw
8738 */
8739 static void
8740ex_redraw(eap)
8741 exarg_T *eap;
8742{
8743 int r = RedrawingDisabled;
8744 int p = p_lz;
8745
8746 RedrawingDisabled = 0;
8747 p_lz = FALSE;
8748 update_topline();
8749 update_screen(eap->forceit ? CLEAR :
8750#ifdef FEAT_VISUAL
8751 VIsual_active ? INVERTED :
8752#endif
8753 0);
8754#ifdef FEAT_TITLE
8755 if (need_maketitle)
8756 maketitle();
8757#endif
8758 RedrawingDisabled = r;
8759 p_lz = p;
8760
8761 /* Reset msg_didout, so that a message that's there is overwritten. */
8762 msg_didout = FALSE;
8763 msg_col = 0;
8764
8765 out_flush();
8766}
8767
8768/*
8769 * ":redrawstatus": force redraw of status line(s)
8770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 static void
8772ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008773 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775#if defined(FEAT_WINDOWS)
8776 int r = RedrawingDisabled;
8777 int p = p_lz;
8778
8779 RedrawingDisabled = 0;
8780 p_lz = FALSE;
8781 if (eap->forceit)
8782 status_redraw_all();
8783 else
8784 status_redraw_curbuf();
8785 update_screen(
8786# ifdef FEAT_VISUAL
8787 VIsual_active ? INVERTED :
8788# endif
8789 0);
8790 RedrawingDisabled = r;
8791 p_lz = p;
8792 out_flush();
8793#endif
8794}
8795
8796 static void
8797close_redir()
8798{
8799 if (redir_fd != NULL)
8800 {
8801 fclose(redir_fd);
8802 redir_fd = NULL;
8803 }
8804#ifdef FEAT_EVAL
8805 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008806 if (redir_vname)
8807 {
8808 var_redir_stop();
8809 redir_vname = 0;
8810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811#endif
8812}
8813
8814#if defined(FEAT_SESSION) && defined(USE_CRNL)
8815# define MKSESSION_NL
8816static int mksession_nl = FALSE; /* use NL only in put_eol() */
8817#endif
8818
8819/*
8820 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8821 */
8822 static void
8823ex_mkrc(eap)
8824 exarg_T *eap;
8825{
8826 FILE *fd;
8827 int failed = FALSE;
8828 char_u *fname;
8829#ifdef FEAT_BROWSE
8830 char_u *browseFile = NULL;
8831#endif
8832#ifdef FEAT_SESSION
8833 int view_session = FALSE;
8834 int using_vdir = FALSE; /* using 'viewdir'? */
8835 char_u *viewFile = NULL;
8836 unsigned *flagp;
8837#endif
8838
8839 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8840 {
8841#ifdef FEAT_SESSION
8842 view_session = TRUE;
8843#else
8844 ex_ni(eap);
8845 return;
8846#endif
8847 }
8848
8849#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008850 /* Use the short file name until ":lcd" is used. We also don't use the
8851 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008852 did_lcd = FALSE;
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8855 if (eap->cmdidx == CMD_mkview
8856 && (*eap->arg == NUL
8857 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8858 {
8859 eap->forceit = TRUE;
8860 fname = get_view_file(*eap->arg);
8861 if (fname == NULL)
8862 return;
8863 viewFile = fname;
8864 using_vdir = TRUE;
8865 }
8866 else
8867#endif
8868 if (*eap->arg != NUL)
8869 fname = eap->arg;
8870 else if (eap->cmdidx == CMD_mkvimrc)
8871 fname = (char_u *)VIMRC_FILE;
8872#ifdef FEAT_SESSION
8873 else if (eap->cmdidx == CMD_mksession)
8874 fname = (char_u *)SESSION_FILE;
8875#endif
8876 else
8877 fname = (char_u *)EXRC_FILE;
8878
8879#ifdef FEAT_BROWSE
8880 if (cmdmod.browse)
8881 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008882 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883# ifdef FEAT_SESSION
8884 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8885 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8886# endif
8887 (char_u *)_("Save Setup"),
8888 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8889 if (browseFile == NULL)
8890 goto theend;
8891 fname = browseFile;
8892 eap->forceit = TRUE; /* since dialog already asked */
8893 }
8894#endif
8895
8896#if defined(FEAT_SESSION) && defined(vim_mkdir)
8897 /* When using 'viewdir' may have to create the directory. */
8898 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008899 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900#endif
8901
8902 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8903 if (fd != NULL)
8904 {
8905#ifdef FEAT_SESSION
8906 if (eap->cmdidx == CMD_mkview)
8907 flagp = &vop_flags;
8908 else
8909 flagp = &ssop_flags;
8910#endif
8911
8912#ifdef MKSESSION_NL
8913 /* "unix" in 'sessionoptions': use NL line separator */
8914 if (view_session && (*flagp & SSOP_UNIX))
8915 mksession_nl = TRUE;
8916#endif
8917
8918 /* Write the version command for :mkvimrc */
8919 if (eap->cmdidx == CMD_mkvimrc)
8920 (void)put_line(fd, "version 6.0");
8921
8922#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008923 if (eap->cmdidx == CMD_mksession)
8924 {
8925 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8926 failed = TRUE;
8927 }
8928
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 if (eap->cmdidx != CMD_mkview)
8930#endif
8931 {
8932 /* Write setting 'compatible' first, because it has side effects.
8933 * For that same reason only do it when needed. */
8934 if (p_cp)
8935 (void)put_line(fd, "if !&cp | set cp | endif");
8936 else
8937 (void)put_line(fd, "if &cp | set nocp | endif");
8938 }
8939
8940#ifdef FEAT_SESSION
8941 if (!view_session
8942 || (eap->cmdidx == CMD_mksession
8943 && (*flagp & SSOP_OPTIONS)))
8944#endif
8945 failed |= (makemap(fd, NULL) == FAIL
8946 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8947
8948#ifdef FEAT_SESSION
8949 if (!failed && view_session)
8950 {
8951 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8952 failed = TRUE;
8953 if (eap->cmdidx == CMD_mksession)
8954 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008955 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
Bram Moolenaard9462e32011-04-11 21:35:11 +02008957 dirnow = alloc(MAXPATHL);
8958 if (dirnow == NULL)
8959 failed = TRUE;
8960 else
8961 {
8962 /*
8963 * Change to session file's dir.
8964 */
8965 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008967 *dirnow = NUL;
8968 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8969 {
8970 if (vim_chdirfile(fname) == OK)
8971 shorten_fnames(TRUE);
8972 }
8973 else if (*dirnow != NUL
8974 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8975 {
8976 if (mch_chdir((char *)globaldir) == 0)
8977 shorten_fnames(TRUE);
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaard9462e32011-04-11 21:35:11 +02008980 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
Bram Moolenaard9462e32011-04-11 21:35:11 +02008982 /* restore original dir */
8983 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008985 {
8986 if (mch_chdir((char *)dirnow) != 0)
8987 EMSG(_(e_prev_dir));
8988 shorten_fnames(TRUE);
8989 }
8990 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
8992 }
8993 else
8994 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008995 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8996 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 }
8998 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8999 == FAIL)
9000 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009001 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9002 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009003 if (eap->cmdidx == CMD_mksession)
9004 {
9005 if (put_line(fd, "unlet SessionLoad") == FAIL)
9006 failed = TRUE;
9007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 }
9009#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009010 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9011 failed = TRUE;
9012
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 failed |= fclose(fd);
9014
9015 if (failed)
9016 EMSG(_(e_write));
9017#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9018 else if (eap->cmdidx == CMD_mksession)
9019 {
9020 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009021 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022
Bram Moolenaard9462e32011-04-11 21:35:11 +02009023 tbuf = alloc(MAXPATHL);
9024 if (tbuf != NULL)
9025 {
9026 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9027 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9028 vim_free(tbuf);
9029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
9031#endif
9032#ifdef MKSESSION_NL
9033 mksession_nl = FALSE;
9034#endif
9035 }
9036
9037#ifdef FEAT_BROWSE
9038theend:
9039 vim_free(browseFile);
9040#endif
9041#ifdef FEAT_SESSION
9042 vim_free(viewFile);
9043#endif
9044}
9045
Bram Moolenaardf177f62005-02-22 08:39:57 +00009046#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9047 || defined(PROTO)
9048 int
9049vim_mkdir_emsg(name, prot)
9050 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009051 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009052{
9053 if (vim_mkdir(name, prot) != 0)
9054 {
9055 EMSG2(_("E739: Cannot create directory: %s"), name);
9056 return FAIL;
9057 }
9058 return OK;
9059}
9060#endif
9061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
9063 * Open a file for writing for an Ex command, with some checks.
9064 * Return file descriptor, or NULL on failure.
9065 */
9066 FILE *
9067open_exfile(fname, forceit, mode)
9068 char_u *fname;
9069 int forceit;
9070 char *mode; /* "w" for create new file or "a" for append */
9071{
9072 FILE *fd;
9073
9074#ifdef UNIX
9075 /* with Unix it is possible to open a directory */
9076 if (mch_isdir(fname))
9077 {
9078 EMSG2(_(e_isadir2), fname);
9079 return NULL;
9080 }
9081#endif
9082 if (!forceit && *mode != 'a' && vim_fexists(fname))
9083 {
9084 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9085 return NULL;
9086 }
9087
9088 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9089 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9090
9091 return fd;
9092}
9093
9094/*
9095 * ":mark" and ":k".
9096 */
9097 static void
9098ex_mark(eap)
9099 exarg_T *eap;
9100{
9101 pos_T pos;
9102
9103 if (*eap->arg == NUL) /* No argument? */
9104 EMSG(_(e_argreq));
9105 else if (eap->arg[1] != NUL) /* more than one character? */
9106 EMSG(_(e_trailing));
9107 else
9108 {
9109 pos = curwin->w_cursor; /* save curwin->w_cursor */
9110 curwin->w_cursor.lnum = eap->line2;
9111 beginline(BL_WHITE | BL_FIX);
9112 if (setmark(*eap->arg) == FAIL) /* set mark */
9113 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9114 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9115 }
9116}
9117
9118/*
9119 * Update w_topline, w_leftcol and the cursor position.
9120 */
9121 void
9122update_topline_cursor()
9123{
9124 check_cursor(); /* put cursor on valid line */
9125 update_topline();
9126 if (!curwin->w_p_wrap)
9127 validate_cursor();
9128 update_curswant();
9129}
9130
9131#ifdef FEAT_EX_EXTRA
9132/*
9133 * ":normal[!] {commands}": Execute normal mode commands.
9134 */
9135 static void
9136ex_normal(eap)
9137 exarg_T *eap;
9138{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 int save_msg_scroll = msg_scroll;
9140 int save_restart_edit = restart_edit;
9141 int save_msg_didout = msg_didout;
9142 int save_State = State;
9143 tasave_T tabuf;
9144 int save_insertmode = p_im;
9145 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009146 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#ifdef FEAT_MBYTE
9148 char_u *arg = NULL;
9149 int l;
9150 char_u *p;
9151#endif
9152
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009153 if (ex_normal_lock > 0)
9154 {
9155 EMSG(_(e_secure));
9156 return;
9157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 if (ex_normal_busy >= p_mmd)
9159 {
9160 EMSG(_("E192: Recursive use of :normal too deep"));
9161 return;
9162 }
9163 ++ex_normal_busy;
9164
9165 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9166 restart_edit = 0; /* don't go to Insert mode */
9167 p_im = FALSE; /* don't use 'insertmode' */
9168
9169#ifdef FEAT_MBYTE
9170 /*
9171 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9172 * this for the K_SPECIAL leading byte, otherwise special keys will not
9173 * work.
9174 */
9175 if (has_mbyte)
9176 {
9177 int len = 0;
9178
9179 /* Count the number of characters to be escaped. */
9180 for (p = eap->arg; *p != NUL; ++p)
9181 {
9182# ifdef FEAT_GUI
9183 if (*p == CSI) /* leadbyte CSI */
9184 len += 2;
9185# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009186 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9188# ifdef FEAT_GUI
9189 || *p == CSI
9190# endif
9191 )
9192 len += 2;
9193 }
9194 if (len > 0)
9195 {
9196 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9197 if (arg != NULL)
9198 {
9199 len = 0;
9200 for (p = eap->arg; *p != NUL; ++p)
9201 {
9202 arg[len++] = *p;
9203# ifdef FEAT_GUI
9204 if (*p == CSI)
9205 {
9206 arg[len++] = KS_EXTRA;
9207 arg[len++] = (int)KE_CSI;
9208 }
9209# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009210 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 {
9212 arg[len++] = *++p;
9213 if (*p == K_SPECIAL)
9214 {
9215 arg[len++] = KS_SPECIAL;
9216 arg[len++] = KE_FILLER;
9217 }
9218# ifdef FEAT_GUI
9219 else if (*p == CSI)
9220 {
9221 arg[len++] = KS_EXTRA;
9222 arg[len++] = (int)KE_CSI;
9223 }
9224# endif
9225 }
9226 arg[len] = NUL;
9227 }
9228 }
9229 }
9230 }
9231#endif
9232
9233 /*
9234 * Save the current typeahead. This is required to allow using ":normal"
9235 * from an event handler and makes sure we don't hang when the argument
9236 * ends with half a command.
9237 */
9238 save_typeahead(&tabuf);
9239 if (tabuf.typebuf_valid)
9240 {
9241 /*
9242 * Repeat the :normal command for each line in the range. When no
9243 * range given, execute it just once, without positioning the cursor
9244 * first.
9245 */
9246 do
9247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 if (eap->addr_count != 0)
9249 {
9250 curwin->w_cursor.lnum = eap->line1++;
9251 curwin->w_cursor.col = 0;
9252 }
9253
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009254 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255#ifdef FEAT_MBYTE
9256 arg != NULL ? arg :
9257#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009258 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
9260 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9261 }
9262
9263 /* Might not return to the main loop when in an event handler. */
9264 update_topline_cursor();
9265
9266 /* Restore the previous typeahead. */
9267 restore_typeahead(&tabuf);
9268
9269 --ex_normal_busy;
9270 msg_scroll = save_msg_scroll;
9271 restart_edit = save_restart_edit;
9272 p_im = save_insertmode;
9273 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009274 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9276
9277 /* Restore the state (needed when called from a function executed for
9278 * 'indentexpr'). */
9279 State = save_State;
9280#ifdef FEAT_MBYTE
9281 vim_free(arg);
9282#endif
9283}
9284
9285/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009286 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 */
9288 static void
9289ex_startinsert(eap)
9290 exarg_T *eap;
9291{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009292 if (eap->forceit)
9293 {
9294 coladvance((colnr_T)MAXCOL);
9295 curwin->w_curswant = MAXCOL;
9296 curwin->w_set_curswant = FALSE;
9297 }
9298
Bram Moolenaara40c5002005-01-09 21:16:21 +00009299 /* Ignore the command when already in Insert mode. Inserting an
9300 * expression register that invokes a function can do this. */
9301 if (State & INSERT)
9302 return;
9303
Bram Moolenaar64969662005-12-14 21:59:55 +00009304 if (eap->cmdidx == CMD_startinsert)
9305 restart_edit = 'a';
9306 else if (eap->cmdidx == CMD_startreplace)
9307 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009309 restart_edit = 'V';
9310
9311 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009313 if (eap->cmdidx == CMD_startinsert)
9314 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 curwin->w_curswant = 0; /* avoid MAXCOL */
9316 }
9317}
9318
9319/*
9320 * ":stopinsert"
9321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 static void
9323ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009324 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 restart_edit = 0;
9327 stop_insert_mode = TRUE;
9328}
9329#endif
9330
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009331#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9332/*
9333 * Execute normal mode command "cmd".
9334 * "remap" can be REMAP_NONE or REMAP_YES.
9335 */
9336 void
9337exec_normal_cmd(cmd, remap, silent)
9338 char_u *cmd;
9339 int remap;
9340 int silent;
9341{
9342 oparg_T oa;
9343
9344 /*
9345 * Stuff the argument into the typeahead buffer.
9346 * Execute normal_cmd() until there is no typeahead left.
9347 */
9348 clear_oparg(&oa);
9349 finish_op = FALSE;
9350 ins_typebuf(cmd, remap, 0, TRUE, silent);
9351 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9352 && !got_int)
9353 {
9354 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009355 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009356 }
9357}
9358#endif
9359
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360#ifdef FEAT_FIND_ID
9361 static void
9362ex_checkpath(eap)
9363 exarg_T *eap;
9364{
9365 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9366 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9367 (linenr_T)1, (linenr_T)MAXLNUM);
9368}
9369
9370#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9371/*
9372 * ":psearch"
9373 */
9374 static void
9375ex_psearch(eap)
9376 exarg_T *eap;
9377{
9378 g_do_tagpreview = p_pvh;
9379 ex_findpat(eap);
9380 g_do_tagpreview = 0;
9381}
9382#endif
9383
9384 static void
9385ex_findpat(eap)
9386 exarg_T *eap;
9387{
9388 int whole = TRUE;
9389 long n;
9390 char_u *p;
9391 int action;
9392
9393 switch (cmdnames[eap->cmdidx].cmd_name[2])
9394 {
9395 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9396 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9397 action = ACTION_GOTO;
9398 else
9399 action = ACTION_SHOW;
9400 break;
9401 case 'i': /* ":ilist" and ":dlist" */
9402 action = ACTION_SHOW_ALL;
9403 break;
9404 case 'u': /* ":ijump" and ":djump" */
9405 action = ACTION_GOTO;
9406 break;
9407 default: /* ":isplit" and ":dsplit" */
9408 action = ACTION_SPLIT;
9409 break;
9410 }
9411
9412 n = 1;
9413 if (vim_isdigit(*eap->arg)) /* get count */
9414 {
9415 n = getdigits(&eap->arg);
9416 eap->arg = skipwhite(eap->arg);
9417 }
9418 if (*eap->arg == '/') /* Match regexp, not just whole words */
9419 {
9420 whole = FALSE;
9421 ++eap->arg;
9422 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9423 if (*p)
9424 {
9425 *p++ = NUL;
9426 p = skipwhite(p);
9427
9428 /* Check for trailing illegal characters */
9429 if (!ends_excmd(*p))
9430 eap->errmsg = e_trailing;
9431 else
9432 eap->nextcmd = check_nextcmd(p);
9433 }
9434 }
9435 if (!eap->skip)
9436 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9437 whole, !eap->forceit,
9438 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9439 n, action, eap->line1, eap->line2);
9440}
9441#endif
9442
9443#ifdef FEAT_WINDOWS
9444
9445# ifdef FEAT_QUICKFIX
9446/*
9447 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9448 */
9449 static void
9450ex_ptag(eap)
9451 exarg_T *eap;
9452{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009453 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9455}
9456
9457/*
9458 * ":pedit"
9459 */
9460 static void
9461ex_pedit(eap)
9462 exarg_T *eap;
9463{
9464 win_T *curwin_save = curwin;
9465
9466 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009467 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 keep_help_flag = curwin_save->w_buffer->b_help;
9469 do_exedit(eap, NULL);
9470 keep_help_flag = FALSE;
9471 if (curwin != curwin_save && win_valid(curwin_save))
9472 {
9473 /* Return cursor to where we were */
9474 validate_cursor();
9475 redraw_later(VALID);
9476 win_enter(curwin_save, TRUE);
9477 }
9478 g_do_tagpreview = 0;
9479}
9480# endif
9481
9482/*
9483 * ":stag", ":stselect" and ":stjump".
9484 */
9485 static void
9486ex_stag(eap)
9487 exarg_T *eap;
9488{
9489 postponed_split = -1;
9490 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009491 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9493 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009494 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495}
9496#endif
9497
9498/*
9499 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9500 */
9501 static void
9502ex_tag(eap)
9503 exarg_T *eap;
9504{
9505 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9506}
9507
9508 static void
9509ex_tag_cmd(eap, name)
9510 exarg_T *eap;
9511 char_u *name;
9512{
9513 int cmd;
9514
9515 switch (name[1])
9516 {
9517 case 'j': cmd = DT_JUMP; /* ":tjump" */
9518 break;
9519 case 's': cmd = DT_SELECT; /* ":tselect" */
9520 break;
9521 case 'p': cmd = DT_PREV; /* ":tprevious" */
9522 break;
9523 case 'N': cmd = DT_PREV; /* ":tNext" */
9524 break;
9525 case 'n': cmd = DT_NEXT; /* ":tnext" */
9526 break;
9527 case 'o': cmd = DT_POP; /* ":pop" */
9528 break;
9529 case 'f': /* ":tfirst" */
9530 case 'r': cmd = DT_FIRST; /* ":trewind" */
9531 break;
9532 case 'l': cmd = DT_LAST; /* ":tlast" */
9533 break;
9534 default: /* ":tag" */
9535#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009536 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
9538 do_cstag(eap);
9539 return;
9540 }
9541#endif
9542 cmd = DT_TAG;
9543 break;
9544 }
9545
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009546 if (name[0] == 'l')
9547 {
9548#ifndef FEAT_QUICKFIX
9549 ex_ni(eap);
9550 return;
9551#else
9552 cmd = DT_LTAG;
9553#endif
9554 }
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9557 eap->forceit, TRUE);
9558}
9559
9560/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009561 * Check "str" for starting with a special cmdline variable.
9562 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9563 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9564 */
9565 int
9566find_cmdline_var(src, usedlen)
9567 char_u *src;
9568 int *usedlen;
9569{
9570 int len;
9571 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009572 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009573 "%",
9574#define SPEC_PERC 0
9575 "#",
9576#define SPEC_HASH 1
9577 "<cword>", /* cursor word */
9578#define SPEC_CWORD 2
9579 "<cWORD>", /* cursor WORD */
9580#define SPEC_CCWORD 3
9581 "<cfile>", /* cursor path name */
9582#define SPEC_CFILE 4
9583 "<sfile>", /* ":so" file name */
9584#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009585 "<slnum>", /* ":so" file line number */
9586#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009587#ifdef FEAT_AUTOCMD
9588 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009589# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009590 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009591# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009592 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009593# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009594#endif
9595#ifdef FEAT_CLIENTSERVER
9596 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009597# ifdef FEAT_AUTOCMD
9598# define SPEC_CLIENT 10
9599# else
9600# define SPEC_CLIENT 7
9601# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009602#endif
9603 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009604
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009605 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009606 {
9607 len = (int)STRLEN(spec_str[i]);
9608 if (STRNCMP(src, spec_str[i], len) == 0)
9609 {
9610 *usedlen = len;
9611 return i;
9612 }
9613 }
9614 return -1;
9615}
9616
9617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 * Evaluate cmdline variables.
9619 *
9620 * change '%' to curbuf->b_ffname
9621 * '#' to curwin->w_altfile
9622 * '<cword>' to word under the cursor
9623 * '<cWORD>' to WORD under the cursor
9624 * '<cfile>' to path name under the cursor
9625 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009626 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 * '<afile>' to file name for autocommand
9628 * '<abuf>' to buffer number for autocommand
9629 * '<amatch>' to matching name for autocommand
9630 *
9631 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9632 * "" for error without a message) and NULL is returned.
9633 * Returns an allocated string if a valid match was found.
9634 * Returns NULL if no match was found. "usedlen" then still contains the
9635 * number of characters to skip.
9636 */
9637 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009638eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009640 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 int *usedlen; /* characters after src that are used */
9642 linenr_T *lnump; /* line number for :e command, or NULL */
9643 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009644 int *escaped; /* return value has escaped white space (can
9645 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
9647 int i;
9648 char_u *s;
9649 char_u *result;
9650 char_u *resultbuf = NULL;
9651 int resultlen;
9652 buf_T *buf;
9653 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9654 int spec_idx;
9655#ifdef FEAT_MODIFY_FNAME
9656 int skip_mod = FALSE;
9657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659
9660 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009661 if (escaped != NULL)
9662 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663
9664 /*
9665 * Check if there is something to do.
9666 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009667 spec_idx = find_cmdline_var(src, usedlen);
9668 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 {
9670 *usedlen = 1;
9671 return NULL;
9672 }
9673
9674 /*
9675 * Skip when preceded with a backslash "\%" and "\#".
9676 * Note: In "\\%" the % is also not recognized!
9677 */
9678 if (src > srcstart && src[-1] == '\\')
9679 {
9680 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009681 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 return NULL;
9683 }
9684
9685 /*
9686 * word or WORD under cursor
9687 */
9688 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9689 {
9690 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9691 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9692 if (resultlen == 0)
9693 {
9694 *errormsg = (char_u *)"";
9695 return NULL;
9696 }
9697 }
9698
9699 /*
9700 * '#': Alternate file name
9701 * '%': Current file name
9702 * File name under the cursor
9703 * File name for autocommand
9704 * and following modifiers
9705 */
9706 else
9707 {
9708 switch (spec_idx)
9709 {
9710 case SPEC_PERC: /* '%': current file */
9711 if (curbuf->b_fname == NULL)
9712 {
9713 result = (char_u *)"";
9714 valid = 0; /* Must have ":p:h" to be valid */
9715 }
9716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 break;
9719
9720 case SPEC_HASH: /* '#' or "#99": alternate file */
9721 if (src[1] == '#') /* "##": the argument list */
9722 {
9723 result = arg_all();
9724 resultbuf = result;
9725 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009726 if (escaped != NULL)
9727 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728#ifdef FEAT_MODIFY_FNAME
9729 skip_mod = TRUE;
9730#endif
9731 break;
9732 }
9733 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009734 if (*s == '<') /* "#<99" uses v:oldfiles */
9735 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 i = (int)getdigits(&s);
9737 *usedlen = (int)(s - src); /* length of what we expand */
9738
Bram Moolenaard812df62008-11-09 12:46:09 +00009739 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009741 if (*usedlen < 2)
9742 {
9743 /* Should we give an error message for #<text? */
9744 *usedlen = 1;
9745 return NULL;
9746 }
9747#ifdef FEAT_EVAL
9748 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9749 (long)i);
9750 if (result == NULL)
9751 {
9752 *errormsg = (char_u *)"";
9753 return NULL;
9754 }
9755#else
9756 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 }
9760 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009761 {
9762 buf = buflist_findnr(i);
9763 if (buf == NULL)
9764 {
9765 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9766 return NULL;
9767 }
9768 if (lnump != NULL)
9769 *lnump = ECMD_LAST;
9770 if (buf->b_fname == NULL)
9771 {
9772 result = (char_u *)"";
9773 valid = 0; /* Must have ":p:h" to be valid */
9774 }
9775 else
9776 result = buf->b_fname;
9777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 break;
9779
9780#ifdef FEAT_SEARCHPATH
9781 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009782 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 if (result == NULL)
9784 {
9785 *errormsg = (char_u *)"";
9786 return NULL;
9787 }
9788 resultbuf = result; /* remember allocated string */
9789 break;
9790#endif
9791
9792#ifdef FEAT_AUTOCMD
9793 case SPEC_AFILE: /* file name for autocommand */
9794 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009795 if (result != NULL && !autocmd_fname_full)
9796 {
9797 /* Still need to turn the fname into a full path. It is
9798 * postponed to avoid a delay when <afile> is not used. */
9799 autocmd_fname_full = TRUE;
9800 result = FullName_save(autocmd_fname, FALSE);
9801 vim_free(autocmd_fname);
9802 autocmd_fname = result;
9803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (result == NULL)
9805 {
9806 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9807 return NULL;
9808 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009809 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 break;
9811
9812 case SPEC_ABUF: /* buffer number for autocommand */
9813 if (autocmd_bufnr <= 0)
9814 {
9815 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9816 return NULL;
9817 }
9818 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9819 result = strbuf;
9820 break;
9821
9822 case SPEC_AMATCH: /* match name for autocommand */
9823 result = autocmd_match;
9824 if (result == NULL)
9825 {
9826 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9827 return NULL;
9828 }
9829 break;
9830
9831#endif
9832 case SPEC_SFILE: /* file name for ":so" command */
9833 result = sourcing_name;
9834 if (result == NULL)
9835 {
9836 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9837 return NULL;
9838 }
9839 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009840 case SPEC_SLNUM: /* line in file for ":so" command */
9841 if (sourcing_name == NULL || sourcing_lnum == 0)
9842 {
9843 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9844 return NULL;
9845 }
9846 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9847 result = strbuf;
9848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849#if defined(FEAT_CLIENTSERVER)
9850 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009851 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9852 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 result = strbuf;
9854 break;
9855#endif
9856 }
9857
9858 resultlen = (int)STRLEN(result); /* length of new string */
9859 if (src[*usedlen] == '<') /* remove the file name extension */
9860 {
9861 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 resultlen = (int)(s - result);
9864 }
9865#ifdef FEAT_MODIFY_FNAME
9866 else if (!skip_mod)
9867 {
9868 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9869 &resultlen);
9870 if (result == NULL)
9871 {
9872 *errormsg = (char_u *)"";
9873 return NULL;
9874 }
9875 }
9876#endif
9877 }
9878
9879 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9880 {
9881 if (valid != VALID_HEAD + VALID_PATH)
9882 /* xgettext:no-c-format */
9883 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9884 else
9885 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9886 result = NULL;
9887 }
9888 else
9889 result = vim_strnsave(result, resultlen);
9890 vim_free(resultbuf);
9891 return result;
9892}
9893
9894/*
9895 * Concatenate all files in the argument list, separated by spaces, and return
9896 * it in one allocated string.
9897 * Spaces and backslashes in the file names are escaped with a backslash.
9898 * Returns NULL when out of memory.
9899 */
9900 static char_u *
9901arg_all()
9902{
9903 int len;
9904 int idx;
9905 char_u *retval = NULL;
9906 char_u *p;
9907
9908 /*
9909 * Do this loop two times:
9910 * first time: compute the total length
9911 * second time: concatenate the names
9912 */
9913 for (;;)
9914 {
9915 len = 0;
9916 for (idx = 0; idx < ARGCOUNT; ++idx)
9917 {
9918 p = alist_name(&ARGLIST[idx]);
9919 if (p != NULL)
9920 {
9921 if (len > 0)
9922 {
9923 /* insert a space in between names */
9924 if (retval != NULL)
9925 retval[len] = ' ';
9926 ++len;
9927 }
9928 for ( ; *p != NUL; ++p)
9929 {
9930 if (*p == ' ' || *p == '\\')
9931 {
9932 /* insert a backslash */
9933 if (retval != NULL)
9934 retval[len] = '\\';
9935 ++len;
9936 }
9937 if (retval != NULL)
9938 retval[len] = *p;
9939 ++len;
9940 }
9941 }
9942 }
9943
9944 /* second time: break here */
9945 if (retval != NULL)
9946 {
9947 retval[len] = NUL;
9948 break;
9949 }
9950
9951 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009952 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (retval == NULL)
9954 break;
9955 }
9956
9957 return retval;
9958}
9959
9960#if defined(FEAT_AUTOCMD) || defined(PROTO)
9961/*
9962 * Expand the <sfile> string in "arg".
9963 *
9964 * Returns an allocated string, or NULL for any error.
9965 */
9966 char_u *
9967expand_sfile(arg)
9968 char_u *arg;
9969{
9970 char_u *errormsg;
9971 int len;
9972 char_u *result;
9973 char_u *newres;
9974 char_u *repl;
9975 int srclen;
9976 char_u *p;
9977
9978 result = vim_strsave(arg);
9979 if (result == NULL)
9980 return NULL;
9981
9982 for (p = result; *p; )
9983 {
9984 if (STRNCMP(p, "<sfile>", 7) != 0)
9985 ++p;
9986 else
9987 {
9988 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009989 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 if (errormsg != NULL)
9991 {
9992 if (*errormsg)
9993 emsg(errormsg);
9994 vim_free(result);
9995 return NULL;
9996 }
9997 if (repl == NULL) /* no match (cannot happen) */
9998 {
9999 p += srclen;
10000 continue;
10001 }
10002 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10003 newres = alloc(len);
10004 if (newres == NULL)
10005 {
10006 vim_free(repl);
10007 vim_free(result);
10008 return NULL;
10009 }
10010 mch_memmove(newres, result, (size_t)(p - result));
10011 STRCPY(newres + (p - result), repl);
10012 len = (int)STRLEN(newres);
10013 STRCAT(newres, p + srclen);
10014 vim_free(repl);
10015 vim_free(result);
10016 result = newres;
10017 p = newres + len; /* continue after the match */
10018 }
10019 }
10020
10021 return result;
10022}
10023#endif
10024
10025#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010026static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10027 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10029static frame_T *ses_skipframe __ARGS((frame_T *fr));
10030static int ses_do_frame __ARGS((frame_T *fr));
10031static int ses_do_win __ARGS((win_T *wp));
10032static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10033static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10034static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10035
10036/*
10037 * Write openfile commands for the current buffers to an .exrc file.
10038 * Return FAIL on error, OK otherwise.
10039 */
10040 static int
10041makeopens(fd, dirnow)
10042 FILE *fd;
10043 char_u *dirnow; /* Current directory name */
10044{
10045 buf_T *buf;
10046 int only_save_windows = TRUE;
10047 int nr;
10048 int cnr = 1;
10049 int restore_size = TRUE;
10050 win_T *wp;
10051 char_u *sname;
10052 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010053 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010054 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010055 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010056 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010057 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
10059 if (ssop_flags & SSOP_BUFFERS)
10060 only_save_windows = FALSE; /* Save ALL buffers */
10061
10062 /*
10063 * Begin by setting the this_session variable, and then other
10064 * sessionable variables.
10065 */
10066#ifdef FEAT_EVAL
10067 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10068 return FAIL;
10069 if (ssop_flags & SSOP_GLOBALS)
10070 if (store_session_globals(fd) == FAIL)
10071 return FAIL;
10072#endif
10073
10074 /*
10075 * Close all windows but one.
10076 */
10077 if (put_line(fd, "silent only") == FAIL)
10078 return FAIL;
10079
10080 /*
10081 * Now a :cd command to the session directory or the current directory
10082 */
10083 if (ssop_flags & SSOP_SESDIR)
10084 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010085 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10086 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 return FAIL;
10088 }
10089 else if (ssop_flags & SSOP_CURDIR)
10090 {
10091 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10092 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010093 || fputs("cd ", fd) < 0
10094 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10095 || put_eol(fd) == FAIL)
10096 {
10097 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 vim_free(sname);
10101 }
10102
10103 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010104 * If there is an empty, unnamed buffer we will wipe it out later.
10105 * Remember the buffer number.
10106 */
10107 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10108 return FAIL;
10109 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10110 return FAIL;
10111 if (put_line(fd, "endif") == FAIL)
10112 return FAIL;
10113
10114 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 * Now save the current files, current buffer first.
10116 */
10117 if (put_line(fd, "set shortmess=aoO") == FAIL)
10118 return FAIL;
10119
10120 /* Now put the other buffers into the buffer list */
10121 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10122 {
10123 if (!(only_save_windows && buf->b_nwindows == 0)
10124 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10125 && buf->b_fname != NULL
10126 && buf->b_p_bl)
10127 {
10128 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10129 : buf->b_wininfo->wi_fpos.lnum) < 0
10130 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10131 return FAIL;
10132 }
10133 }
10134
10135 /* the global argument list */
10136 if (ses_arglist(fd, "args", &global_alist.al_ga,
10137 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10138 return FAIL;
10139
10140 if (ssop_flags & SSOP_RESIZE)
10141 {
10142 /* Note: after the restore we still check it worked!*/
10143 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10144 || put_eol(fd) == FAIL)
10145 return FAIL;
10146 }
10147
10148#ifdef FEAT_GUI
10149 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10150 {
10151 int x, y;
10152
10153 if (gui_mch_get_winpos(&x, &y) == OK)
10154 {
10155 /* Note: after the restore we still check it worked!*/
10156 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10157 return FAIL;
10158 }
10159 }
10160#endif
10161
10162 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010163 * May repeat putting Windows for each tab, when "tabpages" is in
10164 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010165 * Don't use goto_tabpage(), it may change directory and trigger
10166 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010168 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010169 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010170 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010172 int need_tabnew = FALSE;
10173
Bram Moolenaar18144c82006-04-12 21:52:12 +000010174 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010176 tabpage_T *tp = find_tabpage(tabnr);
10177
10178 if (tp == NULL)
10179 break; /* done all tab pages */
10180 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010181 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010182 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010183 tab_topframe = topframe;
10184 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010185 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010186 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010187 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010188 tab_topframe = tp->tp_topframe;
10189 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010190 if (tabnr > 1)
10191 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193
Bram Moolenaar18144c82006-04-12 21:52:12 +000010194 /*
10195 * Before creating the window layout, try loading one file. If this
10196 * is aborted we don't end up with a number of useless windows.
10197 * This may have side effects! (e.g., compressed or network file).
10198 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010199 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010200 {
10201 if (ses_do_win(wp)
10202 && wp->w_buffer->b_ffname != NULL
10203 && !wp->w_buffer->b_help
10204#ifdef FEAT_QUICKFIX
10205 && !bt_nofile(wp->w_buffer)
10206#endif
10207 )
10208 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010209 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010210 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10211 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010212 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010213 if (!wp->w_arg_idx_invalid)
10214 edited_win = wp;
10215 break;
10216 }
10217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010219 /* If no file got edited create an empty tab page. */
10220 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10221 return FAIL;
10222
Bram Moolenaar18144c82006-04-12 21:52:12 +000010223 /*
10224 * Save current window layout.
10225 */
10226 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010228 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010230 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10231 return FAIL;
10232 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
Bram Moolenaar18144c82006-04-12 21:52:12 +000010235 /*
10236 * Check if window sizes can be restored (no windows omitted).
10237 * Remember the window number of the current window after restoring.
10238 */
10239 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010240 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010241 {
10242 if (ses_do_win(wp))
10243 ++nr;
10244 else
10245 restore_size = FALSE;
10246 if (curwin == wp)
10247 cnr = nr;
10248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249
Bram Moolenaar18144c82006-04-12 21:52:12 +000010250 /* Go to the first window. */
10251 if (put_line(fd, "wincmd t") == FAIL)
10252 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010253
Bram Moolenaar18144c82006-04-12 21:52:12 +000010254 /*
10255 * If more than one window, see if sizes can be restored.
10256 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10257 * resized when moving between windows.
10258 * Do this before restoring the view, so that the topline and the
10259 * cursor can be set. This is done again below.
10260 */
10261 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10262 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010263 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265
Bram Moolenaar18144c82006-04-12 21:52:12 +000010266 /*
10267 * Restore the view of the window (options, file, cursor, etc.).
10268 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010269 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010270 {
10271 if (!ses_do_win(wp))
10272 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010273 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10274 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010275 return FAIL;
10276 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10277 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010278 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010279 }
10280
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010281 /* The argument index in the first tab page is zero, need to set it in
10282 * each window. For further tab pages it's the window where we do
10283 * "tabedit". */
10284 cur_arg_idx = next_arg_idx;
10285
Bram Moolenaar18144c82006-04-12 21:52:12 +000010286 /*
10287 * Restore cursor to the current window if it's not the first one.
10288 */
10289 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10290 || put_eol(fd) == FAIL))
10291 return FAIL;
10292
10293 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010294 * Restore window sizes again after jumping around in windows, because
10295 * the current window has a minimum size while others may not.
10296 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010297 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010298 return FAIL;
10299
Bram Moolenaar18144c82006-04-12 21:52:12 +000010300 /* Don't continue in another tab page when doing only the current one
10301 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010302 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010303 break;
10304 }
10305
10306 if (ssop_flags & SSOP_TABPAGES)
10307 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010308 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10309 || put_eol(fd) == FAIL)
10310 return FAIL;
10311 }
10312
Bram Moolenaar9c102382006-05-03 21:26:49 +000010313 /*
10314 * Wipe out an empty unnamed buffer we started in.
10315 */
10316 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10317 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010318 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010319 return FAIL;
10320 if (put_line(fd, "endif") == FAIL)
10321 return FAIL;
10322 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10323 return FAIL;
10324
10325 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10326 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10327 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329
10330 /*
10331 * Lastly, execute the x.vim file if it exists.
10332 */
10333 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10334 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010335 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 || put_line(fd, "endif") == FAIL)
10337 return FAIL;
10338
10339 return OK;
10340}
10341
10342 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010343ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 FILE *fd;
10345 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010346 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
10348 int n = 0;
10349 win_T *wp;
10350
10351 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10352 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010353 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 {
10355 if (!ses_do_win(wp))
10356 continue;
10357 ++n;
10358
10359 /* restore height when not full height */
10360 if (wp->w_height + wp->w_status_height < topframe->fr_height
10361 && (fprintf(fd,
10362 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10363 n, (long)wp->w_height, Rows / 2, Rows) < 0
10364 || put_eol(fd) == FAIL))
10365 return FAIL;
10366
10367 /* restore width when not full width */
10368 if (wp->w_width < Columns && (fprintf(fd,
10369 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10370 n, (long)wp->w_width, Columns / 2, Columns) < 0
10371 || put_eol(fd) == FAIL))
10372 return FAIL;
10373 }
10374 }
10375 else
10376 {
10377 /* Just equalise window sizes */
10378 if (put_line(fd, "wincmd =") == FAIL)
10379 return FAIL;
10380 }
10381 return OK;
10382}
10383
10384/*
10385 * Write commands to "fd" to recursively create windows for frame "fr",
10386 * horizontally and vertically split.
10387 * After the commands the last window in the frame is the current window.
10388 * Returns FAIL when writing the commands to "fd" fails.
10389 */
10390 static int
10391ses_win_rec(fd, fr)
10392 FILE *fd;
10393 frame_T *fr;
10394{
10395 frame_T *frc;
10396 int count = 0;
10397
10398 if (fr->fr_layout != FR_LEAF)
10399 {
10400 /* Find first frame that's not skipped and then create a window for
10401 * each following one (first frame is already there). */
10402 frc = ses_skipframe(fr->fr_child);
10403 if (frc != NULL)
10404 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10405 {
10406 /* Make window as big as possible so that we have lots of room
10407 * to split. */
10408 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10409 || put_line(fd, fr->fr_layout == FR_COL
10410 ? "split" : "vsplit") == FAIL)
10411 return FAIL;
10412 ++count;
10413 }
10414
10415 /* Go back to the first window. */
10416 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10417 ? "%dwincmd k" : "%dwincmd h", count) < 0
10418 || put_eol(fd) == FAIL))
10419 return FAIL;
10420
10421 /* Recursively create frames/windows in each window of this column or
10422 * row. */
10423 frc = ses_skipframe(fr->fr_child);
10424 while (frc != NULL)
10425 {
10426 ses_win_rec(fd, frc);
10427 frc = ses_skipframe(frc->fr_next);
10428 /* Go to next window. */
10429 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10430 return FAIL;
10431 }
10432 }
10433 return OK;
10434}
10435
10436/*
10437 * Skip frames that don't contain windows we want to save in the Session.
10438 * Returns NULL when there none.
10439 */
10440 static frame_T *
10441ses_skipframe(fr)
10442 frame_T *fr;
10443{
10444 frame_T *frc;
10445
10446 for (frc = fr; frc != NULL; frc = frc->fr_next)
10447 if (ses_do_frame(frc))
10448 break;
10449 return frc;
10450}
10451
10452/*
10453 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10454 * the Session.
10455 */
10456 static int
10457ses_do_frame(fr)
10458 frame_T *fr;
10459{
10460 frame_T *frc;
10461
10462 if (fr->fr_layout == FR_LEAF)
10463 return ses_do_win(fr->fr_win);
10464 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10465 if (ses_do_frame(frc))
10466 return TRUE;
10467 return FALSE;
10468}
10469
10470/*
10471 * Return non-zero if window "wp" is to be stored in the Session.
10472 */
10473 static int
10474ses_do_win(wp)
10475 win_T *wp;
10476{
10477 if (wp->w_buffer->b_fname == NULL
10478#ifdef FEAT_QUICKFIX
10479 /* When 'buftype' is "nofile" can't restore the window contents. */
10480 || bt_nofile(wp->w_buffer)
10481#endif
10482 )
10483 return (ssop_flags & SSOP_BLANK);
10484 if (wp->w_buffer->b_help)
10485 return (ssop_flags & SSOP_HELP);
10486 return TRUE;
10487}
10488
10489/*
10490 * Write commands to "fd" to restore the view of a window.
10491 * Caller must make sure 'scrolloff' is zero.
10492 */
10493 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010494put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 FILE *fd;
10496 win_T *wp;
10497 int add_edit; /* add ":edit" command to view */
10498 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010499 int current_arg_idx; /* current argument index of the window, use
10500 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 win_T *save_curwin;
10503 int f;
10504 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010505 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
10507 /* Always restore cursor position for ":mksession". For ":mkview" only
10508 * when 'viewoptions' contains "cursor". */
10509 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10510
10511 /*
10512 * Local argument list.
10513 */
10514 if (wp->w_alist == &global_alist)
10515 {
10516 if (put_line(fd, "argglobal") == FAIL)
10517 return FAIL;
10518 }
10519 else
10520 {
10521 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10522 flagp == &vop_flags
10523 || !(*flagp & SSOP_CURDIR)
10524 || wp->w_localdir != NULL, flagp) == FAIL)
10525 return FAIL;
10526 }
10527
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010528 /* Only when part of a session: restore the argument index. Some
10529 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010530 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010531 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010533 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 || put_eol(fd) == FAIL)
10535 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010536 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538
10539 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010540 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
10542 /*
10543 * Load the file.
10544 */
10545 if (wp->w_buffer->b_ffname != NULL
10546#ifdef FEAT_QUICKFIX
10547 && !bt_nofile(wp->w_buffer)
10548#endif
10549 )
10550 {
10551 /*
10552 * Editing a file in this buffer: use ":edit file".
10553 * This may have side effects! (e.g., compressed or network file).
10554 */
10555 if (fputs("edit ", fd) < 0
10556 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10557 return FAIL;
10558 }
10559 else
10560 {
10561 /* No file in this buffer, just make it empty. */
10562 if (put_line(fd, "enew") == FAIL)
10563 return FAIL;
10564#ifdef FEAT_QUICKFIX
10565 if (wp->w_buffer->b_ffname != NULL)
10566 {
10567 /* The buffer does have a name, but it's not a file name. */
10568 if (fputs("file ", fd) < 0
10569 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10570 return FAIL;
10571 }
10572#endif
10573 do_cursor = FALSE;
10574 }
10575 }
10576
10577 /*
10578 * Local mappings and abbreviations.
10579 */
10580 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10581 && makemap(fd, wp->w_buffer) == FAIL)
10582 return FAIL;
10583
10584 /*
10585 * Local options. Need to go to the window temporarily.
10586 * Store only local values when using ":mkview" and when ":mksession" is
10587 * used and 'sessionoptions' doesn't include "options".
10588 * Some folding options are always stored when "folds" is included,
10589 * otherwise the folds would not be restored correctly.
10590 */
10591 save_curwin = curwin;
10592 curwin = wp;
10593 curbuf = curwin->w_buffer;
10594 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10595 f = makeset(fd, OPT_LOCAL,
10596 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10597#ifdef FEAT_FOLDING
10598 else if (*flagp & SSOP_FOLDS)
10599 f = makefoldset(fd);
10600#endif
10601 else
10602 f = OK;
10603 curwin = save_curwin;
10604 curbuf = curwin->w_buffer;
10605 if (f == FAIL)
10606 return FAIL;
10607
10608#ifdef FEAT_FOLDING
10609 /*
10610 * Save Folds when 'buftype' is empty and for help files.
10611 */
10612 if ((*flagp & SSOP_FOLDS)
10613 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010614# ifdef FEAT_QUICKFIX
10615 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10616# endif
10617 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 {
10619 if (put_folds(fd, wp) == FAIL)
10620 return FAIL;
10621 }
10622#endif
10623
10624 /*
10625 * Set the cursor after creating folds, since that moves the cursor.
10626 */
10627 if (do_cursor)
10628 {
10629
10630 /* Restore the cursor line in the file and relatively in the
10631 * window. Don't use "G", it changes the jumplist. */
10632 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10633 (long)wp->w_cursor.lnum,
10634 (long)(wp->w_cursor.lnum - wp->w_topline),
10635 (long)wp->w_height / 2, (long)wp->w_height) < 0
10636 || put_eol(fd) == FAIL
10637 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10638 || put_line(fd, "exe s:l") == FAIL
10639 || put_line(fd, "normal! zt") == FAIL
10640 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10641 || put_eol(fd) == FAIL)
10642 return FAIL;
10643 /* Restore the cursor column and left offset when not wrapping. */
10644 if (wp->w_cursor.col == 0)
10645 {
10646 if (put_line(fd, "normal! 0") == FAIL)
10647 return FAIL;
10648 }
10649 else
10650 {
10651 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10652 {
10653 if (fprintf(fd,
10654 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10655 (long)wp->w_cursor.col,
10656 (long)(wp->w_cursor.col - wp->w_leftcol),
10657 (long)wp->w_width / 2, (long)wp->w_width) < 0
10658 || put_eol(fd) == FAIL
10659 || put_line(fd, "if s:c > 0") == FAIL
10660 || fprintf(fd,
10661 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10662 (long)wp->w_cursor.col) < 0
10663 || put_eol(fd) == FAIL
10664 || put_line(fd, "else") == FAIL
10665 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10666 || put_eol(fd) == FAIL
10667 || put_line(fd, "endif") == FAIL)
10668 return FAIL;
10669 }
10670 else
10671 {
10672 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10673 || put_eol(fd) == FAIL)
10674 return FAIL;
10675 }
10676 }
10677 }
10678
10679 /*
10680 * Local directory.
10681 */
10682 if (wp->w_localdir != NULL)
10683 {
10684 if (fputs("lcd ", fd) < 0
10685 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10686 || put_eol(fd) == FAIL)
10687 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010688 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 }
10690
10691 return OK;
10692}
10693
10694/*
10695 * Write an argument list to the session file.
10696 * Returns FAIL if writing fails.
10697 */
10698 static int
10699ses_arglist(fd, cmd, gap, fullname, flagp)
10700 FILE *fd;
10701 char *cmd;
10702 garray_T *gap;
10703 int fullname; /* TRUE: use full path name */
10704 unsigned *flagp;
10705{
10706 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010707 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 char_u *s;
10709
10710 if (gap->ga_len == 0)
10711 return put_line(fd, "silent! argdel *");
10712 if (fputs(cmd, fd) < 0)
10713 return FAIL;
10714 for (i = 0; i < gap->ga_len; ++i)
10715 {
10716 /* NULL file names are skipped (only happens when out of memory). */
10717 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10718 if (s != NULL)
10719 {
10720 if (fullname)
10721 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010722 buf = alloc(MAXPATHL);
10723 if (buf != NULL)
10724 {
10725 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10726 s = buf;
10727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 }
10729 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010730 {
10731 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010733 }
10734 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 }
10736 }
10737 return put_eol(fd);
10738}
10739
10740/*
10741 * Write a buffer name to the session file.
10742 * Also ends the line.
10743 * Returns FAIL if writing fails.
10744 */
10745 static int
10746ses_fname(fd, buf, flagp)
10747 FILE *fd;
10748 buf_T *buf;
10749 unsigned *flagp;
10750{
10751 char_u *name;
10752
10753 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010754 * the session file will be sourced.
10755 * Don't do this for ":mkview", we don't know the current directory.
10756 * Don't do this after ":lcd", we don't keep track of what the current
10757 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 if (buf->b_sfname != NULL
10759 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010760 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010761#ifdef FEAT_AUTOCHDIR
10762 && !p_acd
10763#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010764 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 name = buf->b_sfname;
10766 else
10767 name = buf->b_ffname;
10768 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10769 return FAIL;
10770 return OK;
10771}
10772
10773/*
10774 * Write a file name to the session file.
10775 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10776 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010777 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 */
10779 static int
10780ses_put_fname(fd, name, flagp)
10781 FILE *fd;
10782 char_u *name;
10783 unsigned *flagp;
10784{
10785 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010786 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788
10789 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010790 if (sname == NULL)
10791 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010793 if (*flagp & SSOP_SLASH)
10794 {
10795 /* change all backslashes to forward slashes */
10796 for (p = sname; *p != NUL; mb_ptr_adv(p))
10797 if (*p == '\\')
10798 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010800
10801 /* escapse special characters */
10802 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010804 if (p == NULL)
10805 return FAIL;
10806
10807 /* write the result */
10808 if (fputs((char *)p, fd) < 0)
10809 retval = FAIL;
10810
10811 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 return retval;
10813}
10814
10815/*
10816 * ":loadview [nr]"
10817 */
10818 static void
10819ex_loadview(eap)
10820 exarg_T *eap;
10821{
10822 char_u *fname;
10823
10824 fname = get_view_file(*eap->arg);
10825 if (fname != NULL)
10826 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010827 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 vim_free(fname);
10829 }
10830}
10831
10832/*
10833 * Get the name of the view file for the current buffer.
10834 */
10835 static char_u *
10836get_view_file(c)
10837 int c;
10838{
10839 int len = 0;
10840 char_u *p, *s;
10841 char_u *retval;
10842 char_u *sname;
10843
10844 if (curbuf->b_ffname == NULL)
10845 {
10846 EMSG(_(e_noname));
10847 return NULL;
10848 }
10849 sname = home_replace_save(NULL, curbuf->b_ffname);
10850 if (sname == NULL)
10851 return NULL;
10852
10853 /*
10854 * We want a file name without separators, because we're not going to make
10855 * a directory.
10856 * "normal" path separator -> "=+"
10857 * "=" -> "=="
10858 * ":" path separator -> "=-"
10859 */
10860 for (p = sname; *p; ++p)
10861 if (*p == '=' || vim_ispathsep(*p))
10862 ++len;
10863 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10864 if (retval != NULL)
10865 {
10866 STRCPY(retval, p_vdir);
10867 add_pathsep(retval);
10868 s = retval + STRLEN(retval);
10869 for (p = sname; *p; ++p)
10870 {
10871 if (*p == '=')
10872 {
10873 *s++ = '=';
10874 *s++ = '=';
10875 }
10876 else if (vim_ispathsep(*p))
10877 {
10878 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010879#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 if (*p == ':')
10881 *s++ = '-';
10882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010884 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 }
10886 else
10887 *s++ = *p;
10888 }
10889 *s++ = '=';
10890 *s++ = c;
10891 STRCPY(s, ".vim");
10892 }
10893
10894 vim_free(sname);
10895 return retval;
10896}
10897
10898#endif /* FEAT_SESSION */
10899
10900/*
10901 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10902 * Return FAIL for a write error.
10903 */
10904 int
10905put_eol(fd)
10906 FILE *fd;
10907{
10908 if (
10909#ifdef USE_CRNL
10910 (
10911# ifdef MKSESSION_NL
10912 !mksession_nl &&
10913# endif
10914 (putc('\r', fd) < 0)) ||
10915#endif
10916 (putc('\n', fd) < 0))
10917 return FAIL;
10918 return OK;
10919}
10920
10921/*
10922 * Write a line to "fd".
10923 * Return FAIL for a write error.
10924 */
10925 int
10926put_line(fd, s)
10927 FILE *fd;
10928 char *s;
10929{
10930 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10931 return FAIL;
10932 return OK;
10933}
10934
10935#ifdef FEAT_VIMINFO
10936/*
10937 * ":rviminfo" and ":wviminfo".
10938 */
10939 static void
10940ex_viminfo(eap)
10941 exarg_T *eap;
10942{
10943 char_u *save_viminfo;
10944
10945 save_viminfo = p_viminfo;
10946 if (*p_viminfo == NUL)
10947 p_viminfo = (char_u *)"'100";
10948 if (eap->cmdidx == CMD_rviminfo)
10949 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010950 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10951 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 EMSG(_("E195: Cannot open viminfo file for reading"));
10953 }
10954 else
10955 write_viminfo(eap->arg, eap->forceit);
10956 p_viminfo = save_viminfo;
10957}
10958#endif
10959
10960#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010961/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010962 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010963 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 void
10966dialog_msg(buff, format, fname)
10967 char_u *buff;
10968 char *format;
10969 char_u *fname;
10970{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 if (fname == NULL)
10972 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010973 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975#endif
10976
10977/*
10978 * ":behave {mswin,xterm}"
10979 */
10980 static void
10981ex_behave(eap)
10982 exarg_T *eap;
10983{
10984 if (STRCMP(eap->arg, "mswin") == 0)
10985 {
10986 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10987 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10988 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10989 set_option_value((char_u *)"keymodel", 0L,
10990 (char_u *)"startsel,stopsel", 0);
10991 }
10992 else if (STRCMP(eap->arg, "xterm") == 0)
10993 {
10994 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10995 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10996 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10997 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10998 }
10999 else
11000 EMSG2(_(e_invarg2), eap->arg);
11001}
11002
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011003#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11004/*
11005 * Function given to ExpandGeneric() to obtain the possible arguments of the
11006 * ":behave {mswin,xterm}" command.
11007 */
11008 char_u *
11009get_behave_arg(xp, idx)
11010 expand_T *xp UNUSED;
11011 int idx;
11012{
11013 if (idx == 0)
11014 return (char_u *)"mswin";
11015 if (idx == 1)
11016 return (char_u *)"xterm";
11017 return NULL;
11018}
11019#endif
11020
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021#ifdef FEAT_AUTOCMD
11022static int filetype_detect = FALSE;
11023static int filetype_plugin = FALSE;
11024static int filetype_indent = FALSE;
11025
11026/*
11027 * ":filetype [plugin] [indent] {on,off,detect}"
11028 * on: Load the filetype.vim file to install autocommands for file types.
11029 * off: Load the ftoff.vim file to remove all autocommands for file types.
11030 * plugin on: load filetype.vim and ftplugin.vim
11031 * plugin off: load ftplugof.vim
11032 * indent on: load filetype.vim and indent.vim
11033 * indent off: load indoff.vim
11034 */
11035 static void
11036ex_filetype(eap)
11037 exarg_T *eap;
11038{
11039 char_u *arg = eap->arg;
11040 int plugin = FALSE;
11041 int indent = FALSE;
11042
11043 if (*eap->arg == NUL)
11044 {
11045 /* Print current status. */
11046 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11047 filetype_detect ? "ON" : "OFF",
11048 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11049 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11050 return;
11051 }
11052
11053 /* Accept "plugin" and "indent" in any order. */
11054 for (;;)
11055 {
11056 if (STRNCMP(arg, "plugin", 6) == 0)
11057 {
11058 plugin = TRUE;
11059 arg = skipwhite(arg + 6);
11060 continue;
11061 }
11062 if (STRNCMP(arg, "indent", 6) == 0)
11063 {
11064 indent = TRUE;
11065 arg = skipwhite(arg + 6);
11066 continue;
11067 }
11068 break;
11069 }
11070 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11071 {
11072 if (*arg == 'o' || !filetype_detect)
11073 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011074 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 filetype_detect = TRUE;
11076 if (plugin)
11077 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011078 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 filetype_plugin = TRUE;
11080 }
11081 if (indent)
11082 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011083 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 filetype_indent = TRUE;
11085 }
11086 }
11087 if (*arg == 'd')
11088 {
11089 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011090 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 }
11092 }
11093 else if (STRCMP(arg, "off") == 0)
11094 {
11095 if (plugin || indent)
11096 {
11097 if (plugin)
11098 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011099 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 filetype_plugin = FALSE;
11101 }
11102 if (indent)
11103 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011104 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 filetype_indent = FALSE;
11106 }
11107 }
11108 else
11109 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011110 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 filetype_detect = FALSE;
11112 }
11113 }
11114 else
11115 EMSG2(_(e_invarg2), arg);
11116}
11117
11118/*
11119 * ":setfiletype {name}"
11120 */
11121 static void
11122ex_setfiletype(eap)
11123 exarg_T *eap;
11124{
11125 if (!did_filetype)
11126 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11127}
11128#endif
11129
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 static void
11131ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011132 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133{
11134#ifdef FEAT_DIGRAPHS
11135 if (*eap->arg != NUL)
11136 putdigraph(eap->arg);
11137 else
11138 listdigraphs();
11139#else
11140 EMSG(_("E196: No digraphs in this version"));
11141#endif
11142}
11143
11144 static void
11145ex_set(eap)
11146 exarg_T *eap;
11147{
11148 int flags = 0;
11149
11150 if (eap->cmdidx == CMD_setlocal)
11151 flags = OPT_LOCAL;
11152 else if (eap->cmdidx == CMD_setglobal)
11153 flags = OPT_GLOBAL;
11154#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11155 if (cmdmod.browse && flags == 0)
11156 ex_options(eap);
11157 else
11158#endif
11159 (void)do_set(eap->arg, flags);
11160}
11161
11162#ifdef FEAT_SEARCH_EXTRA
11163/*
11164 * ":nohlsearch"
11165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 static void
11167ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011168 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
11170 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011171 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172}
11173
11174/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011175 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 * Sets nextcmd to the start of the next command, if any. Also called when
11177 * skipping commands to find the next command.
11178 */
11179 static void
11180ex_match(eap)
11181 exarg_T *eap;
11182{
11183 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011184 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 char_u *end;
11186 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011187 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011188
11189 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011190 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011191 else
11192 {
11193 EMSG(e_invcmd);
11194 return;
11195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196
11197 /* First clear any old pattern. */
11198 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011199 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200
11201 if (ends_excmd(*eap->arg))
11202 end = eap->arg;
11203 else if ((STRNICMP(eap->arg, "none", 4) == 0
11204 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11205 end = eap->arg + 4;
11206 else
11207 {
11208 p = skiptowhite(eap->arg);
11209 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011210 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 p = skipwhite(p);
11212 if (*p == NUL)
11213 {
11214 /* There must be two arguments. */
11215 EMSG2(_(e_invarg2), eap->arg);
11216 return;
11217 }
11218 end = skip_regexp(p + 1, *p, TRUE, NULL);
11219 if (!eap->skip)
11220 {
11221 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11222 {
11223 eap->errmsg = e_trailing;
11224 return;
11225 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011226 if (*end != *p)
11227 {
11228 EMSG2(_(e_invarg2), p);
11229 return;
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231
11232 c = *end;
11233 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011234 match_add(curwin, g, p + 1, 10, id);
11235 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011236 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238 }
11239 eap->nextcmd = find_nextcmd(end);
11240}
11241#endif
11242
11243#ifdef FEAT_CRYPT
11244/*
11245 * ":X": Get crypt key
11246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 static void
11248ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011249 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011251 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011252 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253}
11254#endif
11255
11256#ifdef FEAT_FOLDING
11257 static void
11258ex_fold(eap)
11259 exarg_T *eap;
11260{
11261 if (foldManualAllowed(TRUE))
11262 foldCreate(eap->line1, eap->line2);
11263}
11264
11265 static void
11266ex_foldopen(eap)
11267 exarg_T *eap;
11268{
11269 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11270 eap->forceit, FALSE);
11271}
11272
11273 static void
11274ex_folddo(eap)
11275 exarg_T *eap;
11276{
11277 linenr_T lnum;
11278
11279 /* First set the marks for all lines closed/open. */
11280 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11281 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11282 ml_setmarked(lnum);
11283
11284 /* Execute the command on the marked lines. */
11285 global_exe(eap->arg);
11286 ml_clearmarked(); /* clear rest of the marks */
11287}
11288#endif