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