blob: 27048773e20c7fb2bd491b997a3aa89f8da0ec52 [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
2427 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2428 && USER_CMDIDX(ea.cmdidx)))
2429 /* Do not allow register = for user commands */
2430 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2431#else
2432 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2433#endif
2434 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2435 {
2436 ea.regname = *ea.arg++;
2437#ifdef FEAT_EVAL
2438 /* for '=' register: accept the rest of the line as an expression */
2439 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2440 {
2441 set_expr_line(vim_strsave(ea.arg));
2442 ea.arg += STRLEN(ea.arg);
2443 }
2444#endif
2445 ea.arg = skipwhite(ea.arg);
2446 }
2447
2448 /*
2449 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2450 * count, it's a buffer name.
2451 */
2452 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2453 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2454 || vim_iswhite(*p)))
2455 {
2456 n = getdigits(&ea.arg);
2457 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002458 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 errormsg = (char_u *)_(e_zerocount);
2461 goto doend;
2462 }
2463 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2464 {
2465 ea.line2 = n;
2466 if (ea.addr_count == 0)
2467 ea.addr_count = 1;
2468 }
2469 else
2470 {
2471 ea.line1 = ea.line2;
2472 ea.line2 += n - 1;
2473 ++ea.addr_count;
2474 /*
2475 * Be vi compatible: no error message for out of range.
2476 */
2477 if (ea.line2 > curbuf->b_ml.ml_line_count)
2478 ea.line2 = curbuf->b_ml.ml_line_count;
2479 }
2480 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002481
2482 /*
2483 * Check for flags: 'l', 'p' and '#'.
2484 */
2485 if (ea.argt & EXFLAGS)
2486 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002488 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002489 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 errormsg = (char_u *)_(e_trailing);
2492 goto doend;
2493 }
2494
2495 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2496 {
2497 errormsg = (char_u *)_(e_argreq);
2498 goto doend;
2499 }
2500
2501#ifdef FEAT_EVAL
2502 /*
2503 * Skip the command when it's not going to be executed.
2504 * The commands like :if, :endif, etc. always need to be executed.
2505 * Also make an exception for commands that handle a trailing command
2506 * themselves.
2507 */
2508 if (ea.skip)
2509 {
2510 switch (ea.cmdidx)
2511 {
2512 /* commands that need evaluation */
2513 case CMD_while:
2514 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002515 case CMD_for:
2516 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_if:
2518 case CMD_elseif:
2519 case CMD_else:
2520 case CMD_endif:
2521 case CMD_try:
2522 case CMD_catch:
2523 case CMD_finally:
2524 case CMD_endtry:
2525 case CMD_function:
2526 break;
2527
2528 /* Commands that handle '|' themselves. Check: A command should
2529 * either have the TRLBAR flag, appear in this list or appear in
2530 * the list at ":help :bar". */
2531 case CMD_aboveleft:
2532 case CMD_and:
2533 case CMD_belowright:
2534 case CMD_botright:
2535 case CMD_browse:
2536 case CMD_call:
2537 case CMD_confirm:
2538 case CMD_delfunction:
2539 case CMD_djump:
2540 case CMD_dlist:
2541 case CMD_dsearch:
2542 case CMD_dsplit:
2543 case CMD_echo:
2544 case CMD_echoerr:
2545 case CMD_echomsg:
2546 case CMD_echon:
2547 case CMD_execute:
2548 case CMD_help:
2549 case CMD_hide:
2550 case CMD_ijump:
2551 case CMD_ilist:
2552 case CMD_isearch:
2553 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002554 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 case CMD_keepjumps:
2556 case CMD_keepmarks:
2557 case CMD_leftabove:
2558 case CMD_let:
2559 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002560 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002562 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 case CMD_perl:
2564 case CMD_psearch:
2565 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002566 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002567 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 case CMD_return:
2569 case CMD_rightbelow:
2570 case CMD_ruby:
2571 case CMD_silent:
2572 case CMD_smagic:
2573 case CMD_snomagic:
2574 case CMD_substitute:
2575 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002576 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 case CMD_tcl:
2578 case CMD_throw:
2579 case CMD_tilde:
2580 case CMD_topleft:
2581 case CMD_unlet:
2582 case CMD_verbose:
2583 case CMD_vertical:
2584 break;
2585
2586 default: goto doend;
2587 }
2588 }
2589#endif
2590
2591 if (ea.argt & XFILE)
2592 {
2593 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2594 goto doend;
2595 }
2596
2597#ifdef FEAT_LISTCMDS
2598 /*
2599 * Accept buffer name. Cannot be used at the same time with a buffer
2600 * number. Don't do this for a user command.
2601 */
2602 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2603# ifdef FEAT_USR_CMDS
2604 && !USER_CMDIDX(ea.cmdidx)
2605# endif
2606 )
2607 {
2608 /*
2609 * :bdelete, :bwipeout and :bunload take several arguments, separated
2610 * by spaces: find next space (skipping over escaped characters).
2611 * The others take one argument: ignore trailing spaces.
2612 */
2613 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2614 || ea.cmdidx == CMD_bunload)
2615 p = skiptowhite_esc(ea.arg);
2616 else
2617 {
2618 p = ea.arg + STRLEN(ea.arg);
2619 while (p > ea.arg && vim_iswhite(p[-1]))
2620 --p;
2621 }
2622 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2623 if (ea.line2 < 0) /* failed */
2624 goto doend;
2625 ea.addr_count = 1;
2626 ea.arg = skipwhite(p);
2627 }
2628#endif
2629
2630/*
2631 * 6. switch on command name
2632 *
2633 * The "ea" structure holds the arguments that can be used.
2634 */
2635 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002636 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ea.cookie = cookie;
2638#ifdef FEAT_EVAL
2639 ea.cstack = cstack;
2640#endif
2641
2642#ifdef FEAT_USR_CMDS
2643 if (USER_CMDIDX(ea.cmdidx))
2644 {
2645 /*
2646 * Execute a user-defined command.
2647 */
2648 do_ucmd(&ea);
2649 }
2650 else
2651#endif
2652 {
2653 /*
2654 * Call the function to execute the command.
2655 */
2656 ea.errmsg = NULL;
2657 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2658 if (ea.errmsg != NULL)
2659 errormsg = (char_u *)_(ea.errmsg);
2660 }
2661
2662#ifdef FEAT_EVAL
2663 /*
2664 * If the command just executed called do_cmdline(), any throw or ":return"
2665 * or ":finish" encountered there must also check the cstack of the still
2666 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2667 * exception, or reanimate a returned function or finished script file and
2668 * return or finish it again.
2669 */
2670 if (need_rethrow)
2671 do_throw(cstack);
2672 else if (check_cstack)
2673 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002674 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002676 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 && current_func_returned())
2678 do_return(&ea, TRUE, FALSE, NULL);
2679 }
2680 need_rethrow = check_cstack = FALSE;
2681#endif
2682
2683doend:
2684 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2685 curwin->w_cursor.lnum = 1;
2686
2687 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2688 {
2689 if (sourcing)
2690 {
2691 if (errormsg != IObuff)
2692 {
2693 STRCPY(IObuff, errormsg);
2694 errormsg = IObuff;
2695 }
2696 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002697 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 emsg(errormsg);
2700 }
2701#ifdef FEAT_EVAL
2702 do_errthrow(cstack,
2703 (ea.cmdidx != CMD_SIZE
2704# ifdef FEAT_USR_CMDS
2705 && !USER_CMDIDX(ea.cmdidx)
2706# endif
2707 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2708#endif
2709
2710 if (verbose_save >= 0)
2711 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002712#ifdef FEAT_AUTOCMD
2713 if (cmdmod.save_ei != NULL)
2714 {
2715 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002716 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2717 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002718 free_string_option(cmdmod.save_ei);
2719 }
2720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 cmdmod = save_cmdmod;
2723
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002724 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 /* messages could be enabled for a serious error, need to check if the
2727 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002728 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002729 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 emsg_silent -= did_esilent;
2731 if (emsg_silent < 0)
2732 emsg_silent = 0;
2733 /* Restore msg_scroll, it's set by file I/O commands, even when no
2734 * message is actually displayed. */
2735 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002736
2737 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2738 * somewhere in the line. Put it back in the first column. */
2739 if (redirecting())
2740 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002743#ifdef HAVE_SANDBOX
2744 if (did_sandbox)
2745 --sandbox;
2746#endif
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2749 ea.nextcmd = NULL;
2750
2751#ifdef FEAT_EVAL
2752 --ex_nesting_level;
2753#endif
2754
2755 return ea.nextcmd;
2756}
2757#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002758 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#endif
2760
2761/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002762 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * If there is a match advance "pp" to the argument and return TRUE.
2764 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002765 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002767 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 char *cmd; /* name of command */
2769 int len; /* required length */
2770{
2771 int i;
2772
2773 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002774 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 break;
2776 if (i >= len && !isalpha((*pp)[i]))
2777 {
2778 *pp = skipwhite(*pp + i);
2779 return TRUE;
2780 }
2781 return FALSE;
2782}
2783
2784/*
2785 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002786 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002788 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * Returns NULL for an ambiguous user command.
2790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 static char_u *
2792find_command(eap, full)
2793 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002794 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 int len;
2797 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002798 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /*
2801 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002802 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * - the 'k' command can directly be followed by any character.
2804 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2805 * but :sre[wind] is another command, as are :scrip[tnames],
2806 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002807 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 p = eap->cmd;
2810 if (*p == 'k')
2811 {
2812 eap->cmdidx = CMD_k;
2813 ++p;
2814 }
2815 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002816 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2817 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 || p[1] == 'g'
2819 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2820 || p[1] == 'I'
2821 || (p[1] == 'r' && p[2] != 'e')))
2822 {
2823 eap->cmdidx = CMD_substitute;
2824 ++p;
2825 }
2826 else
2827 {
2828 while (ASCII_ISALPHA(*p))
2829 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002830 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002831 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002832 while (ASCII_ISALNUM(*p))
2833 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /* check for non-alpha command */
2836 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2837 ++p;
2838 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002839 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2840 {
2841 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2842 * :delete with the 'l' flag. Same for 'p'. */
2843 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002844 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002845 break;
2846 if (i == len - 1)
2847 {
2848 --len;
2849 if (p[-1] == 'l')
2850 eap->flags |= EXFLAG_LIST;
2851 else
2852 eap->flags |= EXFLAG_PRINT;
2853 }
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 if (ASCII_ISLOWER(*eap->cmd))
2857 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2858 else
2859 eap->cmdidx = cmdidxs[26];
2860
2861 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2862 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2863 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2864 (size_t)len) == 0)
2865 {
2866#ifdef FEAT_EVAL
2867 if (full != NULL
2868 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2869 *full = TRUE;
2870#endif
2871 break;
2872 }
2873
2874#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002875 /* Look for a user defined command as a last resort. Let ":Print" be
2876 * overruled by a user defined command. */
2877 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2878 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002880 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 while (ASCII_ISALNUM(*p))
2882 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002886 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 eap->cmdidx = CMD_SIZE;
2888 }
2889
2890 return p;
2891}
2892
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002893#ifdef FEAT_USR_CMDS
2894/*
2895 * Search for a user command that matches "eap->cmd".
2896 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2897 * Return a pointer to just after the command.
2898 * Return NULL if there is no matching command.
2899 */
2900 static char_u *
2901find_ucmd(eap, p, full, xp, compl)
2902 exarg_T *eap;
2903 char_u *p; /* end of the command (possibly including count) */
2904 int *full; /* set to TRUE for a full match */
2905 expand_T *xp; /* used for completion, NULL otherwise */
2906 int *compl; /* completion flags or NULL */
2907{
2908 int len = (int)(p - eap->cmd);
2909 int j, k, matchlen = 0;
2910 ucmd_T *uc;
2911 int found = FALSE;
2912 int possible = FALSE;
2913 char_u *cp, *np; /* Point into typed cmd and test name */
2914 garray_T *gap;
2915 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2916 only full match global is accepted. */
2917
2918 /*
2919 * Look for buffer-local user commands first, then global ones.
2920 */
2921 gap = &curbuf->b_ucmds;
2922 for (;;)
2923 {
2924 for (j = 0; j < gap->ga_len; ++j)
2925 {
2926 uc = USER_CMD_GA(gap, j);
2927 cp = eap->cmd;
2928 np = uc->uc_name;
2929 k = 0;
2930 while (k < len && *np != NUL && *cp++ == *np++)
2931 k++;
2932 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2933 {
2934 /* If finding a second match, the command is ambiguous. But
2935 * not if a buffer-local command wasn't a full match and a
2936 * global command is a full match. */
2937 if (k == len && found && *np != NUL)
2938 {
2939 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002940 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002941 amb_local = TRUE;
2942 }
2943
2944 if (!found || (k == len && *np == NUL))
2945 {
2946 /* If we matched up to a digit, then there could
2947 * be another command including the digit that we
2948 * should use instead.
2949 */
2950 if (k == len)
2951 found = TRUE;
2952 else
2953 possible = TRUE;
2954
2955 if (gap == &ucmds)
2956 eap->cmdidx = CMD_USER;
2957 else
2958 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002959 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002960 eap->useridx = j;
2961
2962# ifdef FEAT_CMDL_COMPL
2963 if (compl != NULL)
2964 *compl = uc->uc_compl;
2965# ifdef FEAT_EVAL
2966 if (xp != NULL)
2967 {
2968 xp->xp_arg = uc->uc_compl_arg;
2969 xp->xp_scriptID = uc->uc_scriptID;
2970 }
2971# endif
2972# endif
2973 /* Do not search for further abbreviations
2974 * if this is an exact match. */
2975 matchlen = k;
2976 if (k == len && *np == NUL)
2977 {
2978 if (full != NULL)
2979 *full = TRUE;
2980 amb_local = FALSE;
2981 break;
2982 }
2983 }
2984 }
2985 }
2986
2987 /* Stop if we found a full match or searched all. */
2988 if (j < gap->ga_len || gap == &ucmds)
2989 break;
2990 gap = &ucmds;
2991 }
2992
2993 /* Only found ambiguous matches. */
2994 if (amb_local)
2995 {
2996 if (xp != NULL)
2997 xp->xp_context = EXPAND_UNSUCCESSFUL;
2998 return NULL;
2999 }
3000
3001 /* The match we found may be followed immediately by a number. Move "p"
3002 * back to point to it. */
3003 if (found || possible)
3004 return p + (matchlen - len);
3005 return p;
3006}
3007#endif
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003010static struct cmdmod
3011{
3012 char *name;
3013 int minlen;
3014 int has_count; /* :123verbose :3tab */
3015} cmdmods[] = {
3016 {"aboveleft", 3, FALSE},
3017 {"belowright", 3, FALSE},
3018 {"botright", 2, FALSE},
3019 {"browse", 3, FALSE},
3020 {"confirm", 4, FALSE},
3021 {"hide", 3, FALSE},
3022 {"keepalt", 5, FALSE},
3023 {"keepjumps", 5, FALSE},
3024 {"keepmarks", 3, FALSE},
3025 {"leftabove", 5, FALSE},
3026 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003027 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003028 {"rightbelow", 6, FALSE},
3029 {"sandbox", 3, FALSE},
3030 {"silent", 3, FALSE},
3031 {"tab", 3, TRUE},
3032 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003033 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003034 {"verbose", 4, TRUE},
3035 {"vertical", 4, FALSE},
3036};
3037
3038/*
3039 * Return length of a command modifier (including optional count).
3040 * Return zero when it's not a modifier.
3041 */
3042 int
3043modifier_len(cmd)
3044 char_u *cmd;
3045{
3046 int i, j;
3047 char_u *p = cmd;
3048
3049 if (VIM_ISDIGIT(*cmd))
3050 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003051 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003052 {
3053 for (j = 0; p[j] != NUL; ++j)
3054 if (p[j] != cmdmods[i].name[j])
3055 break;
3056 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3057 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003058 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003059 }
3060 return 0;
3061}
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063/*
3064 * Return > 0 if an Ex command "name" exists.
3065 * Return 2 if there is an exact match.
3066 * Return 3 if there is an ambiguous match.
3067 */
3068 int
3069cmd_exists(name)
3070 char_u *name;
3071{
3072 exarg_T ea;
3073 int full = FALSE;
3074 int i;
3075 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003076 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003079 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
3081 for (j = 0; name[j] != NUL; ++j)
3082 if (name[j] != cmdmods[i].name[j])
3083 break;
3084 if (name[j] == NUL && j >= cmdmods[i].minlen)
3085 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3086 }
3087
Bram Moolenaara9587612006-05-04 21:47:50 +00003088 /* Check built-in commands and user defined commands.
3089 * For ":2match" and ":3match" we need to skip the number. */
3090 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003092 p = find_command(&ea, &full);
3093 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003095 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3096 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003097 if (*skipwhite(p) != NUL)
3098 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3100}
3101#endif
3102
3103/*
3104 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3105 * we don't need/want deleted. Maybe this could be done better if we didn't
3106 * repeat all this stuff. The only problem is that they may not stay
3107 * perfectly compatible with each other, but then the command line syntax
3108 * probably won't change that much -- webb.
3109 */
3110 char_u *
3111set_one_cmd_context(xp, buff)
3112 expand_T *xp;
3113 char_u *buff; /* buffer for command string */
3114{
3115 char_u *p;
3116 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003117 int len = 0;
3118 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3120 int compl = EXPAND_NOTHING;
3121#endif
3122#ifdef FEAT_CMDL_COMPL
3123 int delim;
3124#endif
3125 int forceit = FALSE;
3126 int usefilter = FALSE; /* filter instead of file name */
3127
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003128 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 xp->xp_pattern = buff;
3130 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133/*
3134 * 2. skip comment lines and leading space, colons or bars
3135 */
3136 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3137 ;
3138 xp->xp_pattern = cmd;
3139
3140 if (*cmd == NUL)
3141 return NULL;
3142 if (*cmd == '"') /* ignore comment lines */
3143 {
3144 xp->xp_context = EXPAND_NOTHING;
3145 return NULL;
3146 }
3147
3148/*
3149 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3150 */
3151 cmd = skip_range(cmd, &xp->xp_context);
3152
3153/*
3154 * 4. parse command
3155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 xp->xp_pattern = cmd;
3157 if (*cmd == NUL)
3158 return NULL;
3159 if (*cmd == '"')
3160 {
3161 xp->xp_context = EXPAND_NOTHING;
3162 return NULL;
3163 }
3164
3165 if (*cmd == '|' || *cmd == '\n')
3166 return cmd + 1; /* There's another command */
3167
3168 /*
3169 * Isolate the command and search for it in the command table.
3170 * Exceptions:
3171 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003172 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3174 */
3175 if (*cmd == 'k' && cmd[1] != 'e')
3176 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 p = cmd + 1;
3179 }
3180 else
3181 {
3182 p = cmd;
3183 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3184 ++p;
3185 /* check for non-alpha command */
3186 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3187 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 xp->xp_context = EXPAND_UNSUCCESSFUL;
3193 return NULL;
3194 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003196 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3197 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3198 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 break;
3200
3201#ifdef FEAT_USR_CMDS
3202 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3204 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#endif
3206 }
3207
3208 /*
3209 * If the cursor is touching the command, and it ends in an alpha-numeric
3210 * character, complete the command name.
3211 */
3212 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3213 return NULL;
3214
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3218 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 p = cmd + 1;
3221 }
3222#ifdef FEAT_USR_CMDS
3223 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3224 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225 ea.cmd = cmd;
3226 p = find_ucmd(&ea, p, NULL, xp,
3227# if defined(FEAT_CMDL_COMPL)
3228 &compl
3229# else
3230 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003233 if (p == NULL)
3234 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236#endif
3237 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 /* Not still touching the command and it was an illegal one */
3241 xp->xp_context = EXPAND_UNSUCCESSFUL;
3242 return NULL;
3243 }
3244
3245 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3246
3247 if (*p == '!') /* forced commands */
3248 {
3249 forceit = TRUE;
3250 ++p;
3251 }
3252
3253/*
3254 * 5. parse arguments
3255 */
3256#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 arg = skipwhite(p);
3262
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 if (*arg == '>') /* append */
3266 {
3267 if (*++arg == '>')
3268 ++arg;
3269 arg = skipwhite(arg);
3270 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 ++arg;
3274 usefilter = TRUE;
3275 }
3276 }
3277
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 usefilter = forceit; /* :r! filter if forced */
3281 if (*arg == '!') /* :r !filter */
3282 {
3283 ++arg;
3284 usefilter = TRUE;
3285 }
3286 }
3287
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003288 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 while (*arg == *cmd) /* allow any number of '>' or '<' */
3291 ++arg;
3292 arg = skipwhite(arg);
3293 }
3294
3295 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 /* Check if we're in the +command */
3299 p = arg + 1;
3300 arg = skip_cmd_arg(arg, FALSE);
3301
3302 /* Still touching the command after '+'? */
3303 if (*arg == NUL)
3304 return p;
3305
3306 /* Skip space(s) after +command to get to the real argument */
3307 arg = skipwhite(arg);
3308 }
3309
3310 /*
3311 * Check for '|' to separate commands and '"' to start comments.
3312 * Don't do this for ":read !cmd" and ":write !cmd".
3313 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003314 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316 p = arg;
3317 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 p += 2;
3320 while (*p)
3321 {
3322 if (*p == Ctrl_V)
3323 {
3324 if (p[1] != NUL)
3325 ++p;
3326 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 || *p == '|' || *p == '\n')
3329 {
3330 if (*(p - 1) != '\\')
3331 {
3332 if (*p == '|' || *p == '\n')
3333 return p + 1;
3334 return NULL; /* It's a comment */
3335 }
3336 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003337 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 }
3340
3341 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 vim_strchr((char_u *)"|\"", *arg) == NULL)
3344 return NULL;
3345
3346 /* Find start of last argument (argument just before cursor): */
3347 p = buff + STRLEN(buff);
3348 while (p != arg && *p != ' ' && *p != TAB)
3349 p--;
3350 if (*p == ' ' || *p == TAB)
3351 p++;
3352 xp->xp_pattern = p;
3353
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003354 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003356 int c;
3357 int in_quote = FALSE;
3358 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 /*
3361 * Allow spaces within back-quotes to count as part of the argument
3362 * being expanded.
3363 */
3364 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003365 p = xp->xp_pattern;
3366 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003368#ifdef FEAT_MBYTE
3369 if (has_mbyte)
3370 c = mb_ptr2char(p);
3371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003373 c = *p;
3374 if (c == '\\' && p[1] != NUL)
3375 ++p;
3376 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 if (!in_quote)
3379 {
3380 xp->xp_pattern = p;
3381 bow = p + 1;
3382 }
3383 in_quote = !in_quote;
3384 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003385 /* An argument can contain just about everything, except
3386 * characters that end the command and white space. */
3387 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003388#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003389 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003390#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003391 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003392 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003393 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003394 while (*p != NUL)
3395 {
3396#ifdef FEAT_MBYTE
3397 if (has_mbyte)
3398 c = mb_ptr2char(p);
3399 else
3400#endif
3401 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003402 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003403 break;
3404#ifdef FEAT_MBYTE
3405 if (has_mbyte)
3406 len = (*mb_ptr2len)(p);
3407 else
3408#endif
3409 len = 1;
3410 mb_ptr_adv(p);
3411 }
3412 if (in_quote)
3413 bow = p;
3414 else
3415 xp->xp_pattern = p;
3416 p -= len;
3417 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003418 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
3420
3421 /*
3422 * If we are still inside the quotes, and we passed a space, just
3423 * expand from there.
3424 */
3425 if (bow != NULL && in_quote)
3426 xp->xp_pattern = bow;
3427 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003428
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003429 /* For a shell command more chars need to be escaped. */
3430 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003431 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003432#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003433 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003434#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003435 /* When still after the command name expand executables. */
3436 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003437 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /* Check for environment variable */
3441 if (*xp->xp_pattern == '$'
3442#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3443 || *xp->xp_pattern == '%'
3444#endif
3445 )
3446 {
3447 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3448 if (!vim_isIDc(*p))
3449 break;
3450 if (*p == NUL)
3451 {
3452 xp->xp_context = EXPAND_ENV_VARS;
3453 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003454#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3455 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003456 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003457 compl = EXPAND_ENV_VARS;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 }
3461 }
3462
3463/*
3464 * 6. switch on command name
3465 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003468 case CMD_find:
3469 case CMD_sfind:
3470 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003471 if (xp->xp_context == EXPAND_FILES)
3472 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 case CMD_cd:
3475 case CMD_chdir:
3476 case CMD_lcd:
3477 case CMD_lchdir:
3478 if (xp->xp_context == EXPAND_FILES)
3479 xp->xp_context = EXPAND_DIRECTORIES;
3480 break;
3481 case CMD_help:
3482 xp->xp_context = EXPAND_HELP;
3483 xp->xp_pattern = arg;
3484 break;
3485
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003486 /* Command modifiers: return the argument.
3487 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003489 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 case CMD_belowright:
3491 case CMD_botright:
3492 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003493 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003495 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 case CMD_folddoclosed:
3497 case CMD_folddoopen:
3498 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003499 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 case CMD_keepjumps:
3501 case CMD_keepmarks:
3502 case CMD_leftabove:
3503 case CMD_lockmarks:
3504 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003505 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003507 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 case CMD_topleft:
3509 case CMD_verbose:
3510 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003511 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 return arg;
3513
Bram Moolenaar4f688582007-07-24 12:34:30 +00003514#ifdef FEAT_CMDL_COMPL
3515# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 case CMD_match:
3517 if (*arg == NUL || !ends_excmd(*arg))
3518 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003519 /* also complete "None" */
3520 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 arg = skipwhite(skiptowhite(arg));
3522 if (*arg != NUL)
3523 {
3524 xp->xp_context = EXPAND_NOTHING;
3525 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3526 }
3527 }
3528 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531/*
3532 * All completion for the +cmdline_compl feature goes here.
3533 */
3534
3535# ifdef FEAT_USR_CMDS
3536 case CMD_command:
3537 /* Check for attributes */
3538 while (*arg == '-')
3539 {
3540 arg++; /* Skip "-" */
3541 p = skiptowhite(arg);
3542 if (*p == NUL)
3543 {
3544 /* Cursor is still in the attribute */
3545 p = vim_strchr(arg, '=');
3546 if (p == NULL)
3547 {
3548 /* No "=", so complete attribute names */
3549 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3550 xp->xp_pattern = arg;
3551 return NULL;
3552 }
3553
3554 /* For the -complete and -nargs attributes, we complete
3555 * their arguments as well.
3556 */
3557 if (STRNICMP(arg, "complete", p - arg) == 0)
3558 {
3559 xp->xp_context = EXPAND_USER_COMPLETE;
3560 xp->xp_pattern = p + 1;
3561 return NULL;
3562 }
3563 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3564 {
3565 xp->xp_context = EXPAND_USER_NARGS;
3566 xp->xp_pattern = p + 1;
3567 return NULL;
3568 }
3569 return NULL;
3570 }
3571 arg = skipwhite(p);
3572 }
3573
3574 /* After the attributes comes the new command name */
3575 p = skiptowhite(arg);
3576 if (*p == NUL)
3577 {
3578 xp->xp_context = EXPAND_USER_COMMANDS;
3579 xp->xp_pattern = arg;
3580 break;
3581 }
3582
3583 /* And finally comes a normal command */
3584 return skipwhite(p);
3585
3586 case CMD_delcommand:
3587 xp->xp_context = EXPAND_USER_COMMANDS;
3588 xp->xp_pattern = arg;
3589 break;
3590# endif
3591
3592 case CMD_global:
3593 case CMD_vglobal:
3594 delim = *arg; /* get the delimiter */
3595 if (delim)
3596 ++arg; /* skip delimiter if there is one */
3597
3598 while (arg[0] != NUL && arg[0] != delim)
3599 {
3600 if (arg[0] == '\\' && arg[1] != NUL)
3601 ++arg;
3602 ++arg;
3603 }
3604 if (arg[0] != NUL)
3605 return arg + 1;
3606 break;
3607 case CMD_and:
3608 case CMD_substitute:
3609 delim = *arg;
3610 if (delim)
3611 {
3612 /* skip "from" part */
3613 ++arg;
3614 arg = skip_regexp(arg, delim, p_magic, NULL);
3615 }
3616 /* skip "to" part */
3617 while (arg[0] != NUL && arg[0] != delim)
3618 {
3619 if (arg[0] == '\\' && arg[1] != NUL)
3620 ++arg;
3621 ++arg;
3622 }
3623 if (arg[0] != NUL) /* skip delimiter */
3624 ++arg;
3625 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3626 ++arg;
3627 if (arg[0] != NUL)
3628 return arg;
3629 break;
3630 case CMD_isearch:
3631 case CMD_dsearch:
3632 case CMD_ilist:
3633 case CMD_dlist:
3634 case CMD_ijump:
3635 case CMD_psearch:
3636 case CMD_djump:
3637 case CMD_isplit:
3638 case CMD_dsplit:
3639 arg = skipwhite(skipdigits(arg)); /* skip count */
3640 if (*arg == '/') /* Match regexp, not just whole words */
3641 {
3642 for (++arg; *arg && *arg != '/'; arg++)
3643 if (*arg == '\\' && arg[1] != NUL)
3644 arg++;
3645 if (*arg)
3646 {
3647 arg = skipwhite(arg + 1);
3648
3649 /* Check for trailing illegal characters */
3650 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3651 xp->xp_context = EXPAND_NOTHING;
3652 else
3653 return arg;
3654 }
3655 }
3656 break;
3657#ifdef FEAT_AUTOCMD
3658 case CMD_autocmd:
3659 return set_context_in_autocmd(xp, arg, FALSE);
3660
3661 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003662 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return set_context_in_autocmd(xp, arg, TRUE);
3664#endif
3665 case CMD_set:
3666 set_context_in_set_cmd(xp, arg, 0);
3667 break;
3668 case CMD_setglobal:
3669 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3670 break;
3671 case CMD_setlocal:
3672 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3673 break;
3674 case CMD_tag:
3675 case CMD_stag:
3676 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003677 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 case CMD_tselect:
3679 case CMD_stselect:
3680 case CMD_ptselect:
3681 case CMD_tjump:
3682 case CMD_stjump:
3683 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003684 if (*p_wop != NUL)
3685 xp->xp_context = EXPAND_TAGS_LISTFILES;
3686 else
3687 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 xp->xp_pattern = arg;
3689 break;
3690 case CMD_augroup:
3691 xp->xp_context = EXPAND_AUGROUP;
3692 xp->xp_pattern = arg;
3693 break;
3694#ifdef FEAT_SYN_HL
3695 case CMD_syntax:
3696 set_context_in_syntax_cmd(xp, arg);
3697 break;
3698#endif
3699#ifdef FEAT_EVAL
3700 case CMD_let:
3701 case CMD_if:
3702 case CMD_elseif:
3703 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003704 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 case CMD_echo:
3706 case CMD_echon:
3707 case CMD_execute:
3708 case CMD_echomsg:
3709 case CMD_echoerr:
3710 case CMD_call:
3711 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003712 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 break;
3714
3715 case CMD_unlet:
3716 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3717 arg = xp->xp_pattern + 1;
3718 xp->xp_context = EXPAND_USER_VARS;
3719 xp->xp_pattern = arg;
3720 break;
3721
3722 case CMD_function:
3723 case CMD_delfunction:
3724 xp->xp_context = EXPAND_USER_FUNC;
3725 xp->xp_pattern = arg;
3726 break;
3727
3728 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003729 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 break;
3731#endif
3732 case CMD_highlight:
3733 set_context_in_highlight_cmd(xp, arg);
3734 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003735#ifdef FEAT_CSCOPE
3736 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003737 case CMD_lcscope:
3738 case CMD_scscope:
3739 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003740 break;
3741#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003742#ifdef FEAT_SIGNS
3743 case CMD_sign:
3744 set_context_in_sign_cmd(xp, arg);
3745 break;
3746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#ifdef FEAT_LISTCMDS
3748 case CMD_bdelete:
3749 case CMD_bwipeout:
3750 case CMD_bunload:
3751 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3752 arg = xp->xp_pattern + 1;
3753 /*FALLTHROUGH*/
3754 case CMD_buffer:
3755 case CMD_sbuffer:
3756 case CMD_checktime:
3757 xp->xp_context = EXPAND_BUFFERS;
3758 xp->xp_pattern = arg;
3759 break;
3760#endif
3761#ifdef FEAT_USR_CMDS
3762 case CMD_USER:
3763 case CMD_USER_BUF:
3764 if (compl != EXPAND_NOTHING)
3765 {
3766 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003767 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769# ifdef FEAT_MENU
3770 if (compl == EXPAND_MENUS)
3771 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3772# endif
3773 if (compl == EXPAND_COMMANDS)
3774 return arg;
3775 if (compl == EXPAND_MAPPINGS)
3776 return set_context_in_map_cmd(xp, (char_u *)"map",
3777 arg, forceit, FALSE, FALSE, CMD_map);
3778 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3779 arg = xp->xp_pattern + 1;
3780 xp->xp_pattern = arg;
3781 }
3782 xp->xp_context = compl;
3783 }
3784 break;
3785#endif
3786 case CMD_map: case CMD_noremap:
3787 case CMD_nmap: case CMD_nnoremap:
3788 case CMD_vmap: case CMD_vnoremap:
3789 case CMD_omap: case CMD_onoremap:
3790 case CMD_imap: case CMD_inoremap:
3791 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003792 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003794 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 case CMD_unmap:
3796 case CMD_nunmap:
3797 case CMD_vunmap:
3798 case CMD_ounmap:
3799 case CMD_iunmap:
3800 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003801 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003803 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 case CMD_abbreviate: case CMD_noreabbrev:
3805 case CMD_cabbrev: case CMD_cnoreabbrev:
3806 case CMD_iabbrev: case CMD_inoreabbrev:
3807 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003808 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_unabbreviate:
3810 case CMD_cunabbrev:
3811 case CMD_iunabbrev:
3812 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003813 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814#ifdef FEAT_MENU
3815 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3816 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3817 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3818 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3819 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3820 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3821 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3822 case CMD_tmenu: case CMD_tunmenu:
3823 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3824 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3825#endif
3826
3827 case CMD_colorscheme:
3828 xp->xp_context = EXPAND_COLORS;
3829 xp->xp_pattern = arg;
3830 break;
3831
3832 case CMD_compiler:
3833 xp->xp_context = EXPAND_COMPILER;
3834 xp->xp_pattern = arg;
3835 break;
3836
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003837 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003838 xp->xp_context = EXPAND_OWNSYNTAX;
3839 xp->xp_pattern = arg;
3840 break;
3841
3842 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003843 xp->xp_context = EXPAND_FILETYPE;
3844 xp->xp_pattern = arg;
3845 break;
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3848 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3849 case CMD_language:
3850 if (*skiptowhite(arg) == NUL)
3851 {
3852 xp->xp_context = EXPAND_LANGUAGE;
3853 xp->xp_pattern = arg;
3854 }
3855 else
3856 xp->xp_context = EXPAND_NOTHING;
3857 break;
3858#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003859#if defined(FEAT_PROFILE)
3860 case CMD_profile:
3861 set_context_in_profile_cmd(xp, arg);
3862 break;
3863#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003864 case CMD_behave:
3865 xp->xp_context = EXPAND_BEHAVE;
3866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868#endif /* FEAT_CMDL_COMPL */
3869
3870 default:
3871 break;
3872 }
3873 return NULL;
3874}
3875
3876/*
3877 * skip a range specifier of the form: addr [,addr] [;addr] ..
3878 *
3879 * Backslashed delimiters after / or ? will be skipped, and commands will
3880 * not be expanded between /'s and ?'s or after "'".
3881 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003882 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 * Returns the "cmd" pointer advanced to beyond the range.
3884 */
3885 char_u *
3886skip_range(cmd, ctx)
3887 char_u *cmd;
3888 int *ctx; /* pointer to xp_context or NULL */
3889{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003890 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaardf177f62005-02-22 08:39:57 +00003892 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
3894 if (*cmd == '\'')
3895 {
3896 if (*++cmd == NUL && ctx != NULL)
3897 *ctx = EXPAND_NOTHING;
3898 }
3899 else if (*cmd == '/' || *cmd == '?')
3900 {
3901 delim = *cmd++;
3902 while (*cmd != NUL && *cmd != delim)
3903 if (*cmd++ == '\\' && *cmd != NUL)
3904 ++cmd;
3905 if (*cmd == NUL && ctx != NULL)
3906 *ctx = EXPAND_NOTHING;
3907 }
3908 if (*cmd != NUL)
3909 ++cmd;
3910 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003911
3912 /* Skip ":" and white space. */
3913 while (*cmd == ':')
3914 cmd = skipwhite(cmd + 1);
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 return cmd;
3917}
3918
3919/*
3920 * get a single EX address
3921 *
3922 * Set ptr to the next character after the part that was interpreted.
3923 * Set ptr to NULL when an error is encountered.
3924 *
3925 * Return MAXLNUM when no Ex address was found.
3926 */
3927 static linenr_T
3928get_address(ptr, skip, to_other_file)
3929 char_u **ptr;
3930 int skip; /* only skip the address, don't use it */
3931 int to_other_file; /* flag: may jump to other file */
3932{
3933 int c;
3934 int i;
3935 long n;
3936 char_u *cmd;
3937 pos_T pos;
3938 pos_T *fp;
3939 linenr_T lnum;
3940
3941 cmd = skipwhite(*ptr);
3942 lnum = MAXLNUM;
3943 do
3944 {
3945 switch (*cmd)
3946 {
3947 case '.': /* '.' - Cursor position */
3948 ++cmd;
3949 lnum = curwin->w_cursor.lnum;
3950 break;
3951
3952 case '$': /* '$' - last line */
3953 ++cmd;
3954 lnum = curbuf->b_ml.ml_line_count;
3955 break;
3956
3957 case '\'': /* ''' - mark */
3958 if (*++cmd == NUL)
3959 {
3960 cmd = NULL;
3961 goto error;
3962 }
3963 if (skip)
3964 ++cmd;
3965 else
3966 {
3967 /* Only accept a mark in another file when it is
3968 * used by itself: ":'M". */
3969 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3970 ++cmd;
3971 if (fp == (pos_T *)-1)
3972 /* Jumped to another file. */
3973 lnum = curwin->w_cursor.lnum;
3974 else
3975 {
3976 if (check_mark(fp) == FAIL)
3977 {
3978 cmd = NULL;
3979 goto error;
3980 }
3981 lnum = fp->lnum;
3982 }
3983 }
3984 break;
3985
3986 case '/':
3987 case '?': /* '/' or '?' - search */
3988 c = *cmd++;
3989 if (skip) /* skip "/pat/" */
3990 {
3991 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3992 if (*cmd == c)
3993 ++cmd;
3994 }
3995 else
3996 {
3997 pos = curwin->w_cursor; /* save curwin->w_cursor */
3998 /*
3999 * When '/' or '?' follows another address, start
4000 * from there.
4001 */
4002 if (lnum != MAXLNUM)
4003 curwin->w_cursor.lnum = lnum;
4004 /*
4005 * Start a forward search at the end of the line.
4006 * Start a backward search at the start of the line.
4007 * This makes sure we never match in the current
4008 * line, and can match anywhere in the
4009 * next/previous line.
4010 */
4011 if (c == '/')
4012 curwin->w_cursor.col = MAXCOL;
4013 else
4014 curwin->w_cursor.col = 0;
4015 searchcmdlen = 0;
4016 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004017 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
4019 curwin->w_cursor = pos;
4020 cmd = NULL;
4021 goto error;
4022 }
4023 lnum = curwin->w_cursor.lnum;
4024 curwin->w_cursor = pos;
4025 /* adjust command string pointer */
4026 cmd += searchcmdlen;
4027 }
4028 break;
4029
4030 case '\\': /* "\?", "\/" or "\&", repeat search */
4031 ++cmd;
4032 if (*cmd == '&')
4033 i = RE_SUBST;
4034 else if (*cmd == '?' || *cmd == '/')
4035 i = RE_SEARCH;
4036 else
4037 {
4038 EMSG(_(e_backslash));
4039 cmd = NULL;
4040 goto error;
4041 }
4042
4043 if (!skip)
4044 {
4045 /*
4046 * When search follows another address, start from
4047 * there.
4048 */
4049 if (lnum != MAXLNUM)
4050 pos.lnum = lnum;
4051 else
4052 pos.lnum = curwin->w_cursor.lnum;
4053
4054 /*
4055 * Start the search just like for the above
4056 * do_search().
4057 */
4058 if (*cmd != '?')
4059 pos.col = MAXCOL;
4060 else
4061 pos.col = 0;
4062 if (searchit(curwin, curbuf, &pos,
4063 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004064 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004065 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 lnum = pos.lnum;
4067 else
4068 {
4069 cmd = NULL;
4070 goto error;
4071 }
4072 }
4073 ++cmd;
4074 break;
4075
4076 default:
4077 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4078 lnum = getdigits(&cmd);
4079 }
4080
4081 for (;;)
4082 {
4083 cmd = skipwhite(cmd);
4084 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4085 break;
4086
4087 if (lnum == MAXLNUM)
4088 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4089 if (VIM_ISDIGIT(*cmd))
4090 i = '+'; /* "number" is same as "+number" */
4091 else
4092 i = *cmd++;
4093 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4094 n = 1;
4095 else
4096 n = getdigits(&cmd);
4097 if (i == '-')
4098 lnum -= n;
4099 else
4100 lnum += n;
4101 }
4102 } while (*cmd == '/' || *cmd == '?');
4103
4104error:
4105 *ptr = cmd;
4106 return lnum;
4107}
4108
4109/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004110 * Get flags from an Ex command argument.
4111 */
4112 static void
4113get_flags(eap)
4114 exarg_T *eap;
4115{
4116 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4117 {
4118 if (*eap->arg == 'l')
4119 eap->flags |= EXFLAG_LIST;
4120 else if (*eap->arg == 'p')
4121 eap->flags |= EXFLAG_PRINT;
4122 else
4123 eap->flags |= EXFLAG_NR;
4124 eap->arg = skipwhite(eap->arg + 1);
4125 }
4126}
4127
4128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Function called for command which is Not Implemented. NI!
4130 */
4131 void
4132ex_ni(eap)
4133 exarg_T *eap;
4134{
4135 if (!eap->skip)
4136 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4137}
4138
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004139#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140/*
4141 * Function called for script command which is Not Implemented. NI!
4142 * Skips over ":perl <<EOF" constructs.
4143 */
4144 static void
4145ex_script_ni(eap)
4146 exarg_T *eap;
4147{
4148 if (!eap->skip)
4149 ex_ni(eap);
4150 else
4151 vim_free(script_get(eap, eap->arg));
4152}
4153#endif
4154
4155/*
4156 * Check range in Ex command for validity.
4157 * Return NULL when valid, error message when invalid.
4158 */
4159 static char_u *
4160invalid_range(eap)
4161 exarg_T *eap;
4162{
4163 if ( eap->line1 < 0
4164 || eap->line2 < 0
4165 || eap->line1 > eap->line2
4166 || ((eap->argt & RANGE)
4167 && !(eap->argt & NOTADR)
4168 && eap->line2 > curbuf->b_ml.ml_line_count
4169#ifdef FEAT_DIFF
4170 + (eap->cmdidx == CMD_diffget)
4171#endif
4172 ))
4173 return (char_u *)_(e_invrange);
4174 return NULL;
4175}
4176
4177/*
4178 * Correct the range for zero line number, if required.
4179 */
4180 static void
4181correct_range(eap)
4182 exarg_T *eap;
4183{
4184 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4185 {
4186 if (eap->line1 == 0)
4187 eap->line1 = 1;
4188 if (eap->line2 == 0)
4189 eap->line2 = 1;
4190 }
4191}
4192
Bram Moolenaar748bf032005-02-02 23:04:36 +00004193#ifdef FEAT_QUICKFIX
4194static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4195
4196/*
4197 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4198 * pattern. Otherwise return eap->arg.
4199 */
4200 static char_u *
4201skip_grep_pat(eap)
4202 exarg_T *eap;
4203{
4204 char_u *p = eap->arg;
4205
Bram Moolenaara37420f2006-02-04 22:37:47 +00004206 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4207 || eap->cmdidx == CMD_vimgrepadd
4208 || eap->cmdidx == CMD_lvimgrepadd
4209 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004210 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004211 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004212 if (p == NULL)
4213 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004214 }
4215 return p;
4216}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004217
4218/*
4219 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4220 * in the command line, so that things like % get expanded.
4221 */
4222 static char_u *
4223replace_makeprg(eap, p, cmdlinep)
4224 exarg_T *eap;
4225 char_u *p;
4226 char_u **cmdlinep;
4227{
4228 char_u *new_cmdline;
4229 char_u *program;
4230 char_u *pos;
4231 char_u *ptr;
4232 int len;
4233 int i;
4234
4235 /*
4236 * Don't do it when ":vimgrep" is used for ":grep".
4237 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004238 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4239 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4240 || eap->cmdidx == CMD_grepadd
4241 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004242 && !grep_internal(eap->cmdidx))
4243 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004244 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4245 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004246 {
4247 if (*curbuf->b_p_gp == NUL)
4248 program = p_gp;
4249 else
4250 program = curbuf->b_p_gp;
4251 }
4252 else
4253 {
4254 if (*curbuf->b_p_mp == NUL)
4255 program = p_mp;
4256 else
4257 program = curbuf->b_p_mp;
4258 }
4259
4260 p = skipwhite(p);
4261
4262 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4263 {
4264 /* replace $* by given arguments */
4265 i = 1;
4266 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4267 ++i;
4268 len = (int)STRLEN(p);
4269 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4270 if (new_cmdline == NULL)
4271 return NULL; /* out of memory */
4272 ptr = new_cmdline;
4273 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4274 {
4275 i = (int)(pos - program);
4276 STRNCPY(ptr, program, i);
4277 STRCPY(ptr += i, p);
4278 ptr += len;
4279 program = pos + 2;
4280 }
4281 STRCPY(ptr, program);
4282 }
4283 else
4284 {
4285 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4286 if (new_cmdline == NULL)
4287 return NULL; /* out of memory */
4288 STRCPY(new_cmdline, program);
4289 STRCAT(new_cmdline, " ");
4290 STRCAT(new_cmdline, p);
4291 }
4292 msg_make(p);
4293
4294 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4295 vim_free(*cmdlinep);
4296 *cmdlinep = new_cmdline;
4297 p = new_cmdline;
4298 }
4299 return p;
4300}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004301#endif
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303/*
4304 * Expand file name in Ex command argument.
4305 * Return FAIL for failure, OK otherwise.
4306 */
4307 int
4308expand_filename(eap, cmdlinep, errormsgp)
4309 exarg_T *eap;
4310 char_u **cmdlinep;
4311 char_u **errormsgp;
4312{
4313 int has_wildcards; /* need to expand wildcards */
4314 char_u *repl;
4315 int srclen;
4316 char_u *p;
4317 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004318 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar748bf032005-02-02 23:04:36 +00004320#ifdef FEAT_QUICKFIX
4321 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4322 p = skip_grep_pat(eap);
4323#else
4324 p = eap->arg;
4325#endif
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /*
4328 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4329 * the file name contains a wildcard it should not cause expanding.
4330 * (it will be expanded anyway if there is a wildcard before replacing).
4331 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004332 has_wildcards = mch_has_wildcard(p);
4333 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004335#ifdef FEAT_EVAL
4336 /* Skip over `=expr`, wildcards in it are not expanded. */
4337 if (p[0] == '`' && p[1] == '=')
4338 {
4339 p += 2;
4340 (void)skip_expr(&p);
4341 if (*p == '`')
4342 ++p;
4343 continue;
4344 }
4345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /*
4347 * Quick check if this cannot be the start of a special string.
4348 * Also removes backslash before '%', '#' and '<'.
4349 */
4350 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4351 {
4352 ++p;
4353 continue;
4354 }
4355
4356 /*
4357 * Try to find a match at this position.
4358 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004359 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4360 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (*errormsgp != NULL) /* error detected */
4362 return FAIL;
4363 if (repl == NULL) /* no match found */
4364 {
4365 p += srclen;
4366 continue;
4367 }
4368
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004369 /* Wildcards won't be expanded below, the replacement is taken
4370 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4371 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4372 {
4373 char_u *l = repl;
4374
4375 repl = expand_env_save(repl);
4376 vim_free(l);
4377 }
4378
Bram Moolenaar63b92542007-03-27 14:57:09 +00004379 /* Need to escape white space et al. with a backslash.
4380 * Don't do this for:
4381 * - replacement that already has been escaped: "##"
4382 * - shell commands (may have to use quotes instead).
4383 * - non-unix systems when there is a single argument (spaces don't
4384 * separate arguments then).
4385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004387 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 && eap->cmdidx != CMD_bang
4389 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004390 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004392 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004394 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#ifndef UNIX
4396 && !(eap->argt & NOSPC)
4397#endif
4398 )
4399 {
4400 char_u *l;
4401#ifdef BACKSLASH_IN_FILENAME
4402 /* Don't escape a backslash here, because rem_backslash() doesn't
4403 * remove it later. */
4404 static char_u *nobslash = (char_u *)" \t\"|";
4405# define ESCAPE_CHARS nobslash
4406#else
4407# define ESCAPE_CHARS escape_chars
4408#endif
4409
4410 for (l = repl; *l; ++l)
4411 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4412 {
4413 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4414 if (l != NULL)
4415 {
4416 vim_free(repl);
4417 repl = l;
4418 }
4419 break;
4420 }
4421 }
4422
4423 /* For a shell command a '!' must be escaped. */
4424 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004425 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
4427 char_u *l;
4428
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004429 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (l != NULL)
4431 {
4432 vim_free(repl);
4433 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004434 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (strstr((char *)p_sh, "sh") != NULL)
4436 {
4437 l = vim_strsave_escaped(repl, (char_u *)"!");
4438 if (l != NULL)
4439 {
4440 vim_free(repl);
4441 repl = l;
4442 }
4443 }
4444 }
4445 }
4446
4447 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4448 vim_free(repl);
4449 if (p == NULL)
4450 return FAIL;
4451 }
4452
4453 /*
4454 * One file argument: Expand wildcards.
4455 * Don't do this with ":r !command" or ":w !command".
4456 */
4457 if ((eap->argt & NOSPC) && !eap->usefilter)
4458 {
4459 /*
4460 * May do this twice:
4461 * 1. Replace environment variables.
4462 * 2. Replace any other wildcards, remove backslashes.
4463 */
4464 for (n = 1; n <= 2; ++n)
4465 {
4466 if (n == 2)
4467 {
4468#ifdef UNIX
4469 /*
4470 * Only for Unix we check for more than one file name.
4471 * For other systems spaces are considered to be part
4472 * of the file name.
4473 * Only check here if there is no wildcard, otherwise
4474 * ExpandOne() will check for errors. This allows
4475 * ":e `ls ve*.c`" on Unix.
4476 */
4477 if (!has_wildcards)
4478 for (p = eap->arg; *p; ++p)
4479 {
4480 /* skip escaped characters */
4481 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4482 ++p;
4483 else if (vim_iswhite(*p))
4484 {
4485 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4486 return FAIL;
4487 }
4488 }
4489#endif
4490
4491 /*
4492 * Halve the number of backslashes (this is Vi compatible).
4493 * For Unix and OS/2, when wildcards are expanded, this is
4494 * done by ExpandOne() below.
4495 */
4496#if defined(UNIX) || defined(OS2)
4497 if (!has_wildcards)
4498#endif
4499 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501
4502 if (has_wildcards)
4503 {
4504 if (n == 1)
4505 {
4506 /*
4507 * First loop: May expand environment variables. This
4508 * can be done much faster with expand_env() than with
4509 * something else (e.g., calling a shell).
4510 * After expanding environment variables, check again
4511 * if there are still wildcards present.
4512 */
4513 if (vim_strchr(eap->arg, '$') != NULL
4514 || vim_strchr(eap->arg, '~') != NULL)
4515 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004516 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004517 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 has_wildcards = mch_has_wildcard(NameBuff);
4519 p = NameBuff;
4520 }
4521 else
4522 p = NULL;
4523 }
4524 else /* n == 2 */
4525 {
4526 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004527 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 ExpandInit(&xpc);
4530 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004531 if (p_wic)
4532 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004534 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (p == NULL)
4536 return FAIL;
4537 }
4538 if (p != NULL)
4539 {
4540 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4541 p, cmdlinep);
4542 if (n == 2) /* p came from ExpandOne() */
4543 vim_free(p);
4544 }
4545 }
4546 }
4547 }
4548 return OK;
4549}
4550
4551/*
4552 * Replace part of the command line, keeping eap->cmd, eap->arg and
4553 * eap->nextcmd correct.
4554 * "src" points to the part that is to be replaced, of length "srclen".
4555 * "repl" is the replacement string.
4556 * Returns a pointer to the character after the replaced string.
4557 * Returns NULL for failure.
4558 */
4559 static char_u *
4560repl_cmdline(eap, src, srclen, repl, cmdlinep)
4561 exarg_T *eap;
4562 char_u *src;
4563 int srclen;
4564 char_u *repl;
4565 char_u **cmdlinep;
4566{
4567 int len;
4568 int i;
4569 char_u *new_cmdline;
4570
4571 /*
4572 * The new command line is build in new_cmdline[].
4573 * First allocate it.
4574 * Careful: a "+cmd" argument may have been NUL terminated.
4575 */
4576 len = (int)STRLEN(repl);
4577 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004578 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4580 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4581 return NULL; /* out of memory! */
4582
4583 /*
4584 * Copy the stuff before the expanded part.
4585 * Copy the expanded stuff.
4586 * Copy what came after the expanded part.
4587 * Copy the next commands, if there are any.
4588 */
4589 i = (int)(src - *cmdlinep); /* length of part before match */
4590 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 mch_memmove(new_cmdline + i, repl, (size_t)len);
4593 i += len; /* remember the end of the string */
4594 STRCPY(new_cmdline + i, src + srclen);
4595 src = new_cmdline + i; /* remember where to continue */
4596
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004597 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 i = (int)STRLEN(new_cmdline) + 1;
4600 STRCPY(new_cmdline + i, eap->nextcmd);
4601 eap->nextcmd = new_cmdline + i;
4602 }
4603 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4604 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4605 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4606 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4607 vim_free(*cmdlinep);
4608 *cmdlinep = new_cmdline;
4609
4610 return src;
4611}
4612
4613/*
4614 * Check for '|' to separate commands and '"' to start comments.
4615 */
4616 void
4617separate_nextcmd(eap)
4618 exarg_T *eap;
4619{
4620 char_u *p;
4621
Bram Moolenaar86b68352004-12-27 21:59:20 +00004622#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004623 p = skip_grep_pat(eap);
4624#else
4625 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004626#endif
4627
4628 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
4630 if (*p == Ctrl_V)
4631 {
4632 if (eap->argt & (USECTRLV | XFILE))
4633 ++p; /* skip CTRL-V and next char */
4634 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004635 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004636 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (*p == NUL) /* stop at NUL after CTRL-V */
4638 break;
4639 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004640
4641#ifdef FEAT_EVAL
4642 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004644 {
4645 p += 2;
4646 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004647 }
4648#endif
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /* Check for '"': start of comment or '|': next command */
4651 /* :@" and :*" do not start a comment!
4652 * :redir @" doesn't either. */
4653 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4654 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4655 || p != eap->arg)
4656 && (eap->cmdidx != CMD_redir
4657 || p != eap->arg + 1 || p[-1] != '@'))
4658 || *p == '|' || *p == '\n')
4659 {
4660 /*
4661 * We remove the '\' before the '|', unless USECTRLV is used
4662 * AND 'b' is present in 'cpoptions'.
4663 */
4664 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4665 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4666 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004667 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 --p;
4669 }
4670 else
4671 {
4672 eap->nextcmd = check_nextcmd(p);
4673 *p = NUL;
4674 break;
4675 }
4676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4680 del_trailing_spaces(eap->arg);
4681}
4682
4683/*
4684 * get + command from ex argument
4685 */
4686 static char_u *
4687getargcmd(argp)
4688 char_u **argp;
4689{
4690 char_u *arg = *argp;
4691 char_u *command = NULL;
4692
4693 if (*arg == '+') /* +[command] */
4694 {
4695 ++arg;
4696 if (vim_isspace(*arg))
4697 command = dollar_command;
4698 else
4699 {
4700 command = arg;
4701 arg = skip_cmd_arg(command, TRUE);
4702 if (*arg != NUL)
4703 *arg++ = NUL; /* terminate command with NUL */
4704 }
4705
4706 arg = skipwhite(arg); /* skip over spaces */
4707 *argp = arg;
4708 }
4709 return command;
4710}
4711
4712/*
4713 * Find end of "+command" argument. Skip over "\ " and "\\".
4714 */
4715 static char_u *
4716skip_cmd_arg(p, rembs)
4717 char_u *p;
4718 int rembs; /* TRUE to halve the number of backslashes */
4719{
4720 while (*p && !vim_isspace(*p))
4721 {
4722 if (*p == '\\' && p[1] != NUL)
4723 {
4724 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004725 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 else
4727 ++p;
4728 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004729 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 return p;
4732}
4733
4734/*
4735 * Get "++opt=arg" argument.
4736 * Return FAIL or OK.
4737 */
4738 static int
4739getargopt(eap)
4740 exarg_T *eap;
4741{
4742 char_u *arg = eap->arg + 2;
4743 int *pp = NULL;
4744#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004745 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 char_u *p;
4747#endif
4748
4749 /* ":edit ++[no]bin[ary] file" */
4750 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4751 {
4752 if (*arg == 'n')
4753 {
4754 arg += 2;
4755 eap->force_bin = FORCE_NOBIN;
4756 }
4757 else
4758 eap->force_bin = FORCE_BIN;
4759 if (!checkforcmd(&arg, "binary", 3))
4760 return FAIL;
4761 eap->arg = skipwhite(arg);
4762 return OK;
4763 }
4764
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004765 /* ":read ++edit file" */
4766 if (STRNCMP(arg, "edit", 4) == 0)
4767 {
4768 eap->read_edit = TRUE;
4769 eap->arg = skipwhite(arg + 4);
4770 return OK;
4771 }
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (STRNCMP(arg, "ff", 2) == 0)
4774 {
4775 arg += 2;
4776 pp = &eap->force_ff;
4777 }
4778 else if (STRNCMP(arg, "fileformat", 10) == 0)
4779 {
4780 arg += 10;
4781 pp = &eap->force_ff;
4782 }
4783#ifdef FEAT_MBYTE
4784 else if (STRNCMP(arg, "enc", 3) == 0)
4785 {
4786 arg += 3;
4787 pp = &eap->force_enc;
4788 }
4789 else if (STRNCMP(arg, "encoding", 8) == 0)
4790 {
4791 arg += 8;
4792 pp = &eap->force_enc;
4793 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004794 else if (STRNCMP(arg, "bad", 3) == 0)
4795 {
4796 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004797 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#endif
4800
4801 if (pp == NULL || *arg != '=')
4802 return FAIL;
4803
4804 ++arg;
4805 *pp = (int)(arg - eap->cmd);
4806 arg = skip_cmd_arg(arg, FALSE);
4807 eap->arg = skipwhite(arg);
4808 *arg = NUL;
4809
4810#ifdef FEAT_MBYTE
4811 if (pp == &eap->force_ff)
4812 {
4813#endif
4814 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4815 return FAIL;
4816#ifdef FEAT_MBYTE
4817 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004818 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 /* Make 'fileencoding' lower case. */
4821 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4822 *p = TOLOWER_ASC(*p);
4823 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004824 else
4825 {
4826 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4827 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004828 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004829 if (STRICMP(p, "keep") == 0)
4830 eap->bad_char = BAD_KEEP;
4831 else if (STRICMP(p, "drop") == 0)
4832 eap->bad_char = BAD_DROP;
4833 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4834 eap->bad_char = *p;
4835 else
4836 return FAIL;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
4839
4840 return OK;
4841}
4842
4843/*
4844 * ":abbreviate" and friends.
4845 */
4846 static void
4847ex_abbreviate(eap)
4848 exarg_T *eap;
4849{
4850 do_exmap(eap, TRUE); /* almost the same as mapping */
4851}
4852
4853/*
4854 * ":map" and friends.
4855 */
4856 static void
4857ex_map(eap)
4858 exarg_T *eap;
4859{
4860 /*
4861 * If we are sourcing .exrc or .vimrc in current directory we
4862 * print the mappings for security reasons.
4863 */
4864 if (secure)
4865 {
4866 secure = 2;
4867 msg_outtrans(eap->cmd);
4868 msg_putchar('\n');
4869 }
4870 do_exmap(eap, FALSE);
4871}
4872
4873/*
4874 * ":unmap" and friends.
4875 */
4876 static void
4877ex_unmap(eap)
4878 exarg_T *eap;
4879{
4880 do_exmap(eap, FALSE);
4881}
4882
4883/*
4884 * ":mapclear" and friends.
4885 */
4886 static void
4887ex_mapclear(eap)
4888 exarg_T *eap;
4889{
4890 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4891}
4892
4893/*
4894 * ":abclear" and friends.
4895 */
4896 static void
4897ex_abclear(eap)
4898 exarg_T *eap;
4899{
4900 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4901}
4902
4903#ifdef FEAT_AUTOCMD
4904 static void
4905ex_autocmd(eap)
4906 exarg_T *eap;
4907{
4908 /*
4909 * Disallow auto commands from .exrc and .vimrc in current
4910 * directory for security reasons.
4911 */
4912 if (secure)
4913 {
4914 secure = 2;
4915 eap->errmsg = e_curdir;
4916 }
4917 else if (eap->cmdidx == CMD_autocmd)
4918 do_autocmd(eap->arg, eap->forceit);
4919 else
4920 do_augroup(eap->arg, eap->forceit);
4921}
4922
4923/*
4924 * ":doautocmd": Apply the automatic commands to the current buffer.
4925 */
4926 static void
4927ex_doautocmd(eap)
4928 exarg_T *eap;
4929{
4930 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004931 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932}
4933#endif
4934
4935#ifdef FEAT_LISTCMDS
4936/*
4937 * :[N]bunload[!] [N] [bufname] unload buffer
4938 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4939 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4940 */
4941 static void
4942ex_bunload(eap)
4943 exarg_T *eap;
4944{
4945 eap->errmsg = do_bufdel(
4946 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4947 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4948 : DOBUF_UNLOAD, eap->arg,
4949 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4950}
4951
4952/*
4953 * :[N]buffer [N] to buffer N
4954 * :[N]sbuffer [N] to buffer N
4955 */
4956 static void
4957ex_buffer(eap)
4958 exarg_T *eap;
4959{
4960 if (*eap->arg)
4961 eap->errmsg = e_trailing;
4962 else
4963 {
4964 if (eap->addr_count == 0) /* default is current buffer */
4965 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4966 else
4967 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4968 }
4969}
4970
4971/*
4972 * :[N]bmodified [N] to next mod. buffer
4973 * :[N]sbmodified [N] to next mod. buffer
4974 */
4975 static void
4976ex_bmodified(eap)
4977 exarg_T *eap;
4978{
4979 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4980}
4981
4982/*
4983 * :[N]bnext [N] to next buffer
4984 * :[N]sbnext [N] split and to next buffer
4985 */
4986 static void
4987ex_bnext(eap)
4988 exarg_T *eap;
4989{
4990 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4991}
4992
4993/*
4994 * :[N]bNext [N] to previous buffer
4995 * :[N]bprevious [N] to previous buffer
4996 * :[N]sbNext [N] split and to previous buffer
4997 * :[N]sbprevious [N] split and to previous buffer
4998 */
4999 static void
5000ex_bprevious(eap)
5001 exarg_T *eap;
5002{
5003 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5004}
5005
5006/*
5007 * :brewind to first buffer
5008 * :bfirst to first buffer
5009 * :sbrewind split and to first buffer
5010 * :sbfirst split and to first buffer
5011 */
5012 static void
5013ex_brewind(eap)
5014 exarg_T *eap;
5015{
5016 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5017}
5018
5019/*
5020 * :blast to last buffer
5021 * :sblast split and to last buffer
5022 */
5023 static void
5024ex_blast(eap)
5025 exarg_T *eap;
5026{
5027 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5028}
5029#endif
5030
5031 int
5032ends_excmd(c)
5033 int c;
5034{
5035 return (c == NUL || c == '|' || c == '"' || c == '\n');
5036}
5037
5038#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5039 || defined(PROTO)
5040/*
5041 * Return the next command, after the first '|' or '\n'.
5042 * Return NULL if not found.
5043 */
5044 char_u *
5045find_nextcmd(p)
5046 char_u *p;
5047{
5048 while (*p != '|' && *p != '\n')
5049 {
5050 if (*p == NUL)
5051 return NULL;
5052 ++p;
5053 }
5054 return (p + 1);
5055}
5056#endif
5057
5058/*
5059 * Check if *p is a separator between Ex commands.
5060 * Return NULL if it isn't, (p + 1) if it is.
5061 */
5062 char_u *
5063check_nextcmd(p)
5064 char_u *p;
5065{
5066 p = skipwhite(p);
5067 if (*p == '|' || *p == '\n')
5068 return (p + 1);
5069 else
5070 return NULL;
5071}
5072
5073/*
5074 * - if there are more files to edit
5075 * - and this is the last window
5076 * - and forceit not used
5077 * - and not repeated twice on a row
5078 * return FAIL and give error message if 'message' TRUE
5079 * return OK otherwise
5080 */
5081 static int
5082check_more(message, forceit)
5083 int message; /* when FALSE check only, no messages */
5084 int forceit;
5085{
5086 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5087
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005088 if (!forceit && only_one_window()
5089 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 if (message)
5092 {
5093#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5094 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5095 {
5096 char_u buff[IOSIZE];
5097
5098 if (n == 1)
5099 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5100 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005101 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 _("%d more files to edit. Quit anyway?"), n);
5103 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5104 return OK;
5105 return FAIL;
5106 }
5107#endif
5108 if (n == 1)
5109 EMSG(_("E173: 1 more file to edit"));
5110 else
5111 EMSGN(_("E173: %ld more files to edit"), n);
5112 quitmore = 2; /* next try to quit is allowed */
5113 }
5114 return FAIL;
5115 }
5116 return OK;
5117}
5118
5119#ifdef FEAT_CMDL_COMPL
5120/*
5121 * Function given to ExpandGeneric() to obtain the list of command names.
5122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 char_u *
5124get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005125 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 int idx;
5127{
5128 if (idx >= (int)CMD_SIZE)
5129# ifdef FEAT_USR_CMDS
5130 return get_user_command_name(idx);
5131# else
5132 return NULL;
5133# endif
5134 return cmdnames[idx].cmd_name;
5135}
5136#endif
5137
5138#if defined(FEAT_USR_CMDS) || defined(PROTO)
5139static 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));
5140static void uc_list __ARGS((char_u *name, size_t name_len));
5141static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5142static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5143static 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));
5144
5145 static int
5146uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5147 char_u *name;
5148 size_t name_len;
5149 char_u *rep;
5150 long argt;
5151 long def;
5152 int flags;
5153 int compl;
5154 char_u *compl_arg;
5155 int force;
5156{
5157 ucmd_T *cmd = NULL;
5158 char_u *p;
5159 int i;
5160 int cmp = 1;
5161 char_u *rep_buf = NULL;
5162 garray_T *gap;
5163
Bram Moolenaar9c102382006-05-03 21:26:49 +00005164 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (rep_buf == NULL)
5166 {
5167 /* Can't replace termcodes - try using the string as is */
5168 rep_buf = vim_strsave(rep);
5169
5170 /* Give up if out of memory */
5171 if (rep_buf == NULL)
5172 return FAIL;
5173 }
5174
5175 /* get address of growarray: global or in curbuf */
5176 if (flags & UC_BUFFER)
5177 {
5178 gap = &curbuf->b_ucmds;
5179 if (gap->ga_itemsize == 0)
5180 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5181 }
5182 else
5183 gap = &ucmds;
5184
5185 /* Search for the command in the already defined commands. */
5186 for (i = 0; i < gap->ga_len; ++i)
5187 {
5188 size_t len;
5189
5190 cmd = USER_CMD_GA(gap, i);
5191 len = STRLEN(cmd->uc_name);
5192 cmp = STRNCMP(name, cmd->uc_name, name_len);
5193 if (cmp == 0)
5194 {
5195 if (name_len < len)
5196 cmp = -1;
5197 else if (name_len > len)
5198 cmp = 1;
5199 }
5200
5201 if (cmp == 0)
5202 {
5203 if (!force)
5204 {
5205 EMSG(_("E174: Command already exists: add ! to replace it"));
5206 goto fail;
5207 }
5208
5209 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005210 cmd->uc_rep = NULL;
5211#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5212 vim_free(cmd->uc_compl_arg);
5213 cmd->uc_compl_arg = NULL;
5214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
5217
5218 /* Stop as soon as we pass the name to add */
5219 if (cmp < 0)
5220 break;
5221 }
5222
5223 /* Extend the array unless we're replacing an existing command */
5224 if (cmp != 0)
5225 {
5226 if (ga_grow(gap, 1) != OK)
5227 goto fail;
5228 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5229 goto fail;
5230
5231 cmd = USER_CMD_GA(gap, i);
5232 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5233
5234 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 cmd->uc_name = p;
5237 }
5238
5239 cmd->uc_rep = rep_buf;
5240 cmd->uc_argt = argt;
5241 cmd->uc_def = def;
5242 cmd->uc_compl = compl;
5243#ifdef FEAT_EVAL
5244 cmd->uc_scriptID = current_SID;
5245# ifdef FEAT_CMDL_COMPL
5246 cmd->uc_compl_arg = compl_arg;
5247# endif
5248#endif
5249
5250 return OK;
5251
5252fail:
5253 vim_free(rep_buf);
5254#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5255 vim_free(compl_arg);
5256#endif
5257 return FAIL;
5258}
5259
5260/*
5261 * List of names for completion for ":command" with the EXPAND_ flag.
5262 * Must be alphabetical for completion.
5263 */
5264static struct
5265{
5266 int expand;
5267 char *name;
5268} command_complete[] =
5269{
5270 {EXPAND_AUGROUP, "augroup"},
5271 {EXPAND_BUFFERS, "buffer"},
5272 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005273#if defined(FEAT_CSCOPE)
5274 {EXPAND_CSCOPE, "cscope"},
5275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5277 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005278 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280 {EXPAND_DIRECTORIES, "dir"},
5281 {EXPAND_ENV_VARS, "environment"},
5282 {EXPAND_EVENTS, "event"},
5283 {EXPAND_EXPRESSION, "expression"},
5284 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005285 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {EXPAND_FUNCTIONS, "function"},
5287 {EXPAND_HELP, "help"},
5288 {EXPAND_HIGHLIGHT, "highlight"},
5289 {EXPAND_MAPPINGS, "mapping"},
5290 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005291 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005293 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005294#if defined(FEAT_SIGNS)
5295 {EXPAND_SIGN, "sign"},
5296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {EXPAND_TAGS, "tag"},
5298 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5299 {EXPAND_USER_VARS, "var"},
5300 {0, NULL}
5301};
5302
5303 static void
5304uc_list(name, name_len)
5305 char_u *name;
5306 size_t name_len;
5307{
5308 int i, j;
5309 int found = FALSE;
5310 ucmd_T *cmd;
5311 int len;
5312 long a;
5313 garray_T *gap;
5314
5315 gap = &curbuf->b_ucmds;
5316 for (;;)
5317 {
5318 for (i = 0; i < gap->ga_len; ++i)
5319 {
5320 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005321 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 /* Skip commands which don't match the requested prefix */
5324 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5325 continue;
5326
5327 /* Put out the title first time */
5328 if (!found)
5329 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5330 found = TRUE;
5331 msg_putchar('\n');
5332 if (got_int)
5333 break;
5334
5335 /* Special cases */
5336 msg_putchar(a & BANG ? '!' : ' ');
5337 msg_putchar(a & REGSTR ? '"' : ' ');
5338 msg_putchar(gap != &ucmds ? 'b' : ' ');
5339 msg_putchar(' ');
5340
5341 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5342 len = (int)STRLEN(cmd->uc_name) + 4;
5343
5344 do {
5345 msg_putchar(' ');
5346 ++len;
5347 } while (len < 16);
5348
5349 len = 0;
5350
5351 /* Arguments */
5352 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5353 {
5354 case 0: IObuff[len++] = '0'; break;
5355 case (EXTRA): IObuff[len++] = '*'; break;
5356 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5357 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5358 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5359 }
5360
5361 do {
5362 IObuff[len++] = ' ';
5363 } while (len < 5);
5364
5365 /* Range */
5366 if (a & (RANGE|COUNT))
5367 {
5368 if (a & COUNT)
5369 {
5370 /* -count=N */
5371 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5372 len += (int)STRLEN(IObuff + len);
5373 }
5374 else if (a & DFLALL)
5375 IObuff[len++] = '%';
5376 else if (cmd->uc_def >= 0)
5377 {
5378 /* -range=N */
5379 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5380 len += (int)STRLEN(IObuff + len);
5381 }
5382 else
5383 IObuff[len++] = '.';
5384 }
5385
5386 do {
5387 IObuff[len++] = ' ';
5388 } while (len < 11);
5389
5390 /* Completion */
5391 for (j = 0; command_complete[j].expand != 0; ++j)
5392 if (command_complete[j].expand == cmd->uc_compl)
5393 {
5394 STRCPY(IObuff + len, command_complete[j].name);
5395 len += (int)STRLEN(IObuff + len);
5396 break;
5397 }
5398
5399 do {
5400 IObuff[len++] = ' ';
5401 } while (len < 21);
5402
5403 IObuff[len] = '\0';
5404 msg_outtrans(IObuff);
5405
5406 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005407#ifdef FEAT_EVAL
5408 if (p_verbose > 0)
5409 last_set_msg(cmd->uc_scriptID);
5410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 out_flush();
5412 ui_breakcheck();
5413 if (got_int)
5414 break;
5415 }
5416 if (gap == &ucmds || i < gap->ga_len)
5417 break;
5418 gap = &ucmds;
5419 }
5420
5421 if (!found)
5422 MSG(_("No user-defined commands found"));
5423}
5424
5425 static char_u *
5426uc_fun_cmd()
5427{
5428 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5429 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5430 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5431 0xb9, 0x7f, 0};
5432 int i;
5433
5434 for (i = 0; fcmd[i]; ++i)
5435 IObuff[i] = fcmd[i] - 0x40;
5436 IObuff[i] = 0;
5437 return IObuff;
5438}
5439
5440 static int
5441uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5442 char_u *attr;
5443 size_t len;
5444 long *argt;
5445 long *def;
5446 int *flags;
5447 int *compl;
5448 char_u **compl_arg;
5449{
5450 char_u *p;
5451
5452 if (len == 0)
5453 {
5454 EMSG(_("E175: No attribute specified"));
5455 return FAIL;
5456 }
5457
5458 /* First, try the simple attributes (no arguments) */
5459 if (STRNICMP(attr, "bang", len) == 0)
5460 *argt |= BANG;
5461 else if (STRNICMP(attr, "buffer", len) == 0)
5462 *flags |= UC_BUFFER;
5463 else if (STRNICMP(attr, "register", len) == 0)
5464 *argt |= REGSTR;
5465 else if (STRNICMP(attr, "bar", len) == 0)
5466 *argt |= TRLBAR;
5467 else
5468 {
5469 int i;
5470 char_u *val = NULL;
5471 size_t vallen = 0;
5472 size_t attrlen = len;
5473
5474 /* Look for the attribute name - which is the part before any '=' */
5475 for (i = 0; i < (int)len; ++i)
5476 {
5477 if (attr[i] == '=')
5478 {
5479 val = &attr[i + 1];
5480 vallen = len - i - 1;
5481 attrlen = i;
5482 break;
5483 }
5484 }
5485
5486 if (STRNICMP(attr, "nargs", attrlen) == 0)
5487 {
5488 if (vallen == 1)
5489 {
5490 if (*val == '0')
5491 /* Do nothing - this is the default */;
5492 else if (*val == '1')
5493 *argt |= (EXTRA | NOSPC | NEEDARG);
5494 else if (*val == '*')
5495 *argt |= EXTRA;
5496 else if (*val == '?')
5497 *argt |= (EXTRA | NOSPC);
5498 else if (*val == '+')
5499 *argt |= (EXTRA | NEEDARG);
5500 else
5501 goto wrong_nargs;
5502 }
5503 else
5504 {
5505wrong_nargs:
5506 EMSG(_("E176: Invalid number of arguments"));
5507 return FAIL;
5508 }
5509 }
5510 else if (STRNICMP(attr, "range", attrlen) == 0)
5511 {
5512 *argt |= RANGE;
5513 if (vallen == 1 && *val == '%')
5514 *argt |= DFLALL;
5515 else if (val != NULL)
5516 {
5517 p = val;
5518 if (*def >= 0)
5519 {
5520two_count:
5521 EMSG(_("E177: Count cannot be specified twice"));
5522 return FAIL;
5523 }
5524
5525 *def = getdigits(&p);
5526 *argt |= (ZEROR | NOTADR);
5527
5528 if (p != val + vallen || vallen == 0)
5529 {
5530invalid_count:
5531 EMSG(_("E178: Invalid default value for count"));
5532 return FAIL;
5533 }
5534 }
5535 }
5536 else if (STRNICMP(attr, "count", attrlen) == 0)
5537 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005538 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 if (val != NULL)
5541 {
5542 p = val;
5543 if (*def >= 0)
5544 goto two_count;
5545
5546 *def = getdigits(&p);
5547
5548 if (p != val + vallen)
5549 goto invalid_count;
5550 }
5551
5552 if (*def < 0)
5553 *def = 0;
5554 }
5555 else if (STRNICMP(attr, "complete", attrlen) == 0)
5556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (val == NULL)
5558 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005559 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 return FAIL;
5561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005563 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5564 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 else
5568 {
5569 char_u ch = attr[len];
5570 attr[len] = '\0';
5571 EMSG2(_("E181: Invalid attribute: %s"), attr);
5572 attr[len] = ch;
5573 return FAIL;
5574 }
5575 }
5576
5577 return OK;
5578}
5579
Bram Moolenaara850a712009-01-28 14:42:59 +00005580/*
5581 * ":command ..."
5582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 static void
5584ex_command(eap)
5585 exarg_T *eap;
5586{
5587 char_u *name;
5588 char_u *end;
5589 char_u *p;
5590 long argt = 0;
5591 long def = -1;
5592 int flags = 0;
5593 int compl = EXPAND_NOTHING;
5594 char_u *compl_arg = NULL;
5595 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005596 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598 p = eap->arg;
5599
5600 /* Check for attributes */
5601 while (*p == '-')
5602 {
5603 ++p;
5604 end = skiptowhite(p);
5605 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5606 == FAIL)
5607 return;
5608 p = skipwhite(end);
5609 }
5610
5611 /* Get the name (if any) and skip to the following argument */
5612 name = p;
5613 if (ASCII_ISALPHA(*p))
5614 while (ASCII_ISALNUM(*p))
5615 ++p;
5616 if (!ends_excmd(*p) && !vim_iswhite(*p))
5617 {
5618 EMSG(_("E182: Invalid command name"));
5619 return;
5620 }
5621 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005622 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 /* If there is nothing after the name, and no attributes were specified,
5625 * we are listing commands
5626 */
5627 p = skipwhite(end);
5628 if (!has_attr && ends_excmd(*p))
5629 {
5630 uc_list(name, end - name);
5631 }
5632 else if (!ASCII_ISUPPER(*name))
5633 {
5634 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5635 return;
5636 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005637 else if ((name_len == 1 && *name == 'X')
5638 || (name_len <= 4
5639 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5640 {
5641 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5642 return;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 else
5645 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5646 eap->forceit);
5647}
5648
5649/*
5650 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 * Clear all user commands, global and for current buffer.
5652 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005653 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005655 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 uc_clear(&ucmds);
5658 uc_clear(&curbuf->b_ucmds);
5659}
5660
5661/*
5662 * Clear all user commands for "gap".
5663 */
5664 void
5665uc_clear(gap)
5666 garray_T *gap;
5667{
5668 int i;
5669 ucmd_T *cmd;
5670
5671 for (i = 0; i < gap->ga_len; ++i)
5672 {
5673 cmd = USER_CMD_GA(gap, i);
5674 vim_free(cmd->uc_name);
5675 vim_free(cmd->uc_rep);
5676# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5677 vim_free(cmd->uc_compl_arg);
5678# endif
5679 }
5680 ga_clear(gap);
5681}
5682
5683 static void
5684ex_delcommand(eap)
5685 exarg_T *eap;
5686{
5687 int i = 0;
5688 ucmd_T *cmd = NULL;
5689 int cmp = -1;
5690 garray_T *gap;
5691
5692 gap = &curbuf->b_ucmds;
5693 for (;;)
5694 {
5695 for (i = 0; i < gap->ga_len; ++i)
5696 {
5697 cmd = USER_CMD_GA(gap, i);
5698 cmp = STRCMP(eap->arg, cmd->uc_name);
5699 if (cmp <= 0)
5700 break;
5701 }
5702 if (gap == &ucmds || cmp == 0)
5703 break;
5704 gap = &ucmds;
5705 }
5706
5707 if (cmp != 0)
5708 {
5709 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5710 return;
5711 }
5712
5713 vim_free(cmd->uc_name);
5714 vim_free(cmd->uc_rep);
5715# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5716 vim_free(cmd->uc_compl_arg);
5717# endif
5718
5719 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 if (i < gap->ga_len)
5722 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5723}
5724
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005725/*
5726 * split and quote args for <f-args>
5727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 static char_u *
5729uc_split_args(arg, lenp)
5730 char_u *arg;
5731 size_t *lenp;
5732{
5733 char_u *buf;
5734 char_u *p;
5735 char_u *q;
5736 int len;
5737
5738 /* Precalculate length */
5739 p = arg;
5740 len = 2; /* Initial and final quotes */
5741
5742 while (*p)
5743 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005744 if (p[0] == '\\' && p[1] == '\\')
5745 {
5746 len += 2;
5747 p += 2;
5748 }
5749 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 len += 1;
5752 p += 2;
5753 }
5754 else if (*p == '\\' || *p == '"')
5755 {
5756 len += 2;
5757 p += 1;
5758 }
5759 else if (vim_iswhite(*p))
5760 {
5761 p = skipwhite(p);
5762 if (*p == NUL)
5763 break;
5764 len += 3; /* "," */
5765 }
5766 else
5767 {
5768 ++len;
5769 ++p;
5770 }
5771 }
5772
5773 buf = alloc(len + 1);
5774 if (buf == NULL)
5775 {
5776 *lenp = 0;
5777 return buf;
5778 }
5779
5780 p = arg;
5781 q = buf;
5782 *q++ = '"';
5783 while (*p)
5784 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005785 if (p[0] == '\\' && p[1] == '\\')
5786 {
5787 *q++ = '\\';
5788 *q++ = '\\';
5789 p += 2;
5790 }
5791 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 *q++ = p[1];
5794 p += 2;
5795 }
5796 else if (*p == '\\' || *p == '"')
5797 {
5798 *q++ = '\\';
5799 *q++ = *p++;
5800 }
5801 else if (vim_iswhite(*p))
5802 {
5803 p = skipwhite(p);
5804 if (*p == NUL)
5805 break;
5806 *q++ = '"';
5807 *q++ = ',';
5808 *q++ = '"';
5809 }
5810 else
5811 {
5812 *q++ = *p++;
5813 }
5814 }
5815 *q++ = '"';
5816 *q = 0;
5817
5818 *lenp = len;
5819 return buf;
5820}
5821
5822/*
5823 * Check for a <> code in a user command.
5824 * "code" points to the '<'. "len" the length of the <> (inclusive).
5825 * "buf" is where the result is to be added.
5826 * "split_buf" points to a buffer used for splitting, caller should free it.
5827 * "split_len" is the length of what "split_buf" contains.
5828 * Returns the length of the replacement, which has been added to "buf".
5829 * Returns -1 if there was no match, and only the "<" has been copied.
5830 */
5831 static size_t
5832uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5833 char_u *code;
5834 size_t len;
5835 char_u *buf;
5836 ucmd_T *cmd; /* the user command we're expanding */
5837 exarg_T *eap; /* ex arguments */
5838 char_u **split_buf;
5839 size_t *split_len;
5840{
5841 size_t result = 0;
5842 char_u *p = code + 1;
5843 size_t l = len - 2;
5844 int quote = 0;
5845 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5846 ct_LT, ct_NONE } type = ct_NONE;
5847
5848 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5849 {
5850 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5851 p += 2;
5852 l -= 2;
5853 }
5854
Bram Moolenaar371d5402006-03-20 21:47:49 +00005855 ++l;
5856 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005858 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005860 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005862 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005864 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005866 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005868 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005870 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 type = ct_REGISTER;
5872
5873 switch (type)
5874 {
5875 case ct_ARGS:
5876 /* Simple case first */
5877 if (*eap->arg == NUL)
5878 {
5879 if (quote == 1)
5880 {
5881 result = 2;
5882 if (buf != NULL)
5883 STRCPY(buf, "''");
5884 }
5885 else
5886 result = 0;
5887 break;
5888 }
5889
5890 /* When specified there is a single argument don't split it.
5891 * Works for ":Cmd %" when % is "a b c". */
5892 if ((eap->argt & NOSPC) && quote == 2)
5893 quote = 1;
5894
5895 switch (quote)
5896 {
5897 case 0: /* No quoting, no splitting */
5898 result = STRLEN(eap->arg);
5899 if (buf != NULL)
5900 STRCPY(buf, eap->arg);
5901 break;
5902 case 1: /* Quote, but don't split */
5903 result = STRLEN(eap->arg) + 2;
5904 for (p = eap->arg; *p; ++p)
5905 {
5906 if (*p == '\\' || *p == '"')
5907 ++result;
5908 }
5909
5910 if (buf != NULL)
5911 {
5912 *buf++ = '"';
5913 for (p = eap->arg; *p; ++p)
5914 {
5915 if (*p == '\\' || *p == '"')
5916 *buf++ = '\\';
5917 *buf++ = *p;
5918 }
5919 *buf = '"';
5920 }
5921
5922 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005923 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 /* This is hard, so only do it once, and cache the result */
5925 if (*split_buf == NULL)
5926 *split_buf = uc_split_args(eap->arg, split_len);
5927
5928 result = *split_len;
5929 if (buf != NULL && result != 0)
5930 STRCPY(buf, *split_buf);
5931
5932 break;
5933 }
5934 break;
5935
5936 case ct_BANG:
5937 result = eap->forceit ? 1 : 0;
5938 if (quote)
5939 result += 2;
5940 if (buf != NULL)
5941 {
5942 if (quote)
5943 *buf++ = '"';
5944 if (eap->forceit)
5945 *buf++ = '!';
5946 if (quote)
5947 *buf = '"';
5948 }
5949 break;
5950
5951 case ct_LINE1:
5952 case ct_LINE2:
5953 case ct_COUNT:
5954 {
5955 char num_buf[20];
5956 long num = (type == ct_LINE1) ? eap->line1 :
5957 (type == ct_LINE2) ? eap->line2 :
5958 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5959 size_t num_len;
5960
5961 sprintf(num_buf, "%ld", num);
5962 num_len = STRLEN(num_buf);
5963 result = num_len;
5964
5965 if (quote)
5966 result += 2;
5967
5968 if (buf != NULL)
5969 {
5970 if (quote)
5971 *buf++ = '"';
5972 STRCPY(buf, num_buf);
5973 buf += num_len;
5974 if (quote)
5975 *buf = '"';
5976 }
5977
5978 break;
5979 }
5980
5981 case ct_REGISTER:
5982 result = eap->regname ? 1 : 0;
5983 if (quote)
5984 result += 2;
5985 if (buf != NULL)
5986 {
5987 if (quote)
5988 *buf++ = '\'';
5989 if (eap->regname)
5990 *buf++ = eap->regname;
5991 if (quote)
5992 *buf = '\'';
5993 }
5994 break;
5995
5996 case ct_LT:
5997 result = 1;
5998 if (buf != NULL)
5999 *buf = '<';
6000 break;
6001
6002 default:
6003 /* Not recognized: just copy the '<' and return -1. */
6004 result = (size_t)-1;
6005 if (buf != NULL)
6006 *buf = '<';
6007 break;
6008 }
6009
6010 return result;
6011}
6012
6013 static void
6014do_ucmd(eap)
6015 exarg_T *eap;
6016{
6017 char_u *buf;
6018 char_u *p;
6019 char_u *q;
6020
6021 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006022 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006023 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 size_t len, totlen;
6025
6026 size_t split_len = 0;
6027 char_u *split_buf = NULL;
6028 ucmd_T *cmd;
6029#ifdef FEAT_EVAL
6030 scid_T save_current_SID = current_SID;
6031#endif
6032
6033 if (eap->cmdidx == CMD_USER)
6034 cmd = USER_CMD(eap->useridx);
6035 else
6036 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6037
6038 /*
6039 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006040 * First round: "buf" is NULL, compute length, allocate "buf".
6041 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 */
6043 buf = NULL;
6044 for (;;)
6045 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006046 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006047 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006049
6050 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006052 start = vim_strchr(p, '<');
6053 if (start != NULL)
6054 end = vim_strchr(start + 1, '>');
6055 if (buf != NULL)
6056 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006057 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6058 ;
6059 if (*ksp == K_SPECIAL
6060 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006061 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6062# ifdef FEAT_GUI
6063 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6064# endif
6065 ))
6066 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006067 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006068 * KS_SPECIAL KE_FILLER, like for mappings, but
6069 * do_cmdline() doesn't handle that, so convert it back.
6070 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6071 len = ksp - p;
6072 if (len > 0)
6073 {
6074 mch_memmove(q, p, len);
6075 q += len;
6076 }
6077 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6078 p = ksp + 3;
6079 continue;
6080 }
6081 }
6082
6083 /* break if there no <item> is found */
6084 if (start == NULL || end == NULL)
6085 break;
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /* Include the '>' */
6088 ++end;
6089
6090 /* Take everything up to the '<' */
6091 len = start - p;
6092 if (buf == NULL)
6093 totlen += len;
6094 else
6095 {
6096 mch_memmove(q, p, len);
6097 q += len;
6098 }
6099
6100 len = uc_check_code(start, end - start, q, cmd, eap,
6101 &split_buf, &split_len);
6102 if (len == (size_t)-1)
6103 {
6104 /* no match, continue after '<' */
6105 p = start + 1;
6106 len = 1;
6107 }
6108 else
6109 p = end;
6110 if (buf == NULL)
6111 totlen += len;
6112 else
6113 q += len;
6114 }
6115 if (buf != NULL) /* second time here, finished */
6116 {
6117 STRCPY(q, p);
6118 break;
6119 }
6120
6121 totlen += STRLEN(p); /* Add on the trailing characters */
6122 buf = alloc((unsigned)(totlen + 1));
6123 if (buf == NULL)
6124 {
6125 vim_free(split_buf);
6126 return;
6127 }
6128 }
6129
6130#ifdef FEAT_EVAL
6131 current_SID = cmd->uc_scriptID;
6132#endif
6133 (void)do_cmdline(buf, eap->getline, eap->cookie,
6134 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6135#ifdef FEAT_EVAL
6136 current_SID = save_current_SID;
6137#endif
6138 vim_free(buf);
6139 vim_free(split_buf);
6140}
6141
6142# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6143 static char_u *
6144get_user_command_name(idx)
6145 int idx;
6146{
6147 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6148}
6149
6150/*
6151 * Function given to ExpandGeneric() to obtain the list of user command names.
6152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 char_u *
6154get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006155 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 int idx;
6157{
6158 if (idx < curbuf->b_ucmds.ga_len)
6159 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6160 idx -= curbuf->b_ucmds.ga_len;
6161 if (idx < ucmds.ga_len)
6162 return USER_CMD(idx)->uc_name;
6163 return NULL;
6164}
6165
6166/*
6167 * Function given to ExpandGeneric() to obtain the list of user command
6168 * attributes.
6169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 char_u *
6171get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006172 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 int idx;
6174{
6175 static char *user_cmd_flags[] =
6176 {"bang", "bar", "buffer", "complete", "count",
6177 "nargs", "range", "register"};
6178
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006179 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 return NULL;
6181 return (char_u *)user_cmd_flags[idx];
6182}
6183
6184/*
6185 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 char_u *
6188get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006189 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 int idx;
6191{
6192 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6193
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006194 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 return NULL;
6196 return (char_u *)user_cmd_nargs[idx];
6197}
6198
6199/*
6200 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 char_u *
6203get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006204 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 int idx;
6206{
6207 return (char_u *)command_complete[idx].name;
6208}
6209# endif /* FEAT_CMDL_COMPL */
6210
6211#endif /* FEAT_USR_CMDS */
6212
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006213#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6214/*
6215 * Parse a completion argument "value[vallen]".
6216 * The detected completion goes in "*complp", argument type in "*argt".
6217 * When there is an argument, for function and user defined completion, it's
6218 * copied to allocated memory and stored in "*compl_arg".
6219 * Returns FAIL if something is wrong.
6220 */
6221 int
6222parse_compl_arg(value, vallen, complp, argt, compl_arg)
6223 char_u *value;
6224 int vallen;
6225 int *complp;
6226 long *argt;
6227 char_u **compl_arg;
6228{
6229 char_u *arg = NULL;
6230 size_t arglen = 0;
6231 int i;
6232 int valend = vallen;
6233
6234 /* Look for any argument part - which is the part after any ',' */
6235 for (i = 0; i < vallen; ++i)
6236 {
6237 if (value[i] == ',')
6238 {
6239 arg = &value[i + 1];
6240 arglen = vallen - i - 1;
6241 valend = i;
6242 break;
6243 }
6244 }
6245
6246 for (i = 0; command_complete[i].expand != 0; ++i)
6247 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006248 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006249 && STRNCMP(value, command_complete[i].name, valend) == 0)
6250 {
6251 *complp = command_complete[i].expand;
6252 if (command_complete[i].expand == EXPAND_BUFFERS)
6253 *argt |= BUFNAME;
6254 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6255 || command_complete[i].expand == EXPAND_FILES)
6256 *argt |= XFILE;
6257 break;
6258 }
6259 }
6260
6261 if (command_complete[i].expand == 0)
6262 {
6263 EMSG2(_("E180: Invalid complete value: %s"), value);
6264 return FAIL;
6265 }
6266
6267# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6268 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6269 && arg != NULL)
6270# else
6271 if (arg != NULL)
6272# endif
6273 {
6274 EMSG(_("E468: Completion argument only allowed for custom completion"));
6275 return FAIL;
6276 }
6277
6278# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6279 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6280 && arg == NULL)
6281 {
6282 EMSG(_("E467: Custom completion requires a function argument"));
6283 return FAIL;
6284 }
6285
6286 if (arg != NULL)
6287 *compl_arg = vim_strnsave(arg, (int)arglen);
6288# endif
6289 return OK;
6290}
6291#endif
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 static void
6294ex_colorscheme(eap)
6295 exarg_T *eap;
6296{
Bram Moolenaare6850792010-05-14 15:28:44 +02006297 if (*eap->arg == NUL)
6298 {
6299#ifdef FEAT_EVAL
6300 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6301 char_u *p = NULL;
6302
6303 if (expr != NULL)
6304 {
6305 ++emsg_off;
6306 p = eval_to_string(expr, NULL, FALSE);
6307 --emsg_off;
6308 vim_free(expr);
6309 }
6310 if (p != NULL)
6311 {
6312 MSG(p);
6313 vim_free(p);
6314 }
6315 else
6316 MSG("default");
6317#else
6318 MSG(_("unknown"));
6319#endif
6320 }
6321 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6323}
6324
6325 static void
6326ex_highlight(eap)
6327 exarg_T *eap;
6328{
6329 if (*eap->arg == NUL && eap->cmd[2] == '!')
6330 MSG(_("Greetings, Vim user!"));
6331 do_highlight(eap->arg, eap->forceit, FALSE);
6332}
6333
6334
6335/*
6336 * Call this function if we thought we were going to exit, but we won't
6337 * (because of an error). May need to restore the terminal mode.
6338 */
6339 void
6340not_exiting()
6341{
6342 exiting = FALSE;
6343 settmode(TMODE_RAW);
6344}
6345
6346/*
6347 * ":quit": quit current window, quit Vim if closed the last window.
6348 */
6349 static void
6350ex_quit(eap)
6351 exarg_T *eap;
6352{
6353#ifdef FEAT_CMDWIN
6354 if (cmdwin_type != 0)
6355 {
6356 cmdwin_result = Ctrl_C;
6357 return;
6358 }
6359#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006360 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006361 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006362 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006363 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006364 return;
6365 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006366#ifdef FEAT_AUTOCMD
6367 if (curbuf_locked())
6368 return;
6369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
6371#ifdef FEAT_NETBEANS_INTG
6372 netbeansForcedQuit = eap->forceit;
6373#endif
6374
6375 /*
6376 * If there are more files or windows we won't exit.
6377 */
6378 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6379 exiting = TRUE;
6380 if ((!P_HID(curbuf)
6381 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6382 || check_more(TRUE, eap->forceit) == FAIL
6383 || (only_one_window() && check_changed_any(eap->forceit)))
6384 {
6385 not_exiting();
6386 }
6387 else
6388 {
6389#ifdef FEAT_WINDOWS
6390 if (only_one_window()) /* quit last window */
6391#endif
6392 getout(0);
6393#ifdef FEAT_WINDOWS
6394# ifdef FEAT_GUI
6395 need_mouse_correct = TRUE;
6396# endif
6397 /* close window; may free buffer */
6398 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6399#endif
6400 }
6401}
6402
6403/*
6404 * ":cquit".
6405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 static void
6407ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006408 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 getout(1); /* this does not always pass on the exit code to the Manx
6411 compiler. why? */
6412}
6413
6414/*
6415 * ":qall": try to quit all windows
6416 */
6417 static void
6418ex_quit_all(eap)
6419 exarg_T *eap;
6420{
6421# ifdef FEAT_CMDWIN
6422 if (cmdwin_type != 0)
6423 {
6424 if (eap->forceit)
6425 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6426 else
6427 cmdwin_result = K_XF2;
6428 return;
6429 }
6430# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006431
6432 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006433 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006434 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006435 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006436 return;
6437 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006438#ifdef FEAT_AUTOCMD
6439 if (curbuf_locked())
6440 return;
6441#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006442
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 exiting = TRUE;
6444 if (eap->forceit || !check_changed_any(FALSE))
6445 getout(0);
6446 not_exiting();
6447}
6448
Bram Moolenaard9967712006-03-11 21:18:15 +00006449#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450/*
6451 * ":close": close current window, unless it is the last one
6452 */
6453 static void
6454ex_close(eap)
6455 exarg_T *eap;
6456{
6457# ifdef FEAT_CMDWIN
6458 if (cmdwin_type != 0)
6459 cmdwin_result = K_IGNORE;
6460 else
6461# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462 if (!text_locked()
6463#ifdef FEAT_AUTOCMD
6464 && !curbuf_locked()
6465#endif
6466 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006467 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468}
6469
Bram Moolenaard9967712006-03-11 21:18:15 +00006470# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471/*
6472 * ":pclose": Close any preview window.
6473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477{
6478 win_T *win;
6479
6480 for (win = firstwin; win != NULL; win = win->w_next)
6481 if (win->w_p_pvw)
6482 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006483 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484 break;
6485 }
6486}
Bram Moolenaard9967712006-03-11 21:18:15 +00006487# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488
Bram Moolenaarf740b292006-02-16 22:11:02 +00006489/*
6490 * Close window "win" and take care of handling closing the last window for a
6491 * modified buffer.
6492 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006494ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006497 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 int need_hide;
6500 buf_T *buf = win->w_buffer;
6501
6502 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006503 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006505# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if ((p_confirm || cmdmod.confirm) && p_write)
6507 {
6508 dialog_changed(buf, FALSE);
6509 if (buf_valid(buf) && bufIsChanged(buf))
6510 return;
6511 need_hide = FALSE;
6512 }
6513 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 EMSG(_(e_nowrtmsg));
6517 return;
6518 }
6519 }
6520
Bram Moolenaard9967712006-03-11 21:18:15 +00006521# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006523# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006526 if (tp == NULL)
6527 win_close(win, !need_hide && !P_HID(buf));
6528 else
6529 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006533 * ":tabclose": close current tab page, unless it is the last one.
6534 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 */
6536 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 exarg_T *eap;
6539{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006540 tabpage_T *tp;
6541
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542# ifdef FEAT_CMDWIN
6543 if (cmdwin_type != 0)
6544 cmdwin_result = K_IGNORE;
6545 else
6546# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006547 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006548 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006552 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006553 tp = find_tabpage((int)eap->line2);
6554 if (tp == NULL)
6555 {
6556 beep_flush();
6557 return;
6558 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006559 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006560 {
6561 tabpage_close_other(tp, eap->forceit);
6562 return;
6563 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006564 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006565 if (!text_locked()
6566#ifdef FEAT_AUTOCMD
6567 && !curbuf_locked()
6568#endif
6569 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006570 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006572}
6573
6574/*
6575 * ":tabonly": close all tab pages except the current one
6576 */
6577 static void
6578ex_tabonly(eap)
6579 exarg_T *eap;
6580{
6581 tabpage_T *tp;
6582 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006583
6584# ifdef FEAT_CMDWIN
6585 if (cmdwin_type != 0)
6586 cmdwin_result = K_IGNORE;
6587 else
6588# endif
6589 if (first_tabpage->tp_next == NULL)
6590 MSG(_("Already only one tab page"));
6591 else
6592 {
6593 /* Repeat this up to a 1000 times, because autocommands may mess
6594 * up the lists. */
6595 for (done = 0; done < 1000; ++done)
6596 {
6597 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6598 if (tp->tp_topframe != topframe)
6599 {
6600 tabpage_close_other(tp, eap->forceit);
6601 /* if we failed to close it quit */
6602 if (valid_tabpage(tp))
6603 done = 1000;
6604 /* start over, "tp" is now invalid */
6605 break;
6606 }
6607 if (first_tabpage->tp_next == NULL)
6608 break;
6609 }
6610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
6613/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006614 * Close the current tab page.
6615 */
6616 void
6617tabpage_close(forceit)
6618 int forceit;
6619{
6620 /* First close all the windows but the current one. If that worked then
6621 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006622 if (lastwin != firstwin)
6623 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006624 if (lastwin == firstwin)
6625 ex_win_close(forceit, curwin, NULL);
6626# ifdef FEAT_GUI
6627 need_mouse_correct = TRUE;
6628# endif
6629}
6630
6631/*
6632 * Close tab page "tp", which is not the current tab page.
6633 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006634 * Also takes care of the tab pages line disappearing when closing the
6635 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006636 */
6637 void
6638tabpage_close_other(tp, forceit)
6639 tabpage_T *tp;
6640 int forceit;
6641{
6642 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006643 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006644 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006645
6646 /* Limit to 1000 windows, autocommands may add a window while we close
6647 * one. OK, so I'm paranoid... */
6648 while (++done < 1000)
6649 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006650 wp = tp->tp_firstwin;
6651 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006652
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006653 /* Autocommands may delete the tab page under our fingers and we may
6654 * fail to close a window with a modified buffer. */
6655 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006656 break;
6657 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006658
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006659 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006660 if (h != tabline_height())
6661 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006662}
6663
6664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 * ":only".
6666 */
6667 static void
6668ex_only(eap)
6669 exarg_T *eap;
6670{
6671# ifdef FEAT_GUI
6672 need_mouse_correct = TRUE;
6673# endif
6674 close_others(TRUE, eap->forceit);
6675}
6676
6677/*
6678 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006679 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006681 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682ex_all(eap)
6683 exarg_T *eap;
6684{
6685 if (eap->addr_count == 0)
6686 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006687 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689#endif /* FEAT_WINDOWS */
6690
6691 static void
6692ex_hide(eap)
6693 exarg_T *eap;
6694{
6695 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6696 eap->errmsg = e_invarg;
6697 else
6698 {
6699 /* ":hide" or ":hide | cmd": hide current window */
6700 eap->nextcmd = check_nextcmd(eap->arg);
6701#ifdef FEAT_WINDOWS
6702 if (!eap->skip)
6703 {
6704# ifdef FEAT_GUI
6705 need_mouse_correct = TRUE;
6706# endif
6707 win_close(curwin, FALSE); /* don't free buffer */
6708 }
6709#endif
6710 }
6711}
6712
6713/*
6714 * ":stop" and ":suspend": Suspend Vim.
6715 */
6716 static void
6717ex_stop(eap)
6718 exarg_T *eap;
6719{
6720 /*
6721 * Disallow suspending for "rvim".
6722 */
6723 if (!check_restricted()
6724#ifdef WIN3264
6725 /*
6726 * Check if external commands are allowed now.
6727 */
6728 && can_end_termcap_mode(TRUE)
6729#endif
6730 )
6731 {
6732 if (!eap->forceit)
6733 autowrite_all();
6734 windgoto((int)Rows - 1, 0);
6735 out_char('\n');
6736 out_flush();
6737 stoptermcap();
6738 out_flush(); /* needed for SUN to restore xterm buffer */
6739#ifdef FEAT_TITLE
6740 mch_restore_title(3); /* restore window titles */
6741#endif
6742 ui_suspend(); /* call machine specific function */
6743#ifdef FEAT_TITLE
6744 maketitle();
6745 resettitle(); /* force updating the title */
6746#endif
6747 starttermcap();
6748 scroll_start(); /* scroll screen before redrawing */
6749 redraw_later_clear();
6750 shell_resized(); /* may have resized window */
6751 }
6752}
6753
6754/*
6755 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6756 */
6757 static void
6758ex_exit(eap)
6759 exarg_T *eap;
6760{
6761#ifdef FEAT_CMDWIN
6762 if (cmdwin_type != 0)
6763 {
6764 cmdwin_result = Ctrl_C;
6765 return;
6766 }
6767#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006768 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006769 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006770 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006771 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006772 return;
6773 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006774#ifdef FEAT_AUTOCMD
6775 if (curbuf_locked())
6776 return;
6777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779 /*
6780 * if more files or windows we won't exit
6781 */
6782 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6783 exiting = TRUE;
6784 if ( ((eap->cmdidx == CMD_wq
6785 || curbufIsChanged())
6786 && do_write(eap) == FAIL)
6787 || check_more(TRUE, eap->forceit) == FAIL
6788 || (only_one_window() && check_changed_any(eap->forceit)))
6789 {
6790 not_exiting();
6791 }
6792 else
6793 {
6794#ifdef FEAT_WINDOWS
6795 if (only_one_window()) /* quit last window, exit Vim */
6796#endif
6797 getout(0);
6798#ifdef FEAT_WINDOWS
6799# ifdef FEAT_GUI
6800 need_mouse_correct = TRUE;
6801# endif
6802 /* quit current window, may free buffer */
6803 win_close(curwin, !P_HID(curwin->w_buffer));
6804#endif
6805 }
6806}
6807
6808/*
6809 * ":print", ":list", ":number".
6810 */
6811 static void
6812ex_print(eap)
6813 exarg_T *eap;
6814{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006815 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6816 EMSG(_(e_emptybuf));
6817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006819 for ( ;!got_int; ui_breakcheck())
6820 {
6821 print_line(eap->line1,
6822 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6823 || (eap->flags & EXFLAG_NR)),
6824 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6825 if (++eap->line1 > eap->line2)
6826 break;
6827 out_flush(); /* show one line at a time */
6828 }
6829 setpcmark();
6830 /* put cursor at last line */
6831 curwin->w_cursor.lnum = eap->line2;
6832 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836}
6837
6838#ifdef FEAT_BYTEOFF
6839 static void
6840ex_goto(eap)
6841 exarg_T *eap;
6842{
6843 goto_byte(eap->line2);
6844}
6845#endif
6846
6847/*
6848 * ":shell".
6849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 static void
6851ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006852 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 do_shell(NULL, 0);
6855}
6856
6857#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6858 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006859 || defined(FEAT_GUI_MSWIN) \
6860 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 || defined(PROTO)
6862
6863/*
6864 * Handle a file drop. The code is here because a drop is *nearly* like an
6865 * :args command, but not quite (we have a list of exact filenames, so we
6866 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6867 * code is very similar to :args and hence needs access to a lot of the static
6868 * functions in this file.
6869 *
6870 * The list should be allocated using alloc(), as should each item in the
6871 * list. This function takes over responsibility for freeing the list.
6872 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006873 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6875 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6876 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6877 * file functionality is (currently) not in EMX this is not presently a
6878 * problem.
6879 */
6880 void
6881handle_drop(filec, filev, split)
6882 int filec; /* the number of files dropped */
6883 char_u **filev; /* the list of files dropped */
6884 int split; /* force splitting the window */
6885{
6886 exarg_T ea;
6887 int save_msg_scroll = msg_scroll;
6888
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006889 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006890 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006892#ifdef FEAT_AUTOCMD
6893 if (curbuf_locked())
6894 return;
6895#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006896 /* When the screen is being updated we should not change buffers and
6897 * windows structures, it may cause freed memory to be used. */
6898 if (updating_screen)
6899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
6901 /* Check whether the current buffer is changed. If so, we will need
6902 * to split the current window or data could be lost.
6903 * We don't need to check if the 'hidden' option is set, as in this
6904 * case the buffer won't be lost.
6905 */
6906 if (!P_HID(curbuf) && !split)
6907 {
6908 ++emsg_off;
6909 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6910 --emsg_off;
6911 }
6912 if (split)
6913 {
6914# ifdef FEAT_WINDOWS
6915 if (win_split(0, 0) == FAIL)
6916 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006917 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
6919 /* When splitting the window, create a new alist. Otherwise the
6920 * existing one is overwritten. */
6921 alist_unlink(curwin->w_alist);
6922 alist_new();
6923# else
6924 return; /* can't split, always fail */
6925# endif
6926 }
6927
6928 /*
6929 * Set up the new argument list.
6930 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006931 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
6933 /*
6934 * Move to the first file.
6935 */
6936 /* Fake up a minimal "next" command for do_argfile() */
6937 vim_memset(&ea, 0, sizeof(ea));
6938 ea.cmd = (char_u *)"next";
6939 do_argfile(&ea, 0);
6940
6941 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6942 * mode that is not needed here. */
6943 need_start_insertmode = FALSE;
6944
6945 /* Restore msg_scroll, otherwise a following command may cause scrolling
6946 * unexpectedly. The screen will be redrawn by the caller, thus
6947 * msg_scroll being set by displaying a message is irrelevant. */
6948 msg_scroll = save_msg_scroll;
6949}
6950#endif
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952/*
6953 * Clear an argument list: free all file names and reset it to zero entries.
6954 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006955 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956alist_clear(al)
6957 alist_T *al;
6958{
6959 while (--al->al_ga.ga_len >= 0)
6960 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6961 ga_clear(&al->al_ga);
6962}
6963
6964/*
6965 * Init an argument list.
6966 */
6967 void
6968alist_init(al)
6969 alist_T *al;
6970{
6971 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6972}
6973
6974#if defined(FEAT_WINDOWS) || defined(PROTO)
6975
6976/*
6977 * Remove a reference from an argument list.
6978 * Ignored when the argument list is the global one.
6979 * If the argument list is no longer used by any window, free it.
6980 */
6981 void
6982alist_unlink(al)
6983 alist_T *al;
6984{
6985 if (al != &global_alist && --al->al_refcount <= 0)
6986 {
6987 alist_clear(al);
6988 vim_free(al);
6989 }
6990}
6991
6992# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6993/*
6994 * Create a new argument list and use it for the current window.
6995 */
6996 void
6997alist_new()
6998{
6999 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7000 if (curwin->w_alist == NULL)
7001 {
7002 curwin->w_alist = &global_alist;
7003 ++global_alist.al_refcount;
7004 }
7005 else
7006 {
7007 curwin->w_alist->al_refcount = 1;
7008 alist_init(curwin->w_alist);
7009 }
7010}
7011# endif
7012#endif
7013
7014#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7015/*
7016 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007017 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7018 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 */
7020 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007021alist_expand(fnum_list, fnum_len)
7022 int *fnum_list;
7023 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007026 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 char_u **new_arg_files;
7028 int new_arg_file_count;
7029 char_u *save_p_su = p_su;
7030 int i;
7031
7032 /* Don't use 'suffixes' here. This should work like the shell did the
7033 * expansion. Also, the vimrc file isn't read yet, thus the user
7034 * can't set the options. */
7035 p_su = empty_option;
7036 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7037 if (old_arg_files != NULL)
7038 {
7039 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007040 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7041 old_arg_count = GARGCOUNT;
7042 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 &new_arg_file_count, &new_arg_files,
7044 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7045 && new_arg_file_count > 0)
7046 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007047 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7048 TRUE, fnum_list, fnum_len);
7049 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 }
7052 p_su = save_p_su;
7053}
7054#endif
7055
7056/*
7057 * Set the argument list for the current window.
7058 * Takes over the allocated files[] and the allocated fnames in it.
7059 */
7060 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007061alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 alist_T *al;
7063 int count;
7064 char_u **files;
7065 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007066 int *fnum_list;
7067 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 int i;
7070
7071 alist_clear(al);
7072 if (ga_grow(&al->al_ga, count) == OK)
7073 {
7074 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007075 {
7076 if (got_int)
7077 {
7078 /* When adding many buffers this can take a long time. Allow
7079 * interrupting here. */
7080 while (i < count)
7081 vim_free(files[i++]);
7082 break;
7083 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007084
7085 /* May set buffer name of a buffer previously used for the
7086 * argument list, so that it's re-used by alist_add. */
7087 if (fnum_list != NULL && i < fnum_len)
7088 buf_set_name(fnum_list[i], files[i]);
7089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007091 ui_breakcheck();
7092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 vim_free(files);
7094 }
7095 else
7096 FreeWild(count, files);
7097#ifdef FEAT_WINDOWS
7098 if (al == &global_alist)
7099#endif
7100 arg_had_last = FALSE;
7101}
7102
7103/*
7104 * Add file "fname" to argument list "al".
7105 * "fname" must have been allocated and "al" must have been checked for room.
7106 */
7107 void
7108alist_add(al, fname, set_fnum)
7109 alist_T *al;
7110 char_u *fname;
7111 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7112{
7113 if (fname == NULL) /* don't add NULL file names */
7114 return;
7115#ifdef BACKSLASH_IN_FILENAME
7116 slash_adjust(fname);
7117#endif
7118 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7119 if (set_fnum > 0)
7120 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7121 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7122 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123}
7124
7125#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7126/*
7127 * Adjust slashes in file names. Called after 'shellslash' was set.
7128 */
7129 void
7130alist_slash_adjust()
7131{
7132 int i;
7133# ifdef FEAT_WINDOWS
7134 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007135 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136# endif
7137
7138 for (i = 0; i < GARGCOUNT; ++i)
7139 if (GARGLIST[i].ae_fname != NULL)
7140 slash_adjust(GARGLIST[i].ae_fname);
7141# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007142 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (wp->w_alist != &global_alist)
7144 for (i = 0; i < WARGCOUNT(wp); ++i)
7145 if (WARGLIST(wp)[i].ae_fname != NULL)
7146 slash_adjust(WARGLIST(wp)[i].ae_fname);
7147# endif
7148}
7149#endif
7150
7151/*
7152 * ":preserve".
7153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 static void
7155ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007156 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007158 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 ml_preserve(curbuf, TRUE);
7160}
7161
7162/*
7163 * ":recover".
7164 */
7165 static void
7166ex_recover(eap)
7167 exarg_T *eap;
7168{
7169 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7170 recoverymode = TRUE;
7171 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7172 && (*eap->arg == NUL
7173 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7174 ml_recover();
7175 recoverymode = FALSE;
7176}
7177
7178/*
7179 * Command modifier used in a wrong way.
7180 */
7181 static void
7182ex_wrongmodifier(eap)
7183 exarg_T *eap;
7184{
7185 eap->errmsg = e_invcmd;
7186}
7187
7188#ifdef FEAT_WINDOWS
7189/*
7190 * :sview [+command] file split window with new file, read-only
7191 * :split [[+command] file] split window with current or new file
7192 * :vsplit [[+command] file] split window vertically with current or new file
7193 * :new [[+command] file] split window with no or new file
7194 * :vnew [[+command] file] split vertically window with no or new file
7195 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007196 *
7197 * :tabedit open new Tab page with empty window
7198 * :tabedit [+command] file open new Tab page and edit "file"
7199 * :tabnew [[+command] file] just like :tabedit
7200 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
7202 void
7203ex_splitview(eap)
7204 exarg_T *eap;
7205{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007206 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007209# endif
7210# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007214# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7216 {
7217 ex_ni(eap);
7218 return;
7219 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007222# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007226# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007228 * quickfix windows. But it's OK when doing ":tab split". */
7229 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {
7231 if (eap->cmdidx == CMD_split)
7232 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007233# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 if (eap->cmdidx == CMD_vsplit)
7235 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007240# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007241 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7244 FNAME_MESS, TRUE, curbuf->b_ffname);
7245 if (fname == NULL)
7246 goto theend;
7247 eap->arg = fname;
7248 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007249# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007253# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007255# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 && eap->cmdidx != CMD_new)
7259 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007260# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007261 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007262# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007263 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007264# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007265 au_has_group((char_u *)"FileExplorer"))
7266 {
7267 /* No browsing supported but we do have the file explorer:
7268 * Edit the directory. */
7269 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7270 eap->arg = (char_u *)".";
7271 }
7272 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007273# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007274 {
7275 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007277 if (fname == NULL)
7278 goto theend;
7279 eap->arg = fname;
7280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007285 /*
7286 * Either open new tab page or split the window.
7287 */
7288 if (eap->cmdidx == CMD_tabedit
7289 || eap->cmdidx == CMD_tabfind
7290 || eap->cmdidx == CMD_tabnew)
7291 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007292 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7293 : eap->addr_count == 0 ? 0
7294 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007295 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007296 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007297
7298 /* set the alternate buffer for the window we came from */
7299 if (curwin != old_curwin
7300 && win_valid(old_curwin)
7301 && old_curwin->w_buffer != curbuf
7302 && !cmdmod.keepalt)
7303 old_curwin->w_alt_fnum = curbuf->b_fnum;
7304 }
7305 }
7306 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7308 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007309# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 /* Reset 'scrollbind' when editing another file, but keep it when
7311 * doing ":split" without arguments. */
7312 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007313# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007317 {
7318 RESET_BINDING(curwin);
7319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 else
7321 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 do_exedit(eap, old_curwin);
7324 }
7325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007326# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007330# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331theend:
7332 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007335
7336/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007337 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007338 */
7339 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007340tabpage_new()
7341{
7342 exarg_T ea;
7343
7344 vim_memset(&ea, 0, sizeof(ea));
7345 ea.cmdidx = CMD_tabnew;
7346 ea.cmd = (char_u *)"tabn";
7347 ea.arg = (char_u *)"";
7348 ex_splitview(&ea);
7349}
7350
7351/*
7352 * :tabnext command
7353 */
7354 static void
7355ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007356 exarg_T *eap;
7357{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007358 switch (eap->cmdidx)
7359 {
7360 case CMD_tabfirst:
7361 case CMD_tabrewind:
7362 goto_tabpage(1);
7363 break;
7364 case CMD_tablast:
7365 goto_tabpage(9999);
7366 break;
7367 case CMD_tabprevious:
7368 case CMD_tabNext:
7369 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7370 break;
7371 default: /* CMD_tabnext */
7372 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7373 break;
7374 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007375}
7376
7377/*
7378 * :tabmove command
7379 */
7380 static void
7381ex_tabmove(eap)
7382 exarg_T *eap;
7383{
7384 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385}
7386
7387/*
7388 * :tabs command: List tabs and their contents.
7389 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390 static void
7391ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007392 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007393{
7394 tabpage_T *tp;
7395 win_T *wp;
7396 int tabcount = 1;
7397
7398 msg_start();
7399 msg_scroll = TRUE;
7400 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7401 {
7402 msg_putchar('\n');
7403 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7404 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7405 out_flush(); /* output one line at a time */
7406 ui_breakcheck();
7407
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007408 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409 wp = firstwin;
7410 else
7411 wp = tp->tp_firstwin;
7412 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7413 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007414 msg_putchar('\n');
7415 msg_putchar(wp == curwin ? '>' : ' ');
7416 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007417 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7418 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419 if (buf_spname(wp->w_buffer) != NULL)
7420 STRCPY(IObuff, buf_spname(wp->w_buffer));
7421 else
7422 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7423 IObuff, IOSIZE, TRUE);
7424 msg_outtrans(IObuff);
7425 out_flush(); /* output one line at a time */
7426 ui_breakcheck();
7427 }
7428 }
7429}
7430
7431#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433/*
7434 * ":mode": Set screen mode.
7435 * If no argument given, just get the screen size and redraw.
7436 */
7437 static void
7438ex_mode(eap)
7439 exarg_T *eap;
7440{
7441 if (*eap->arg == NUL)
7442 shell_resized();
7443 else
7444 mch_screenmode(eap->arg);
7445}
7446
7447#ifdef FEAT_WINDOWS
7448/*
7449 * ":resize".
7450 * set, increment or decrement current window height
7451 */
7452 static void
7453ex_resize(eap)
7454 exarg_T *eap;
7455{
7456 int n;
7457 win_T *wp = curwin;
7458
7459 if (eap->addr_count > 0)
7460 {
7461 n = eap->line2;
7462 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7463 ;
7464 }
7465
7466#ifdef FEAT_GUI
7467 need_mouse_correct = TRUE;
7468#endif
7469 n = atol((char *)eap->arg);
7470#ifdef FEAT_VERTSPLIT
7471 if (cmdmod.split & WSP_VERT)
7472 {
7473 if (*eap->arg == '-' || *eap->arg == '+')
7474 n += W_WIDTH(curwin);
7475 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7476 n = 9999;
7477 win_setwidth_win((int)n, wp);
7478 }
7479 else
7480#endif
7481 {
7482 if (*eap->arg == '-' || *eap->arg == '+')
7483 n += curwin->w_height;
7484 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7485 n = 9999;
7486 win_setheight_win((int)n, wp);
7487 }
7488}
7489#endif
7490
7491/*
7492 * ":find [+command] <file>" command.
7493 */
7494 static void
7495ex_find(eap)
7496 exarg_T *eap;
7497{
7498#ifdef FEAT_SEARCHPATH
7499 char_u *fname;
7500 int count;
7501
7502 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7503 TRUE, curbuf->b_ffname);
7504 if (eap->addr_count > 0)
7505 {
7506 /* Repeat finding the file "count" times. This matters when it
7507 * appears several times in the path. */
7508 count = eap->line2;
7509 while (fname != NULL && --count > 0)
7510 {
7511 vim_free(fname);
7512 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7513 FALSE, curbuf->b_ffname);
7514 }
7515 }
7516
7517 if (fname != NULL)
7518 {
7519 eap->arg = fname;
7520#endif
7521 do_exedit(eap, NULL);
7522#ifdef FEAT_SEARCHPATH
7523 vim_free(fname);
7524 }
7525#endif
7526}
7527
7528/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007529 * ":open" simulation: for now just work like ":visual".
7530 */
7531 static void
7532ex_open(eap)
7533 exarg_T *eap;
7534{
7535 regmatch_T regmatch;
7536 char_u *p;
7537
7538 curwin->w_cursor.lnum = eap->line2;
7539 beginline(BL_SOL | BL_FIX);
7540 if (*eap->arg == '/')
7541 {
7542 /* ":open /pattern/": put cursor in column found with pattern */
7543 ++eap->arg;
7544 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7545 *p = NUL;
7546 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7547 if (regmatch.regprog != NULL)
7548 {
7549 regmatch.rm_ic = p_ic;
7550 p = ml_get_curline();
7551 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007552 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 else
7554 EMSG(_(e_nomatch));
7555 vim_free(regmatch.regprog);
7556 }
7557 /* Move to the NUL, ignore any other arguments. */
7558 eap->arg += STRLEN(eap->arg);
7559 }
7560 check_cursor();
7561
7562 eap->cmdidx = CMD_visual;
7563 do_exedit(eap, NULL);
7564}
7565
7566/*
7567 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 */
7569 static void
7570ex_edit(eap)
7571 exarg_T *eap;
7572{
7573 do_exedit(eap, NULL);
7574}
7575
7576/*
7577 * ":edit <file>" command and alikes.
7578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 void
7580do_exedit(eap, old_curwin)
7581 exarg_T *eap;
7582 win_T *old_curwin; /* curwin before doing a split or NULL */
7583{
7584 int n;
7585#ifdef FEAT_WINDOWS
7586 int need_hide;
7587#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007588 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
7590 /*
7591 * ":vi" command ends Ex mode.
7592 */
7593 if (exmode_active && (eap->cmdidx == CMD_visual
7594 || eap->cmdidx == CMD_view))
7595 {
7596 exmode_active = FALSE;
7597 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007598 {
7599 /* Special case: ":global/pat/visual\NLvi-commands" */
7600 if (global_busy)
7601 {
7602 int rd = RedrawingDisabled;
7603 int nwr = no_wait_return;
7604 int ms = msg_scroll;
7605#ifdef FEAT_GUI
7606 int he = hold_gui_events;
7607#endif
7608
7609 if (eap->nextcmd != NULL)
7610 {
7611 stuffReadbuff(eap->nextcmd);
7612 eap->nextcmd = NULL;
7613 }
7614
7615 if (exmode_was != EXMODE_VIM)
7616 settmode(TMODE_RAW);
7617 RedrawingDisabled = 0;
7618 no_wait_return = 0;
7619 need_wait_return = FALSE;
7620 msg_scroll = 0;
7621#ifdef FEAT_GUI
7622 hold_gui_events = 0;
7623#endif
7624 must_redraw = CLEAR;
7625
7626 main_loop(FALSE, TRUE);
7627
7628 RedrawingDisabled = rd;
7629 no_wait_return = nwr;
7630 msg_scroll = ms;
7631#ifdef FEAT_GUI
7632 hold_gui_events = he;
7633#endif
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
7638
7639 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007640 || eap->cmdidx == CMD_tabnew
7641 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642#ifdef FEAT_VERTSPLIT
7643 || eap->cmdidx == CMD_vnew
7644#endif
7645 ) && *eap->arg == NUL)
7646 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007647 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 setpcmark();
7649 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007650 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7651 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
7653 else if ((eap->cmdidx != CMD_split
7654#ifdef FEAT_VERTSPLIT
7655 && eap->cmdidx != CMD_vsplit
7656#endif
7657 )
7658 || *eap->arg != NUL
7659#ifdef FEAT_BROWSE
7660 || cmdmod.browse
7661#endif
7662 )
7663 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007664#ifdef FEAT_AUTOCMD
7665 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7666 * can bring us here, others are stopped earlier. */
7667 if (*eap->arg != NUL && curbuf_locked())
7668 return;
7669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 n = readonlymode;
7671 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7672 readonlymode = TRUE;
7673 else if (eap->cmdidx == CMD_enew)
7674 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7675 empty buffer */
7676 setpcmark();
7677 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7678 NULL, eap,
7679 /* ":edit" goes to first line if Vi compatible */
7680 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7681 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7682 ? ECMD_ONE : eap->do_ecmd_lnum,
7683 (P_HID(curbuf) ? ECMD_HIDE : 0)
7684 + (eap->forceit ? ECMD_FORCEIT : 0)
7685#ifdef FEAT_LISTCMDS
7686 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7687#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007688 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
7690 /* Editing the file failed. If the window was split, close it. */
7691#ifdef FEAT_WINDOWS
7692 if (old_curwin != NULL)
7693 {
7694 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7695 if (!need_hide || P_HID(curbuf))
7696 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007697# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7698 cleanup_T cs;
7699
7700 /* Reset the error/interrupt/exception state here so that
7701 * aborting() returns FALSE when closing a window. */
7702 enter_cleanup(&cs);
7703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704# ifdef FEAT_GUI
7705 need_mouse_correct = TRUE;
7706# endif
7707 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007708
7709# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7710 /* Restore the error/interrupt/exception state if not
7711 * discarded by a new aborting error, interrupt, or
7712 * uncaught exception. */
7713 leave_cleanup(&cs);
7714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716 }
7717#endif
7718 }
7719 else if (readonlymode && curbuf->b_nwindows == 1)
7720 {
7721 /* When editing an already visited buffer, 'readonly' won't be set
7722 * but the previous value is kept. With ":view" and ":sview" we
7723 * want the file to be readonly, except when another window is
7724 * editing the same buffer. */
7725 curbuf->b_p_ro = TRUE;
7726 }
7727 readonlymode = n;
7728 }
7729 else
7730 {
7731 if (eap->do_ecmd_cmd != NULL)
7732 do_cmdline_cmd(eap->do_ecmd_cmd);
7733#ifdef FEAT_TITLE
7734 n = curwin->w_arg_idx_invalid;
7735#endif
7736 check_arg_idx(curwin);
7737#ifdef FEAT_TITLE
7738 if (n != curwin->w_arg_idx_invalid)
7739 maketitle();
7740#endif
7741 }
7742
7743#ifdef FEAT_WINDOWS
7744 /*
7745 * if ":split file" worked, set alternate file name in old window to new
7746 * file
7747 */
7748 if (old_curwin != NULL
7749 && *eap->arg != NUL
7750 && curwin != old_curwin
7751 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007752 && old_curwin->w_buffer != curbuf
7753 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 old_curwin->w_alt_fnum = curbuf->b_fnum;
7755#endif
7756
7757 ex_no_reprint = TRUE;
7758}
7759
7760#ifndef FEAT_GUI
7761/*
7762 * ":gui" and ":gvim" when there is no GUI.
7763 */
7764 static void
7765ex_nogui(eap)
7766 exarg_T *eap;
7767{
7768 eap->errmsg = e_nogvim;
7769}
7770#endif
7771
7772#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7773 static void
7774ex_tearoff(eap)
7775 exarg_T *eap;
7776{
7777 gui_make_tearoff(eap->arg);
7778}
7779#endif
7780
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007781#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 static void
7783ex_popup(eap)
7784 exarg_T *eap;
7785{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007786 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788#endif
7789
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 static void
7791ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007792 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7795 MSG(_("No swap file"));
7796 else
7797 msg(curbuf->b_ml.ml_mfp->mf_fname);
7798}
7799
7800/*
7801 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7802 * offset.
7803 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
7806ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007807 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809#ifdef FEAT_SCROLLBIND
7810 win_T *wp;
7811 long topline;
7812 long y;
7813 linenr_T old_linenr = curwin->w_cursor.lnum;
7814
7815 setpcmark();
7816
7817 /*
7818 * determine max topline
7819 */
7820 if (curwin->w_p_scb)
7821 {
7822 topline = curwin->w_topline;
7823 for (wp = firstwin; wp; wp = wp->w_next)
7824 {
7825 if (wp->w_p_scb && wp->w_buffer)
7826 {
7827 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7828 if (topline > y)
7829 topline = y;
7830 }
7831 }
7832 if (topline < 1)
7833 topline = 1;
7834 }
7835 else
7836 {
7837 topline = 1;
7838 }
7839
7840
7841 /*
7842 * set all scrollbind windows to the same topline
7843 */
7844 wp = curwin;
7845 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7846 {
7847 if (curwin->w_p_scb)
7848 {
7849 y = topline - curwin->w_topline;
7850 if (y > 0)
7851 scrollup(y, TRUE);
7852 else
7853 scrolldown(-y, TRUE);
7854 curwin->w_scbind_pos = topline;
7855 redraw_later(VALID);
7856 cursor_correct();
7857#ifdef FEAT_WINDOWS
7858 curwin->w_redr_status = TRUE;
7859#endif
7860 }
7861 }
7862 curwin = wp;
7863 if (curwin->w_p_scb)
7864 {
7865 did_syncbind = TRUE;
7866 checkpcmark();
7867 if (old_linenr != curwin->w_cursor.lnum)
7868 {
7869 char_u ctrl_o[2];
7870
7871 ctrl_o[0] = Ctrl_O;
7872 ctrl_o[1] = 0;
7873 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7874 }
7875 }
7876#endif
7877}
7878
7879
7880 static void
7881ex_read(eap)
7882 exarg_T *eap;
7883{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007884 int i;
7885 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7886 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
7888 if (eap->usefilter) /* :r!cmd */
7889 do_bang(1, eap, FALSE, FALSE, TRUE);
7890 else
7891 {
7892 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7893 return;
7894
7895#ifdef FEAT_BROWSE
7896 if (cmdmod.browse)
7897 {
7898 char_u *browseFile;
7899
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007900 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 NULL, NULL, NULL, curbuf);
7902 if (browseFile != NULL)
7903 {
7904 i = readfile(browseFile, NULL,
7905 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7906 vim_free(browseFile);
7907 }
7908 else
7909 i = OK;
7910 }
7911 else
7912#endif
7913 if (*eap->arg == NUL)
7914 {
7915 if (check_fname() == FAIL) /* check for no file name */
7916 return;
7917 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7918 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7919 }
7920 else
7921 {
7922 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7923 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7924 i = readfile(eap->arg, NULL,
7925 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7926
7927 }
7928 if (i == FAIL)
7929 {
7930#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7931 if (!aborting())
7932#endif
7933 EMSG2(_(e_notopen), eap->arg);
7934 }
7935 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007936 {
7937 if (empty && exmode_active)
7938 {
7939 /* Delete the empty line that remains. Historically ex does
7940 * this but vi doesn't. */
7941 if (eap->line2 == 0)
7942 lnum = curbuf->b_ml.ml_line_count;
7943 else
7944 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007945 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007946 {
7947 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007948 if (curwin->w_cursor.lnum > 1
7949 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007951 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 }
7953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957}
7958
Bram Moolenaarea408852005-06-25 22:49:46 +00007959static char_u *prev_dir = NULL;
7960
7961#if defined(EXITFREE) || defined(PROTO)
7962 void
7963free_cd_dir()
7964{
7965 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007966 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007967
7968 vim_free(globaldir);
7969 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007970}
7971#endif
7972
7973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974/*
7975 * ":cd", ":lcd", ":chdir" and ":lchdir".
7976 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007977 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978ex_cd(eap)
7979 exarg_T *eap;
7980{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 char_u *new_dir;
7982 char_u *tofree;
7983
7984 new_dir = eap->arg;
7985#if !defined(UNIX) && !defined(VMS)
7986 /* for non-UNIX ":cd" means: print current directory */
7987 if (*new_dir == NUL)
7988 ex_pwd(NULL);
7989 else
7990#endif
7991 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007992#ifdef FEAT_AUTOCMD
7993 if (allbuf_locked())
7994 return;
7995#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007996 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7997 && !eap->forceit)
7998 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007999 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008000 return;
8001 }
8002
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 /* ":cd -": Change to previous directory */
8004 if (STRCMP(new_dir, "-") == 0)
8005 {
8006 if (prev_dir == NULL)
8007 {
8008 EMSG(_("E186: No previous directory"));
8009 return;
8010 }
8011 new_dir = prev_dir;
8012 }
8013
8014 /* Save current directory for next ":cd -" */
8015 tofree = prev_dir;
8016 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8017 prev_dir = vim_strsave(NameBuff);
8018 else
8019 prev_dir = NULL;
8020
8021#if defined(UNIX) || defined(VMS)
8022 /* for UNIX ":cd" means: go to home directory */
8023 if (*new_dir == NUL)
8024 {
8025 /* use NameBuff for home directory name */
8026# ifdef VMS
8027 char_u *p;
8028
8029 p = mch_getenv((char_u *)"SYS$LOGIN");
8030 if (p == NULL || *p == NUL) /* empty is the same as not set */
8031 NameBuff[0] = NUL;
8032 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008033 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034# else
8035 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8036# endif
8037 new_dir = NameBuff;
8038 }
8039#endif
8040 if (new_dir == NULL || vim_chdir(new_dir))
8041 EMSG(_(e_failed));
8042 else
8043 {
8044 vim_free(curwin->w_localdir);
8045 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8046 {
8047 /* If still in global directory, need to remember current
8048 * directory as global directory. */
8049 if (globaldir == NULL && prev_dir != NULL)
8050 globaldir = vim_strsave(prev_dir);
8051 /* Remember this local directory for the window. */
8052 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8053 curwin->w_localdir = vim_strsave(NameBuff);
8054 }
8055 else
8056 {
8057 /* We are now in the global directory, no need to remember its
8058 * name. */
8059 vim_free(globaldir);
8060 globaldir = NULL;
8061 curwin->w_localdir = NULL;
8062 }
8063
8064 shorten_fnames(TRUE);
8065
8066 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008067 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 ex_pwd(eap);
8069 }
8070 vim_free(tofree);
8071 }
8072}
8073
8074/*
8075 * ":pwd".
8076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 static void
8078ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008079 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8082 {
8083#ifdef BACKSLASH_IN_FILENAME
8084 slash_adjust(NameBuff);
8085#endif
8086 msg(NameBuff);
8087 }
8088 else
8089 EMSG(_("E187: Unknown"));
8090}
8091
8092/*
8093 * ":=".
8094 */
8095 static void
8096ex_equal(eap)
8097 exarg_T *eap;
8098{
8099 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008100 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101}
8102
8103 static void
8104ex_sleep(eap)
8105 exarg_T *eap;
8106{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008107 int n;
8108 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109
8110 if (cursor_valid())
8111 {
8112 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8113 if (n >= 0)
8114 windgoto((int)n, curwin->w_wcol);
8115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008116
8117 len = eap->line2;
8118 switch (*eap->arg)
8119 {
8120 case 'm': break;
8121 case NUL: len *= 1000L; break;
8122 default: EMSG2(_(e_invarg2), eap->arg); return;
8123 }
8124 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125}
8126
8127/*
8128 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8129 */
8130 void
8131do_sleep(msec)
8132 long msec;
8133{
8134 long done;
8135
8136 cursor_on();
8137 out_flush();
8138 for (done = 0; !got_int && done < msec; done += 1000L)
8139 {
8140 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8141 ui_breakcheck();
8142 }
8143}
8144
8145 static void
8146do_exmap(eap, isabbrev)
8147 exarg_T *eap;
8148 int isabbrev;
8149{
8150 int mode;
8151 char_u *cmdp;
8152
8153 cmdp = eap->cmd;
8154 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8155
8156 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8157 eap->arg, mode, isabbrev))
8158 {
8159 case 1: EMSG(_(e_invarg));
8160 break;
8161 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8162 break;
8163 }
8164}
8165
8166/*
8167 * ":winsize" command (obsolete).
8168 */
8169 static void
8170ex_winsize(eap)
8171 exarg_T *eap;
8172{
8173 int w, h;
8174 char_u *arg = eap->arg;
8175 char_u *p;
8176
8177 w = getdigits(&arg);
8178 arg = skipwhite(arg);
8179 p = arg;
8180 h = getdigits(&arg);
8181 if (*p != NUL && *arg == NUL)
8182 set_shellsize(w, h, TRUE);
8183 else
8184 EMSG(_("E465: :winsize requires two number arguments"));
8185}
8186
8187#ifdef FEAT_WINDOWS
8188 static void
8189ex_wincmd(eap)
8190 exarg_T *eap;
8191{
8192 int xchar = NUL;
8193 char_u *p;
8194
8195 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8196 {
8197 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8198 if (eap->arg[1] == NUL)
8199 {
8200 EMSG(_(e_invarg));
8201 return;
8202 }
8203 xchar = eap->arg[1];
8204 p = eap->arg + 2;
8205 }
8206 else
8207 p = eap->arg + 1;
8208
8209 eap->nextcmd = check_nextcmd(p);
8210 p = skipwhite(p);
8211 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8212 EMSG(_(e_invarg));
8213 else
8214 {
8215 /* Pass flags on for ":vertical wincmd ]". */
8216 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008217 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8219 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008220 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
8222}
8223#endif
8224
Bram Moolenaar843ee412004-06-30 16:16:41 +00008225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226/*
8227 * ":winpos".
8228 */
8229 static void
8230ex_winpos(eap)
8231 exarg_T *eap;
8232{
8233 int x, y;
8234 char_u *arg = eap->arg;
8235 char_u *p;
8236
8237 if (*arg == NUL)
8238 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008239# if defined(FEAT_GUI) || defined(MSWIN)
8240# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008242# else
8243 if (mch_get_winpos(&x, &y) != FAIL)
8244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
8246 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8247 msg(IObuff);
8248 }
8249 else
8250# endif
8251 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8252 }
8253 else
8254 {
8255 x = getdigits(&arg);
8256 arg = skipwhite(arg);
8257 p = arg;
8258 y = getdigits(&arg);
8259 if (*p == NUL || *arg != NUL)
8260 {
8261 EMSG(_("E466: :winpos requires two number arguments"));
8262 return;
8263 }
8264# ifdef FEAT_GUI
8265 if (gui.in_use)
8266 gui_mch_set_winpos(x, y);
8267 else if (gui.starting)
8268 {
8269 /* Remember the coordinates for when the window is opened. */
8270 gui_win_x = x;
8271 gui_win_y = y;
8272 }
8273# ifdef HAVE_TGETENT
8274 else
8275# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008276# else
8277# ifdef MSWIN
8278 mch_set_winpos(x, y);
8279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280# endif
8281# ifdef HAVE_TGETENT
8282 if (*T_CWP)
8283 term_set_winpos(x, y);
8284# endif
8285 }
8286}
8287#endif
8288
8289/*
8290 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8291 */
8292 static void
8293ex_operators(eap)
8294 exarg_T *eap;
8295{
8296 oparg_T oa;
8297
8298 clear_oparg(&oa);
8299 oa.regname = eap->regname;
8300 oa.start.lnum = eap->line1;
8301 oa.end.lnum = eap->line2;
8302 oa.line_count = eap->line2 - eap->line1 + 1;
8303 oa.motion_type = MLINE;
8304#ifdef FEAT_VIRTUALEDIT
8305 virtual_op = FALSE;
8306#endif
8307 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8308 {
8309 setpcmark();
8310 curwin->w_cursor.lnum = eap->line1;
8311 beginline(BL_SOL | BL_FIX);
8312 }
8313
8314 switch (eap->cmdidx)
8315 {
8316 case CMD_delete:
8317 oa.op_type = OP_DELETE;
8318 op_delete(&oa);
8319 break;
8320
8321 case CMD_yank:
8322 oa.op_type = OP_YANK;
8323 (void)op_yank(&oa, FALSE, TRUE);
8324 break;
8325
8326 default: /* CMD_rshift or CMD_lshift */
8327 if ((eap->cmdidx == CMD_rshift)
8328#ifdef FEAT_RIGHTLEFT
8329 ^ curwin->w_p_rl
8330#endif
8331 )
8332 oa.op_type = OP_RSHIFT;
8333 else
8334 oa.op_type = OP_LSHIFT;
8335 op_shift(&oa, FALSE, eap->amount);
8336 break;
8337 }
8338#ifdef FEAT_VIRTUALEDIT
8339 virtual_op = MAYBE;
8340#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008341 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
8344/*
8345 * ":put".
8346 */
8347 static void
8348ex_put(eap)
8349 exarg_T *eap;
8350{
8351 /* ":0put" works like ":1put!". */
8352 if (eap->line2 == 0)
8353 {
8354 eap->line2 = 1;
8355 eap->forceit = TRUE;
8356 }
8357 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008358 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8359 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360}
8361
8362/*
8363 * Handle ":copy" and ":move".
8364 */
8365 static void
8366ex_copymove(eap)
8367 exarg_T *eap;
8368{
8369 long n;
8370
8371 n = get_address(&eap->arg, FALSE, FALSE);
8372 if (eap->arg == NULL) /* error detected */
8373 {
8374 eap->nextcmd = NULL;
8375 return;
8376 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008377 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 /*
8380 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8381 */
8382 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8383 {
8384 EMSG(_(e_invaddr));
8385 return;
8386 }
8387
8388 if (eap->cmdidx == CMD_move)
8389 {
8390 if (do_move(eap->line1, eap->line2, n) == FAIL)
8391 return;
8392 }
8393 else
8394 ex_copy(eap->line1, eap->line2, n);
8395 u_clearline();
8396 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008397 ex_may_print(eap);
8398}
8399
8400/*
8401 * Print the current line if flags were given to the Ex command.
8402 */
8403 static void
8404ex_may_print(eap)
8405 exarg_T *eap;
8406{
8407 if (eap->flags != 0)
8408 {
8409 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8410 (eap->flags & EXFLAG_LIST));
8411 ex_no_reprint = TRUE;
8412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413}
8414
8415/*
8416 * ":smagic" and ":snomagic".
8417 */
8418 static void
8419ex_submagic(eap)
8420 exarg_T *eap;
8421{
8422 int magic_save = p_magic;
8423
8424 p_magic = (eap->cmdidx == CMD_smagic);
8425 do_sub(eap);
8426 p_magic = magic_save;
8427}
8428
8429/*
8430 * ":join".
8431 */
8432 static void
8433ex_join(eap)
8434 exarg_T *eap;
8435{
8436 curwin->w_cursor.lnum = eap->line1;
8437 if (eap->line1 == eap->line2)
8438 {
8439 if (eap->addr_count >= 2) /* :2,2join does nothing */
8440 return;
8441 if (eap->line2 == curbuf->b_ml.ml_line_count)
8442 {
8443 beep_flush();
8444 return;
8445 }
8446 ++eap->line2;
8447 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008448 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008450 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451}
8452
8453/*
8454 * ":[addr]@r" or ":[addr]*r": execute register
8455 */
8456 static void
8457ex_at(eap)
8458 exarg_T *eap;
8459{
8460 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008461 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
8463 curwin->w_cursor.lnum = eap->line2;
8464
8465#ifdef USE_ON_FLY_SCROLL
8466 dont_scroll = TRUE; /* disallow scrolling here */
8467#endif
8468
8469 /* get the register name. No name means to use the previous one */
8470 c = *eap->arg;
8471 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8472 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008473 /* Put the register in the typeahead buffer with the "silent" flag. */
8474 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8475 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 else
8480 {
8481 int save_efr = exec_from_reg;
8482
8483 exec_from_reg = TRUE;
8484
8485 /*
8486 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008487 * Continue until the stuff buffer is empty and all added characters
8488 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008490 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8492
8493 exec_from_reg = save_efr;
8494 }
8495}
8496
8497/*
8498 * ":!".
8499 */
8500 static void
8501ex_bang(eap)
8502 exarg_T *eap;
8503{
8504 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8505}
8506
8507/*
8508 * ":undo".
8509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 static void
8511ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008512 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008514 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008515 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008516 else
8517 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518}
8519
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008520#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008521 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008522ex_wundo(eap)
8523 exarg_T *eap;
8524{
8525 char_u hash[UNDO_HASH_SIZE];
8526
8527 u_compute_hash(hash);
8528 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8529}
8530
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008531 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008532ex_rundo(eap)
8533 exarg_T *eap;
8534{
8535 char_u hash[UNDO_HASH_SIZE];
8536
8537 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008538 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008539}
8540#endif
8541
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542/*
8543 * ":redo".
8544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 static void
8546ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008547 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 u_redo(1);
8550}
8551
8552/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008553 * ":earlier" and ":later".
8554 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008555 static void
8556ex_later(eap)
8557 exarg_T *eap;
8558{
8559 long count = 0;
8560 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008561 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008562 char_u *p = eap->arg;
8563
8564 if (*p == NUL)
8565 count = 1;
8566 else if (isdigit(*p))
8567 {
8568 count = getdigits(&p);
8569 switch (*p)
8570 {
8571 case 's': ++p; sec = TRUE; break;
8572 case 'm': ++p; sec = TRUE; count *= 60; break;
8573 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008574 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8575 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008576 }
8577 }
8578
8579 if (*p != NUL)
8580 EMSG2(_(e_invarg2), eap->arg);
8581 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008582 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8583 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008584}
8585
8586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 * ":redir": start/stop redirection.
8588 */
8589 static void
8590ex_redir(eap)
8591 exarg_T *eap;
8592{
8593 char *mode;
8594 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008595 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596
8597 if (STRICMP(eap->arg, "END") == 0)
8598 close_redir();
8599 else
8600 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008601 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008603 ++arg;
8604 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008606 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 mode = "a";
8608 }
8609 else
8610 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008611 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
8613 close_redir();
8614
8615 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008616 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (fname == NULL)
8618 return;
8619#ifdef FEAT_BROWSE
8620 if (cmdmod.browse)
8621 {
8622 char_u *browseFile;
8623
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008624 browseFile = do_browse(BROWSE_SAVE,
8625 (char_u *)_("Save Redirection"),
8626 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 if (browseFile == NULL)
8628 return; /* operation cancelled */
8629 vim_free(fname);
8630 fname = browseFile;
8631 eap->forceit = TRUE; /* since dialog already asked */
8632 }
8633#endif
8634
8635 redir_fd = open_exfile(fname, eap->forceit, mode);
8636 vim_free(fname);
8637 }
8638#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008639 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 {
8641 /* redirect to a register a-z (resp. A-Z for appending) */
8642 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008643 ++arg;
8644 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008646 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008647 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008649 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008651 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008652 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008653 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008656 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008657 if (*arg == '>')
8658 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008659 /* Make register empty when not using @A-@Z and the
8660 * command is valid. */
8661 if (*arg == NUL && !isupper(redir_reg))
8662 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008664 }
8665 if (*arg != NUL)
8666 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008668 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 }
8670 }
8671 else if (*arg == '=' && arg[1] == '>')
8672 {
8673 int append;
8674
8675 /* redirect to a variable */
8676 close_redir();
8677 arg += 2;
8678
8679 if (*arg == '>')
8680 {
8681 ++arg;
8682 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008685 append = FALSE;
8686
8687 if (var_redir_start(skipwhite(arg), append) == OK)
8688 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 }
8690#endif
8691
8692 /* TODO: redirect to a buffer */
8693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008695 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008697
8698 /* Make sure redirection is not off. Can happen for cmdline completion
8699 * that indirectly invokes a command to catch its output. */
8700 if (redir_fd != NULL
8701#ifdef FEAT_EVAL
8702 || redir_reg || redir_vname
8703#endif
8704 )
8705 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706}
8707
8708/*
8709 * ":redraw": force redraw
8710 */
8711 static void
8712ex_redraw(eap)
8713 exarg_T *eap;
8714{
8715 int r = RedrawingDisabled;
8716 int p = p_lz;
8717
8718 RedrawingDisabled = 0;
8719 p_lz = FALSE;
8720 update_topline();
8721 update_screen(eap->forceit ? CLEAR :
8722#ifdef FEAT_VISUAL
8723 VIsual_active ? INVERTED :
8724#endif
8725 0);
8726#ifdef FEAT_TITLE
8727 if (need_maketitle)
8728 maketitle();
8729#endif
8730 RedrawingDisabled = r;
8731 p_lz = p;
8732
8733 /* Reset msg_didout, so that a message that's there is overwritten. */
8734 msg_didout = FALSE;
8735 msg_col = 0;
8736
8737 out_flush();
8738}
8739
8740/*
8741 * ":redrawstatus": force redraw of status line(s)
8742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 static void
8744ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008745 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747#if defined(FEAT_WINDOWS)
8748 int r = RedrawingDisabled;
8749 int p = p_lz;
8750
8751 RedrawingDisabled = 0;
8752 p_lz = FALSE;
8753 if (eap->forceit)
8754 status_redraw_all();
8755 else
8756 status_redraw_curbuf();
8757 update_screen(
8758# ifdef FEAT_VISUAL
8759 VIsual_active ? INVERTED :
8760# endif
8761 0);
8762 RedrawingDisabled = r;
8763 p_lz = p;
8764 out_flush();
8765#endif
8766}
8767
8768 static void
8769close_redir()
8770{
8771 if (redir_fd != NULL)
8772 {
8773 fclose(redir_fd);
8774 redir_fd = NULL;
8775 }
8776#ifdef FEAT_EVAL
8777 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008778 if (redir_vname)
8779 {
8780 var_redir_stop();
8781 redir_vname = 0;
8782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783#endif
8784}
8785
8786#if defined(FEAT_SESSION) && defined(USE_CRNL)
8787# define MKSESSION_NL
8788static int mksession_nl = FALSE; /* use NL only in put_eol() */
8789#endif
8790
8791/*
8792 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8793 */
8794 static void
8795ex_mkrc(eap)
8796 exarg_T *eap;
8797{
8798 FILE *fd;
8799 int failed = FALSE;
8800 char_u *fname;
8801#ifdef FEAT_BROWSE
8802 char_u *browseFile = NULL;
8803#endif
8804#ifdef FEAT_SESSION
8805 int view_session = FALSE;
8806 int using_vdir = FALSE; /* using 'viewdir'? */
8807 char_u *viewFile = NULL;
8808 unsigned *flagp;
8809#endif
8810
8811 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8812 {
8813#ifdef FEAT_SESSION
8814 view_session = TRUE;
8815#else
8816 ex_ni(eap);
8817 return;
8818#endif
8819 }
8820
8821#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008822 /* Use the short file name until ":lcd" is used. We also don't use the
8823 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008824 did_lcd = FALSE;
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8827 if (eap->cmdidx == CMD_mkview
8828 && (*eap->arg == NUL
8829 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8830 {
8831 eap->forceit = TRUE;
8832 fname = get_view_file(*eap->arg);
8833 if (fname == NULL)
8834 return;
8835 viewFile = fname;
8836 using_vdir = TRUE;
8837 }
8838 else
8839#endif
8840 if (*eap->arg != NUL)
8841 fname = eap->arg;
8842 else if (eap->cmdidx == CMD_mkvimrc)
8843 fname = (char_u *)VIMRC_FILE;
8844#ifdef FEAT_SESSION
8845 else if (eap->cmdidx == CMD_mksession)
8846 fname = (char_u *)SESSION_FILE;
8847#endif
8848 else
8849 fname = (char_u *)EXRC_FILE;
8850
8851#ifdef FEAT_BROWSE
8852 if (cmdmod.browse)
8853 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008854 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855# ifdef FEAT_SESSION
8856 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8857 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8858# endif
8859 (char_u *)_("Save Setup"),
8860 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8861 if (browseFile == NULL)
8862 goto theend;
8863 fname = browseFile;
8864 eap->forceit = TRUE; /* since dialog already asked */
8865 }
8866#endif
8867
8868#if defined(FEAT_SESSION) && defined(vim_mkdir)
8869 /* When using 'viewdir' may have to create the directory. */
8870 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008871 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872#endif
8873
8874 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8875 if (fd != NULL)
8876 {
8877#ifdef FEAT_SESSION
8878 if (eap->cmdidx == CMD_mkview)
8879 flagp = &vop_flags;
8880 else
8881 flagp = &ssop_flags;
8882#endif
8883
8884#ifdef MKSESSION_NL
8885 /* "unix" in 'sessionoptions': use NL line separator */
8886 if (view_session && (*flagp & SSOP_UNIX))
8887 mksession_nl = TRUE;
8888#endif
8889
8890 /* Write the version command for :mkvimrc */
8891 if (eap->cmdidx == CMD_mkvimrc)
8892 (void)put_line(fd, "version 6.0");
8893
8894#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008895 if (eap->cmdidx == CMD_mksession)
8896 {
8897 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8898 failed = TRUE;
8899 }
8900
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 if (eap->cmdidx != CMD_mkview)
8902#endif
8903 {
8904 /* Write setting 'compatible' first, because it has side effects.
8905 * For that same reason only do it when needed. */
8906 if (p_cp)
8907 (void)put_line(fd, "if !&cp | set cp | endif");
8908 else
8909 (void)put_line(fd, "if &cp | set nocp | endif");
8910 }
8911
8912#ifdef FEAT_SESSION
8913 if (!view_session
8914 || (eap->cmdidx == CMD_mksession
8915 && (*flagp & SSOP_OPTIONS)))
8916#endif
8917 failed |= (makemap(fd, NULL) == FAIL
8918 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8919
8920#ifdef FEAT_SESSION
8921 if (!failed && view_session)
8922 {
8923 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8924 failed = TRUE;
8925 if (eap->cmdidx == CMD_mksession)
8926 {
8927 char_u dirnow[MAXPATHL]; /* current directory */
8928
8929 /*
8930 * Change to session file's dir.
8931 */
8932 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8933 || mch_chdir((char *)dirnow) != 0)
8934 *dirnow = NUL;
8935 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8936 {
8937 if (vim_chdirfile(fname) == OK)
8938 shorten_fnames(TRUE);
8939 }
8940 else if (*dirnow != NUL
8941 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8942 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008943 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008944 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 }
8946
8947 failed |= (makeopens(fd, dirnow) == FAIL);
8948
8949 /* restore original dir */
8950 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8951 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8952 {
8953 if (mch_chdir((char *)dirnow) != 0)
8954 EMSG(_(e_prev_dir));
8955 shorten_fnames(TRUE);
8956 }
8957 }
8958 else
8959 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008960 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8961 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8964 == FAIL)
8965 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008966 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8967 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008968 if (eap->cmdidx == CMD_mksession)
8969 {
8970 if (put_line(fd, "unlet SessionLoad") == FAIL)
8971 failed = TRUE;
8972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 }
8974#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008975 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8976 failed = TRUE;
8977
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 failed |= fclose(fd);
8979
8980 if (failed)
8981 EMSG(_(e_write));
8982#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8983 else if (eap->cmdidx == CMD_mksession)
8984 {
8985 /* successful session write - set this_session var */
8986 char_u tbuf[MAXPATHL];
8987
8988 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8989 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8990 }
8991#endif
8992#ifdef MKSESSION_NL
8993 mksession_nl = FALSE;
8994#endif
8995 }
8996
8997#ifdef FEAT_BROWSE
8998theend:
8999 vim_free(browseFile);
9000#endif
9001#ifdef FEAT_SESSION
9002 vim_free(viewFile);
9003#endif
9004}
9005
Bram Moolenaardf177f62005-02-22 08:39:57 +00009006#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9007 || defined(PROTO)
9008 int
9009vim_mkdir_emsg(name, prot)
9010 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009011 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009012{
9013 if (vim_mkdir(name, prot) != 0)
9014 {
9015 EMSG2(_("E739: Cannot create directory: %s"), name);
9016 return FAIL;
9017 }
9018 return OK;
9019}
9020#endif
9021
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022/*
9023 * Open a file for writing for an Ex command, with some checks.
9024 * Return file descriptor, or NULL on failure.
9025 */
9026 FILE *
9027open_exfile(fname, forceit, mode)
9028 char_u *fname;
9029 int forceit;
9030 char *mode; /* "w" for create new file or "a" for append */
9031{
9032 FILE *fd;
9033
9034#ifdef UNIX
9035 /* with Unix it is possible to open a directory */
9036 if (mch_isdir(fname))
9037 {
9038 EMSG2(_(e_isadir2), fname);
9039 return NULL;
9040 }
9041#endif
9042 if (!forceit && *mode != 'a' && vim_fexists(fname))
9043 {
9044 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9045 return NULL;
9046 }
9047
9048 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9049 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9050
9051 return fd;
9052}
9053
9054/*
9055 * ":mark" and ":k".
9056 */
9057 static void
9058ex_mark(eap)
9059 exarg_T *eap;
9060{
9061 pos_T pos;
9062
9063 if (*eap->arg == NUL) /* No argument? */
9064 EMSG(_(e_argreq));
9065 else if (eap->arg[1] != NUL) /* more than one character? */
9066 EMSG(_(e_trailing));
9067 else
9068 {
9069 pos = curwin->w_cursor; /* save curwin->w_cursor */
9070 curwin->w_cursor.lnum = eap->line2;
9071 beginline(BL_WHITE | BL_FIX);
9072 if (setmark(*eap->arg) == FAIL) /* set mark */
9073 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9074 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9075 }
9076}
9077
9078/*
9079 * Update w_topline, w_leftcol and the cursor position.
9080 */
9081 void
9082update_topline_cursor()
9083{
9084 check_cursor(); /* put cursor on valid line */
9085 update_topline();
9086 if (!curwin->w_p_wrap)
9087 validate_cursor();
9088 update_curswant();
9089}
9090
9091#ifdef FEAT_EX_EXTRA
9092/*
9093 * ":normal[!] {commands}": Execute normal mode commands.
9094 */
9095 static void
9096ex_normal(eap)
9097 exarg_T *eap;
9098{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 int save_msg_scroll = msg_scroll;
9100 int save_restart_edit = restart_edit;
9101 int save_msg_didout = msg_didout;
9102 int save_State = State;
9103 tasave_T tabuf;
9104 int save_insertmode = p_im;
9105 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009106 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107#ifdef FEAT_MBYTE
9108 char_u *arg = NULL;
9109 int l;
9110 char_u *p;
9111#endif
9112
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009113 if (ex_normal_lock > 0)
9114 {
9115 EMSG(_(e_secure));
9116 return;
9117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 if (ex_normal_busy >= p_mmd)
9119 {
9120 EMSG(_("E192: Recursive use of :normal too deep"));
9121 return;
9122 }
9123 ++ex_normal_busy;
9124
9125 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9126 restart_edit = 0; /* don't go to Insert mode */
9127 p_im = FALSE; /* don't use 'insertmode' */
9128
9129#ifdef FEAT_MBYTE
9130 /*
9131 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9132 * this for the K_SPECIAL leading byte, otherwise special keys will not
9133 * work.
9134 */
9135 if (has_mbyte)
9136 {
9137 int len = 0;
9138
9139 /* Count the number of characters to be escaped. */
9140 for (p = eap->arg; *p != NUL; ++p)
9141 {
9142# ifdef FEAT_GUI
9143 if (*p == CSI) /* leadbyte CSI */
9144 len += 2;
9145# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009146 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9148# ifdef FEAT_GUI
9149 || *p == CSI
9150# endif
9151 )
9152 len += 2;
9153 }
9154 if (len > 0)
9155 {
9156 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9157 if (arg != NULL)
9158 {
9159 len = 0;
9160 for (p = eap->arg; *p != NUL; ++p)
9161 {
9162 arg[len++] = *p;
9163# ifdef FEAT_GUI
9164 if (*p == CSI)
9165 {
9166 arg[len++] = KS_EXTRA;
9167 arg[len++] = (int)KE_CSI;
9168 }
9169# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009170 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 {
9172 arg[len++] = *++p;
9173 if (*p == K_SPECIAL)
9174 {
9175 arg[len++] = KS_SPECIAL;
9176 arg[len++] = KE_FILLER;
9177 }
9178# ifdef FEAT_GUI
9179 else if (*p == CSI)
9180 {
9181 arg[len++] = KS_EXTRA;
9182 arg[len++] = (int)KE_CSI;
9183 }
9184# endif
9185 }
9186 arg[len] = NUL;
9187 }
9188 }
9189 }
9190 }
9191#endif
9192
9193 /*
9194 * Save the current typeahead. This is required to allow using ":normal"
9195 * from an event handler and makes sure we don't hang when the argument
9196 * ends with half a command.
9197 */
9198 save_typeahead(&tabuf);
9199 if (tabuf.typebuf_valid)
9200 {
9201 /*
9202 * Repeat the :normal command for each line in the range. When no
9203 * range given, execute it just once, without positioning the cursor
9204 * first.
9205 */
9206 do
9207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 if (eap->addr_count != 0)
9209 {
9210 curwin->w_cursor.lnum = eap->line1++;
9211 curwin->w_cursor.col = 0;
9212 }
9213
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009214 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#ifdef FEAT_MBYTE
9216 arg != NULL ? arg :
9217#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009218 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
9220 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9221 }
9222
9223 /* Might not return to the main loop when in an event handler. */
9224 update_topline_cursor();
9225
9226 /* Restore the previous typeahead. */
9227 restore_typeahead(&tabuf);
9228
9229 --ex_normal_busy;
9230 msg_scroll = save_msg_scroll;
9231 restart_edit = save_restart_edit;
9232 p_im = save_insertmode;
9233 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009234 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9236
9237 /* Restore the state (needed when called from a function executed for
9238 * 'indentexpr'). */
9239 State = save_State;
9240#ifdef FEAT_MBYTE
9241 vim_free(arg);
9242#endif
9243}
9244
9245/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009246 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 */
9248 static void
9249ex_startinsert(eap)
9250 exarg_T *eap;
9251{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009252 if (eap->forceit)
9253 {
9254 coladvance((colnr_T)MAXCOL);
9255 curwin->w_curswant = MAXCOL;
9256 curwin->w_set_curswant = FALSE;
9257 }
9258
Bram Moolenaara40c5002005-01-09 21:16:21 +00009259 /* Ignore the command when already in Insert mode. Inserting an
9260 * expression register that invokes a function can do this. */
9261 if (State & INSERT)
9262 return;
9263
Bram Moolenaar64969662005-12-14 21:59:55 +00009264 if (eap->cmdidx == CMD_startinsert)
9265 restart_edit = 'a';
9266 else if (eap->cmdidx == CMD_startreplace)
9267 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009269 restart_edit = 'V';
9270
9271 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009273 if (eap->cmdidx == CMD_startinsert)
9274 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 curwin->w_curswant = 0; /* avoid MAXCOL */
9276 }
9277}
9278
9279/*
9280 * ":stopinsert"
9281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 static void
9283ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009284 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 restart_edit = 0;
9287 stop_insert_mode = TRUE;
9288}
9289#endif
9290
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009291#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9292/*
9293 * Execute normal mode command "cmd".
9294 * "remap" can be REMAP_NONE or REMAP_YES.
9295 */
9296 void
9297exec_normal_cmd(cmd, remap, silent)
9298 char_u *cmd;
9299 int remap;
9300 int silent;
9301{
9302 oparg_T oa;
9303
9304 /*
9305 * Stuff the argument into the typeahead buffer.
9306 * Execute normal_cmd() until there is no typeahead left.
9307 */
9308 clear_oparg(&oa);
9309 finish_op = FALSE;
9310 ins_typebuf(cmd, remap, 0, TRUE, silent);
9311 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9312 && !got_int)
9313 {
9314 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009315 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009316 }
9317}
9318#endif
9319
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320#ifdef FEAT_FIND_ID
9321 static void
9322ex_checkpath(eap)
9323 exarg_T *eap;
9324{
9325 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9326 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9327 (linenr_T)1, (linenr_T)MAXLNUM);
9328}
9329
9330#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9331/*
9332 * ":psearch"
9333 */
9334 static void
9335ex_psearch(eap)
9336 exarg_T *eap;
9337{
9338 g_do_tagpreview = p_pvh;
9339 ex_findpat(eap);
9340 g_do_tagpreview = 0;
9341}
9342#endif
9343
9344 static void
9345ex_findpat(eap)
9346 exarg_T *eap;
9347{
9348 int whole = TRUE;
9349 long n;
9350 char_u *p;
9351 int action;
9352
9353 switch (cmdnames[eap->cmdidx].cmd_name[2])
9354 {
9355 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9356 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9357 action = ACTION_GOTO;
9358 else
9359 action = ACTION_SHOW;
9360 break;
9361 case 'i': /* ":ilist" and ":dlist" */
9362 action = ACTION_SHOW_ALL;
9363 break;
9364 case 'u': /* ":ijump" and ":djump" */
9365 action = ACTION_GOTO;
9366 break;
9367 default: /* ":isplit" and ":dsplit" */
9368 action = ACTION_SPLIT;
9369 break;
9370 }
9371
9372 n = 1;
9373 if (vim_isdigit(*eap->arg)) /* get count */
9374 {
9375 n = getdigits(&eap->arg);
9376 eap->arg = skipwhite(eap->arg);
9377 }
9378 if (*eap->arg == '/') /* Match regexp, not just whole words */
9379 {
9380 whole = FALSE;
9381 ++eap->arg;
9382 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9383 if (*p)
9384 {
9385 *p++ = NUL;
9386 p = skipwhite(p);
9387
9388 /* Check for trailing illegal characters */
9389 if (!ends_excmd(*p))
9390 eap->errmsg = e_trailing;
9391 else
9392 eap->nextcmd = check_nextcmd(p);
9393 }
9394 }
9395 if (!eap->skip)
9396 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9397 whole, !eap->forceit,
9398 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9399 n, action, eap->line1, eap->line2);
9400}
9401#endif
9402
9403#ifdef FEAT_WINDOWS
9404
9405# ifdef FEAT_QUICKFIX
9406/*
9407 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9408 */
9409 static void
9410ex_ptag(eap)
9411 exarg_T *eap;
9412{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009413 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9415}
9416
9417/*
9418 * ":pedit"
9419 */
9420 static void
9421ex_pedit(eap)
9422 exarg_T *eap;
9423{
9424 win_T *curwin_save = curwin;
9425
9426 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009427 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 keep_help_flag = curwin_save->w_buffer->b_help;
9429 do_exedit(eap, NULL);
9430 keep_help_flag = FALSE;
9431 if (curwin != curwin_save && win_valid(curwin_save))
9432 {
9433 /* Return cursor to where we were */
9434 validate_cursor();
9435 redraw_later(VALID);
9436 win_enter(curwin_save, TRUE);
9437 }
9438 g_do_tagpreview = 0;
9439}
9440# endif
9441
9442/*
9443 * ":stag", ":stselect" and ":stjump".
9444 */
9445 static void
9446ex_stag(eap)
9447 exarg_T *eap;
9448{
9449 postponed_split = -1;
9450 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009451 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9453 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009454 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455}
9456#endif
9457
9458/*
9459 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9460 */
9461 static void
9462ex_tag(eap)
9463 exarg_T *eap;
9464{
9465 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9466}
9467
9468 static void
9469ex_tag_cmd(eap, name)
9470 exarg_T *eap;
9471 char_u *name;
9472{
9473 int cmd;
9474
9475 switch (name[1])
9476 {
9477 case 'j': cmd = DT_JUMP; /* ":tjump" */
9478 break;
9479 case 's': cmd = DT_SELECT; /* ":tselect" */
9480 break;
9481 case 'p': cmd = DT_PREV; /* ":tprevious" */
9482 break;
9483 case 'N': cmd = DT_PREV; /* ":tNext" */
9484 break;
9485 case 'n': cmd = DT_NEXT; /* ":tnext" */
9486 break;
9487 case 'o': cmd = DT_POP; /* ":pop" */
9488 break;
9489 case 'f': /* ":tfirst" */
9490 case 'r': cmd = DT_FIRST; /* ":trewind" */
9491 break;
9492 case 'l': cmd = DT_LAST; /* ":tlast" */
9493 break;
9494 default: /* ":tag" */
9495#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009496 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 {
9498 do_cstag(eap);
9499 return;
9500 }
9501#endif
9502 cmd = DT_TAG;
9503 break;
9504 }
9505
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009506 if (name[0] == 'l')
9507 {
9508#ifndef FEAT_QUICKFIX
9509 ex_ni(eap);
9510 return;
9511#else
9512 cmd = DT_LTAG;
9513#endif
9514 }
9515
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9517 eap->forceit, TRUE);
9518}
9519
9520/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009521 * Check "str" for starting with a special cmdline variable.
9522 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9523 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9524 */
9525 int
9526find_cmdline_var(src, usedlen)
9527 char_u *src;
9528 int *usedlen;
9529{
9530 int len;
9531 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009532 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009533 "%",
9534#define SPEC_PERC 0
9535 "#",
9536#define SPEC_HASH 1
9537 "<cword>", /* cursor word */
9538#define SPEC_CWORD 2
9539 "<cWORD>", /* cursor WORD */
9540#define SPEC_CCWORD 3
9541 "<cfile>", /* cursor path name */
9542#define SPEC_CFILE 4
9543 "<sfile>", /* ":so" file name */
9544#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009545 "<slnum>", /* ":so" file line number */
9546#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009547#ifdef FEAT_AUTOCMD
9548 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009549# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009550 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009551# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009552 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009553# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009554#endif
9555#ifdef FEAT_CLIENTSERVER
9556 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009557# ifdef FEAT_AUTOCMD
9558# define SPEC_CLIENT 10
9559# else
9560# define SPEC_CLIENT 7
9561# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009562#endif
9563 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009564
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009565 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009566 {
9567 len = (int)STRLEN(spec_str[i]);
9568 if (STRNCMP(src, spec_str[i], len) == 0)
9569 {
9570 *usedlen = len;
9571 return i;
9572 }
9573 }
9574 return -1;
9575}
9576
9577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 * Evaluate cmdline variables.
9579 *
9580 * change '%' to curbuf->b_ffname
9581 * '#' to curwin->w_altfile
9582 * '<cword>' to word under the cursor
9583 * '<cWORD>' to WORD under the cursor
9584 * '<cfile>' to path name under the cursor
9585 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009586 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 * '<afile>' to file name for autocommand
9588 * '<abuf>' to buffer number for autocommand
9589 * '<amatch>' to matching name for autocommand
9590 *
9591 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9592 * "" for error without a message) and NULL is returned.
9593 * Returns an allocated string if a valid match was found.
9594 * Returns NULL if no match was found. "usedlen" then still contains the
9595 * number of characters to skip.
9596 */
9597 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009598eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009600 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 int *usedlen; /* characters after src that are used */
9602 linenr_T *lnump; /* line number for :e command, or NULL */
9603 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009604 int *escaped; /* return value has escaped white space (can
9605 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
9607 int i;
9608 char_u *s;
9609 char_u *result;
9610 char_u *resultbuf = NULL;
9611 int resultlen;
9612 buf_T *buf;
9613 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9614 int spec_idx;
9615#ifdef FEAT_MODIFY_FNAME
9616 int skip_mod = FALSE;
9617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619
9620 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009621 if (escaped != NULL)
9622 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623
9624 /*
9625 * Check if there is something to do.
9626 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009627 spec_idx = find_cmdline_var(src, usedlen);
9628 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 {
9630 *usedlen = 1;
9631 return NULL;
9632 }
9633
9634 /*
9635 * Skip when preceded with a backslash "\%" and "\#".
9636 * Note: In "\\%" the % is also not recognized!
9637 */
9638 if (src > srcstart && src[-1] == '\\')
9639 {
9640 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009641 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 return NULL;
9643 }
9644
9645 /*
9646 * word or WORD under cursor
9647 */
9648 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9649 {
9650 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9651 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9652 if (resultlen == 0)
9653 {
9654 *errormsg = (char_u *)"";
9655 return NULL;
9656 }
9657 }
9658
9659 /*
9660 * '#': Alternate file name
9661 * '%': Current file name
9662 * File name under the cursor
9663 * File name for autocommand
9664 * and following modifiers
9665 */
9666 else
9667 {
9668 switch (spec_idx)
9669 {
9670 case SPEC_PERC: /* '%': current file */
9671 if (curbuf->b_fname == NULL)
9672 {
9673 result = (char_u *)"";
9674 valid = 0; /* Must have ":p:h" to be valid */
9675 }
9676 else
9677#ifdef RISCOS
9678 /* Always use the full path for RISC OS if possible. */
9679 result = curbuf->b_ffname;
9680 if (result == NULL)
9681 result = curbuf->b_fname;
9682#else
9683 result = curbuf->b_fname;
9684#endif
9685 break;
9686
9687 case SPEC_HASH: /* '#' or "#99": alternate file */
9688 if (src[1] == '#') /* "##": the argument list */
9689 {
9690 result = arg_all();
9691 resultbuf = result;
9692 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009693 if (escaped != NULL)
9694 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695#ifdef FEAT_MODIFY_FNAME
9696 skip_mod = TRUE;
9697#endif
9698 break;
9699 }
9700 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009701 if (*s == '<') /* "#<99" uses v:oldfiles */
9702 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 i = (int)getdigits(&s);
9704 *usedlen = (int)(s - src); /* length of what we expand */
9705
Bram Moolenaard812df62008-11-09 12:46:09 +00009706 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009708 if (*usedlen < 2)
9709 {
9710 /* Should we give an error message for #<text? */
9711 *usedlen = 1;
9712 return NULL;
9713 }
9714#ifdef FEAT_EVAL
9715 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9716 (long)i);
9717 if (result == NULL)
9718 {
9719 *errormsg = (char_u *)"";
9720 return NULL;
9721 }
9722#else
9723 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 }
9727 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009728 {
9729 buf = buflist_findnr(i);
9730 if (buf == NULL)
9731 {
9732 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9733 return NULL;
9734 }
9735 if (lnump != NULL)
9736 *lnump = ECMD_LAST;
9737 if (buf->b_fname == NULL)
9738 {
9739 result = (char_u *)"";
9740 valid = 0; /* Must have ":p:h" to be valid */
9741 }
9742 else
9743 result = buf->b_fname;
9744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 break;
9746
9747#ifdef FEAT_SEARCHPATH
9748 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009749 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (result == NULL)
9751 {
9752 *errormsg = (char_u *)"";
9753 return NULL;
9754 }
9755 resultbuf = result; /* remember allocated string */
9756 break;
9757#endif
9758
9759#ifdef FEAT_AUTOCMD
9760 case SPEC_AFILE: /* file name for autocommand */
9761 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009762 if (result != NULL && !autocmd_fname_full)
9763 {
9764 /* Still need to turn the fname into a full path. It is
9765 * postponed to avoid a delay when <afile> is not used. */
9766 autocmd_fname_full = TRUE;
9767 result = FullName_save(autocmd_fname, FALSE);
9768 vim_free(autocmd_fname);
9769 autocmd_fname = result;
9770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 if (result == NULL)
9772 {
9773 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9774 return NULL;
9775 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009776 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 break;
9778
9779 case SPEC_ABUF: /* buffer number for autocommand */
9780 if (autocmd_bufnr <= 0)
9781 {
9782 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9783 return NULL;
9784 }
9785 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9786 result = strbuf;
9787 break;
9788
9789 case SPEC_AMATCH: /* match name for autocommand */
9790 result = autocmd_match;
9791 if (result == NULL)
9792 {
9793 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9794 return NULL;
9795 }
9796 break;
9797
9798#endif
9799 case SPEC_SFILE: /* file name for ":so" command */
9800 result = sourcing_name;
9801 if (result == NULL)
9802 {
9803 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9804 return NULL;
9805 }
9806 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009807 case SPEC_SLNUM: /* line in file for ":so" command */
9808 if (sourcing_name == NULL || sourcing_lnum == 0)
9809 {
9810 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9811 return NULL;
9812 }
9813 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9814 result = strbuf;
9815 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816#if defined(FEAT_CLIENTSERVER)
9817 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009818 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9819 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 result = strbuf;
9821 break;
9822#endif
9823 }
9824
9825 resultlen = (int)STRLEN(result); /* length of new string */
9826 if (src[*usedlen] == '<') /* remove the file name extension */
9827 {
9828 ++*usedlen;
9829#ifdef RISCOS
9830 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9831#else
9832 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9833#endif
9834 resultlen = (int)(s - result);
9835 }
9836#ifdef FEAT_MODIFY_FNAME
9837 else if (!skip_mod)
9838 {
9839 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9840 &resultlen);
9841 if (result == NULL)
9842 {
9843 *errormsg = (char_u *)"";
9844 return NULL;
9845 }
9846 }
9847#endif
9848 }
9849
9850 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9851 {
9852 if (valid != VALID_HEAD + VALID_PATH)
9853 /* xgettext:no-c-format */
9854 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9855 else
9856 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9857 result = NULL;
9858 }
9859 else
9860 result = vim_strnsave(result, resultlen);
9861 vim_free(resultbuf);
9862 return result;
9863}
9864
9865/*
9866 * Concatenate all files in the argument list, separated by spaces, and return
9867 * it in one allocated string.
9868 * Spaces and backslashes in the file names are escaped with a backslash.
9869 * Returns NULL when out of memory.
9870 */
9871 static char_u *
9872arg_all()
9873{
9874 int len;
9875 int idx;
9876 char_u *retval = NULL;
9877 char_u *p;
9878
9879 /*
9880 * Do this loop two times:
9881 * first time: compute the total length
9882 * second time: concatenate the names
9883 */
9884 for (;;)
9885 {
9886 len = 0;
9887 for (idx = 0; idx < ARGCOUNT; ++idx)
9888 {
9889 p = alist_name(&ARGLIST[idx]);
9890 if (p != NULL)
9891 {
9892 if (len > 0)
9893 {
9894 /* insert a space in between names */
9895 if (retval != NULL)
9896 retval[len] = ' ';
9897 ++len;
9898 }
9899 for ( ; *p != NUL; ++p)
9900 {
9901 if (*p == ' ' || *p == '\\')
9902 {
9903 /* insert a backslash */
9904 if (retval != NULL)
9905 retval[len] = '\\';
9906 ++len;
9907 }
9908 if (retval != NULL)
9909 retval[len] = *p;
9910 ++len;
9911 }
9912 }
9913 }
9914
9915 /* second time: break here */
9916 if (retval != NULL)
9917 {
9918 retval[len] = NUL;
9919 break;
9920 }
9921
9922 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009923 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (retval == NULL)
9925 break;
9926 }
9927
9928 return retval;
9929}
9930
9931#if defined(FEAT_AUTOCMD) || defined(PROTO)
9932/*
9933 * Expand the <sfile> string in "arg".
9934 *
9935 * Returns an allocated string, or NULL for any error.
9936 */
9937 char_u *
9938expand_sfile(arg)
9939 char_u *arg;
9940{
9941 char_u *errormsg;
9942 int len;
9943 char_u *result;
9944 char_u *newres;
9945 char_u *repl;
9946 int srclen;
9947 char_u *p;
9948
9949 result = vim_strsave(arg);
9950 if (result == NULL)
9951 return NULL;
9952
9953 for (p = result; *p; )
9954 {
9955 if (STRNCMP(p, "<sfile>", 7) != 0)
9956 ++p;
9957 else
9958 {
9959 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009960 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 if (errormsg != NULL)
9962 {
9963 if (*errormsg)
9964 emsg(errormsg);
9965 vim_free(result);
9966 return NULL;
9967 }
9968 if (repl == NULL) /* no match (cannot happen) */
9969 {
9970 p += srclen;
9971 continue;
9972 }
9973 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9974 newres = alloc(len);
9975 if (newres == NULL)
9976 {
9977 vim_free(repl);
9978 vim_free(result);
9979 return NULL;
9980 }
9981 mch_memmove(newres, result, (size_t)(p - result));
9982 STRCPY(newres + (p - result), repl);
9983 len = (int)STRLEN(newres);
9984 STRCAT(newres, p + srclen);
9985 vim_free(repl);
9986 vim_free(result);
9987 result = newres;
9988 p = newres + len; /* continue after the match */
9989 }
9990 }
9991
9992 return result;
9993}
9994#endif
9995
9996#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009997static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9998 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10000static frame_T *ses_skipframe __ARGS((frame_T *fr));
10001static int ses_do_frame __ARGS((frame_T *fr));
10002static int ses_do_win __ARGS((win_T *wp));
10003static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10004static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10005static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10006
10007/*
10008 * Write openfile commands for the current buffers to an .exrc file.
10009 * Return FAIL on error, OK otherwise.
10010 */
10011 static int
10012makeopens(fd, dirnow)
10013 FILE *fd;
10014 char_u *dirnow; /* Current directory name */
10015{
10016 buf_T *buf;
10017 int only_save_windows = TRUE;
10018 int nr;
10019 int cnr = 1;
10020 int restore_size = TRUE;
10021 win_T *wp;
10022 char_u *sname;
10023 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010024 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010025 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010026 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010027 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010028 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029
10030 if (ssop_flags & SSOP_BUFFERS)
10031 only_save_windows = FALSE; /* Save ALL buffers */
10032
10033 /*
10034 * Begin by setting the this_session variable, and then other
10035 * sessionable variables.
10036 */
10037#ifdef FEAT_EVAL
10038 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10039 return FAIL;
10040 if (ssop_flags & SSOP_GLOBALS)
10041 if (store_session_globals(fd) == FAIL)
10042 return FAIL;
10043#endif
10044
10045 /*
10046 * Close all windows but one.
10047 */
10048 if (put_line(fd, "silent only") == FAIL)
10049 return FAIL;
10050
10051 /*
10052 * Now a :cd command to the session directory or the current directory
10053 */
10054 if (ssop_flags & SSOP_SESDIR)
10055 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010056 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10057 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 return FAIL;
10059 }
10060 else if (ssop_flags & SSOP_CURDIR)
10061 {
10062 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10063 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010064 || fputs("cd ", fd) < 0
10065 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10066 || put_eol(fd) == FAIL)
10067 {
10068 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 vim_free(sname);
10072 }
10073
10074 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010075 * If there is an empty, unnamed buffer we will wipe it out later.
10076 * Remember the buffer number.
10077 */
10078 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10079 return FAIL;
10080 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10081 return FAIL;
10082 if (put_line(fd, "endif") == FAIL)
10083 return FAIL;
10084
10085 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 * Now save the current files, current buffer first.
10087 */
10088 if (put_line(fd, "set shortmess=aoO") == FAIL)
10089 return FAIL;
10090
10091 /* Now put the other buffers into the buffer list */
10092 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10093 {
10094 if (!(only_save_windows && buf->b_nwindows == 0)
10095 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10096 && buf->b_fname != NULL
10097 && buf->b_p_bl)
10098 {
10099 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10100 : buf->b_wininfo->wi_fpos.lnum) < 0
10101 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10102 return FAIL;
10103 }
10104 }
10105
10106 /* the global argument list */
10107 if (ses_arglist(fd, "args", &global_alist.al_ga,
10108 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10109 return FAIL;
10110
10111 if (ssop_flags & SSOP_RESIZE)
10112 {
10113 /* Note: after the restore we still check it worked!*/
10114 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10115 || put_eol(fd) == FAIL)
10116 return FAIL;
10117 }
10118
10119#ifdef FEAT_GUI
10120 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10121 {
10122 int x, y;
10123
10124 if (gui_mch_get_winpos(&x, &y) == OK)
10125 {
10126 /* Note: after the restore we still check it worked!*/
10127 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10128 return FAIL;
10129 }
10130 }
10131#endif
10132
10133 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010134 * May repeat putting Windows for each tab, when "tabpages" is in
10135 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010136 * Don't use goto_tabpage(), it may change directory and trigger
10137 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010139 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010140 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010141 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010143 int need_tabnew = FALSE;
10144
Bram Moolenaar18144c82006-04-12 21:52:12 +000010145 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010147 tabpage_T *tp = find_tabpage(tabnr);
10148
10149 if (tp == NULL)
10150 break; /* done all tab pages */
10151 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010152 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010153 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010154 tab_topframe = topframe;
10155 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010156 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010157 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010158 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010159 tab_topframe = tp->tp_topframe;
10160 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010161 if (tabnr > 1)
10162 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164
Bram Moolenaar18144c82006-04-12 21:52:12 +000010165 /*
10166 * Before creating the window layout, try loading one file. If this
10167 * is aborted we don't end up with a number of useless windows.
10168 * This may have side effects! (e.g., compressed or network file).
10169 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010170 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010171 {
10172 if (ses_do_win(wp)
10173 && wp->w_buffer->b_ffname != NULL
10174 && !wp->w_buffer->b_help
10175#ifdef FEAT_QUICKFIX
10176 && !bt_nofile(wp->w_buffer)
10177#endif
10178 )
10179 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010180 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010181 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10182 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010183 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010184 if (!wp->w_arg_idx_invalid)
10185 edited_win = wp;
10186 break;
10187 }
10188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010190 /* If no file got edited create an empty tab page. */
10191 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10192 return FAIL;
10193
Bram Moolenaar18144c82006-04-12 21:52:12 +000010194 /*
10195 * Save current window layout.
10196 */
10197 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010199 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010201 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10202 return FAIL;
10203 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10204 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaar18144c82006-04-12 21:52:12 +000010206 /*
10207 * Check if window sizes can be restored (no windows omitted).
10208 * Remember the window number of the current window after restoring.
10209 */
10210 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010211 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010212 {
10213 if (ses_do_win(wp))
10214 ++nr;
10215 else
10216 restore_size = FALSE;
10217 if (curwin == wp)
10218 cnr = nr;
10219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220
Bram Moolenaar18144c82006-04-12 21:52:12 +000010221 /* Go to the first window. */
10222 if (put_line(fd, "wincmd t") == FAIL)
10223 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010224
Bram Moolenaar18144c82006-04-12 21:52:12 +000010225 /*
10226 * If more than one window, see if sizes can be restored.
10227 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10228 * resized when moving between windows.
10229 * Do this before restoring the view, so that the topline and the
10230 * cursor can be set. This is done again below.
10231 */
10232 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10233 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010234 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236
Bram Moolenaar18144c82006-04-12 21:52:12 +000010237 /*
10238 * Restore the view of the window (options, file, cursor, etc.).
10239 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010240 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010241 {
10242 if (!ses_do_win(wp))
10243 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010244 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10245 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010246 return FAIL;
10247 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10248 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010249 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010250 }
10251
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010252 /* The argument index in the first tab page is zero, need to set it in
10253 * each window. For further tab pages it's the window where we do
10254 * "tabedit". */
10255 cur_arg_idx = next_arg_idx;
10256
Bram Moolenaar18144c82006-04-12 21:52:12 +000010257 /*
10258 * Restore cursor to the current window if it's not the first one.
10259 */
10260 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10261 || put_eol(fd) == FAIL))
10262 return FAIL;
10263
10264 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010265 * Restore window sizes again after jumping around in windows, because
10266 * the current window has a minimum size while others may not.
10267 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010268 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010269 return FAIL;
10270
Bram Moolenaar18144c82006-04-12 21:52:12 +000010271 /* Don't continue in another tab page when doing only the current one
10272 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010273 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010274 break;
10275 }
10276
10277 if (ssop_flags & SSOP_TABPAGES)
10278 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010279 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10280 || put_eol(fd) == FAIL)
10281 return FAIL;
10282 }
10283
Bram Moolenaar9c102382006-05-03 21:26:49 +000010284 /*
10285 * Wipe out an empty unnamed buffer we started in.
10286 */
10287 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10288 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010289 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010290 return FAIL;
10291 if (put_line(fd, "endif") == FAIL)
10292 return FAIL;
10293 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10294 return FAIL;
10295
10296 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10297 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10298 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300
10301 /*
10302 * Lastly, execute the x.vim file if it exists.
10303 */
10304 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10305 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010306 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 || put_line(fd, "endif") == FAIL)
10308 return FAIL;
10309
10310 return OK;
10311}
10312
10313 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010314ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 FILE *fd;
10316 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010317 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318{
10319 int n = 0;
10320 win_T *wp;
10321
10322 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10323 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010324 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 {
10326 if (!ses_do_win(wp))
10327 continue;
10328 ++n;
10329
10330 /* restore height when not full height */
10331 if (wp->w_height + wp->w_status_height < topframe->fr_height
10332 && (fprintf(fd,
10333 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10334 n, (long)wp->w_height, Rows / 2, Rows) < 0
10335 || put_eol(fd) == FAIL))
10336 return FAIL;
10337
10338 /* restore width when not full width */
10339 if (wp->w_width < Columns && (fprintf(fd,
10340 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10341 n, (long)wp->w_width, Columns / 2, Columns) < 0
10342 || put_eol(fd) == FAIL))
10343 return FAIL;
10344 }
10345 }
10346 else
10347 {
10348 /* Just equalise window sizes */
10349 if (put_line(fd, "wincmd =") == FAIL)
10350 return FAIL;
10351 }
10352 return OK;
10353}
10354
10355/*
10356 * Write commands to "fd" to recursively create windows for frame "fr",
10357 * horizontally and vertically split.
10358 * After the commands the last window in the frame is the current window.
10359 * Returns FAIL when writing the commands to "fd" fails.
10360 */
10361 static int
10362ses_win_rec(fd, fr)
10363 FILE *fd;
10364 frame_T *fr;
10365{
10366 frame_T *frc;
10367 int count = 0;
10368
10369 if (fr->fr_layout != FR_LEAF)
10370 {
10371 /* Find first frame that's not skipped and then create a window for
10372 * each following one (first frame is already there). */
10373 frc = ses_skipframe(fr->fr_child);
10374 if (frc != NULL)
10375 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10376 {
10377 /* Make window as big as possible so that we have lots of room
10378 * to split. */
10379 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10380 || put_line(fd, fr->fr_layout == FR_COL
10381 ? "split" : "vsplit") == FAIL)
10382 return FAIL;
10383 ++count;
10384 }
10385
10386 /* Go back to the first window. */
10387 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10388 ? "%dwincmd k" : "%dwincmd h", count) < 0
10389 || put_eol(fd) == FAIL))
10390 return FAIL;
10391
10392 /* Recursively create frames/windows in each window of this column or
10393 * row. */
10394 frc = ses_skipframe(fr->fr_child);
10395 while (frc != NULL)
10396 {
10397 ses_win_rec(fd, frc);
10398 frc = ses_skipframe(frc->fr_next);
10399 /* Go to next window. */
10400 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10401 return FAIL;
10402 }
10403 }
10404 return OK;
10405}
10406
10407/*
10408 * Skip frames that don't contain windows we want to save in the Session.
10409 * Returns NULL when there none.
10410 */
10411 static frame_T *
10412ses_skipframe(fr)
10413 frame_T *fr;
10414{
10415 frame_T *frc;
10416
10417 for (frc = fr; frc != NULL; frc = frc->fr_next)
10418 if (ses_do_frame(frc))
10419 break;
10420 return frc;
10421}
10422
10423/*
10424 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10425 * the Session.
10426 */
10427 static int
10428ses_do_frame(fr)
10429 frame_T *fr;
10430{
10431 frame_T *frc;
10432
10433 if (fr->fr_layout == FR_LEAF)
10434 return ses_do_win(fr->fr_win);
10435 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10436 if (ses_do_frame(frc))
10437 return TRUE;
10438 return FALSE;
10439}
10440
10441/*
10442 * Return non-zero if window "wp" is to be stored in the Session.
10443 */
10444 static int
10445ses_do_win(wp)
10446 win_T *wp;
10447{
10448 if (wp->w_buffer->b_fname == NULL
10449#ifdef FEAT_QUICKFIX
10450 /* When 'buftype' is "nofile" can't restore the window contents. */
10451 || bt_nofile(wp->w_buffer)
10452#endif
10453 )
10454 return (ssop_flags & SSOP_BLANK);
10455 if (wp->w_buffer->b_help)
10456 return (ssop_flags & SSOP_HELP);
10457 return TRUE;
10458}
10459
10460/*
10461 * Write commands to "fd" to restore the view of a window.
10462 * Caller must make sure 'scrolloff' is zero.
10463 */
10464 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010465put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 FILE *fd;
10467 win_T *wp;
10468 int add_edit; /* add ":edit" command to view */
10469 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010470 int current_arg_idx; /* current argument index of the window, use
10471 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 win_T *save_curwin;
10474 int f;
10475 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010476 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477
10478 /* Always restore cursor position for ":mksession". For ":mkview" only
10479 * when 'viewoptions' contains "cursor". */
10480 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10481
10482 /*
10483 * Local argument list.
10484 */
10485 if (wp->w_alist == &global_alist)
10486 {
10487 if (put_line(fd, "argglobal") == FAIL)
10488 return FAIL;
10489 }
10490 else
10491 {
10492 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10493 flagp == &vop_flags
10494 || !(*flagp & SSOP_CURDIR)
10495 || wp->w_localdir != NULL, flagp) == FAIL)
10496 return FAIL;
10497 }
10498
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010499 /* Only when part of a session: restore the argument index. Some
10500 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010501 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010502 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010504 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 || put_eol(fd) == FAIL)
10506 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010507 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 }
10509
10510 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010511 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 {
10513 /*
10514 * Load the file.
10515 */
10516 if (wp->w_buffer->b_ffname != NULL
10517#ifdef FEAT_QUICKFIX
10518 && !bt_nofile(wp->w_buffer)
10519#endif
10520 )
10521 {
10522 /*
10523 * Editing a file in this buffer: use ":edit file".
10524 * This may have side effects! (e.g., compressed or network file).
10525 */
10526 if (fputs("edit ", fd) < 0
10527 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10528 return FAIL;
10529 }
10530 else
10531 {
10532 /* No file in this buffer, just make it empty. */
10533 if (put_line(fd, "enew") == FAIL)
10534 return FAIL;
10535#ifdef FEAT_QUICKFIX
10536 if (wp->w_buffer->b_ffname != NULL)
10537 {
10538 /* The buffer does have a name, but it's not a file name. */
10539 if (fputs("file ", fd) < 0
10540 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10541 return FAIL;
10542 }
10543#endif
10544 do_cursor = FALSE;
10545 }
10546 }
10547
10548 /*
10549 * Local mappings and abbreviations.
10550 */
10551 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10552 && makemap(fd, wp->w_buffer) == FAIL)
10553 return FAIL;
10554
10555 /*
10556 * Local options. Need to go to the window temporarily.
10557 * Store only local values when using ":mkview" and when ":mksession" is
10558 * used and 'sessionoptions' doesn't include "options".
10559 * Some folding options are always stored when "folds" is included,
10560 * otherwise the folds would not be restored correctly.
10561 */
10562 save_curwin = curwin;
10563 curwin = wp;
10564 curbuf = curwin->w_buffer;
10565 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10566 f = makeset(fd, OPT_LOCAL,
10567 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10568#ifdef FEAT_FOLDING
10569 else if (*flagp & SSOP_FOLDS)
10570 f = makefoldset(fd);
10571#endif
10572 else
10573 f = OK;
10574 curwin = save_curwin;
10575 curbuf = curwin->w_buffer;
10576 if (f == FAIL)
10577 return FAIL;
10578
10579#ifdef FEAT_FOLDING
10580 /*
10581 * Save Folds when 'buftype' is empty and for help files.
10582 */
10583 if ((*flagp & SSOP_FOLDS)
10584 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010585# ifdef FEAT_QUICKFIX
10586 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10587# endif
10588 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 {
10590 if (put_folds(fd, wp) == FAIL)
10591 return FAIL;
10592 }
10593#endif
10594
10595 /*
10596 * Set the cursor after creating folds, since that moves the cursor.
10597 */
10598 if (do_cursor)
10599 {
10600
10601 /* Restore the cursor line in the file and relatively in the
10602 * window. Don't use "G", it changes the jumplist. */
10603 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10604 (long)wp->w_cursor.lnum,
10605 (long)(wp->w_cursor.lnum - wp->w_topline),
10606 (long)wp->w_height / 2, (long)wp->w_height) < 0
10607 || put_eol(fd) == FAIL
10608 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10609 || put_line(fd, "exe s:l") == FAIL
10610 || put_line(fd, "normal! zt") == FAIL
10611 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10612 || put_eol(fd) == FAIL)
10613 return FAIL;
10614 /* Restore the cursor column and left offset when not wrapping. */
10615 if (wp->w_cursor.col == 0)
10616 {
10617 if (put_line(fd, "normal! 0") == FAIL)
10618 return FAIL;
10619 }
10620 else
10621 {
10622 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10623 {
10624 if (fprintf(fd,
10625 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10626 (long)wp->w_cursor.col,
10627 (long)(wp->w_cursor.col - wp->w_leftcol),
10628 (long)wp->w_width / 2, (long)wp->w_width) < 0
10629 || put_eol(fd) == FAIL
10630 || put_line(fd, "if s:c > 0") == FAIL
10631 || fprintf(fd,
10632 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10633 (long)wp->w_cursor.col) < 0
10634 || put_eol(fd) == FAIL
10635 || put_line(fd, "else") == FAIL
10636 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10637 || put_eol(fd) == FAIL
10638 || put_line(fd, "endif") == FAIL)
10639 return FAIL;
10640 }
10641 else
10642 {
10643 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10644 || put_eol(fd) == FAIL)
10645 return FAIL;
10646 }
10647 }
10648 }
10649
10650 /*
10651 * Local directory.
10652 */
10653 if (wp->w_localdir != NULL)
10654 {
10655 if (fputs("lcd ", fd) < 0
10656 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10657 || put_eol(fd) == FAIL)
10658 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010659 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 }
10661
10662 return OK;
10663}
10664
10665/*
10666 * Write an argument list to the session file.
10667 * Returns FAIL if writing fails.
10668 */
10669 static int
10670ses_arglist(fd, cmd, gap, fullname, flagp)
10671 FILE *fd;
10672 char *cmd;
10673 garray_T *gap;
10674 int fullname; /* TRUE: use full path name */
10675 unsigned *flagp;
10676{
10677 int i;
10678 char_u buf[MAXPATHL];
10679 char_u *s;
10680
10681 if (gap->ga_len == 0)
10682 return put_line(fd, "silent! argdel *");
10683 if (fputs(cmd, fd) < 0)
10684 return FAIL;
10685 for (i = 0; i < gap->ga_len; ++i)
10686 {
10687 /* NULL file names are skipped (only happens when out of memory). */
10688 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10689 if (s != NULL)
10690 {
10691 if (fullname)
10692 {
10693 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10694 s = buf;
10695 }
10696 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10697 return FAIL;
10698 }
10699 }
10700 return put_eol(fd);
10701}
10702
10703/*
10704 * Write a buffer name to the session file.
10705 * Also ends the line.
10706 * Returns FAIL if writing fails.
10707 */
10708 static int
10709ses_fname(fd, buf, flagp)
10710 FILE *fd;
10711 buf_T *buf;
10712 unsigned *flagp;
10713{
10714 char_u *name;
10715
10716 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010717 * the session file will be sourced.
10718 * Don't do this for ":mkview", we don't know the current directory.
10719 * Don't do this after ":lcd", we don't keep track of what the current
10720 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 if (buf->b_sfname != NULL
10722 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010723 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010724#ifdef FEAT_AUTOCHDIR
10725 && !p_acd
10726#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010727 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 name = buf->b_sfname;
10729 else
10730 name = buf->b_ffname;
10731 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10732 return FAIL;
10733 return OK;
10734}
10735
10736/*
10737 * Write a file name to the session file.
10738 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10739 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010740 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 */
10742 static int
10743ses_put_fname(fd, name, flagp)
10744 FILE *fd;
10745 char_u *name;
10746 unsigned *flagp;
10747{
10748 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010749 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
10752 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010753 if (sname == NULL)
10754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010756 if (*flagp & SSOP_SLASH)
10757 {
10758 /* change all backslashes to forward slashes */
10759 for (p = sname; *p != NUL; mb_ptr_adv(p))
10760 if (*p == '\\')
10761 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010763
10764 /* escapse special characters */
10765 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010767 if (p == NULL)
10768 return FAIL;
10769
10770 /* write the result */
10771 if (fputs((char *)p, fd) < 0)
10772 retval = FAIL;
10773
10774 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 return retval;
10776}
10777
10778/*
10779 * ":loadview [nr]"
10780 */
10781 static void
10782ex_loadview(eap)
10783 exarg_T *eap;
10784{
10785 char_u *fname;
10786
10787 fname = get_view_file(*eap->arg);
10788 if (fname != NULL)
10789 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010790 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 vim_free(fname);
10792 }
10793}
10794
10795/*
10796 * Get the name of the view file for the current buffer.
10797 */
10798 static char_u *
10799get_view_file(c)
10800 int c;
10801{
10802 int len = 0;
10803 char_u *p, *s;
10804 char_u *retval;
10805 char_u *sname;
10806
10807 if (curbuf->b_ffname == NULL)
10808 {
10809 EMSG(_(e_noname));
10810 return NULL;
10811 }
10812 sname = home_replace_save(NULL, curbuf->b_ffname);
10813 if (sname == NULL)
10814 return NULL;
10815
10816 /*
10817 * We want a file name without separators, because we're not going to make
10818 * a directory.
10819 * "normal" path separator -> "=+"
10820 * "=" -> "=="
10821 * ":" path separator -> "=-"
10822 */
10823 for (p = sname; *p; ++p)
10824 if (*p == '=' || vim_ispathsep(*p))
10825 ++len;
10826 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10827 if (retval != NULL)
10828 {
10829 STRCPY(retval, p_vdir);
10830 add_pathsep(retval);
10831 s = retval + STRLEN(retval);
10832 for (p = sname; *p; ++p)
10833 {
10834 if (*p == '=')
10835 {
10836 *s++ = '=';
10837 *s++ = '=';
10838 }
10839 else if (vim_ispathsep(*p))
10840 {
10841 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010842#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 || defined(VMS)
10844 if (*p == ':')
10845 *s++ = '-';
10846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010848 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 }
10850 else
10851 *s++ = *p;
10852 }
10853 *s++ = '=';
10854 *s++ = c;
10855 STRCPY(s, ".vim");
10856 }
10857
10858 vim_free(sname);
10859 return retval;
10860}
10861
10862#endif /* FEAT_SESSION */
10863
10864/*
10865 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10866 * Return FAIL for a write error.
10867 */
10868 int
10869put_eol(fd)
10870 FILE *fd;
10871{
10872 if (
10873#ifdef USE_CRNL
10874 (
10875# ifdef MKSESSION_NL
10876 !mksession_nl &&
10877# endif
10878 (putc('\r', fd) < 0)) ||
10879#endif
10880 (putc('\n', fd) < 0))
10881 return FAIL;
10882 return OK;
10883}
10884
10885/*
10886 * Write a line to "fd".
10887 * Return FAIL for a write error.
10888 */
10889 int
10890put_line(fd, s)
10891 FILE *fd;
10892 char *s;
10893{
10894 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10895 return FAIL;
10896 return OK;
10897}
10898
10899#ifdef FEAT_VIMINFO
10900/*
10901 * ":rviminfo" and ":wviminfo".
10902 */
10903 static void
10904ex_viminfo(eap)
10905 exarg_T *eap;
10906{
10907 char_u *save_viminfo;
10908
10909 save_viminfo = p_viminfo;
10910 if (*p_viminfo == NUL)
10911 p_viminfo = (char_u *)"'100";
10912 if (eap->cmdidx == CMD_rviminfo)
10913 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010914 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10915 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 EMSG(_("E195: Cannot open viminfo file for reading"));
10917 }
10918 else
10919 write_viminfo(eap->arg, eap->forceit);
10920 p_viminfo = save_viminfo;
10921}
10922#endif
10923
10924#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010925/*
10926 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010927 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 void
10930dialog_msg(buff, format, fname)
10931 char_u *buff;
10932 char *format;
10933 char_u *fname;
10934{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 if (fname == NULL)
10936 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010937 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938}
10939#endif
10940
10941/*
10942 * ":behave {mswin,xterm}"
10943 */
10944 static void
10945ex_behave(eap)
10946 exarg_T *eap;
10947{
10948 if (STRCMP(eap->arg, "mswin") == 0)
10949 {
10950 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10951 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10952 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10953 set_option_value((char_u *)"keymodel", 0L,
10954 (char_u *)"startsel,stopsel", 0);
10955 }
10956 else if (STRCMP(eap->arg, "xterm") == 0)
10957 {
10958 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10959 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10960 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10961 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10962 }
10963 else
10964 EMSG2(_(e_invarg2), eap->arg);
10965}
10966
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010967#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10968/*
10969 * Function given to ExpandGeneric() to obtain the possible arguments of the
10970 * ":behave {mswin,xterm}" command.
10971 */
10972 char_u *
10973get_behave_arg(xp, idx)
10974 expand_T *xp UNUSED;
10975 int idx;
10976{
10977 if (idx == 0)
10978 return (char_u *)"mswin";
10979 if (idx == 1)
10980 return (char_u *)"xterm";
10981 return NULL;
10982}
10983#endif
10984
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985#ifdef FEAT_AUTOCMD
10986static int filetype_detect = FALSE;
10987static int filetype_plugin = FALSE;
10988static int filetype_indent = FALSE;
10989
10990/*
10991 * ":filetype [plugin] [indent] {on,off,detect}"
10992 * on: Load the filetype.vim file to install autocommands for file types.
10993 * off: Load the ftoff.vim file to remove all autocommands for file types.
10994 * plugin on: load filetype.vim and ftplugin.vim
10995 * plugin off: load ftplugof.vim
10996 * indent on: load filetype.vim and indent.vim
10997 * indent off: load indoff.vim
10998 */
10999 static void
11000ex_filetype(eap)
11001 exarg_T *eap;
11002{
11003 char_u *arg = eap->arg;
11004 int plugin = FALSE;
11005 int indent = FALSE;
11006
11007 if (*eap->arg == NUL)
11008 {
11009 /* Print current status. */
11010 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11011 filetype_detect ? "ON" : "OFF",
11012 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11013 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11014 return;
11015 }
11016
11017 /* Accept "plugin" and "indent" in any order. */
11018 for (;;)
11019 {
11020 if (STRNCMP(arg, "plugin", 6) == 0)
11021 {
11022 plugin = TRUE;
11023 arg = skipwhite(arg + 6);
11024 continue;
11025 }
11026 if (STRNCMP(arg, "indent", 6) == 0)
11027 {
11028 indent = TRUE;
11029 arg = skipwhite(arg + 6);
11030 continue;
11031 }
11032 break;
11033 }
11034 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11035 {
11036 if (*arg == 'o' || !filetype_detect)
11037 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011038 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 filetype_detect = TRUE;
11040 if (plugin)
11041 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011042 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 filetype_plugin = TRUE;
11044 }
11045 if (indent)
11046 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011047 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 filetype_indent = TRUE;
11049 }
11050 }
11051 if (*arg == 'd')
11052 {
11053 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011054 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
11056 }
11057 else if (STRCMP(arg, "off") == 0)
11058 {
11059 if (plugin || indent)
11060 {
11061 if (plugin)
11062 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011063 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 filetype_plugin = FALSE;
11065 }
11066 if (indent)
11067 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011068 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 filetype_indent = FALSE;
11070 }
11071 }
11072 else
11073 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011074 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 filetype_detect = FALSE;
11076 }
11077 }
11078 else
11079 EMSG2(_(e_invarg2), arg);
11080}
11081
11082/*
11083 * ":setfiletype {name}"
11084 */
11085 static void
11086ex_setfiletype(eap)
11087 exarg_T *eap;
11088{
11089 if (!did_filetype)
11090 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11091}
11092#endif
11093
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 static void
11095ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011096 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098#ifdef FEAT_DIGRAPHS
11099 if (*eap->arg != NUL)
11100 putdigraph(eap->arg);
11101 else
11102 listdigraphs();
11103#else
11104 EMSG(_("E196: No digraphs in this version"));
11105#endif
11106}
11107
11108 static void
11109ex_set(eap)
11110 exarg_T *eap;
11111{
11112 int flags = 0;
11113
11114 if (eap->cmdidx == CMD_setlocal)
11115 flags = OPT_LOCAL;
11116 else if (eap->cmdidx == CMD_setglobal)
11117 flags = OPT_GLOBAL;
11118#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11119 if (cmdmod.browse && flags == 0)
11120 ex_options(eap);
11121 else
11122#endif
11123 (void)do_set(eap->arg, flags);
11124}
11125
11126#ifdef FEAT_SEARCH_EXTRA
11127/*
11128 * ":nohlsearch"
11129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 static void
11131ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011132 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133{
11134 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011135 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011139 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 * Sets nextcmd to the start of the next command, if any. Also called when
11141 * skipping commands to find the next command.
11142 */
11143 static void
11144ex_match(eap)
11145 exarg_T *eap;
11146{
11147 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011148 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 char_u *end;
11150 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011151 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011152
11153 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011154 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011155 else
11156 {
11157 EMSG(e_invcmd);
11158 return;
11159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160
11161 /* First clear any old pattern. */
11162 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011163 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164
11165 if (ends_excmd(*eap->arg))
11166 end = eap->arg;
11167 else if ((STRNICMP(eap->arg, "none", 4) == 0
11168 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11169 end = eap->arg + 4;
11170 else
11171 {
11172 p = skiptowhite(eap->arg);
11173 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011174 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 p = skipwhite(p);
11176 if (*p == NUL)
11177 {
11178 /* There must be two arguments. */
11179 EMSG2(_(e_invarg2), eap->arg);
11180 return;
11181 }
11182 end = skip_regexp(p + 1, *p, TRUE, NULL);
11183 if (!eap->skip)
11184 {
11185 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11186 {
11187 eap->errmsg = e_trailing;
11188 return;
11189 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011190 if (*end != *p)
11191 {
11192 EMSG2(_(e_invarg2), p);
11193 return;
11194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
11196 c = *end;
11197 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011198 match_add(curwin, g, p + 1, 10, id);
11199 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011200 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 }
11202 }
11203 eap->nextcmd = find_nextcmd(end);
11204}
11205#endif
11206
11207#ifdef FEAT_CRYPT
11208/*
11209 * ":X": Get crypt key
11210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 static void
11212ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011213 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011215 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011216 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217}
11218#endif
11219
11220#ifdef FEAT_FOLDING
11221 static void
11222ex_fold(eap)
11223 exarg_T *eap;
11224{
11225 if (foldManualAllowed(TRUE))
11226 foldCreate(eap->line1, eap->line2);
11227}
11228
11229 static void
11230ex_foldopen(eap)
11231 exarg_T *eap;
11232{
11233 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11234 eap->forceit, FALSE);
11235}
11236
11237 static void
11238ex_folddo(eap)
11239 exarg_T *eap;
11240{
11241 linenr_T lnum;
11242
11243 /* First set the marks for all lines closed/open. */
11244 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11245 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11246 ml_setmarked(lnum);
11247
11248 /* Execute the command on the marked lines. */
11249 global_exe(eap->arg);
11250 ml_clearmarked(); /* clear rest of the marks */
11251}
11252#endif