blob: 3265860f408d77d6679e5c7c6dd207262a9a5293 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200132#if !defined(FEAT_PERL) \
133 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
134 || !defined(FEAT_TCL) \
135 || !defined(FEAT_RUBY) \
136 || !defined(FEAT_LUA) \
137 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000138# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void ex_script_ni __ARGS((exarg_T *eap));
140#endif
141static char_u *invalid_range __ARGS((exarg_T *eap));
142static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000143#ifdef FEAT_QUICKFIX
144static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
147static void ex_highlight __ARGS((exarg_T *eap));
148static void ex_colorscheme __ARGS((exarg_T *eap));
149static void ex_quit __ARGS((exarg_T *eap));
150static void ex_cquit __ARGS((exarg_T *eap));
151static void ex_quit_all __ARGS((exarg_T *eap));
152#ifdef FEAT_WINDOWS
153static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000154static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_resize __ARGS((exarg_T *eap));
157static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000159static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000160static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#else
164# define ex_close ex_ni
165# define ex_only ex_ni
166# define ex_all ex_ni
167# define ex_resize ex_ni
168# define ex_splitview ex_ni
169# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000170# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000172# define ex_tabs ex_ni
173# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000174# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
177static void ex_pclose __ARGS((exarg_T *eap));
178static void ex_ptag __ARGS((exarg_T *eap));
179static void ex_pedit __ARGS((exarg_T *eap));
180#else
181# define ex_pclose ex_ni
182# define ex_ptag ex_ni
183# define ex_pedit ex_ni
184#endif
185static void ex_hide __ARGS((exarg_T *eap));
186static void ex_stop __ARGS((exarg_T *eap));
187static void ex_exit __ARGS((exarg_T *eap));
188static void ex_print __ARGS((exarg_T *eap));
189#ifdef FEAT_BYTEOFF
190static void ex_goto __ARGS((exarg_T *eap));
191#else
192# define ex_goto ex_ni
193#endif
194static void ex_shell __ARGS((exarg_T *eap));
195static void ex_preserve __ARGS((exarg_T *eap));
196static void ex_recover __ARGS((exarg_T *eap));
197#ifndef FEAT_LISTCMDS
198# define ex_argedit ex_ni
199# define ex_argadd ex_ni
200# define ex_argdelete ex_ni
201# define ex_listdo ex_ni
202#endif
203static void ex_mode __ARGS((exarg_T *eap));
204static void ex_wrongmodifier __ARGS((exarg_T *eap));
205static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000206static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static void ex_edit __ARGS((exarg_T *eap));
208#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
209# define ex_drop ex_ni
210#endif
211#ifndef FEAT_GUI
212# define ex_gui ex_nogui
213static void ex_nogui __ARGS((exarg_T *eap));
214#endif
215#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
216static void ex_tearoff __ARGS((exarg_T *eap));
217#else
218# define ex_tearoff ex_ni
219#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000220#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221static void ex_popup __ARGS((exarg_T *eap));
222#else
223# define ex_popup ex_ni
224#endif
225#ifndef FEAT_GUI_MSWIN
226# define ex_simalt ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define gui_mch_find_dialog ex_ni
230# define gui_mch_replace_dialog ex_ni
231#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000232#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define ex_helpfind ex_ni
234#endif
235#ifndef FEAT_CSCOPE
236# define do_cscope ex_ni
237# define do_scscope ex_ni
238# define do_cstag ex_ni
239#endif
240#ifndef FEAT_SYN_HL
241# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200242# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#endif
244#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000245# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000247# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000248# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000249# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200251#ifndef FEAT_PERSISTENT_UNDO
252# define ex_rundo ex_ni
253# define ex_wundo ex_ni
254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200255#ifndef FEAT_LUA
256# define ex_lua ex_script_ni
257# define ex_luado ex_ni
258# define ex_luafile ex_ni
259#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000260#ifndef FEAT_MZSCHEME
261# define ex_mzscheme ex_script_ni
262# define ex_mzfile ex_ni
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_PERL
265# define ex_perl ex_script_ni
266# define ex_perldo ex_ni
267#endif
268#ifndef FEAT_PYTHON
269# define ex_python ex_script_ni
270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274# define ex_py3file ex_ni
275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#ifndef FEAT_TCL
277# define ex_tcl ex_script_ni
278# define ex_tcldo ex_ni
279# define ex_tclfile ex_ni
280#endif
281#ifndef FEAT_RUBY
282# define ex_ruby ex_script_ni
283# define ex_rubydo ex_ni
284# define ex_rubyfile ex_ni
285#endif
286#ifndef FEAT_SNIFF
287# define ex_sniff ex_ni
288#endif
289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
292static void ex_swapname __ARGS((exarg_T *eap));
293static void ex_syncbind __ARGS((exarg_T *eap));
294static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static void ex_pwd __ARGS((exarg_T *eap));
296static void ex_equal __ARGS((exarg_T *eap));
297static void ex_sleep __ARGS((exarg_T *eap));
298static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
299static void ex_winsize __ARGS((exarg_T *eap));
300#ifdef FEAT_WINDOWS
301static void ex_wincmd __ARGS((exarg_T *eap));
302#else
303# define ex_wincmd ex_ni
304#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000305#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_winpos __ARGS((exarg_T *eap));
307#else
308# define ex_winpos ex_ni
309#endif
310static void ex_operators __ARGS((exarg_T *eap));
311static void ex_put __ARGS((exarg_T *eap));
312static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000313static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static void ex_submagic __ARGS((exarg_T *eap));
315static void ex_join __ARGS((exarg_T *eap));
316static void ex_at __ARGS((exarg_T *eap));
317static void ex_bang __ARGS((exarg_T *eap));
318static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200319#ifdef FEAT_PERSISTENT_UNDO
320static void ex_wundo __ARGS((exarg_T *eap));
321static void ex_rundo __ARGS((exarg_T *eap));
322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000324static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static void ex_redir __ARGS((exarg_T *eap));
326static void ex_redraw __ARGS((exarg_T *eap));
327static void ex_redrawstatus __ARGS((exarg_T *eap));
328static void close_redir __ARGS((void));
329static void ex_mkrc __ARGS((exarg_T *eap));
330static void ex_mark __ARGS((exarg_T *eap));
331#ifdef FEAT_USR_CMDS
332static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000333static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#endif
335#ifdef FEAT_EX_EXTRA
336static void ex_normal __ARGS((exarg_T *eap));
337static void ex_startinsert __ARGS((exarg_T *eap));
338static void ex_stopinsert __ARGS((exarg_T *eap));
339#else
340# define ex_normal ex_ni
341# define ex_align ex_ni
342# define ex_retab ex_ni
343# define ex_startinsert ex_ni
344# define ex_stopinsert ex_ni
345# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000346# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
348#ifdef FEAT_FIND_ID
349static void ex_checkpath __ARGS((exarg_T *eap));
350static void ex_findpat __ARGS((exarg_T *eap));
351#else
352# define ex_findpat ex_ni
353# define ex_checkpath ex_ni
354#endif
355#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
356static void ex_psearch __ARGS((exarg_T *eap));
357#else
358# define ex_psearch ex_ni
359#endif
360static void ex_tag __ARGS((exarg_T *eap));
361static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
362#ifndef FEAT_EVAL
363# define ex_scriptnames ex_ni
364# define ex_finish ex_ni
365# define ex_echo ex_ni
366# define ex_echohl ex_ni
367# define ex_execute ex_ni
368# define ex_call ex_ni
369# define ex_if ex_ni
370# define ex_endif ex_ni
371# define ex_else ex_ni
372# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000373# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000377# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378# define ex_throw ex_ni
379# define ex_try ex_ni
380# define ex_catch ex_ni
381# define ex_finally ex_ni
382# define ex_endtry ex_ni
383# define ex_endfunction ex_ni
384# define ex_let ex_ni
385# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000386# define ex_lockvar ex_ni
387# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_function ex_ni
389# define ex_delfunction ex_ni
390# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000391# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393static char_u *arg_all __ARGS((void));
394#ifdef FEAT_SESSION
395static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000396static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void ex_loadview __ARGS((exarg_T *eap));
398static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000399static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#else
401# define ex_loadview ex_ni
402#endif
403#ifndef FEAT_EVAL
404# define ex_compiler ex_ni
405#endif
406#ifdef FEAT_VIMINFO
407static void ex_viminfo __ARGS((exarg_T *eap));
408#else
409# define ex_viminfo ex_ni
410#endif
411static void ex_behave __ARGS((exarg_T *eap));
412#ifdef FEAT_AUTOCMD
413static void ex_filetype __ARGS((exarg_T *eap));
414static void ex_setfiletype __ARGS((exarg_T *eap));
415#else
416# define ex_filetype ex_ni
417# define ex_setfiletype ex_ni
418#endif
419#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000420# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421# define ex_diffpatch ex_ni
422# define ex_diffgetput ex_ni
423# define ex_diffsplit ex_ni
424# define ex_diffthis ex_ni
425# define ex_diffupdate ex_ni
426#endif
427static void ex_digraphs __ARGS((exarg_T *eap));
428static void ex_set __ARGS((exarg_T *eap));
429#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
430# define ex_options ex_ni
431#endif
432#ifdef FEAT_SEARCH_EXTRA
433static void ex_nohlsearch __ARGS((exarg_T *eap));
434static void ex_match __ARGS((exarg_T *eap));
435#else
436# define ex_nohlsearch ex_ni
437# define ex_match ex_ni
438#endif
439#ifdef FEAT_CRYPT
440static void ex_X __ARGS((exarg_T *eap));
441#else
442# define ex_X ex_ni
443#endif
444#ifdef FEAT_FOLDING
445static void ex_fold __ARGS((exarg_T *eap));
446static void ex_foldopen __ARGS((exarg_T *eap));
447static void ex_folddo __ARGS((exarg_T *eap));
448#else
449# define ex_fold ex_ni
450# define ex_foldopen ex_ni
451# define ex_folddo ex_ni
452#endif
453#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
454 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
455# define ex_language ex_ni
456#endif
457#ifndef FEAT_SIGNS
458# define ex_sign ex_ni
459#endif
460#ifndef FEAT_SUN_WORKSHOP
461# define ex_wsverb ex_ni
462#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
469#ifndef FEAT_EVAL
470# define ex_debug ex_ni
471# define ex_breakadd ex_ni
472# define ex_debuggreedy ex_ni
473# define ex_breakdel ex_ni
474# define ex_breaklist ex_ni
475#endif
476
477#ifndef FEAT_CMDHIST
478# define ex_history ex_ni
479#endif
480#ifndef FEAT_JUMPLIST
481# define ex_jumps ex_ni
482# define ex_changes ex_ni
483#endif
484
Bram Moolenaar05159a02005-02-26 23:04:13 +0000485#ifndef FEAT_PROFILE
486# define ex_profile ex_ni
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Declare cmdnames[].
491 */
492#define DO_DECLARE_EXCMD
493#include "ex_cmds.h"
494
495/*
496 * Table used to quickly search for a command, based on its first character.
497 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000498static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 CMD_append,
501 CMD_buffer,
502 CMD_change,
503 CMD_delete,
504 CMD_edit,
505 CMD_file,
506 CMD_global,
507 CMD_help,
508 CMD_insert,
509 CMD_join,
510 CMD_k,
511 CMD_list,
512 CMD_move,
513 CMD_next,
514 CMD_open,
515 CMD_print,
516 CMD_quit,
517 CMD_read,
518 CMD_substitute,
519 CMD_t,
520 CMD_undo,
521 CMD_vglobal,
522 CMD_write,
523 CMD_xit,
524 CMD_yank,
525 CMD_z,
526 CMD_bang
527};
528
529static char_u dollar_command[2] = {'$', 0};
530
531
532#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534typedef struct
535{
536 char_u *line; /* command line */
537 linenr_T lnum; /* sourcing_lnum of the line */
538} wcmd_T;
539
540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 garray_T *lines_gap; /* growarray with line info */
548 int current_line; /* last read line from growarray */
549 int repeating; /* TRUE when looping a second time */
550 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
551 char_u *(*getline) __ARGS((int, void *, int));
552 void *cookie;
553};
554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
556static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000558
559/* Struct to save a few things while debugging. Used in do_cmdline() only. */
560struct dbg_stuff
561{
562 int trylevel;
563 int force_abort;
564 except_T *caught_stack;
565 char_u *vv_exception;
566 char_u *vv_throwpoint;
567 int did_emsg;
568 int got_int;
569 int did_throw;
570 int need_rethrow;
571 int check_cstack;
572 except_T *current_exception;
573};
574
575static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577
578 static void
579save_dbg_stuff(dsp)
580 struct dbg_stuff *dsp;
581{
582 dsp->trylevel = trylevel; trylevel = 0;
583 dsp->force_abort = force_abort; force_abort = FALSE;
584 dsp->caught_stack = caught_stack; caught_stack = NULL;
585 dsp->vv_exception = v_exception(NULL);
586 dsp->vv_throwpoint = v_throwpoint(NULL);
587
588 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
589 dsp->did_emsg = did_emsg; did_emsg = FALSE;
590 dsp->got_int = got_int; got_int = FALSE;
591 dsp->did_throw = did_throw; did_throw = FALSE;
592 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
593 dsp->check_cstack = check_cstack; check_cstack = FALSE;
594 dsp->current_exception = current_exception; current_exception = NULL;
595}
596
597 static void
598restore_dbg_stuff(dsp)
599 struct dbg_stuff *dsp;
600{
601 suppress_errthrow = FALSE;
602 trylevel = dsp->trylevel;
603 force_abort = dsp->force_abort;
604 caught_stack = dsp->caught_stack;
605 (void)v_exception(dsp->vv_exception);
606 (void)v_throwpoint(dsp->vv_throwpoint);
607 did_emsg = dsp->did_emsg;
608 got_int = dsp->got_int;
609 did_throw = dsp->did_throw;
610 need_rethrow = dsp->need_rethrow;
611 check_cstack = dsp->check_cstack;
612 current_exception = dsp->current_exception;
613}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615
616
617/*
618 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
619 * command is given.
620 */
621 void
622do_exmode(improved)
623 int improved; /* TRUE for "improved Ex" mode */
624{
625 int save_msg_scroll;
626 int prev_msg_row;
627 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 int changedtick;
629
630 if (improved)
631 exmode_active = EXMODE_VIM;
632 else
633 exmode_active = EXMODE_NORMAL;
634 State = NORMAL;
635
636 /* When using ":global /pat/ visual" and then "Q" we return to continue
637 * the :global command. */
638 if (global_busy)
639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 save_msg_scroll = msg_scroll;
642 ++RedrawingDisabled; /* don't redisplay the window */
643 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_GUI
645 /* Ignore scrollbar and mouse events in Ex mode */
646 ++hold_gui_events;
647#endif
648#ifdef FEAT_SNIFF
649 want_sniff_request = 0; /* No K_SNIFF wanted */
650#endif
651
652 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
653 while (exmode_active)
654 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000655#ifdef FEAT_EX_EXTRA
656 /* Check for a ":normal" command and no more characters left. */
657 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
658 {
659 exmode_active = FALSE;
660 break;
661 }
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 msg_scroll = TRUE;
664 need_wait_return = FALSE;
665 ex_pressedreturn = FALSE;
666 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 prev_msg_row = msg_row;
669 prev_line = curwin->w_cursor.lnum;
670#ifdef FEAT_SNIFF
671 ProcessSniffRequests();
672#endif
673 if (improved)
674 {
675 cmdline_row = msg_row;
676 do_cmdline(NULL, getexline, NULL, 0);
677 }
678 else
679 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
680 lines_left = Rows - 1;
681
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if ((prev_line != curwin->w_cursor.lnum
683 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (curbuf->b_ml.ml_flags & ML_EMPTY)
686 EMSG(_(e_emptybuf));
687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (ex_pressedreturn)
690 {
691 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000692 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 msg_row = prev_msg_row;
694 if (prev_msg_row == Rows - 1)
695 msg_row--;
696 }
697 msg_col = 0;
698 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
699 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
703 {
704 if (curbuf->b_ml.ml_flags & ML_EMPTY)
705 EMSG(_(e_emptybuf));
706 else
707 EMSG(_("E501: At end-of-file"));
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
711#ifdef FEAT_GUI
712 --hold_gui_events;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --RedrawingDisabled;
715 --no_wait_return;
716 update_screen(CLEAR);
717 need_wait_return = FALSE;
718 msg_scroll = save_msg_scroll;
719}
720
721/*
722 * Execute a simple command line. Used for translated commands like "*".
723 */
724 int
725do_cmdline_cmd(cmd)
726 char_u *cmd;
727{
728 return do_cmdline(cmd, NULL, NULL,
729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
730}
731
732/*
733 * do_cmdline(): execute one Ex command line
734 *
735 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * 2. Split up in parts separated with '|'.
738 *
739 * This function can be called recursively!
740 *
741 * flags:
742 * DOCMD_VERBOSE - The command will be included in the error message.
743 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * DOCMD_KEYTYPED - Don't reset KeyTyped.
746 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
747 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
748 *
749 * return FAIL if cmdline could not be executed, OK otherwise
750 */
751 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 char_u *(*fgetline) __ARGS((int, void *, int));
755 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 static int recursive = 0; /* recursive depth */
762 int msg_didout_before_start = 0;
763 int count = 0; /* line number count */
764 int did_inc = FALSE; /* incremented RedrawingDisabled */
765 int retval = OK;
766#ifdef FEAT_EVAL
767 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 int current_line = 0; /* active line in lines_ga */
770 char_u *fname = NULL; /* function or script name */
771 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
772 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000773 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int initial_trylevel;
775 struct msglist **saved_msg_list = NULL;
776 struct msglist *private_msg_list;
777
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 char_u *(*cmd_getline) __ARGS((int, void *, int));
780 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# define cmd_cookie cookie
787#endif
788 static int call_depth = 0; /* recursiveness */
789
790#ifdef FEAT_EVAL
791 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
792 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000793 * This ensures that the do_errthrow() call in do_one_cmd() does not
794 * combine the messages stored by an earlier invocation of do_one_cmd()
795 * with the command name of the later one. This would happen when
796 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 saved_msg_list = msg_list;
798 msg_list = &private_msg_list;
799 private_msg_list = NULL;
800#endif
801
802 /* It's possible to create an endless loop with ":execute", catch that
803 * here. The value of 200 allows nested function calls, ":source", etc. */
804 if (call_depth == 200)
805 {
806 EMSG(_("E169: Command too recursive"));
807#ifdef FEAT_EVAL
808 /* When converting to an exception, we do not include the command name
809 * since this is not an error of the specific command. */
810 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
811 msg_list = saved_msg_list;
812#endif
813 return FAIL;
814 }
815 ++call_depth;
816
817#ifdef FEAT_EVAL
818 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 cstack.cs_trylevel = 0;
821 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
824
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++ex_nesting_level;
831
832 /* Get the function or script name and the address where the next breakpoint
833 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = func_name(real_cookie);
837 breakpoint = func_breakpoint(real_cookie);
838 dbg_tick = func_dbg_tick(real_cookie);
839 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
842 fname = sourcing_name;
843 breakpoint = source_breakpoint(real_cookie);
844 dbg_tick = source_dbg_tick(real_cookie);
845 }
846
847 /*
848 * Initialize "force_abort" and "suppress_errthrow" at the top level.
849 */
850 if (!recursive)
851 {
852 force_abort = FALSE;
853 suppress_errthrow = FALSE;
854 }
855
856 /*
857 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000858 * exception handling (used when debugging). Otherwise clear it to avoid
859 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000861 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000862 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000863 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200864 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 initial_trylevel = trylevel;
867
868 /*
869 * "did_throw" will be set to TRUE when an exception is being thrown.
870 */
871 did_throw = FALSE;
872#endif
873 /*
874 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * If force_abort is set, we cancel everything.
877 */
878 did_emsg = FALSE;
879
880 /*
881 * KeyTyped is only set when calling vgetc(). Reset it here when not
882 * calling vgetc() (sourced command lines).
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (!(flags & DOCMD_KEYTYPED)
885 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 KeyTyped = FALSE;
887
888 /*
889 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000890 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * - for multiple commands on one line, separated with '|'
892 * - when repeating until there are no more lines (for ":source")
893 */
894 next_cmdline = cmdline;
895 do
896 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100898 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899#endif
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (next_cmdline == NULL
903#ifdef FEAT_EVAL
904 && !force_abort
905 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 )
909 did_emsg = FALSE;
910
911 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * 3. If a line is given: Make a copy, so we can mess with it.
915 */
916
917#ifdef FEAT_EVAL
918 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 /* Each '|' separated command is stored separately in lines_ga, to
922 * be able to jump to it. Don't use next_cmdline now. */
923 vim_free(cmdline_copy);
924 cmdline_copy = NULL;
925
926 /* Check if a function has returned or, unless it has an unclosed
927 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000931 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 func_line_end(real_cookie);
933# endif
934 if (func_has_ended(real_cookie))
935 {
936 retval = FAIL;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 script_line_end();
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
952
953 /* If breakpoints have been added/deleted need to check for it. */
954 if (breakpoint != NULL && dbg_tick != NULL
955 && *dbg_tick != debug_tick)
956 {
957 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
962
963 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
964 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
965
966 /* Did we encounter a breakpoint? */
967 if (breakpoint != NULL && *breakpoint != 0
968 && *breakpoint <= sourcing_lnum)
969 {
970 dbg_breakpoint(fname, sourcing_lnum);
971 /* Find next breakpoint. */
972 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 fname, sourcing_lnum);
975 *dbg_tick = debug_tick;
976 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000978 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000979 {
980 if (getline_is_func)
981 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 script_line_start();
984 }
985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * below, so that it stores lines in or reads them from
993 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 * while/for loop. */
995 cmd_getline = get_loop_line;
996 cmd_cookie = (void *)&cmd_loop_cookie;
997 cmd_loop_cookie.lines_gap = &lines_ga;
998 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 cmd_loop_cookie.cookie = cookie;
1001 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 else
1004 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 cmd_cookie = cookie;
1007 }
1008#endif
1009
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (next_cmdline == NULL)
1012 {
1013 /*
1014 * Need to set msg_didout for the first line after an ":if",
1015 * otherwise the ":if" will be overwritten.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_EVAL
1021 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1022#else
1023 0
1024#endif
1025 )) == NULL)
1026 {
1027 /* Don't call wait_return for aborted command line. The NULL
1028 * returned for the end of a sourced file or executed function
1029 * doesn't do this. */
1030 if (KeyTyped && !(flags & DOCMD_REPEAT))
1031 need_wait_return = FALSE;
1032 retval = FAIL;
1033 break;
1034 }
1035 used_getline = TRUE;
1036
1037 /*
1038 * Keep the first typed line. Clear it when more lines are typed.
1039 */
1040 if (flags & DOCMD_KEEPLINE)
1041 {
1042 vim_free(repeat_cmdline);
1043 if (count == 0)
1044 repeat_cmdline = vim_strsave(next_cmdline);
1045 else
1046 repeat_cmdline = NULL;
1047 }
1048 }
1049
1050 /* 3. Make a copy of the command so we can mess with it. */
1051 else if (cmdline_copy == NULL)
1052 {
1053 next_cmdline = vim_strsave(next_cmdline);
1054 if (next_cmdline == NULL)
1055 {
1056 EMSG(_(e_outofmem));
1057 retval = FAIL;
1058 break;
1059 }
1060 }
1061 cmdline_copy = next_cmdline;
1062
1063#ifdef FEAT_EVAL
1064 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 * Save the current line when inside a ":while" or ":for", and when
1066 * the command looks like a ":while" or ":for", because we may need it
1067 * later. When there is a '|' and another command, it is stored
1068 * separately, because we need to be able to jump back to it from an
1069 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (current_line == lines_ga.ga_len
1072 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 retval = FAIL;
1077 break;
1078 }
1079 }
1080 did_endif = FALSE;
1081#endif
1082
1083 if (count++ == 0)
1084 {
1085 /*
1086 * All output from the commands is put below each other, without
1087 * waiting for a return. Don't do this when executing commands
1088 * from a script or when being called recursive (e.g. for ":e
1089 * +command file").
1090 */
1091 if (!(flags & DOCMD_NOWAIT) && !recursive)
1092 {
1093 msg_didout_before_start = msg_didout;
1094 msg_didany = FALSE; /* no output yet */
1095 msg_start();
1096 msg_scroll = TRUE; /* put messages below each other */
1097 ++no_wait_return; /* dont wait for return until finished */
1098 ++RedrawingDisabled;
1099 did_inc = TRUE;
1100 }
1101 }
1102
1103 if (p_verbose >= 15 && sourcing_name != NULL)
1104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001106 verbose_enter_scroll();
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 smsg((char_u *)_("line %ld: %s"),
1109 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 if (msg_silent == 0)
1111 msg_puts((char_u *)"\n"); /* don't overwrite this */
1112
1113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 --no_wait_return;
1115 }
1116
1117 /*
1118 * 2. Execute one '|' separated command.
1119 * do_one_cmd() will return NULL if there is no trailing '|'.
1120 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1121 */
1122 ++recursive;
1123 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1124#ifdef FEAT_EVAL
1125 &cstack,
1126#endif
1127 cmd_getline, cmd_cookie);
1128 --recursive;
1129
1130#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cmd_cookie == (void *)&cmd_loop_cookie)
1132 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 if (next_cmdline == NULL)
1138 {
1139 vim_free(cmdline_copy);
1140 cmdline_copy = NULL;
1141#ifdef FEAT_CMDHIST
1142 /*
1143 * If the command was typed, remember it for the ':' register.
1144 * Do this AFTER executing the command to make :@: work.
1145 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001146 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 && new_last_cmdline != NULL)
1148 {
1149 vim_free(last_cmdline);
1150 last_cmdline = new_last_cmdline;
1151 new_last_cmdline = NULL;
1152 }
1153#endif
1154 }
1155 else
1156 {
1157 /* need to copy the command after the '|' to cmdline_copy, for the
1158 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001159 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 next_cmdline = cmdline_copy;
1161 }
1162
1163
1164#ifdef FEAT_EVAL
1165 /* reset did_emsg for a function that is not aborted by an error */
1166 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 && !func_has_abort(real_cookie))
1169 did_emsg = FALSE;
1170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 ++current_line;
1174
1175 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 * An ":endwhile", ":endfor" and ":continue" is handled here.
1177 * If we were executing commands, jump back to the ":while" or
1178 * ":for".
1179 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* Jump back to the matching ":while" or ":for". Be careful
1186 * not to use a cs_line[] from an entry that isn't a ":while"
1187 * or ":for": It would make "current_line" invalid and can
1188 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (!did_emsg && !got_int && !did_throw
1190 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 && (cstack.cs_flags[cstack.cs_idx]
1192 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && cstack.cs_line[cstack.cs_idx] >= 0
1194 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1195 {
1196 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 /* remember we jumped there */
1198 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 line_breakcheck(); /* check if CTRL-C typed */
1200
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* Check for the next breakpoint at or after the ":while"
1202 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (breakpoint != NULL)
1204 {
1205 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 fname,
1208 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1209 *dbg_tick = debug_tick;
1210 }
1211 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1217 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220
1221 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1228 }
1229 }
1230
1231 /*
1232 * When not inside any ":while" loop, clear remembered lines.
1233 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (lines_ga.ga_len > 0)
1237 {
1238 sourcing_lnum =
1239 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1240 free_cmdlines(&lines_ga);
1241 }
1242 current_line = 0;
1243 }
1244
1245 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1247 * being restored at the ":endtry". Reset them here and set the
1248 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1249 * This includes the case where a missing ":endif", ":endwhile" or
1250 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 cstack.cs_lflags &= ~CSL_HAD_FINA;
1255 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1256 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 did_throw ? (void *)current_exception : NULL);
1258 did_emsg = got_int = did_throw = FALSE;
1259 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1260 }
1261
1262 /* Update global "trylevel" for recursive calls to do_cmdline() from
1263 * within this loop. */
1264 trylevel = initial_trylevel + cstack.cs_trylevel;
1265
1266 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001267 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * files) is aborted because of an error, an interrupt, or an uncaught
1269 * exception, cancel everything. If it is left normally, reset
1270 * force_abort to get the non-EH compatible abortion behavior for
1271 * the rest of the script.
1272 */
1273 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1274 force_abort = FALSE;
1275
1276 /* Convert an interrupt to an exception if appropriate. */
1277 (void)do_intthrow(&cstack);
1278#endif /* FEAT_EVAL */
1279
1280 }
1281 /*
1282 * Continue executing command lines when:
1283 * - no CTRL-C typed, no aborting error, no exception thrown or try
1284 * conditionals need to be checked for executing finally clauses or
1285 * catching an interrupt exception
1286 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * looping for ":source" command or function call.
1289 */
1290 while (!((got_int
1291#ifdef FEAT_EVAL
1292 || (did_emsg && force_abort) || did_throw
1293#endif
1294 )
1295#ifdef FEAT_EVAL
1296 && cstack.cs_trylevel == 0
1297#endif
1298 )
1299 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001300 && (getline_equal(fgetline, cookie, getexmodeline)
1301 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (next_cmdline != NULL
1303#ifdef FEAT_EVAL
1304 || cstack.cs_idx >= 0
1305#endif
1306 || (flags & DOCMD_REPEAT)));
1307
1308 vim_free(cmdline_copy);
1309#ifdef FEAT_EVAL
1310 free_cmdlines(&lines_ga);
1311 ga_clear(&lines_ga);
1312
1313 if (cstack.cs_idx >= 0)
1314 {
1315 /*
1316 * If a sourced file or executed function ran to its end, report the
1317 * unclosed conditional.
1318 */
1319 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 && ((getline_equal(fgetline, cookie, getsourceline)
1321 && !source_finished(fgetline, cookie))
1322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && !func_has_ended(real_cookie))))
1324 {
1325 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1326 EMSG(_(e_endtry));
1327 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1328 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1330 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 EMSG(_(e_endif));
1333 }
1334
1335 /*
1336 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1337 * ":endtry" in a sourced file or executed function. If the try
1338 * conditional is in its finally clause, ignore anything pending.
1339 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001340 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 {
1344 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1345
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001346 if (idx >= 0)
1347 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1349 &cstack.cs_looplevel);
1350 }
1351 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 trylevel = initial_trylevel;
1353 }
1354
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001355 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1356 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001358 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ? (char_u *)"endfunction" : (char_u *)NULL);
1360
1361 if (trylevel == 0)
1362 {
1363 /*
1364 * When an exception is being thrown out of the outermost try
1365 * conditional, discard the uncaught exception, disable the conversion
1366 * of interrupts or errors to exceptions, and ensure that no more
1367 * commands are executed.
1368 */
1369 if (did_throw)
1370 {
1371 void *p = NULL;
1372 char_u *saved_sourcing_name;
1373 int saved_sourcing_lnum;
1374 struct msglist *messages = NULL, *next;
1375
1376 /*
1377 * If the uncaught exception is a user exception, report it as an
1378 * error. If it is an error exception, display the saved error
1379 * message now. For an interrupt exception, do nothing; the
1380 * interrupt message is given elsewhere.
1381 */
1382 switch (current_exception->type)
1383 {
1384 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001385 vim_snprintf((char *)IObuff, IOSIZE,
1386 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 current_exception->value);
1388 p = vim_strsave(IObuff);
1389 break;
1390 case ET_ERROR:
1391 messages = current_exception->messages;
1392 current_exception->messages = NULL;
1393 break;
1394 case ET_INTERRUPT:
1395 break;
1396 default:
1397 p = vim_strsave((char_u *)_(e_internal));
1398 }
1399
1400 saved_sourcing_name = sourcing_name;
1401 saved_sourcing_lnum = sourcing_lnum;
1402 sourcing_name = current_exception->throw_name;
1403 sourcing_lnum = current_exception->throw_lnum;
1404 current_exception->throw_name = NULL;
1405
1406 discard_current_exception(); /* uses IObuff if 'verbose' */
1407 suppress_errthrow = TRUE;
1408 force_abort = TRUE;
1409
1410 if (messages != NULL)
1411 {
1412 do
1413 {
1414 next = messages->next;
1415 emsg(messages->msg);
1416 vim_free(messages->msg);
1417 vim_free(messages);
1418 messages = next;
1419 }
1420 while (messages != NULL);
1421 }
1422 else if (p != NULL)
1423 {
1424 emsg(p);
1425 vim_free(p);
1426 }
1427 vim_free(sourcing_name);
1428 sourcing_name = saved_sourcing_name;
1429 sourcing_lnum = saved_sourcing_lnum;
1430 }
1431
1432 /*
1433 * On an interrupt or an aborting error not converted to an exception,
1434 * disable the conversion of errors to exceptions. (Interrupts are not
1435 * converted any more, here.) This enables also the interrupt message
1436 * when force_abort is set and did_emsg unset in case of an interrupt
1437 * from a finally clause after an error.
1438 */
1439 else if (got_int || (did_emsg && force_abort))
1440 suppress_errthrow = TRUE;
1441 }
1442
1443 /*
1444 * The current cstack will be freed when do_cmdline() returns. An uncaught
1445 * exception will have to be rethrown in the previous cstack. If a function
1446 * has just returned or a script file was just finished and the previous
1447 * cstack belongs to the same function or, respectively, script file, it
1448 * will have to be checked for finally clauses to be executed due to the
1449 * ":return" or ":finish". This is done in do_one_cmd().
1450 */
1451 if (did_throw)
1452 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001453 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001455 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 && ex_nesting_level > func_level(real_cookie) + 1))
1457 {
1458 if (!did_throw)
1459 check_cstack = TRUE;
1460 }
1461 else
1462 {
1463 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 --ex_nesting_level;
1466 /*
1467 * Go to debug mode when returning from a function in which we are
1468 * single-stepping.
1469 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 if ((getline_equal(fgetline, cookie, getsourceline)
1471 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ? (char_u *)_("End of sourced file")
1475 : (char_u *)_("End of function"));
1476 }
1477
1478 /*
1479 * Restore the exception environment (done after returning from the
1480 * debugger).
1481 */
1482 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001483 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 msg_list = saved_msg_list;
1486#endif /* FEAT_EVAL */
1487
1488 /*
1489 * If there was too much output to fit on the command line, ask the user to
1490 * hit return before redrawing the screen. With the ":global" command we do
1491 * this only once after the command is finished.
1492 */
1493 if (did_inc)
1494 {
1495 --RedrawingDisabled;
1496 --no_wait_return;
1497 msg_scroll = FALSE;
1498
1499 /*
1500 * When just finished an ":if"-":else" which was typed, no need to
1501 * wait for hit-return. Also for an error situation.
1502 */
1503 if (retval == FAIL
1504#ifdef FEAT_EVAL
1505 || (did_endif && KeyTyped && !did_emsg)
1506#endif
1507 )
1508 {
1509 need_wait_return = FALSE;
1510 msg_didany = FALSE; /* don't wait when restarting edit */
1511 }
1512 else if (need_wait_return)
1513 {
1514 /*
1515 * The msg_start() above clears msg_didout. The wait_return we do
1516 * here should not overwrite the command that may be shown before
1517 * doing that.
1518 */
1519 msg_didout |= msg_didout_before_start;
1520 wait_return(FALSE);
1521 }
1522 }
1523
1524#ifndef FEAT_EVAL
1525 /*
1526 * Reset if_level, in case a sourced script file contains more ":if" than
1527 * ":endif" (could be ":if x | foo | endif").
1528 */
1529 if_level = 0;
1530#endif
1531
1532 --call_depth;
1533 return retval;
1534}
1535
1536#ifdef FEAT_EVAL
1537/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int c;
1543 void *cookie;
1544 int indent;
1545{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 wcmd_T *wp;
1548 char_u *line;
1549
1550 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1551 {
1552 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001553 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (cp->getline == NULL)
1557 line = getcmdline(c, 0L, indent);
1558 else
1559 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001560 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 ++cp->current_line;
1562
1563 return line;
1564 }
1565
1566 KeyTyped = FALSE;
1567 ++cp->current_line;
1568 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1569 sourcing_lnum = wp->lnum;
1570 return vim_strsave(wp->line);
1571}
1572
1573/*
1574 * Store a line in "gap" so that a ":while" loop can execute it again.
1575 */
1576 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 garray_T *gap;
1579 char_u *line;
1580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
1593free_cmdlines(gap)
1594 garray_T *gap;
1595{
1596 while (gap->ga_len > 0)
1597 {
1598 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1599 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601}
1602#endif
1603
1604/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001605 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1606 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609getline_equal(fgetline, cookie, func)
1610 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001611 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 char_u *(*func) __ARGS((int, void *, int));
1613{
1614#ifdef FEAT_EVAL
1615 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001619 * function that's originally used to obtain the lines. This may be
1620 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 cp = (struct loop_cookie *)cookie;
1623 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 gp = cp->getline;
1626 cp = cp->cookie;
1627 }
1628 return gp == func;
1629#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#endif
1632}
1633
1634#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1635/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * getline function. Otherwise return "cookie".
1638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001641 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644# ifdef FEAT_EVAL
1645 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001646 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar89d40322006-08-29 15:30:07 +00001648 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001649 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001652 cp = (struct loop_cookie *)cookie;
1653 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655 gp = cp->getline;
1656 cp = cp->cookie;
1657 }
1658 return cp;
1659# else
1660 return cookie;
1661# endif
1662}
1663#endif
1664
1665/*
1666 * Execute one Ex command.
1667 *
1668 * If 'sourcing' is TRUE, the command will be included in the error message.
1669 *
1670 * 1. skip comment lines and leading space
1671 * 2. handle command modifiers
1672 * 3. parse range
1673 * 4. parse command
1674 * 5. parse arguments
1675 * 6. switch on command name
1676 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
1679 * This function may be called recursively!
1680 */
1681#if (_MSC_VER == 1200)
1682/*
Bram Moolenaared203462004-06-16 11:19:22 +00001683 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001685 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687 static char_u *
1688do_one_cmd(cmdlinep, sourcing,
1689#ifdef FEAT_EVAL
1690 cstack,
1691#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001692 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 char_u **cmdlinep;
1694 int sourcing;
1695#ifdef FEAT_EVAL
1696 struct condstack *cstack;
1697#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001698 char_u *(*fgetline) __ARGS((int, void *, int));
1699 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 char_u *p;
1702 linenr_T lnum;
1703 long n;
1704 char_u *errormsg = NULL; /* error message */
1705 exarg_T ea; /* Ex command arguments */
1706 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001707 int save_msg_scroll = msg_scroll;
1708 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001710#ifdef HAVE_SANDBOX
1711 int did_sandbox = FALSE;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 cmdmod_T save_cmdmod;
1714 int ni; /* set when Not Implemented */
1715
1716 vim_memset(&ea, 0, sizeof(ea));
1717 ea.line1 = 1;
1718 ea.line2 = 1;
1719#ifdef FEAT_EVAL
1720 ++ex_nesting_level;
1721#endif
1722
1723 /* when not editing the last file :q has to be typed twice */
1724 if (quitmore
1725#ifdef FEAT_EVAL
1726 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001727 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
1729 )
1730 --quitmore;
1731
1732 /*
1733 * Reset browse, confirm, etc.. They are restored when returning, for
1734 * recursive calls.
1735 */
1736 save_cmdmod = cmdmod;
1737 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1738
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001739 /* "#!anything" is handled like a comment. */
1740 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1741 goto doend;
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /*
1744 * Repeat until no more command modifiers are found.
1745 */
1746 ea.cmd = *cmdlinep;
1747 for (;;)
1748 {
1749/*
1750 * 1. skip comment lines and leading white space and colons
1751 */
1752 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1753 ++ea.cmd;
1754
1755 /* in ex mode, an empty line works like :+ */
1756 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 && (getline_equal(fgetline, cookie, getexmodeline)
1758 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001759 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
1761 ea.cmd = (char_u *)"+";
1762 ex_pressedreturn = TRUE;
1763 }
1764
1765 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001766 if (*ea.cmd == '"')
1767 goto doend;
1768 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001769 {
1770 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774/*
1775 * 2. handle command modifiers.
1776 */
1777 p = ea.cmd;
1778 if (VIM_ISDIGIT(*ea.cmd))
1779 p = skipwhite(skipdigits(ea.cmd));
1780 switch (*p)
1781 {
1782 /* When adding an entry, also modify cmd_exists(). */
1783 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1784 break;
1785#ifdef FEAT_WINDOWS
1786 cmdmod.split |= WSP_ABOVE;
1787#endif
1788 continue;
1789
1790 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1791 {
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_BELOW;
1794#endif
1795 continue;
1796 }
1797 if (checkforcmd(&ea.cmd, "browse", 3))
1798 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001799#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 cmdmod.browse = TRUE;
1801#endif
1802 continue;
1803 }
1804 if (!checkforcmd(&ea.cmd, "botright", 2))
1805 break;
1806#ifdef FEAT_WINDOWS
1807 cmdmod.split |= WSP_BOT;
1808#endif
1809 continue;
1810
1811 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1812 break;
1813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1814 cmdmod.confirm = TRUE;
1815#endif
1816 continue;
1817
1818 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1819 {
1820 cmdmod.keepmarks = TRUE;
1821 continue;
1822 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001823 if (checkforcmd(&ea.cmd, "keepalt", 5))
1824 {
1825 cmdmod.keepalt = TRUE;
1826 continue;
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1829 break;
1830 cmdmod.keepjumps = TRUE;
1831 continue;
1832
1833 /* ":hide" and ":hide | cmd" are not modifiers */
1834 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1835 || *p == NUL || ends_excmd(*p))
1836 break;
1837 ea.cmd = p;
1838 cmdmod.hide = TRUE;
1839 continue;
1840
1841 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1842 {
1843 cmdmod.lockmarks = TRUE;
1844 continue;
1845 }
1846
1847 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1848 break;
1849#ifdef FEAT_WINDOWS
1850 cmdmod.split |= WSP_ABOVE;
1851#endif
1852 continue;
1853
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001854 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1855 break;
1856#ifdef FEAT_AUTOCMD
1857 if (cmdmod.save_ei == NULL)
1858 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001859 /* Set 'eventignore' to "all". Restore the
1860 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001861 cmdmod.save_ei = vim_strsave(p_ei);
1862 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001863 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001864 }
1865#endif
1866 continue;
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1869 break;
1870#ifdef FEAT_WINDOWS
1871 cmdmod.split |= WSP_BELOW;
1872#endif
1873 continue;
1874
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001875 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1876 {
1877#ifdef HAVE_SANDBOX
1878 if (!did_sandbox)
1879 ++sandbox;
1880 did_sandbox = TRUE;
1881#endif
1882 continue;
1883 }
1884 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001886 if (save_msg_silent == -1)
1887 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1890 {
1891 /* ":silent!", but not "silent !cmd" */
1892 ea.cmd = skipwhite(ea.cmd + 1);
1893 ++emsg_silent;
1894 ++did_esilent;
1895 }
1896 continue;
1897
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001898 case 't': if (checkforcmd(&p, "tab", 3))
1899 {
1900#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001901 if (vim_isdigit(*ea.cmd))
1902 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1903 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001904 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001905 ea.cmd = p;
1906#endif
1907 continue;
1908 }
1909 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 break;
1911#ifdef FEAT_WINDOWS
1912 cmdmod.split |= WSP_TOP;
1913#endif
1914 continue;
1915
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001916 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1917 break;
1918 if (save_msg_silent == -1)
1919 save_msg_silent = msg_silent;
1920 msg_silent = 0;
1921 continue;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1924 {
1925#ifdef FEAT_VERTSPLIT
1926 cmdmod.split |= WSP_VERT;
1927#endif
1928 continue;
1929 }
1930 if (!checkforcmd(&p, "verbose", 4))
1931 break;
1932 if (verbose_save < 0)
1933 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001934 if (vim_isdigit(*ea.cmd))
1935 p_verbose = atoi((char *)ea.cmd);
1936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 p_verbose = 1;
1938 ea.cmd = p;
1939 continue;
1940 }
1941 break;
1942 }
1943
1944#ifdef FEAT_EVAL
1945 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1946 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1947#else
1948 ea.skip = (if_level > 0);
1949#endif
1950
1951#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001952# ifdef FEAT_PROFILE
1953 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001954 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001955 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001956 if (getline_equal(fgetline, cookie, get_func_line))
1957 func_line_exec(getline_cookie(fgetline, cookie));
1958 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 script_line_exec();
1960 }
1961#endif
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* May go to debug mode. If this happens and the ">quit" debug command is
1964 * used, throw an interrupt exception and skip the next command. */
1965 dbg_check_breakpoint(&ea);
1966 if (!ea.skip && got_int)
1967 {
1968 ea.skip = TRUE;
1969 (void)do_intthrow(cstack);
1970 }
1971#endif
1972
1973/*
1974 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1975 *
1976 * where 'addr' is:
1977 *
1978 * % (entire file)
1979 * $ [+-NUM]
1980 * 'x [+-NUM] (where x denotes a currently defined mark)
1981 * . [+-NUM]
1982 * [+-NUM]..
1983 * NUM
1984 *
1985 * The ea.cmd pointer is updated to point to the first character following the
1986 * range spec. If an initial address is found, but no second, the upper bound
1987 * is equal to the lower.
1988 */
1989
1990 /* repeat for all ',' or ';' separated addresses */
1991 for (;;)
1992 {
1993 ea.line1 = ea.line2;
1994 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1995 ea.cmd = skipwhite(ea.cmd);
1996 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1997 if (ea.cmd == NULL) /* error detected */
1998 goto doend;
1999 if (lnum == MAXLNUM)
2000 {
2001 if (*ea.cmd == '%') /* '%' - all lines */
2002 {
2003 ++ea.cmd;
2004 ea.line1 = 1;
2005 ea.line2 = curbuf->b_ml.ml_line_count;
2006 ++ea.addr_count;
2007 }
2008 /* '*' - visual area */
2009 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2010 {
2011 pos_T *fp;
2012
2013 ++ea.cmd;
2014 if (!ea.skip)
2015 {
2016 fp = getmark('<', FALSE);
2017 if (check_mark(fp) == FAIL)
2018 goto doend;
2019 ea.line1 = fp->lnum;
2020 fp = getmark('>', FALSE);
2021 if (check_mark(fp) == FAIL)
2022 goto doend;
2023 ea.line2 = fp->lnum;
2024 ++ea.addr_count;
2025 }
2026 }
2027 }
2028 else
2029 ea.line2 = lnum;
2030 ea.addr_count++;
2031
2032 if (*ea.cmd == ';')
2033 {
2034 if (!ea.skip)
2035 curwin->w_cursor.lnum = ea.line2;
2036 }
2037 else if (*ea.cmd != ',')
2038 break;
2039 ++ea.cmd;
2040 }
2041
2042 /* One address given: set start and end lines */
2043 if (ea.addr_count == 1)
2044 {
2045 ea.line1 = ea.line2;
2046 /* ... but only implicit: really no address given */
2047 if (lnum == MAXLNUM)
2048 ea.addr_count = 0;
2049 }
2050
2051 /* Don't leave the cursor on an illegal line (caused by ';') */
2052 check_cursor_lnum();
2053
2054/*
2055 * 4. parse command
2056 */
2057
2058 /*
2059 * Skip ':' and any white space
2060 */
2061 ea.cmd = skipwhite(ea.cmd);
2062 while (*ea.cmd == ':')
2063 ea.cmd = skipwhite(ea.cmd + 1);
2064
2065 /*
2066 * If we got a line, but no command, then go to the line.
2067 * If we find a '|' or '\n' we set ea.nextcmd.
2068 */
2069 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2070 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2071 {
2072 /*
2073 * strange vi behaviour:
2074 * ":3" jumps to line 3
2075 * ":3|..." prints line 3
2076 * ":|" prints current line
2077 */
2078 if (ea.skip) /* skip this if inside :if */
2079 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002080 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 ea.cmdidx = CMD_print;
2083 ea.argt = RANGE+COUNT+TRLBAR;
2084 if ((errormsg = invalid_range(&ea)) == NULL)
2085 {
2086 correct_range(&ea);
2087 ex_print(&ea);
2088 }
2089 }
2090 else if (ea.addr_count != 0)
2091 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002092 if (ea.line2 > curbuf->b_ml.ml_line_count)
2093 {
2094 /* With '-' in 'cpoptions' a line number past the file is an
2095 * error, otherwise put it at the end of the file. */
2096 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2097 ea.line2 = -1;
2098 else
2099 ea.line2 = curbuf->b_ml.ml_line_count;
2100 }
2101
2102 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002103 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 else
2105 {
2106 if (ea.line2 == 0)
2107 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
2109 curwin->w_cursor.lnum = ea.line2;
2110 beginline(BL_SOL | BL_FIX);
2111 }
2112 }
2113 goto doend;
2114 }
2115
2116 /* Find the command and let "p" point to after it. */
2117 p = find_command(&ea, NULL);
2118
2119#ifdef FEAT_USR_CMDS
2120 if (p == NULL)
2121 {
2122 if (!ea.skip)
2123 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2124 goto doend;
2125 }
2126 /* Check for wrong commands. */
2127 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2128 {
2129 errormsg = uc_fun_cmd();
2130 goto doend;
2131 }
2132#endif
2133 if (ea.cmdidx == CMD_SIZE)
2134 {
2135 if (!ea.skip)
2136 {
2137 STRCPY(IObuff, _("E492: Not an editor command"));
2138 if (!sourcing)
2139 {
2140 STRCAT(IObuff, ": ");
2141 STRNCAT(IObuff, *cmdlinep, 40);
2142 }
2143 errormsg = IObuff;
2144 }
2145 goto doend;
2146 }
2147
2148 ni = (
2149#ifdef FEAT_USR_CMDS
2150 !USER_CMDIDX(ea.cmdidx) &&
2151#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002152 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002153#ifdef HAVE_EX_SCRIPT_NI
2154 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2155#endif
2156 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158#ifndef FEAT_EVAL
2159 /*
2160 * When the expression evaluation is disabled, recognize the ":if" and
2161 * ":endif" commands and ignore everything in between it.
2162 */
2163 if (ea.cmdidx == CMD_if)
2164 ++if_level;
2165 if (if_level)
2166 {
2167 if (ea.cmdidx == CMD_endif)
2168 --if_level;
2169 goto doend;
2170 }
2171
2172#endif
2173
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002174 /* forced commands */
2175 if (*p == '!' && ea.cmdidx != CMD_substitute
2176 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 ++p;
2179 ea.forceit = TRUE;
2180 }
2181 else
2182 ea.forceit = FALSE;
2183
2184/*
2185 * 5. parse arguments
2186 */
2187#ifdef FEAT_USR_CMDS
2188 if (!USER_CMDIDX(ea.cmdidx))
2189#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002190 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 if (!ea.skip)
2193 {
2194#ifdef HAVE_SANDBOX
2195 if (sandbox != 0 && !(ea.argt & SBOXOK))
2196 {
2197 /* Command not allowed in sandbox. */
2198 errormsg = (char_u *)_(e_sandbox);
2199 goto doend;
2200 }
2201#endif
2202 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2203 {
2204 /* Command not allowed in non-'modifiable' buffer */
2205 errormsg = (char_u *)_(e_modifiable);
2206 goto doend;
2207 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002208
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002209 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# ifdef FEAT_USR_CMDS
2211 && !USER_CMDIDX(ea.cmdidx)
2212# endif
2213 )
2214 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002215 /* Command not allowed when editing the command line. */
2216#ifdef FEAT_CMDWIN
2217 if (cmdwin_type != 0)
2218 errormsg = (char_u *)_(e_cmdwin);
2219 else
2220#endif
2221 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto doend;
2223 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002225 /* Disallow editing another buffer when "curbuf_lock" is set.
2226 * Do allow ":edit" (check for argument later).
2227 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002228 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002229 && ea.cmdidx != CMD_edit
2230 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231# ifdef FEAT_USR_CMDS
2232 && !USER_CMDIDX(ea.cmdidx)
2233# endif
2234 && curbuf_locked())
2235 goto doend;
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2239 {
2240 /* no range allowed */
2241 errormsg = (char_u *)_(e_norange);
2242 goto doend;
2243 }
2244 }
2245
2246 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2247 {
2248 errormsg = (char_u *)_(e_nobang);
2249 goto doend;
2250 }
2251
2252 /*
2253 * Don't complain about the range if it is not used
2254 * (could happen if line_count is accidentally set to 0).
2255 */
2256 if (!ea.skip && !ni)
2257 {
2258 /*
2259 * If the range is backwards, ask for confirmation and, if given, swap
2260 * ea.line1 & ea.line2 so it's forwards again.
2261 * When global command is busy, don't ask, will fail below.
2262 */
2263 if (!global_busy && ea.line1 > ea.line2)
2264 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002265 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002267 if (sourcing || exmode_active)
2268 {
2269 errormsg = (char_u *)_("E493: Backwards range given");
2270 goto doend;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (ask_yesno((char_u *)
2273 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002274 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 lnum = ea.line1;
2277 ea.line1 = ea.line2;
2278 ea.line2 = lnum;
2279 }
2280 if ((errormsg = invalid_range(&ea)) != NULL)
2281 goto doend;
2282 }
2283
2284 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2285 ea.line2 = 1;
2286
2287 correct_range(&ea);
2288
2289#ifdef FEAT_FOLDING
2290 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2291 {
2292 /* Put the first line at the start of a closed fold, put the last line
2293 * at the end of a closed fold. */
2294 (void)hasFolding(ea.line1, &ea.line1, NULL);
2295 (void)hasFolding(ea.line2, NULL, &ea.line2);
2296 }
2297#endif
2298
2299#ifdef FEAT_QUICKFIX
2300 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002301 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * option here, so things like % get expanded.
2303 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002304 p = replace_makeprg(&ea, p, cmdlinep);
2305 if (p == NULL)
2306 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308
2309 /*
2310 * Skip to start of argument.
2311 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2312 */
2313 if (ea.cmdidx == CMD_bang)
2314 ea.arg = p;
2315 else
2316 ea.arg = skipwhite(p);
2317
2318 /*
2319 * Check for "++opt=val" argument.
2320 * Must be first, allow ":w ++enc=utf8 !cmd"
2321 */
2322 if (ea.argt & ARGOPT)
2323 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2324 if (getargopt(&ea) == FAIL && !ni)
2325 {
2326 errormsg = (char_u *)_(e_invarg);
2327 goto doend;
2328 }
2329
2330 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2331 {
2332 if (*ea.arg == '>') /* append */
2333 {
2334 if (*++ea.arg != '>') /* typed wrong */
2335 {
2336 errormsg = (char_u *)_("E494: Use w or w>>");
2337 goto doend;
2338 }
2339 ea.arg = skipwhite(ea.arg + 1);
2340 ea.append = TRUE;
2341 }
2342 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2343 {
2344 ++ea.arg;
2345 ea.usefilter = TRUE;
2346 }
2347 }
2348
2349 if (ea.cmdidx == CMD_read)
2350 {
2351 if (ea.forceit)
2352 {
2353 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2354 ea.forceit = FALSE;
2355 }
2356 else if (*ea.arg == '!') /* :r !filter */
2357 {
2358 ++ea.arg;
2359 ea.usefilter = TRUE;
2360 }
2361 }
2362
2363 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2364 {
2365 ea.amount = 1;
2366 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2367 {
2368 ++ea.arg;
2369 ++ea.amount;
2370 }
2371 ea.arg = skipwhite(ea.arg);
2372 }
2373
2374 /*
2375 * Check for "+command" argument, before checking for next command.
2376 * Don't do this for ":read !cmd" and ":write !cmd".
2377 */
2378 if ((ea.argt & EDITCMD) && !ea.usefilter)
2379 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2380
2381 /*
2382 * Check for '|' to separate commands and '"' to start comments.
2383 * Don't do this for ":read !cmd" and ":write !cmd".
2384 */
2385 if ((ea.argt & TRLBAR) && !ea.usefilter)
2386 separate_nextcmd(&ea);
2387
2388 /*
2389 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2391 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 else if (ea.cmdidx == CMD_bang
2394 || ea.cmdidx == CMD_global
2395 || ea.cmdidx == CMD_vglobal
2396 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 for (p = ea.arg; *p; ++p)
2399 {
2400 /* Remove one backslash before a newline, so that it's possible to
2401 * pass a newline to the shell and also a newline that is preceded
2402 * with a backslash. This makes it impossible to end a shell
2403 * command in a backslash, but that doesn't appear useful.
2404 * Halving the number of backslashes is incompatible with previous
2405 * versions. */
2406 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002407 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 else if (*p == '\n')
2409 {
2410 ea.nextcmd = p + 1;
2411 *p = NUL;
2412 break;
2413 }
2414 }
2415 }
2416
2417 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2418 {
2419 ea.line1 = 1;
2420 ea.line2 = curbuf->b_ml.ml_line_count;
2421 }
2422
2423 /* accept numbered register only when no count allowed (:put) */
2424 if ( (ea.argt & REGSTR)
2425 && *ea.arg != NUL
2426#ifdef FEAT_USR_CMDS
2427 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2428 && USER_CMDIDX(ea.cmdidx)))
2429 /* Do not allow register = for user commands */
2430 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2431#else
2432 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2433#endif
2434 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2435 {
2436 ea.regname = *ea.arg++;
2437#ifdef FEAT_EVAL
2438 /* for '=' register: accept the rest of the line as an expression */
2439 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2440 {
2441 set_expr_line(vim_strsave(ea.arg));
2442 ea.arg += STRLEN(ea.arg);
2443 }
2444#endif
2445 ea.arg = skipwhite(ea.arg);
2446 }
2447
2448 /*
2449 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2450 * count, it's a buffer name.
2451 */
2452 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2453 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2454 || vim_iswhite(*p)))
2455 {
2456 n = getdigits(&ea.arg);
2457 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002458 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 errormsg = (char_u *)_(e_zerocount);
2461 goto doend;
2462 }
2463 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2464 {
2465 ea.line2 = n;
2466 if (ea.addr_count == 0)
2467 ea.addr_count = 1;
2468 }
2469 else
2470 {
2471 ea.line1 = ea.line2;
2472 ea.line2 += n - 1;
2473 ++ea.addr_count;
2474 /*
2475 * Be vi compatible: no error message for out of range.
2476 */
2477 if (ea.line2 > curbuf->b_ml.ml_line_count)
2478 ea.line2 = curbuf->b_ml.ml_line_count;
2479 }
2480 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002481
2482 /*
2483 * Check for flags: 'l', 'p' and '#'.
2484 */
2485 if (ea.argt & EXFLAGS)
2486 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002488 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002489 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 errormsg = (char_u *)_(e_trailing);
2492 goto doend;
2493 }
2494
2495 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2496 {
2497 errormsg = (char_u *)_(e_argreq);
2498 goto doend;
2499 }
2500
2501#ifdef FEAT_EVAL
2502 /*
2503 * Skip the command when it's not going to be executed.
2504 * The commands like :if, :endif, etc. always need to be executed.
2505 * Also make an exception for commands that handle a trailing command
2506 * themselves.
2507 */
2508 if (ea.skip)
2509 {
2510 switch (ea.cmdidx)
2511 {
2512 /* commands that need evaluation */
2513 case CMD_while:
2514 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002515 case CMD_for:
2516 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_if:
2518 case CMD_elseif:
2519 case CMD_else:
2520 case CMD_endif:
2521 case CMD_try:
2522 case CMD_catch:
2523 case CMD_finally:
2524 case CMD_endtry:
2525 case CMD_function:
2526 break;
2527
2528 /* Commands that handle '|' themselves. Check: A command should
2529 * either have the TRLBAR flag, appear in this list or appear in
2530 * the list at ":help :bar". */
2531 case CMD_aboveleft:
2532 case CMD_and:
2533 case CMD_belowright:
2534 case CMD_botright:
2535 case CMD_browse:
2536 case CMD_call:
2537 case CMD_confirm:
2538 case CMD_delfunction:
2539 case CMD_djump:
2540 case CMD_dlist:
2541 case CMD_dsearch:
2542 case CMD_dsplit:
2543 case CMD_echo:
2544 case CMD_echoerr:
2545 case CMD_echomsg:
2546 case CMD_echon:
2547 case CMD_execute:
2548 case CMD_help:
2549 case CMD_hide:
2550 case CMD_ijump:
2551 case CMD_ilist:
2552 case CMD_isearch:
2553 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002554 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 case CMD_keepjumps:
2556 case CMD_keepmarks:
2557 case CMD_leftabove:
2558 case CMD_let:
2559 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002560 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002562 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 case CMD_perl:
2564 case CMD_psearch:
2565 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002566 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002567 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 case CMD_return:
2569 case CMD_rightbelow:
2570 case CMD_ruby:
2571 case CMD_silent:
2572 case CMD_smagic:
2573 case CMD_snomagic:
2574 case CMD_substitute:
2575 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002576 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 case CMD_tcl:
2578 case CMD_throw:
2579 case CMD_tilde:
2580 case CMD_topleft:
2581 case CMD_unlet:
2582 case CMD_verbose:
2583 case CMD_vertical:
2584 break;
2585
2586 default: goto doend;
2587 }
2588 }
2589#endif
2590
2591 if (ea.argt & XFILE)
2592 {
2593 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2594 goto doend;
2595 }
2596
2597#ifdef FEAT_LISTCMDS
2598 /*
2599 * Accept buffer name. Cannot be used at the same time with a buffer
2600 * number. Don't do this for a user command.
2601 */
2602 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2603# ifdef FEAT_USR_CMDS
2604 && !USER_CMDIDX(ea.cmdidx)
2605# endif
2606 )
2607 {
2608 /*
2609 * :bdelete, :bwipeout and :bunload take several arguments, separated
2610 * by spaces: find next space (skipping over escaped characters).
2611 * The others take one argument: ignore trailing spaces.
2612 */
2613 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2614 || ea.cmdidx == CMD_bunload)
2615 p = skiptowhite_esc(ea.arg);
2616 else
2617 {
2618 p = ea.arg + STRLEN(ea.arg);
2619 while (p > ea.arg && vim_iswhite(p[-1]))
2620 --p;
2621 }
2622 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2623 if (ea.line2 < 0) /* failed */
2624 goto doend;
2625 ea.addr_count = 1;
2626 ea.arg = skipwhite(p);
2627 }
2628#endif
2629
2630/*
2631 * 6. switch on command name
2632 *
2633 * The "ea" structure holds the arguments that can be used.
2634 */
2635 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002636 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ea.cookie = cookie;
2638#ifdef FEAT_EVAL
2639 ea.cstack = cstack;
2640#endif
2641
2642#ifdef FEAT_USR_CMDS
2643 if (USER_CMDIDX(ea.cmdidx))
2644 {
2645 /*
2646 * Execute a user-defined command.
2647 */
2648 do_ucmd(&ea);
2649 }
2650 else
2651#endif
2652 {
2653 /*
2654 * Call the function to execute the command.
2655 */
2656 ea.errmsg = NULL;
2657 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2658 if (ea.errmsg != NULL)
2659 errormsg = (char_u *)_(ea.errmsg);
2660 }
2661
2662#ifdef FEAT_EVAL
2663 /*
2664 * If the command just executed called do_cmdline(), any throw or ":return"
2665 * or ":finish" encountered there must also check the cstack of the still
2666 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2667 * exception, or reanimate a returned function or finished script file and
2668 * return or finish it again.
2669 */
2670 if (need_rethrow)
2671 do_throw(cstack);
2672 else if (check_cstack)
2673 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002674 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002676 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 && current_func_returned())
2678 do_return(&ea, TRUE, FALSE, NULL);
2679 }
2680 need_rethrow = check_cstack = FALSE;
2681#endif
2682
2683doend:
2684 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2685 curwin->w_cursor.lnum = 1;
2686
2687 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2688 {
2689 if (sourcing)
2690 {
2691 if (errormsg != IObuff)
2692 {
2693 STRCPY(IObuff, errormsg);
2694 errormsg = IObuff;
2695 }
2696 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002697 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 emsg(errormsg);
2700 }
2701#ifdef FEAT_EVAL
2702 do_errthrow(cstack,
2703 (ea.cmdidx != CMD_SIZE
2704# ifdef FEAT_USR_CMDS
2705 && !USER_CMDIDX(ea.cmdidx)
2706# endif
2707 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2708#endif
2709
2710 if (verbose_save >= 0)
2711 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002712#ifdef FEAT_AUTOCMD
2713 if (cmdmod.save_ei != NULL)
2714 {
2715 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002716 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2717 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002718 free_string_option(cmdmod.save_ei);
2719 }
2720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 cmdmod = save_cmdmod;
2723
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002724 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 /* messages could be enabled for a serious error, need to check if the
2727 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002728 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002729 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 emsg_silent -= did_esilent;
2731 if (emsg_silent < 0)
2732 emsg_silent = 0;
2733 /* Restore msg_scroll, it's set by file I/O commands, even when no
2734 * message is actually displayed. */
2735 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002736
2737 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2738 * somewhere in the line. Put it back in the first column. */
2739 if (redirecting())
2740 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002743#ifdef HAVE_SANDBOX
2744 if (did_sandbox)
2745 --sandbox;
2746#endif
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2749 ea.nextcmd = NULL;
2750
2751#ifdef FEAT_EVAL
2752 --ex_nesting_level;
2753#endif
2754
2755 return ea.nextcmd;
2756}
2757#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002758 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#endif
2760
2761/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002762 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * If there is a match advance "pp" to the argument and return TRUE.
2764 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002765 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002767 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 char *cmd; /* name of command */
2769 int len; /* required length */
2770{
2771 int i;
2772
2773 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002774 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 break;
2776 if (i >= len && !isalpha((*pp)[i]))
2777 {
2778 *pp = skipwhite(*pp + i);
2779 return TRUE;
2780 }
2781 return FALSE;
2782}
2783
2784/*
2785 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002786 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002788 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * Returns NULL for an ambiguous user command.
2790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 static char_u *
2792find_command(eap, full)
2793 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002794 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 int len;
2797 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002798 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /*
2801 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002802 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * - the 'k' command can directly be followed by any character.
2804 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2805 * but :sre[wind] is another command, as are :scrip[tnames],
2806 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002807 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 p = eap->cmd;
2810 if (*p == 'k')
2811 {
2812 eap->cmdidx = CMD_k;
2813 ++p;
2814 }
2815 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002816 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2817 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 || p[1] == 'g'
2819 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2820 || p[1] == 'I'
2821 || (p[1] == 'r' && p[2] != 'e')))
2822 {
2823 eap->cmdidx = CMD_substitute;
2824 ++p;
2825 }
2826 else
2827 {
2828 while (ASCII_ISALPHA(*p))
2829 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002830 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002831 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002832 while (ASCII_ISALNUM(*p))
2833 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /* check for non-alpha command */
2836 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2837 ++p;
2838 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002839 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2840 {
2841 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2842 * :delete with the 'l' flag. Same for 'p'. */
2843 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002844 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002845 break;
2846 if (i == len - 1)
2847 {
2848 --len;
2849 if (p[-1] == 'l')
2850 eap->flags |= EXFLAG_LIST;
2851 else
2852 eap->flags |= EXFLAG_PRINT;
2853 }
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 if (ASCII_ISLOWER(*eap->cmd))
2857 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2858 else
2859 eap->cmdidx = cmdidxs[26];
2860
2861 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2862 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2863 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2864 (size_t)len) == 0)
2865 {
2866#ifdef FEAT_EVAL
2867 if (full != NULL
2868 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2869 *full = TRUE;
2870#endif
2871 break;
2872 }
2873
2874#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002875 /* Look for a user defined command as a last resort. Let ":Print" be
2876 * overruled by a user defined command. */
2877 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2878 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002880 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 while (ASCII_ISALNUM(*p))
2882 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002886 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 eap->cmdidx = CMD_SIZE;
2888 }
2889
2890 return p;
2891}
2892
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002893#ifdef FEAT_USR_CMDS
2894/*
2895 * Search for a user command that matches "eap->cmd".
2896 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2897 * Return a pointer to just after the command.
2898 * Return NULL if there is no matching command.
2899 */
2900 static char_u *
2901find_ucmd(eap, p, full, xp, compl)
2902 exarg_T *eap;
2903 char_u *p; /* end of the command (possibly including count) */
2904 int *full; /* set to TRUE for a full match */
2905 expand_T *xp; /* used for completion, NULL otherwise */
2906 int *compl; /* completion flags or NULL */
2907{
2908 int len = (int)(p - eap->cmd);
2909 int j, k, matchlen = 0;
2910 ucmd_T *uc;
2911 int found = FALSE;
2912 int possible = FALSE;
2913 char_u *cp, *np; /* Point into typed cmd and test name */
2914 garray_T *gap;
2915 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2916 only full match global is accepted. */
2917
2918 /*
2919 * Look for buffer-local user commands first, then global ones.
2920 */
2921 gap = &curbuf->b_ucmds;
2922 for (;;)
2923 {
2924 for (j = 0; j < gap->ga_len; ++j)
2925 {
2926 uc = USER_CMD_GA(gap, j);
2927 cp = eap->cmd;
2928 np = uc->uc_name;
2929 k = 0;
2930 while (k < len && *np != NUL && *cp++ == *np++)
2931 k++;
2932 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2933 {
2934 /* If finding a second match, the command is ambiguous. But
2935 * not if a buffer-local command wasn't a full match and a
2936 * global command is a full match. */
2937 if (k == len && found && *np != NUL)
2938 {
2939 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002940 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002941 amb_local = TRUE;
2942 }
2943
2944 if (!found || (k == len && *np == NUL))
2945 {
2946 /* If we matched up to a digit, then there could
2947 * be another command including the digit that we
2948 * should use instead.
2949 */
2950 if (k == len)
2951 found = TRUE;
2952 else
2953 possible = TRUE;
2954
2955 if (gap == &ucmds)
2956 eap->cmdidx = CMD_USER;
2957 else
2958 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002959 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002960 eap->useridx = j;
2961
2962# ifdef FEAT_CMDL_COMPL
2963 if (compl != NULL)
2964 *compl = uc->uc_compl;
2965# ifdef FEAT_EVAL
2966 if (xp != NULL)
2967 {
2968 xp->xp_arg = uc->uc_compl_arg;
2969 xp->xp_scriptID = uc->uc_scriptID;
2970 }
2971# endif
2972# endif
2973 /* Do not search for further abbreviations
2974 * if this is an exact match. */
2975 matchlen = k;
2976 if (k == len && *np == NUL)
2977 {
2978 if (full != NULL)
2979 *full = TRUE;
2980 amb_local = FALSE;
2981 break;
2982 }
2983 }
2984 }
2985 }
2986
2987 /* Stop if we found a full match or searched all. */
2988 if (j < gap->ga_len || gap == &ucmds)
2989 break;
2990 gap = &ucmds;
2991 }
2992
2993 /* Only found ambiguous matches. */
2994 if (amb_local)
2995 {
2996 if (xp != NULL)
2997 xp->xp_context = EXPAND_UNSUCCESSFUL;
2998 return NULL;
2999 }
3000
3001 /* The match we found may be followed immediately by a number. Move "p"
3002 * back to point to it. */
3003 if (found || possible)
3004 return p + (matchlen - len);
3005 return p;
3006}
3007#endif
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003010static struct cmdmod
3011{
3012 char *name;
3013 int minlen;
3014 int has_count; /* :123verbose :3tab */
3015} cmdmods[] = {
3016 {"aboveleft", 3, FALSE},
3017 {"belowright", 3, FALSE},
3018 {"botright", 2, FALSE},
3019 {"browse", 3, FALSE},
3020 {"confirm", 4, FALSE},
3021 {"hide", 3, FALSE},
3022 {"keepalt", 5, FALSE},
3023 {"keepjumps", 5, FALSE},
3024 {"keepmarks", 3, FALSE},
3025 {"leftabove", 5, FALSE},
3026 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003027 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003028 {"rightbelow", 6, FALSE},
3029 {"sandbox", 3, FALSE},
3030 {"silent", 3, FALSE},
3031 {"tab", 3, TRUE},
3032 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003033 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003034 {"verbose", 4, TRUE},
3035 {"vertical", 4, FALSE},
3036};
3037
3038/*
3039 * Return length of a command modifier (including optional count).
3040 * Return zero when it's not a modifier.
3041 */
3042 int
3043modifier_len(cmd)
3044 char_u *cmd;
3045{
3046 int i, j;
3047 char_u *p = cmd;
3048
3049 if (VIM_ISDIGIT(*cmd))
3050 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003051 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003052 {
3053 for (j = 0; p[j] != NUL; ++j)
3054 if (p[j] != cmdmods[i].name[j])
3055 break;
3056 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3057 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003058 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003059 }
3060 return 0;
3061}
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063/*
3064 * Return > 0 if an Ex command "name" exists.
3065 * Return 2 if there is an exact match.
3066 * Return 3 if there is an ambiguous match.
3067 */
3068 int
3069cmd_exists(name)
3070 char_u *name;
3071{
3072 exarg_T ea;
3073 int full = FALSE;
3074 int i;
3075 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003076 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003079 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
3081 for (j = 0; name[j] != NUL; ++j)
3082 if (name[j] != cmdmods[i].name[j])
3083 break;
3084 if (name[j] == NUL && j >= cmdmods[i].minlen)
3085 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3086 }
3087
Bram Moolenaara9587612006-05-04 21:47:50 +00003088 /* Check built-in commands and user defined commands.
3089 * For ":2match" and ":3match" we need to skip the number. */
3090 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003092 p = find_command(&ea, &full);
3093 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003095 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3096 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003097 if (*skipwhite(p) != NUL)
3098 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3100}
3101#endif
3102
3103/*
3104 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3105 * we don't need/want deleted. Maybe this could be done better if we didn't
3106 * repeat all this stuff. The only problem is that they may not stay
3107 * perfectly compatible with each other, but then the command line syntax
3108 * probably won't change that much -- webb.
3109 */
3110 char_u *
3111set_one_cmd_context(xp, buff)
3112 expand_T *xp;
3113 char_u *buff; /* buffer for command string */
3114{
3115 char_u *p;
3116 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003117 int len = 0;
3118 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3120 int compl = EXPAND_NOTHING;
3121#endif
3122#ifdef FEAT_CMDL_COMPL
3123 int delim;
3124#endif
3125 int forceit = FALSE;
3126 int usefilter = FALSE; /* filter instead of file name */
3127
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003128 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 xp->xp_pattern = buff;
3130 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133/*
3134 * 2. skip comment lines and leading space, colons or bars
3135 */
3136 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3137 ;
3138 xp->xp_pattern = cmd;
3139
3140 if (*cmd == NUL)
3141 return NULL;
3142 if (*cmd == '"') /* ignore comment lines */
3143 {
3144 xp->xp_context = EXPAND_NOTHING;
3145 return NULL;
3146 }
3147
3148/*
3149 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3150 */
3151 cmd = skip_range(cmd, &xp->xp_context);
3152
3153/*
3154 * 4. parse command
3155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 xp->xp_pattern = cmd;
3157 if (*cmd == NUL)
3158 return NULL;
3159 if (*cmd == '"')
3160 {
3161 xp->xp_context = EXPAND_NOTHING;
3162 return NULL;
3163 }
3164
3165 if (*cmd == '|' || *cmd == '\n')
3166 return cmd + 1; /* There's another command */
3167
3168 /*
3169 * Isolate the command and search for it in the command table.
3170 * Exceptions:
3171 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003172 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3174 */
3175 if (*cmd == 'k' && cmd[1] != 'e')
3176 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 p = cmd + 1;
3179 }
3180 else
3181 {
3182 p = cmd;
3183 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3184 ++p;
3185 /* check for non-alpha command */
3186 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3187 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 xp->xp_context = EXPAND_UNSUCCESSFUL;
3193 return NULL;
3194 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003196 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3197 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3198 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 break;
3200
3201#ifdef FEAT_USR_CMDS
3202 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3204 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#endif
3206 }
3207
3208 /*
3209 * If the cursor is touching the command, and it ends in an alpha-numeric
3210 * character, complete the command name.
3211 */
3212 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3213 return NULL;
3214
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3218 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 p = cmd + 1;
3221 }
3222#ifdef FEAT_USR_CMDS
3223 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3224 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225 ea.cmd = cmd;
3226 p = find_ucmd(&ea, p, NULL, xp,
3227# if defined(FEAT_CMDL_COMPL)
3228 &compl
3229# else
3230 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003233 if (p == NULL)
3234 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236#endif
3237 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 /* Not still touching the command and it was an illegal one */
3241 xp->xp_context = EXPAND_UNSUCCESSFUL;
3242 return NULL;
3243 }
3244
3245 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3246
3247 if (*p == '!') /* forced commands */
3248 {
3249 forceit = TRUE;
3250 ++p;
3251 }
3252
3253/*
3254 * 5. parse arguments
3255 */
3256#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 arg = skipwhite(p);
3262
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 if (*arg == '>') /* append */
3266 {
3267 if (*++arg == '>')
3268 ++arg;
3269 arg = skipwhite(arg);
3270 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 ++arg;
3274 usefilter = TRUE;
3275 }
3276 }
3277
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 usefilter = forceit; /* :r! filter if forced */
3281 if (*arg == '!') /* :r !filter */
3282 {
3283 ++arg;
3284 usefilter = TRUE;
3285 }
3286 }
3287
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003288 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 while (*arg == *cmd) /* allow any number of '>' or '<' */
3291 ++arg;
3292 arg = skipwhite(arg);
3293 }
3294
3295 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 /* Check if we're in the +command */
3299 p = arg + 1;
3300 arg = skip_cmd_arg(arg, FALSE);
3301
3302 /* Still touching the command after '+'? */
3303 if (*arg == NUL)
3304 return p;
3305
3306 /* Skip space(s) after +command to get to the real argument */
3307 arg = skipwhite(arg);
3308 }
3309
3310 /*
3311 * Check for '|' to separate commands and '"' to start comments.
3312 * Don't do this for ":read !cmd" and ":write !cmd".
3313 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003314 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316 p = arg;
3317 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 p += 2;
3320 while (*p)
3321 {
3322 if (*p == Ctrl_V)
3323 {
3324 if (p[1] != NUL)
3325 ++p;
3326 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 || *p == '|' || *p == '\n')
3329 {
3330 if (*(p - 1) != '\\')
3331 {
3332 if (*p == '|' || *p == '\n')
3333 return p + 1;
3334 return NULL; /* It's a comment */
3335 }
3336 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003337 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 }
3340
3341 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 vim_strchr((char_u *)"|\"", *arg) == NULL)
3344 return NULL;
3345
3346 /* Find start of last argument (argument just before cursor): */
3347 p = buff + STRLEN(buff);
3348 while (p != arg && *p != ' ' && *p != TAB)
3349 p--;
3350 if (*p == ' ' || *p == TAB)
3351 p++;
3352 xp->xp_pattern = p;
3353
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003354 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003356 int c;
3357 int in_quote = FALSE;
3358 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 /*
3361 * Allow spaces within back-quotes to count as part of the argument
3362 * being expanded.
3363 */
3364 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003365 p = xp->xp_pattern;
3366 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003368#ifdef FEAT_MBYTE
3369 if (has_mbyte)
3370 c = mb_ptr2char(p);
3371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003373 c = *p;
3374 if (c == '\\' && p[1] != NUL)
3375 ++p;
3376 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 if (!in_quote)
3379 {
3380 xp->xp_pattern = p;
3381 bow = p + 1;
3382 }
3383 in_quote = !in_quote;
3384 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003385 /* An argument can contain just about everything, except
3386 * characters that end the command and white space. */
3387 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003388#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003389 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003390#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003391 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003392 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003393 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003394 while (*p != NUL)
3395 {
3396#ifdef FEAT_MBYTE
3397 if (has_mbyte)
3398 c = mb_ptr2char(p);
3399 else
3400#endif
3401 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003402 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003403 break;
3404#ifdef FEAT_MBYTE
3405 if (has_mbyte)
3406 len = (*mb_ptr2len)(p);
3407 else
3408#endif
3409 len = 1;
3410 mb_ptr_adv(p);
3411 }
3412 if (in_quote)
3413 bow = p;
3414 else
3415 xp->xp_pattern = p;
3416 p -= len;
3417 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003418 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
3420
3421 /*
3422 * If we are still inside the quotes, and we passed a space, just
3423 * expand from there.
3424 */
3425 if (bow != NULL && in_quote)
3426 xp->xp_pattern = bow;
3427 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003428
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003429 /* For a shell command more chars need to be escaped. */
3430 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003431 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003432#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003433 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003434#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003435 /* When still after the command name expand executables. */
3436 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003437 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /* Check for environment variable */
3441 if (*xp->xp_pattern == '$'
3442#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3443 || *xp->xp_pattern == '%'
3444#endif
3445 )
3446 {
3447 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3448 if (!vim_isIDc(*p))
3449 break;
3450 if (*p == NUL)
3451 {
3452 xp->xp_context = EXPAND_ENV_VARS;
3453 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003454#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3455 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003456 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003457 compl = EXPAND_ENV_VARS;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 }
3461 }
3462
3463/*
3464 * 6. switch on command name
3465 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003468 case CMD_find:
3469 case CMD_sfind:
3470 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003471 if (xp->xp_context == EXPAND_FILES)
3472 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 case CMD_cd:
3475 case CMD_chdir:
3476 case CMD_lcd:
3477 case CMD_lchdir:
3478 if (xp->xp_context == EXPAND_FILES)
3479 xp->xp_context = EXPAND_DIRECTORIES;
3480 break;
3481 case CMD_help:
3482 xp->xp_context = EXPAND_HELP;
3483 xp->xp_pattern = arg;
3484 break;
3485
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003486 /* Command modifiers: return the argument.
3487 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003489 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 case CMD_belowright:
3491 case CMD_botright:
3492 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003493 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003495 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 case CMD_folddoclosed:
3497 case CMD_folddoopen:
3498 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003499 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 case CMD_keepjumps:
3501 case CMD_keepmarks:
3502 case CMD_leftabove:
3503 case CMD_lockmarks:
3504 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003505 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003507 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 case CMD_topleft:
3509 case CMD_verbose:
3510 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003511 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 return arg;
3513
Bram Moolenaar4f688582007-07-24 12:34:30 +00003514#ifdef FEAT_CMDL_COMPL
3515# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 case CMD_match:
3517 if (*arg == NUL || !ends_excmd(*arg))
3518 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003519 /* also complete "None" */
3520 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 arg = skipwhite(skiptowhite(arg));
3522 if (*arg != NUL)
3523 {
3524 xp->xp_context = EXPAND_NOTHING;
3525 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3526 }
3527 }
3528 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531/*
3532 * All completion for the +cmdline_compl feature goes here.
3533 */
3534
3535# ifdef FEAT_USR_CMDS
3536 case CMD_command:
3537 /* Check for attributes */
3538 while (*arg == '-')
3539 {
3540 arg++; /* Skip "-" */
3541 p = skiptowhite(arg);
3542 if (*p == NUL)
3543 {
3544 /* Cursor is still in the attribute */
3545 p = vim_strchr(arg, '=');
3546 if (p == NULL)
3547 {
3548 /* No "=", so complete attribute names */
3549 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3550 xp->xp_pattern = arg;
3551 return NULL;
3552 }
3553
3554 /* For the -complete and -nargs attributes, we complete
3555 * their arguments as well.
3556 */
3557 if (STRNICMP(arg, "complete", p - arg) == 0)
3558 {
3559 xp->xp_context = EXPAND_USER_COMPLETE;
3560 xp->xp_pattern = p + 1;
3561 return NULL;
3562 }
3563 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3564 {
3565 xp->xp_context = EXPAND_USER_NARGS;
3566 xp->xp_pattern = p + 1;
3567 return NULL;
3568 }
3569 return NULL;
3570 }
3571 arg = skipwhite(p);
3572 }
3573
3574 /* After the attributes comes the new command name */
3575 p = skiptowhite(arg);
3576 if (*p == NUL)
3577 {
3578 xp->xp_context = EXPAND_USER_COMMANDS;
3579 xp->xp_pattern = arg;
3580 break;
3581 }
3582
3583 /* And finally comes a normal command */
3584 return skipwhite(p);
3585
3586 case CMD_delcommand:
3587 xp->xp_context = EXPAND_USER_COMMANDS;
3588 xp->xp_pattern = arg;
3589 break;
3590# endif
3591
3592 case CMD_global:
3593 case CMD_vglobal:
3594 delim = *arg; /* get the delimiter */
3595 if (delim)
3596 ++arg; /* skip delimiter if there is one */
3597
3598 while (arg[0] != NUL && arg[0] != delim)
3599 {
3600 if (arg[0] == '\\' && arg[1] != NUL)
3601 ++arg;
3602 ++arg;
3603 }
3604 if (arg[0] != NUL)
3605 return arg + 1;
3606 break;
3607 case CMD_and:
3608 case CMD_substitute:
3609 delim = *arg;
3610 if (delim)
3611 {
3612 /* skip "from" part */
3613 ++arg;
3614 arg = skip_regexp(arg, delim, p_magic, NULL);
3615 }
3616 /* skip "to" part */
3617 while (arg[0] != NUL && arg[0] != delim)
3618 {
3619 if (arg[0] == '\\' && arg[1] != NUL)
3620 ++arg;
3621 ++arg;
3622 }
3623 if (arg[0] != NUL) /* skip delimiter */
3624 ++arg;
3625 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3626 ++arg;
3627 if (arg[0] != NUL)
3628 return arg;
3629 break;
3630 case CMD_isearch:
3631 case CMD_dsearch:
3632 case CMD_ilist:
3633 case CMD_dlist:
3634 case CMD_ijump:
3635 case CMD_psearch:
3636 case CMD_djump:
3637 case CMD_isplit:
3638 case CMD_dsplit:
3639 arg = skipwhite(skipdigits(arg)); /* skip count */
3640 if (*arg == '/') /* Match regexp, not just whole words */
3641 {
3642 for (++arg; *arg && *arg != '/'; arg++)
3643 if (*arg == '\\' && arg[1] != NUL)
3644 arg++;
3645 if (*arg)
3646 {
3647 arg = skipwhite(arg + 1);
3648
3649 /* Check for trailing illegal characters */
3650 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3651 xp->xp_context = EXPAND_NOTHING;
3652 else
3653 return arg;
3654 }
3655 }
3656 break;
3657#ifdef FEAT_AUTOCMD
3658 case CMD_autocmd:
3659 return set_context_in_autocmd(xp, arg, FALSE);
3660
3661 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003662 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return set_context_in_autocmd(xp, arg, TRUE);
3664#endif
3665 case CMD_set:
3666 set_context_in_set_cmd(xp, arg, 0);
3667 break;
3668 case CMD_setglobal:
3669 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3670 break;
3671 case CMD_setlocal:
3672 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3673 break;
3674 case CMD_tag:
3675 case CMD_stag:
3676 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003677 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 case CMD_tselect:
3679 case CMD_stselect:
3680 case CMD_ptselect:
3681 case CMD_tjump:
3682 case CMD_stjump:
3683 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003684 if (*p_wop != NUL)
3685 xp->xp_context = EXPAND_TAGS_LISTFILES;
3686 else
3687 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 xp->xp_pattern = arg;
3689 break;
3690 case CMD_augroup:
3691 xp->xp_context = EXPAND_AUGROUP;
3692 xp->xp_pattern = arg;
3693 break;
3694#ifdef FEAT_SYN_HL
3695 case CMD_syntax:
3696 set_context_in_syntax_cmd(xp, arg);
3697 break;
3698#endif
3699#ifdef FEAT_EVAL
3700 case CMD_let:
3701 case CMD_if:
3702 case CMD_elseif:
3703 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003704 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 case CMD_echo:
3706 case CMD_echon:
3707 case CMD_execute:
3708 case CMD_echomsg:
3709 case CMD_echoerr:
3710 case CMD_call:
3711 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003712 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 break;
3714
3715 case CMD_unlet:
3716 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3717 arg = xp->xp_pattern + 1;
3718 xp->xp_context = EXPAND_USER_VARS;
3719 xp->xp_pattern = arg;
3720 break;
3721
3722 case CMD_function:
3723 case CMD_delfunction:
3724 xp->xp_context = EXPAND_USER_FUNC;
3725 xp->xp_pattern = arg;
3726 break;
3727
3728 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003729 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 break;
3731#endif
3732 case CMD_highlight:
3733 set_context_in_highlight_cmd(xp, arg);
3734 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003735#ifdef FEAT_CSCOPE
3736 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003737 case CMD_lcscope:
3738 case CMD_scscope:
3739 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003740 break;
3741#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003742#ifdef FEAT_SIGNS
3743 case CMD_sign:
3744 set_context_in_sign_cmd(xp, arg);
3745 break;
3746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#ifdef FEAT_LISTCMDS
3748 case CMD_bdelete:
3749 case CMD_bwipeout:
3750 case CMD_bunload:
3751 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3752 arg = xp->xp_pattern + 1;
3753 /*FALLTHROUGH*/
3754 case CMD_buffer:
3755 case CMD_sbuffer:
3756 case CMD_checktime:
3757 xp->xp_context = EXPAND_BUFFERS;
3758 xp->xp_pattern = arg;
3759 break;
3760#endif
3761#ifdef FEAT_USR_CMDS
3762 case CMD_USER:
3763 case CMD_USER_BUF:
3764 if (compl != EXPAND_NOTHING)
3765 {
3766 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003767 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769# ifdef FEAT_MENU
3770 if (compl == EXPAND_MENUS)
3771 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3772# endif
3773 if (compl == EXPAND_COMMANDS)
3774 return arg;
3775 if (compl == EXPAND_MAPPINGS)
3776 return set_context_in_map_cmd(xp, (char_u *)"map",
3777 arg, forceit, FALSE, FALSE, CMD_map);
3778 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3779 arg = xp->xp_pattern + 1;
3780 xp->xp_pattern = arg;
3781 }
3782 xp->xp_context = compl;
3783 }
3784 break;
3785#endif
3786 case CMD_map: case CMD_noremap:
3787 case CMD_nmap: case CMD_nnoremap:
3788 case CMD_vmap: case CMD_vnoremap:
3789 case CMD_omap: case CMD_onoremap:
3790 case CMD_imap: case CMD_inoremap:
3791 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003792 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003794 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 case CMD_unmap:
3796 case CMD_nunmap:
3797 case CMD_vunmap:
3798 case CMD_ounmap:
3799 case CMD_iunmap:
3800 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003801 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003803 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 case CMD_abbreviate: case CMD_noreabbrev:
3805 case CMD_cabbrev: case CMD_cnoreabbrev:
3806 case CMD_iabbrev: case CMD_inoreabbrev:
3807 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003808 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_unabbreviate:
3810 case CMD_cunabbrev:
3811 case CMD_iunabbrev:
3812 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003813 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814#ifdef FEAT_MENU
3815 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3816 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3817 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3818 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3819 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3820 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3821 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3822 case CMD_tmenu: case CMD_tunmenu:
3823 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3824 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3825#endif
3826
3827 case CMD_colorscheme:
3828 xp->xp_context = EXPAND_COLORS;
3829 xp->xp_pattern = arg;
3830 break;
3831
3832 case CMD_compiler:
3833 xp->xp_context = EXPAND_COMPILER;
3834 xp->xp_pattern = arg;
3835 break;
3836
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003837 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003838 xp->xp_context = EXPAND_OWNSYNTAX;
3839 xp->xp_pattern = arg;
3840 break;
3841
3842 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003843 xp->xp_context = EXPAND_FILETYPE;
3844 xp->xp_pattern = arg;
3845 break;
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3848 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3849 case CMD_language:
3850 if (*skiptowhite(arg) == NUL)
3851 {
3852 xp->xp_context = EXPAND_LANGUAGE;
3853 xp->xp_pattern = arg;
3854 }
3855 else
3856 xp->xp_context = EXPAND_NOTHING;
3857 break;
3858#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003859#if defined(FEAT_PROFILE)
3860 case CMD_profile:
3861 set_context_in_profile_cmd(xp, arg);
3862 break;
3863#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003864 case CMD_behave:
3865 xp->xp_context = EXPAND_BEHAVE;
3866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868#endif /* FEAT_CMDL_COMPL */
3869
3870 default:
3871 break;
3872 }
3873 return NULL;
3874}
3875
3876/*
3877 * skip a range specifier of the form: addr [,addr] [;addr] ..
3878 *
3879 * Backslashed delimiters after / or ? will be skipped, and commands will
3880 * not be expanded between /'s and ?'s or after "'".
3881 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003882 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 * Returns the "cmd" pointer advanced to beyond the range.
3884 */
3885 char_u *
3886skip_range(cmd, ctx)
3887 char_u *cmd;
3888 int *ctx; /* pointer to xp_context or NULL */
3889{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003890 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaardf177f62005-02-22 08:39:57 +00003892 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
3894 if (*cmd == '\'')
3895 {
3896 if (*++cmd == NUL && ctx != NULL)
3897 *ctx = EXPAND_NOTHING;
3898 }
3899 else if (*cmd == '/' || *cmd == '?')
3900 {
3901 delim = *cmd++;
3902 while (*cmd != NUL && *cmd != delim)
3903 if (*cmd++ == '\\' && *cmd != NUL)
3904 ++cmd;
3905 if (*cmd == NUL && ctx != NULL)
3906 *ctx = EXPAND_NOTHING;
3907 }
3908 if (*cmd != NUL)
3909 ++cmd;
3910 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003911
3912 /* Skip ":" and white space. */
3913 while (*cmd == ':')
3914 cmd = skipwhite(cmd + 1);
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 return cmd;
3917}
3918
3919/*
3920 * get a single EX address
3921 *
3922 * Set ptr to the next character after the part that was interpreted.
3923 * Set ptr to NULL when an error is encountered.
3924 *
3925 * Return MAXLNUM when no Ex address was found.
3926 */
3927 static linenr_T
3928get_address(ptr, skip, to_other_file)
3929 char_u **ptr;
3930 int skip; /* only skip the address, don't use it */
3931 int to_other_file; /* flag: may jump to other file */
3932{
3933 int c;
3934 int i;
3935 long n;
3936 char_u *cmd;
3937 pos_T pos;
3938 pos_T *fp;
3939 linenr_T lnum;
3940
3941 cmd = skipwhite(*ptr);
3942 lnum = MAXLNUM;
3943 do
3944 {
3945 switch (*cmd)
3946 {
3947 case '.': /* '.' - Cursor position */
3948 ++cmd;
3949 lnum = curwin->w_cursor.lnum;
3950 break;
3951
3952 case '$': /* '$' - last line */
3953 ++cmd;
3954 lnum = curbuf->b_ml.ml_line_count;
3955 break;
3956
3957 case '\'': /* ''' - mark */
3958 if (*++cmd == NUL)
3959 {
3960 cmd = NULL;
3961 goto error;
3962 }
3963 if (skip)
3964 ++cmd;
3965 else
3966 {
3967 /* Only accept a mark in another file when it is
3968 * used by itself: ":'M". */
3969 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3970 ++cmd;
3971 if (fp == (pos_T *)-1)
3972 /* Jumped to another file. */
3973 lnum = curwin->w_cursor.lnum;
3974 else
3975 {
3976 if (check_mark(fp) == FAIL)
3977 {
3978 cmd = NULL;
3979 goto error;
3980 }
3981 lnum = fp->lnum;
3982 }
3983 }
3984 break;
3985
3986 case '/':
3987 case '?': /* '/' or '?' - search */
3988 c = *cmd++;
3989 if (skip) /* skip "/pat/" */
3990 {
3991 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3992 if (*cmd == c)
3993 ++cmd;
3994 }
3995 else
3996 {
3997 pos = curwin->w_cursor; /* save curwin->w_cursor */
3998 /*
3999 * When '/' or '?' follows another address, start
4000 * from there.
4001 */
4002 if (lnum != MAXLNUM)
4003 curwin->w_cursor.lnum = lnum;
4004 /*
4005 * Start a forward search at the end of the line.
4006 * Start a backward search at the start of the line.
4007 * This makes sure we never match in the current
4008 * line, and can match anywhere in the
4009 * next/previous line.
4010 */
4011 if (c == '/')
4012 curwin->w_cursor.col = MAXCOL;
4013 else
4014 curwin->w_cursor.col = 0;
4015 searchcmdlen = 0;
4016 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004017 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
4019 curwin->w_cursor = pos;
4020 cmd = NULL;
4021 goto error;
4022 }
4023 lnum = curwin->w_cursor.lnum;
4024 curwin->w_cursor = pos;
4025 /* adjust command string pointer */
4026 cmd += searchcmdlen;
4027 }
4028 break;
4029
4030 case '\\': /* "\?", "\/" or "\&", repeat search */
4031 ++cmd;
4032 if (*cmd == '&')
4033 i = RE_SUBST;
4034 else if (*cmd == '?' || *cmd == '/')
4035 i = RE_SEARCH;
4036 else
4037 {
4038 EMSG(_(e_backslash));
4039 cmd = NULL;
4040 goto error;
4041 }
4042
4043 if (!skip)
4044 {
4045 /*
4046 * When search follows another address, start from
4047 * there.
4048 */
4049 if (lnum != MAXLNUM)
4050 pos.lnum = lnum;
4051 else
4052 pos.lnum = curwin->w_cursor.lnum;
4053
4054 /*
4055 * Start the search just like for the above
4056 * do_search().
4057 */
4058 if (*cmd != '?')
4059 pos.col = MAXCOL;
4060 else
4061 pos.col = 0;
4062 if (searchit(curwin, curbuf, &pos,
4063 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004064 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004065 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 lnum = pos.lnum;
4067 else
4068 {
4069 cmd = NULL;
4070 goto error;
4071 }
4072 }
4073 ++cmd;
4074 break;
4075
4076 default:
4077 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4078 lnum = getdigits(&cmd);
4079 }
4080
4081 for (;;)
4082 {
4083 cmd = skipwhite(cmd);
4084 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4085 break;
4086
4087 if (lnum == MAXLNUM)
4088 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4089 if (VIM_ISDIGIT(*cmd))
4090 i = '+'; /* "number" is same as "+number" */
4091 else
4092 i = *cmd++;
4093 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4094 n = 1;
4095 else
4096 n = getdigits(&cmd);
4097 if (i == '-')
4098 lnum -= n;
4099 else
4100 lnum += n;
4101 }
4102 } while (*cmd == '/' || *cmd == '?');
4103
4104error:
4105 *ptr = cmd;
4106 return lnum;
4107}
4108
4109/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004110 * Get flags from an Ex command argument.
4111 */
4112 static void
4113get_flags(eap)
4114 exarg_T *eap;
4115{
4116 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4117 {
4118 if (*eap->arg == 'l')
4119 eap->flags |= EXFLAG_LIST;
4120 else if (*eap->arg == 'p')
4121 eap->flags |= EXFLAG_PRINT;
4122 else
4123 eap->flags |= EXFLAG_NR;
4124 eap->arg = skipwhite(eap->arg + 1);
4125 }
4126}
4127
4128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Function called for command which is Not Implemented. NI!
4130 */
4131 void
4132ex_ni(eap)
4133 exarg_T *eap;
4134{
4135 if (!eap->skip)
4136 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4137}
4138
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004139#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140/*
4141 * Function called for script command which is Not Implemented. NI!
4142 * Skips over ":perl <<EOF" constructs.
4143 */
4144 static void
4145ex_script_ni(eap)
4146 exarg_T *eap;
4147{
4148 if (!eap->skip)
4149 ex_ni(eap);
4150 else
4151 vim_free(script_get(eap, eap->arg));
4152}
4153#endif
4154
4155/*
4156 * Check range in Ex command for validity.
4157 * Return NULL when valid, error message when invalid.
4158 */
4159 static char_u *
4160invalid_range(eap)
4161 exarg_T *eap;
4162{
4163 if ( eap->line1 < 0
4164 || eap->line2 < 0
4165 || eap->line1 > eap->line2
4166 || ((eap->argt & RANGE)
4167 && !(eap->argt & NOTADR)
4168 && eap->line2 > curbuf->b_ml.ml_line_count
4169#ifdef FEAT_DIFF
4170 + (eap->cmdidx == CMD_diffget)
4171#endif
4172 ))
4173 return (char_u *)_(e_invrange);
4174 return NULL;
4175}
4176
4177/*
4178 * Correct the range for zero line number, if required.
4179 */
4180 static void
4181correct_range(eap)
4182 exarg_T *eap;
4183{
4184 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4185 {
4186 if (eap->line1 == 0)
4187 eap->line1 = 1;
4188 if (eap->line2 == 0)
4189 eap->line2 = 1;
4190 }
4191}
4192
Bram Moolenaar748bf032005-02-02 23:04:36 +00004193#ifdef FEAT_QUICKFIX
4194static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4195
4196/*
4197 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4198 * pattern. Otherwise return eap->arg.
4199 */
4200 static char_u *
4201skip_grep_pat(eap)
4202 exarg_T *eap;
4203{
4204 char_u *p = eap->arg;
4205
Bram Moolenaara37420f2006-02-04 22:37:47 +00004206 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4207 || eap->cmdidx == CMD_vimgrepadd
4208 || eap->cmdidx == CMD_lvimgrepadd
4209 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004210 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004211 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004212 if (p == NULL)
4213 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004214 }
4215 return p;
4216}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004217
4218/*
4219 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4220 * in the command line, so that things like % get expanded.
4221 */
4222 static char_u *
4223replace_makeprg(eap, p, cmdlinep)
4224 exarg_T *eap;
4225 char_u *p;
4226 char_u **cmdlinep;
4227{
4228 char_u *new_cmdline;
4229 char_u *program;
4230 char_u *pos;
4231 char_u *ptr;
4232 int len;
4233 int i;
4234
4235 /*
4236 * Don't do it when ":vimgrep" is used for ":grep".
4237 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004238 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4239 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4240 || eap->cmdidx == CMD_grepadd
4241 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004242 && !grep_internal(eap->cmdidx))
4243 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004244 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4245 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004246 {
4247 if (*curbuf->b_p_gp == NUL)
4248 program = p_gp;
4249 else
4250 program = curbuf->b_p_gp;
4251 }
4252 else
4253 {
4254 if (*curbuf->b_p_mp == NUL)
4255 program = p_mp;
4256 else
4257 program = curbuf->b_p_mp;
4258 }
4259
4260 p = skipwhite(p);
4261
4262 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4263 {
4264 /* replace $* by given arguments */
4265 i = 1;
4266 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4267 ++i;
4268 len = (int)STRLEN(p);
4269 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4270 if (new_cmdline == NULL)
4271 return NULL; /* out of memory */
4272 ptr = new_cmdline;
4273 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4274 {
4275 i = (int)(pos - program);
4276 STRNCPY(ptr, program, i);
4277 STRCPY(ptr += i, p);
4278 ptr += len;
4279 program = pos + 2;
4280 }
4281 STRCPY(ptr, program);
4282 }
4283 else
4284 {
4285 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4286 if (new_cmdline == NULL)
4287 return NULL; /* out of memory */
4288 STRCPY(new_cmdline, program);
4289 STRCAT(new_cmdline, " ");
4290 STRCAT(new_cmdline, p);
4291 }
4292 msg_make(p);
4293
4294 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4295 vim_free(*cmdlinep);
4296 *cmdlinep = new_cmdline;
4297 p = new_cmdline;
4298 }
4299 return p;
4300}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004301#endif
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303/*
4304 * Expand file name in Ex command argument.
4305 * Return FAIL for failure, OK otherwise.
4306 */
4307 int
4308expand_filename(eap, cmdlinep, errormsgp)
4309 exarg_T *eap;
4310 char_u **cmdlinep;
4311 char_u **errormsgp;
4312{
4313 int has_wildcards; /* need to expand wildcards */
4314 char_u *repl;
4315 int srclen;
4316 char_u *p;
4317 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004318 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar748bf032005-02-02 23:04:36 +00004320#ifdef FEAT_QUICKFIX
4321 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4322 p = skip_grep_pat(eap);
4323#else
4324 p = eap->arg;
4325#endif
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /*
4328 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4329 * the file name contains a wildcard it should not cause expanding.
4330 * (it will be expanded anyway if there is a wildcard before replacing).
4331 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004332 has_wildcards = mch_has_wildcard(p);
4333 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004335#ifdef FEAT_EVAL
4336 /* Skip over `=expr`, wildcards in it are not expanded. */
4337 if (p[0] == '`' && p[1] == '=')
4338 {
4339 p += 2;
4340 (void)skip_expr(&p);
4341 if (*p == '`')
4342 ++p;
4343 continue;
4344 }
4345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /*
4347 * Quick check if this cannot be the start of a special string.
4348 * Also removes backslash before '%', '#' and '<'.
4349 */
4350 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4351 {
4352 ++p;
4353 continue;
4354 }
4355
4356 /*
4357 * Try to find a match at this position.
4358 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004359 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4360 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (*errormsgp != NULL) /* error detected */
4362 return FAIL;
4363 if (repl == NULL) /* no match found */
4364 {
4365 p += srclen;
4366 continue;
4367 }
4368
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004369 /* Wildcards won't be expanded below, the replacement is taken
4370 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4371 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4372 {
4373 char_u *l = repl;
4374
4375 repl = expand_env_save(repl);
4376 vim_free(l);
4377 }
4378
Bram Moolenaar63b92542007-03-27 14:57:09 +00004379 /* Need to escape white space et al. with a backslash.
4380 * Don't do this for:
4381 * - replacement that already has been escaped: "##"
4382 * - shell commands (may have to use quotes instead).
4383 * - non-unix systems when there is a single argument (spaces don't
4384 * separate arguments then).
4385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004387 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 && eap->cmdidx != CMD_bang
4389 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004390 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004392 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004394 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#ifndef UNIX
4396 && !(eap->argt & NOSPC)
4397#endif
4398 )
4399 {
4400 char_u *l;
4401#ifdef BACKSLASH_IN_FILENAME
4402 /* Don't escape a backslash here, because rem_backslash() doesn't
4403 * remove it later. */
4404 static char_u *nobslash = (char_u *)" \t\"|";
4405# define ESCAPE_CHARS nobslash
4406#else
4407# define ESCAPE_CHARS escape_chars
4408#endif
4409
4410 for (l = repl; *l; ++l)
4411 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4412 {
4413 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4414 if (l != NULL)
4415 {
4416 vim_free(repl);
4417 repl = l;
4418 }
4419 break;
4420 }
4421 }
4422
4423 /* For a shell command a '!' must be escaped. */
4424 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004425 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
4427 char_u *l;
4428
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004429 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (l != NULL)
4431 {
4432 vim_free(repl);
4433 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004434 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (strstr((char *)p_sh, "sh") != NULL)
4436 {
4437 l = vim_strsave_escaped(repl, (char_u *)"!");
4438 if (l != NULL)
4439 {
4440 vim_free(repl);
4441 repl = l;
4442 }
4443 }
4444 }
4445 }
4446
4447 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4448 vim_free(repl);
4449 if (p == NULL)
4450 return FAIL;
4451 }
4452
4453 /*
4454 * One file argument: Expand wildcards.
4455 * Don't do this with ":r !command" or ":w !command".
4456 */
4457 if ((eap->argt & NOSPC) && !eap->usefilter)
4458 {
4459 /*
4460 * May do this twice:
4461 * 1. Replace environment variables.
4462 * 2. Replace any other wildcards, remove backslashes.
4463 */
4464 for (n = 1; n <= 2; ++n)
4465 {
4466 if (n == 2)
4467 {
4468#ifdef UNIX
4469 /*
4470 * Only for Unix we check for more than one file name.
4471 * For other systems spaces are considered to be part
4472 * of the file name.
4473 * Only check here if there is no wildcard, otherwise
4474 * ExpandOne() will check for errors. This allows
4475 * ":e `ls ve*.c`" on Unix.
4476 */
4477 if (!has_wildcards)
4478 for (p = eap->arg; *p; ++p)
4479 {
4480 /* skip escaped characters */
4481 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4482 ++p;
4483 else if (vim_iswhite(*p))
4484 {
4485 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4486 return FAIL;
4487 }
4488 }
4489#endif
4490
4491 /*
4492 * Halve the number of backslashes (this is Vi compatible).
4493 * For Unix and OS/2, when wildcards are expanded, this is
4494 * done by ExpandOne() below.
4495 */
4496#if defined(UNIX) || defined(OS2)
4497 if (!has_wildcards)
4498#endif
4499 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501
4502 if (has_wildcards)
4503 {
4504 if (n == 1)
4505 {
4506 /*
4507 * First loop: May expand environment variables. This
4508 * can be done much faster with expand_env() than with
4509 * something else (e.g., calling a shell).
4510 * After expanding environment variables, check again
4511 * if there are still wildcards present.
4512 */
4513 if (vim_strchr(eap->arg, '$') != NULL
4514 || vim_strchr(eap->arg, '~') != NULL)
4515 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004516 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004517 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 has_wildcards = mch_has_wildcard(NameBuff);
4519 p = NameBuff;
4520 }
4521 else
4522 p = NULL;
4523 }
4524 else /* n == 2 */
4525 {
4526 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004527 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 ExpandInit(&xpc);
4530 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004531 if (p_wic)
4532 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004534 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (p == NULL)
4536 return FAIL;
4537 }
4538 if (p != NULL)
4539 {
4540 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4541 p, cmdlinep);
4542 if (n == 2) /* p came from ExpandOne() */
4543 vim_free(p);
4544 }
4545 }
4546 }
4547 }
4548 return OK;
4549}
4550
4551/*
4552 * Replace part of the command line, keeping eap->cmd, eap->arg and
4553 * eap->nextcmd correct.
4554 * "src" points to the part that is to be replaced, of length "srclen".
4555 * "repl" is the replacement string.
4556 * Returns a pointer to the character after the replaced string.
4557 * Returns NULL for failure.
4558 */
4559 static char_u *
4560repl_cmdline(eap, src, srclen, repl, cmdlinep)
4561 exarg_T *eap;
4562 char_u *src;
4563 int srclen;
4564 char_u *repl;
4565 char_u **cmdlinep;
4566{
4567 int len;
4568 int i;
4569 char_u *new_cmdline;
4570
4571 /*
4572 * The new command line is build in new_cmdline[].
4573 * First allocate it.
4574 * Careful: a "+cmd" argument may have been NUL terminated.
4575 */
4576 len = (int)STRLEN(repl);
4577 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004578 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4580 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4581 return NULL; /* out of memory! */
4582
4583 /*
4584 * Copy the stuff before the expanded part.
4585 * Copy the expanded stuff.
4586 * Copy what came after the expanded part.
4587 * Copy the next commands, if there are any.
4588 */
4589 i = (int)(src - *cmdlinep); /* length of part before match */
4590 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 mch_memmove(new_cmdline + i, repl, (size_t)len);
4593 i += len; /* remember the end of the string */
4594 STRCPY(new_cmdline + i, src + srclen);
4595 src = new_cmdline + i; /* remember where to continue */
4596
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004597 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 i = (int)STRLEN(new_cmdline) + 1;
4600 STRCPY(new_cmdline + i, eap->nextcmd);
4601 eap->nextcmd = new_cmdline + i;
4602 }
4603 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4604 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4605 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4606 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4607 vim_free(*cmdlinep);
4608 *cmdlinep = new_cmdline;
4609
4610 return src;
4611}
4612
4613/*
4614 * Check for '|' to separate commands and '"' to start comments.
4615 */
4616 void
4617separate_nextcmd(eap)
4618 exarg_T *eap;
4619{
4620 char_u *p;
4621
Bram Moolenaar86b68352004-12-27 21:59:20 +00004622#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004623 p = skip_grep_pat(eap);
4624#else
4625 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004626#endif
4627
4628 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
4630 if (*p == Ctrl_V)
4631 {
4632 if (eap->argt & (USECTRLV | XFILE))
4633 ++p; /* skip CTRL-V and next char */
4634 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004635 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004636 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (*p == NUL) /* stop at NUL after CTRL-V */
4638 break;
4639 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004640
4641#ifdef FEAT_EVAL
4642 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004644 {
4645 p += 2;
4646 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004647 }
4648#endif
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /* Check for '"': start of comment or '|': next command */
4651 /* :@" and :*" do not start a comment!
4652 * :redir @" doesn't either. */
4653 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4654 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4655 || p != eap->arg)
4656 && (eap->cmdidx != CMD_redir
4657 || p != eap->arg + 1 || p[-1] != '@'))
4658 || *p == '|' || *p == '\n')
4659 {
4660 /*
4661 * We remove the '\' before the '|', unless USECTRLV is used
4662 * AND 'b' is present in 'cpoptions'.
4663 */
4664 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4665 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4666 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004667 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 --p;
4669 }
4670 else
4671 {
4672 eap->nextcmd = check_nextcmd(p);
4673 *p = NUL;
4674 break;
4675 }
4676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4680 del_trailing_spaces(eap->arg);
4681}
4682
4683/*
4684 * get + command from ex argument
4685 */
4686 static char_u *
4687getargcmd(argp)
4688 char_u **argp;
4689{
4690 char_u *arg = *argp;
4691 char_u *command = NULL;
4692
4693 if (*arg == '+') /* +[command] */
4694 {
4695 ++arg;
4696 if (vim_isspace(*arg))
4697 command = dollar_command;
4698 else
4699 {
4700 command = arg;
4701 arg = skip_cmd_arg(command, TRUE);
4702 if (*arg != NUL)
4703 *arg++ = NUL; /* terminate command with NUL */
4704 }
4705
4706 arg = skipwhite(arg); /* skip over spaces */
4707 *argp = arg;
4708 }
4709 return command;
4710}
4711
4712/*
4713 * Find end of "+command" argument. Skip over "\ " and "\\".
4714 */
4715 static char_u *
4716skip_cmd_arg(p, rembs)
4717 char_u *p;
4718 int rembs; /* TRUE to halve the number of backslashes */
4719{
4720 while (*p && !vim_isspace(*p))
4721 {
4722 if (*p == '\\' && p[1] != NUL)
4723 {
4724 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004725 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 else
4727 ++p;
4728 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004729 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 return p;
4732}
4733
4734/*
4735 * Get "++opt=arg" argument.
4736 * Return FAIL or OK.
4737 */
4738 static int
4739getargopt(eap)
4740 exarg_T *eap;
4741{
4742 char_u *arg = eap->arg + 2;
4743 int *pp = NULL;
4744#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004745 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 char_u *p;
4747#endif
4748
4749 /* ":edit ++[no]bin[ary] file" */
4750 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4751 {
4752 if (*arg == 'n')
4753 {
4754 arg += 2;
4755 eap->force_bin = FORCE_NOBIN;
4756 }
4757 else
4758 eap->force_bin = FORCE_BIN;
4759 if (!checkforcmd(&arg, "binary", 3))
4760 return FAIL;
4761 eap->arg = skipwhite(arg);
4762 return OK;
4763 }
4764
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004765 /* ":read ++edit file" */
4766 if (STRNCMP(arg, "edit", 4) == 0)
4767 {
4768 eap->read_edit = TRUE;
4769 eap->arg = skipwhite(arg + 4);
4770 return OK;
4771 }
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (STRNCMP(arg, "ff", 2) == 0)
4774 {
4775 arg += 2;
4776 pp = &eap->force_ff;
4777 }
4778 else if (STRNCMP(arg, "fileformat", 10) == 0)
4779 {
4780 arg += 10;
4781 pp = &eap->force_ff;
4782 }
4783#ifdef FEAT_MBYTE
4784 else if (STRNCMP(arg, "enc", 3) == 0)
4785 {
4786 arg += 3;
4787 pp = &eap->force_enc;
4788 }
4789 else if (STRNCMP(arg, "encoding", 8) == 0)
4790 {
4791 arg += 8;
4792 pp = &eap->force_enc;
4793 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004794 else if (STRNCMP(arg, "bad", 3) == 0)
4795 {
4796 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004797 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#endif
4800
4801 if (pp == NULL || *arg != '=')
4802 return FAIL;
4803
4804 ++arg;
4805 *pp = (int)(arg - eap->cmd);
4806 arg = skip_cmd_arg(arg, FALSE);
4807 eap->arg = skipwhite(arg);
4808 *arg = NUL;
4809
4810#ifdef FEAT_MBYTE
4811 if (pp == &eap->force_ff)
4812 {
4813#endif
4814 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4815 return FAIL;
4816#ifdef FEAT_MBYTE
4817 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004818 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 /* Make 'fileencoding' lower case. */
4821 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4822 *p = TOLOWER_ASC(*p);
4823 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004824 else
4825 {
4826 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4827 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004828 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004829 if (STRICMP(p, "keep") == 0)
4830 eap->bad_char = BAD_KEEP;
4831 else if (STRICMP(p, "drop") == 0)
4832 eap->bad_char = BAD_DROP;
4833 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4834 eap->bad_char = *p;
4835 else
4836 return FAIL;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
4839
4840 return OK;
4841}
4842
4843/*
4844 * ":abbreviate" and friends.
4845 */
4846 static void
4847ex_abbreviate(eap)
4848 exarg_T *eap;
4849{
4850 do_exmap(eap, TRUE); /* almost the same as mapping */
4851}
4852
4853/*
4854 * ":map" and friends.
4855 */
4856 static void
4857ex_map(eap)
4858 exarg_T *eap;
4859{
4860 /*
4861 * If we are sourcing .exrc or .vimrc in current directory we
4862 * print the mappings for security reasons.
4863 */
4864 if (secure)
4865 {
4866 secure = 2;
4867 msg_outtrans(eap->cmd);
4868 msg_putchar('\n');
4869 }
4870 do_exmap(eap, FALSE);
4871}
4872
4873/*
4874 * ":unmap" and friends.
4875 */
4876 static void
4877ex_unmap(eap)
4878 exarg_T *eap;
4879{
4880 do_exmap(eap, FALSE);
4881}
4882
4883/*
4884 * ":mapclear" and friends.
4885 */
4886 static void
4887ex_mapclear(eap)
4888 exarg_T *eap;
4889{
4890 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4891}
4892
4893/*
4894 * ":abclear" and friends.
4895 */
4896 static void
4897ex_abclear(eap)
4898 exarg_T *eap;
4899{
4900 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4901}
4902
4903#ifdef FEAT_AUTOCMD
4904 static void
4905ex_autocmd(eap)
4906 exarg_T *eap;
4907{
4908 /*
4909 * Disallow auto commands from .exrc and .vimrc in current
4910 * directory for security reasons.
4911 */
4912 if (secure)
4913 {
4914 secure = 2;
4915 eap->errmsg = e_curdir;
4916 }
4917 else if (eap->cmdidx == CMD_autocmd)
4918 do_autocmd(eap->arg, eap->forceit);
4919 else
4920 do_augroup(eap->arg, eap->forceit);
4921}
4922
4923/*
4924 * ":doautocmd": Apply the automatic commands to the current buffer.
4925 */
4926 static void
4927ex_doautocmd(eap)
4928 exarg_T *eap;
4929{
4930 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004931 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932}
4933#endif
4934
4935#ifdef FEAT_LISTCMDS
4936/*
4937 * :[N]bunload[!] [N] [bufname] unload buffer
4938 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4939 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4940 */
4941 static void
4942ex_bunload(eap)
4943 exarg_T *eap;
4944{
4945 eap->errmsg = do_bufdel(
4946 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4947 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4948 : DOBUF_UNLOAD, eap->arg,
4949 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4950}
4951
4952/*
4953 * :[N]buffer [N] to buffer N
4954 * :[N]sbuffer [N] to buffer N
4955 */
4956 static void
4957ex_buffer(eap)
4958 exarg_T *eap;
4959{
4960 if (*eap->arg)
4961 eap->errmsg = e_trailing;
4962 else
4963 {
4964 if (eap->addr_count == 0) /* default is current buffer */
4965 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4966 else
4967 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4968 }
4969}
4970
4971/*
4972 * :[N]bmodified [N] to next mod. buffer
4973 * :[N]sbmodified [N] to next mod. buffer
4974 */
4975 static void
4976ex_bmodified(eap)
4977 exarg_T *eap;
4978{
4979 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4980}
4981
4982/*
4983 * :[N]bnext [N] to next buffer
4984 * :[N]sbnext [N] split and to next buffer
4985 */
4986 static void
4987ex_bnext(eap)
4988 exarg_T *eap;
4989{
4990 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4991}
4992
4993/*
4994 * :[N]bNext [N] to previous buffer
4995 * :[N]bprevious [N] to previous buffer
4996 * :[N]sbNext [N] split and to previous buffer
4997 * :[N]sbprevious [N] split and to previous buffer
4998 */
4999 static void
5000ex_bprevious(eap)
5001 exarg_T *eap;
5002{
5003 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5004}
5005
5006/*
5007 * :brewind to first buffer
5008 * :bfirst to first buffer
5009 * :sbrewind split and to first buffer
5010 * :sbfirst split and to first buffer
5011 */
5012 static void
5013ex_brewind(eap)
5014 exarg_T *eap;
5015{
5016 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5017}
5018
5019/*
5020 * :blast to last buffer
5021 * :sblast split and to last buffer
5022 */
5023 static void
5024ex_blast(eap)
5025 exarg_T *eap;
5026{
5027 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5028}
5029#endif
5030
5031 int
5032ends_excmd(c)
5033 int c;
5034{
5035 return (c == NUL || c == '|' || c == '"' || c == '\n');
5036}
5037
5038#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5039 || defined(PROTO)
5040/*
5041 * Return the next command, after the first '|' or '\n'.
5042 * Return NULL if not found.
5043 */
5044 char_u *
5045find_nextcmd(p)
5046 char_u *p;
5047{
5048 while (*p != '|' && *p != '\n')
5049 {
5050 if (*p == NUL)
5051 return NULL;
5052 ++p;
5053 }
5054 return (p + 1);
5055}
5056#endif
5057
5058/*
5059 * Check if *p is a separator between Ex commands.
5060 * Return NULL if it isn't, (p + 1) if it is.
5061 */
5062 char_u *
5063check_nextcmd(p)
5064 char_u *p;
5065{
5066 p = skipwhite(p);
5067 if (*p == '|' || *p == '\n')
5068 return (p + 1);
5069 else
5070 return NULL;
5071}
5072
5073/*
5074 * - if there are more files to edit
5075 * - and this is the last window
5076 * - and forceit not used
5077 * - and not repeated twice on a row
5078 * return FAIL and give error message if 'message' TRUE
5079 * return OK otherwise
5080 */
5081 static int
5082check_more(message, forceit)
5083 int message; /* when FALSE check only, no messages */
5084 int forceit;
5085{
5086 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5087
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005088 if (!forceit && only_one_window()
5089 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 if (message)
5092 {
5093#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5094 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5095 {
5096 char_u buff[IOSIZE];
5097
5098 if (n == 1)
5099 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5100 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005101 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 _("%d more files to edit. Quit anyway?"), n);
5103 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5104 return OK;
5105 return FAIL;
5106 }
5107#endif
5108 if (n == 1)
5109 EMSG(_("E173: 1 more file to edit"));
5110 else
5111 EMSGN(_("E173: %ld more files to edit"), n);
5112 quitmore = 2; /* next try to quit is allowed */
5113 }
5114 return FAIL;
5115 }
5116 return OK;
5117}
5118
5119#ifdef FEAT_CMDL_COMPL
5120/*
5121 * Function given to ExpandGeneric() to obtain the list of command names.
5122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 char_u *
5124get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005125 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 int idx;
5127{
5128 if (idx >= (int)CMD_SIZE)
5129# ifdef FEAT_USR_CMDS
5130 return get_user_command_name(idx);
5131# else
5132 return NULL;
5133# endif
5134 return cmdnames[idx].cmd_name;
5135}
5136#endif
5137
5138#if defined(FEAT_USR_CMDS) || defined(PROTO)
5139static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5140static void uc_list __ARGS((char_u *name, size_t name_len));
5141static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5142static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5143static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len));
5144
5145 static int
5146uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5147 char_u *name;
5148 size_t name_len;
5149 char_u *rep;
5150 long argt;
5151 long def;
5152 int flags;
5153 int compl;
5154 char_u *compl_arg;
5155 int force;
5156{
5157 ucmd_T *cmd = NULL;
5158 char_u *p;
5159 int i;
5160 int cmp = 1;
5161 char_u *rep_buf = NULL;
5162 garray_T *gap;
5163
Bram Moolenaar9c102382006-05-03 21:26:49 +00005164 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (rep_buf == NULL)
5166 {
5167 /* Can't replace termcodes - try using the string as is */
5168 rep_buf = vim_strsave(rep);
5169
5170 /* Give up if out of memory */
5171 if (rep_buf == NULL)
5172 return FAIL;
5173 }
5174
5175 /* get address of growarray: global or in curbuf */
5176 if (flags & UC_BUFFER)
5177 {
5178 gap = &curbuf->b_ucmds;
5179 if (gap->ga_itemsize == 0)
5180 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5181 }
5182 else
5183 gap = &ucmds;
5184
5185 /* Search for the command in the already defined commands. */
5186 for (i = 0; i < gap->ga_len; ++i)
5187 {
5188 size_t len;
5189
5190 cmd = USER_CMD_GA(gap, i);
5191 len = STRLEN(cmd->uc_name);
5192 cmp = STRNCMP(name, cmd->uc_name, name_len);
5193 if (cmp == 0)
5194 {
5195 if (name_len < len)
5196 cmp = -1;
5197 else if (name_len > len)
5198 cmp = 1;
5199 }
5200
5201 if (cmp == 0)
5202 {
5203 if (!force)
5204 {
5205 EMSG(_("E174: Command already exists: add ! to replace it"));
5206 goto fail;
5207 }
5208
5209 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005210 cmd->uc_rep = NULL;
5211#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5212 vim_free(cmd->uc_compl_arg);
5213 cmd->uc_compl_arg = NULL;
5214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
5217
5218 /* Stop as soon as we pass the name to add */
5219 if (cmp < 0)
5220 break;
5221 }
5222
5223 /* Extend the array unless we're replacing an existing command */
5224 if (cmp != 0)
5225 {
5226 if (ga_grow(gap, 1) != OK)
5227 goto fail;
5228 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5229 goto fail;
5230
5231 cmd = USER_CMD_GA(gap, i);
5232 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5233
5234 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 cmd->uc_name = p;
5237 }
5238
5239 cmd->uc_rep = rep_buf;
5240 cmd->uc_argt = argt;
5241 cmd->uc_def = def;
5242 cmd->uc_compl = compl;
5243#ifdef FEAT_EVAL
5244 cmd->uc_scriptID = current_SID;
5245# ifdef FEAT_CMDL_COMPL
5246 cmd->uc_compl_arg = compl_arg;
5247# endif
5248#endif
5249
5250 return OK;
5251
5252fail:
5253 vim_free(rep_buf);
5254#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5255 vim_free(compl_arg);
5256#endif
5257 return FAIL;
5258}
5259
5260/*
5261 * List of names for completion for ":command" with the EXPAND_ flag.
5262 * Must be alphabetical for completion.
5263 */
5264static struct
5265{
5266 int expand;
5267 char *name;
5268} command_complete[] =
5269{
5270 {EXPAND_AUGROUP, "augroup"},
5271 {EXPAND_BUFFERS, "buffer"},
5272 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005273#if defined(FEAT_CSCOPE)
5274 {EXPAND_CSCOPE, "cscope"},
5275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5277 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005278 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280 {EXPAND_DIRECTORIES, "dir"},
5281 {EXPAND_ENV_VARS, "environment"},
5282 {EXPAND_EVENTS, "event"},
5283 {EXPAND_EXPRESSION, "expression"},
5284 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005285 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {EXPAND_FUNCTIONS, "function"},
5287 {EXPAND_HELP, "help"},
5288 {EXPAND_HIGHLIGHT, "highlight"},
5289 {EXPAND_MAPPINGS, "mapping"},
5290 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005291 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005293 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005294#if defined(FEAT_SIGNS)
5295 {EXPAND_SIGN, "sign"},
5296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {EXPAND_TAGS, "tag"},
5298 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5299 {EXPAND_USER_VARS, "var"},
5300 {0, NULL}
5301};
5302
5303 static void
5304uc_list(name, name_len)
5305 char_u *name;
5306 size_t name_len;
5307{
5308 int i, j;
5309 int found = FALSE;
5310 ucmd_T *cmd;
5311 int len;
5312 long a;
5313 garray_T *gap;
5314
5315 gap = &curbuf->b_ucmds;
5316 for (;;)
5317 {
5318 for (i = 0; i < gap->ga_len; ++i)
5319 {
5320 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005321 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 /* Skip commands which don't match the requested prefix */
5324 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5325 continue;
5326
5327 /* Put out the title first time */
5328 if (!found)
5329 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5330 found = TRUE;
5331 msg_putchar('\n');
5332 if (got_int)
5333 break;
5334
5335 /* Special cases */
5336 msg_putchar(a & BANG ? '!' : ' ');
5337 msg_putchar(a & REGSTR ? '"' : ' ');
5338 msg_putchar(gap != &ucmds ? 'b' : ' ');
5339 msg_putchar(' ');
5340
5341 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5342 len = (int)STRLEN(cmd->uc_name) + 4;
5343
5344 do {
5345 msg_putchar(' ');
5346 ++len;
5347 } while (len < 16);
5348
5349 len = 0;
5350
5351 /* Arguments */
5352 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5353 {
5354 case 0: IObuff[len++] = '0'; break;
5355 case (EXTRA): IObuff[len++] = '*'; break;
5356 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5357 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5358 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5359 }
5360
5361 do {
5362 IObuff[len++] = ' ';
5363 } while (len < 5);
5364
5365 /* Range */
5366 if (a & (RANGE|COUNT))
5367 {
5368 if (a & COUNT)
5369 {
5370 /* -count=N */
5371 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5372 len += (int)STRLEN(IObuff + len);
5373 }
5374 else if (a & DFLALL)
5375 IObuff[len++] = '%';
5376 else if (cmd->uc_def >= 0)
5377 {
5378 /* -range=N */
5379 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5380 len += (int)STRLEN(IObuff + len);
5381 }
5382 else
5383 IObuff[len++] = '.';
5384 }
5385
5386 do {
5387 IObuff[len++] = ' ';
5388 } while (len < 11);
5389
5390 /* Completion */
5391 for (j = 0; command_complete[j].expand != 0; ++j)
5392 if (command_complete[j].expand == cmd->uc_compl)
5393 {
5394 STRCPY(IObuff + len, command_complete[j].name);
5395 len += (int)STRLEN(IObuff + len);
5396 break;
5397 }
5398
5399 do {
5400 IObuff[len++] = ' ';
5401 } while (len < 21);
5402
5403 IObuff[len] = '\0';
5404 msg_outtrans(IObuff);
5405
5406 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005407#ifdef FEAT_EVAL
5408 if (p_verbose > 0)
5409 last_set_msg(cmd->uc_scriptID);
5410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 out_flush();
5412 ui_breakcheck();
5413 if (got_int)
5414 break;
5415 }
5416 if (gap == &ucmds || i < gap->ga_len)
5417 break;
5418 gap = &ucmds;
5419 }
5420
5421 if (!found)
5422 MSG(_("No user-defined commands found"));
5423}
5424
5425 static char_u *
5426uc_fun_cmd()
5427{
5428 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5429 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5430 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5431 0xb9, 0x7f, 0};
5432 int i;
5433
5434 for (i = 0; fcmd[i]; ++i)
5435 IObuff[i] = fcmd[i] - 0x40;
5436 IObuff[i] = 0;
5437 return IObuff;
5438}
5439
5440 static int
5441uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5442 char_u *attr;
5443 size_t len;
5444 long *argt;
5445 long *def;
5446 int *flags;
5447 int *compl;
5448 char_u **compl_arg;
5449{
5450 char_u *p;
5451
5452 if (len == 0)
5453 {
5454 EMSG(_("E175: No attribute specified"));
5455 return FAIL;
5456 }
5457
5458 /* First, try the simple attributes (no arguments) */
5459 if (STRNICMP(attr, "bang", len) == 0)
5460 *argt |= BANG;
5461 else if (STRNICMP(attr, "buffer", len) == 0)
5462 *flags |= UC_BUFFER;
5463 else if (STRNICMP(attr, "register", len) == 0)
5464 *argt |= REGSTR;
5465 else if (STRNICMP(attr, "bar", len) == 0)
5466 *argt |= TRLBAR;
5467 else
5468 {
5469 int i;
5470 char_u *val = NULL;
5471 size_t vallen = 0;
5472 size_t attrlen = len;
5473
5474 /* Look for the attribute name - which is the part before any '=' */
5475 for (i = 0; i < (int)len; ++i)
5476 {
5477 if (attr[i] == '=')
5478 {
5479 val = &attr[i + 1];
5480 vallen = len - i - 1;
5481 attrlen = i;
5482 break;
5483 }
5484 }
5485
5486 if (STRNICMP(attr, "nargs", attrlen) == 0)
5487 {
5488 if (vallen == 1)
5489 {
5490 if (*val == '0')
5491 /* Do nothing - this is the default */;
5492 else if (*val == '1')
5493 *argt |= (EXTRA | NOSPC | NEEDARG);
5494 else if (*val == '*')
5495 *argt |= EXTRA;
5496 else if (*val == '?')
5497 *argt |= (EXTRA | NOSPC);
5498 else if (*val == '+')
5499 *argt |= (EXTRA | NEEDARG);
5500 else
5501 goto wrong_nargs;
5502 }
5503 else
5504 {
5505wrong_nargs:
5506 EMSG(_("E176: Invalid number of arguments"));
5507 return FAIL;
5508 }
5509 }
5510 else if (STRNICMP(attr, "range", attrlen) == 0)
5511 {
5512 *argt |= RANGE;
5513 if (vallen == 1 && *val == '%')
5514 *argt |= DFLALL;
5515 else if (val != NULL)
5516 {
5517 p = val;
5518 if (*def >= 0)
5519 {
5520two_count:
5521 EMSG(_("E177: Count cannot be specified twice"));
5522 return FAIL;
5523 }
5524
5525 *def = getdigits(&p);
5526 *argt |= (ZEROR | NOTADR);
5527
5528 if (p != val + vallen || vallen == 0)
5529 {
5530invalid_count:
5531 EMSG(_("E178: Invalid default value for count"));
5532 return FAIL;
5533 }
5534 }
5535 }
5536 else if (STRNICMP(attr, "count", attrlen) == 0)
5537 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005538 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 if (val != NULL)
5541 {
5542 p = val;
5543 if (*def >= 0)
5544 goto two_count;
5545
5546 *def = getdigits(&p);
5547
5548 if (p != val + vallen)
5549 goto invalid_count;
5550 }
5551
5552 if (*def < 0)
5553 *def = 0;
5554 }
5555 else if (STRNICMP(attr, "complete", attrlen) == 0)
5556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (val == NULL)
5558 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005559 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 return FAIL;
5561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005563 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5564 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 else
5568 {
5569 char_u ch = attr[len];
5570 attr[len] = '\0';
5571 EMSG2(_("E181: Invalid attribute: %s"), attr);
5572 attr[len] = ch;
5573 return FAIL;
5574 }
5575 }
5576
5577 return OK;
5578}
5579
Bram Moolenaara850a712009-01-28 14:42:59 +00005580/*
5581 * ":command ..."
5582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 static void
5584ex_command(eap)
5585 exarg_T *eap;
5586{
5587 char_u *name;
5588 char_u *end;
5589 char_u *p;
5590 long argt = 0;
5591 long def = -1;
5592 int flags = 0;
5593 int compl = EXPAND_NOTHING;
5594 char_u *compl_arg = NULL;
5595 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005596 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598 p = eap->arg;
5599
5600 /* Check for attributes */
5601 while (*p == '-')
5602 {
5603 ++p;
5604 end = skiptowhite(p);
5605 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5606 == FAIL)
5607 return;
5608 p = skipwhite(end);
5609 }
5610
5611 /* Get the name (if any) and skip to the following argument */
5612 name = p;
5613 if (ASCII_ISALPHA(*p))
5614 while (ASCII_ISALNUM(*p))
5615 ++p;
5616 if (!ends_excmd(*p) && !vim_iswhite(*p))
5617 {
5618 EMSG(_("E182: Invalid command name"));
5619 return;
5620 }
5621 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005622 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 /* If there is nothing after the name, and no attributes were specified,
5625 * we are listing commands
5626 */
5627 p = skipwhite(end);
5628 if (!has_attr && ends_excmd(*p))
5629 {
5630 uc_list(name, end - name);
5631 }
5632 else if (!ASCII_ISUPPER(*name))
5633 {
5634 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5635 return;
5636 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005637 else if ((name_len == 1 && *name == 'X')
5638 || (name_len <= 4
5639 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5640 {
5641 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5642 return;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 else
5645 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5646 eap->forceit);
5647}
5648
5649/*
5650 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 * Clear all user commands, global and for current buffer.
5652 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005653 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005655 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 uc_clear(&ucmds);
5658 uc_clear(&curbuf->b_ucmds);
5659}
5660
5661/*
5662 * Clear all user commands for "gap".
5663 */
5664 void
5665uc_clear(gap)
5666 garray_T *gap;
5667{
5668 int i;
5669 ucmd_T *cmd;
5670
5671 for (i = 0; i < gap->ga_len; ++i)
5672 {
5673 cmd = USER_CMD_GA(gap, i);
5674 vim_free(cmd->uc_name);
5675 vim_free(cmd->uc_rep);
5676# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5677 vim_free(cmd->uc_compl_arg);
5678# endif
5679 }
5680 ga_clear(gap);
5681}
5682
5683 static void
5684ex_delcommand(eap)
5685 exarg_T *eap;
5686{
5687 int i = 0;
5688 ucmd_T *cmd = NULL;
5689 int cmp = -1;
5690 garray_T *gap;
5691
5692 gap = &curbuf->b_ucmds;
5693 for (;;)
5694 {
5695 for (i = 0; i < gap->ga_len; ++i)
5696 {
5697 cmd = USER_CMD_GA(gap, i);
5698 cmp = STRCMP(eap->arg, cmd->uc_name);
5699 if (cmp <= 0)
5700 break;
5701 }
5702 if (gap == &ucmds || cmp == 0)
5703 break;
5704 gap = &ucmds;
5705 }
5706
5707 if (cmp != 0)
5708 {
5709 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5710 return;
5711 }
5712
5713 vim_free(cmd->uc_name);
5714 vim_free(cmd->uc_rep);
5715# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5716 vim_free(cmd->uc_compl_arg);
5717# endif
5718
5719 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 if (i < gap->ga_len)
5722 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5723}
5724
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005725/*
5726 * split and quote args for <f-args>
5727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 static char_u *
5729uc_split_args(arg, lenp)
5730 char_u *arg;
5731 size_t *lenp;
5732{
5733 char_u *buf;
5734 char_u *p;
5735 char_u *q;
5736 int len;
5737
5738 /* Precalculate length */
5739 p = arg;
5740 len = 2; /* Initial and final quotes */
5741
5742 while (*p)
5743 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005744 if (p[0] == '\\' && p[1] == '\\')
5745 {
5746 len += 2;
5747 p += 2;
5748 }
5749 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 len += 1;
5752 p += 2;
5753 }
5754 else if (*p == '\\' || *p == '"')
5755 {
5756 len += 2;
5757 p += 1;
5758 }
5759 else if (vim_iswhite(*p))
5760 {
5761 p = skipwhite(p);
5762 if (*p == NUL)
5763 break;
5764 len += 3; /* "," */
5765 }
5766 else
5767 {
5768 ++len;
5769 ++p;
5770 }
5771 }
5772
5773 buf = alloc(len + 1);
5774 if (buf == NULL)
5775 {
5776 *lenp = 0;
5777 return buf;
5778 }
5779
5780 p = arg;
5781 q = buf;
5782 *q++ = '"';
5783 while (*p)
5784 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005785 if (p[0] == '\\' && p[1] == '\\')
5786 {
5787 *q++ = '\\';
5788 *q++ = '\\';
5789 p += 2;
5790 }
5791 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 *q++ = p[1];
5794 p += 2;
5795 }
5796 else if (*p == '\\' || *p == '"')
5797 {
5798 *q++ = '\\';
5799 *q++ = *p++;
5800 }
5801 else if (vim_iswhite(*p))
5802 {
5803 p = skipwhite(p);
5804 if (*p == NUL)
5805 break;
5806 *q++ = '"';
5807 *q++ = ',';
5808 *q++ = '"';
5809 }
5810 else
5811 {
5812 *q++ = *p++;
5813 }
5814 }
5815 *q++ = '"';
5816 *q = 0;
5817
5818 *lenp = len;
5819 return buf;
5820}
5821
5822/*
5823 * Check for a <> code in a user command.
5824 * "code" points to the '<'. "len" the length of the <> (inclusive).
5825 * "buf" is where the result is to be added.
5826 * "split_buf" points to a buffer used for splitting, caller should free it.
5827 * "split_len" is the length of what "split_buf" contains.
5828 * Returns the length of the replacement, which has been added to "buf".
5829 * Returns -1 if there was no match, and only the "<" has been copied.
5830 */
5831 static size_t
5832uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5833 char_u *code;
5834 size_t len;
5835 char_u *buf;
5836 ucmd_T *cmd; /* the user command we're expanding */
5837 exarg_T *eap; /* ex arguments */
5838 char_u **split_buf;
5839 size_t *split_len;
5840{
5841 size_t result = 0;
5842 char_u *p = code + 1;
5843 size_t l = len - 2;
5844 int quote = 0;
5845 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5846 ct_LT, ct_NONE } type = ct_NONE;
5847
5848 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5849 {
5850 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5851 p += 2;
5852 l -= 2;
5853 }
5854
Bram Moolenaar371d5402006-03-20 21:47:49 +00005855 ++l;
5856 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005858 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005860 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005862 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005864 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005866 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005868 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005870 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 type = ct_REGISTER;
5872
5873 switch (type)
5874 {
5875 case ct_ARGS:
5876 /* Simple case first */
5877 if (*eap->arg == NUL)
5878 {
5879 if (quote == 1)
5880 {
5881 result = 2;
5882 if (buf != NULL)
5883 STRCPY(buf, "''");
5884 }
5885 else
5886 result = 0;
5887 break;
5888 }
5889
5890 /* When specified there is a single argument don't split it.
5891 * Works for ":Cmd %" when % is "a b c". */
5892 if ((eap->argt & NOSPC) && quote == 2)
5893 quote = 1;
5894
5895 switch (quote)
5896 {
5897 case 0: /* No quoting, no splitting */
5898 result = STRLEN(eap->arg);
5899 if (buf != NULL)
5900 STRCPY(buf, eap->arg);
5901 break;
5902 case 1: /* Quote, but don't split */
5903 result = STRLEN(eap->arg) + 2;
5904 for (p = eap->arg; *p; ++p)
5905 {
5906 if (*p == '\\' || *p == '"')
5907 ++result;
5908 }
5909
5910 if (buf != NULL)
5911 {
5912 *buf++ = '"';
5913 for (p = eap->arg; *p; ++p)
5914 {
5915 if (*p == '\\' || *p == '"')
5916 *buf++ = '\\';
5917 *buf++ = *p;
5918 }
5919 *buf = '"';
5920 }
5921
5922 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005923 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 /* This is hard, so only do it once, and cache the result */
5925 if (*split_buf == NULL)
5926 *split_buf = uc_split_args(eap->arg, split_len);
5927
5928 result = *split_len;
5929 if (buf != NULL && result != 0)
5930 STRCPY(buf, *split_buf);
5931
5932 break;
5933 }
5934 break;
5935
5936 case ct_BANG:
5937 result = eap->forceit ? 1 : 0;
5938 if (quote)
5939 result += 2;
5940 if (buf != NULL)
5941 {
5942 if (quote)
5943 *buf++ = '"';
5944 if (eap->forceit)
5945 *buf++ = '!';
5946 if (quote)
5947 *buf = '"';
5948 }
5949 break;
5950
5951 case ct_LINE1:
5952 case ct_LINE2:
5953 case ct_COUNT:
5954 {
5955 char num_buf[20];
5956 long num = (type == ct_LINE1) ? eap->line1 :
5957 (type == ct_LINE2) ? eap->line2 :
5958 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5959 size_t num_len;
5960
5961 sprintf(num_buf, "%ld", num);
5962 num_len = STRLEN(num_buf);
5963 result = num_len;
5964
5965 if (quote)
5966 result += 2;
5967
5968 if (buf != NULL)
5969 {
5970 if (quote)
5971 *buf++ = '"';
5972 STRCPY(buf, num_buf);
5973 buf += num_len;
5974 if (quote)
5975 *buf = '"';
5976 }
5977
5978 break;
5979 }
5980
5981 case ct_REGISTER:
5982 result = eap->regname ? 1 : 0;
5983 if (quote)
5984 result += 2;
5985 if (buf != NULL)
5986 {
5987 if (quote)
5988 *buf++ = '\'';
5989 if (eap->regname)
5990 *buf++ = eap->regname;
5991 if (quote)
5992 *buf = '\'';
5993 }
5994 break;
5995
5996 case ct_LT:
5997 result = 1;
5998 if (buf != NULL)
5999 *buf = '<';
6000 break;
6001
6002 default:
6003 /* Not recognized: just copy the '<' and return -1. */
6004 result = (size_t)-1;
6005 if (buf != NULL)
6006 *buf = '<';
6007 break;
6008 }
6009
6010 return result;
6011}
6012
6013 static void
6014do_ucmd(eap)
6015 exarg_T *eap;
6016{
6017 char_u *buf;
6018 char_u *p;
6019 char_u *q;
6020
6021 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006022 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006023 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 size_t len, totlen;
6025
6026 size_t split_len = 0;
6027 char_u *split_buf = NULL;
6028 ucmd_T *cmd;
6029#ifdef FEAT_EVAL
6030 scid_T save_current_SID = current_SID;
6031#endif
6032
6033 if (eap->cmdidx == CMD_USER)
6034 cmd = USER_CMD(eap->useridx);
6035 else
6036 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6037
6038 /*
6039 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006040 * First round: "buf" is NULL, compute length, allocate "buf".
6041 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 */
6043 buf = NULL;
6044 for (;;)
6045 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006046 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006047 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006049
6050 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006052 start = vim_strchr(p, '<');
6053 if (start != NULL)
6054 end = vim_strchr(start + 1, '>');
6055 if (buf != NULL)
6056 {
6057 ksp = vim_strchr(p, K_SPECIAL);
6058 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6059 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6060# ifdef FEAT_GUI
6061 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6062# endif
6063 ))
6064 {
6065 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6066 * KS_SPECIAL KE_FILLER, like for mappings, but
6067 * do_cmdline() doesn't handle that, so convert it back.
6068 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6069 len = ksp - p;
6070 if (len > 0)
6071 {
6072 mch_memmove(q, p, len);
6073 q += len;
6074 }
6075 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6076 p = ksp + 3;
6077 continue;
6078 }
6079 }
6080
6081 /* break if there no <item> is found */
6082 if (start == NULL || end == NULL)
6083 break;
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 /* Include the '>' */
6086 ++end;
6087
6088 /* Take everything up to the '<' */
6089 len = start - p;
6090 if (buf == NULL)
6091 totlen += len;
6092 else
6093 {
6094 mch_memmove(q, p, len);
6095 q += len;
6096 }
6097
6098 len = uc_check_code(start, end - start, q, cmd, eap,
6099 &split_buf, &split_len);
6100 if (len == (size_t)-1)
6101 {
6102 /* no match, continue after '<' */
6103 p = start + 1;
6104 len = 1;
6105 }
6106 else
6107 p = end;
6108 if (buf == NULL)
6109 totlen += len;
6110 else
6111 q += len;
6112 }
6113 if (buf != NULL) /* second time here, finished */
6114 {
6115 STRCPY(q, p);
6116 break;
6117 }
6118
6119 totlen += STRLEN(p); /* Add on the trailing characters */
6120 buf = alloc((unsigned)(totlen + 1));
6121 if (buf == NULL)
6122 {
6123 vim_free(split_buf);
6124 return;
6125 }
6126 }
6127
6128#ifdef FEAT_EVAL
6129 current_SID = cmd->uc_scriptID;
6130#endif
6131 (void)do_cmdline(buf, eap->getline, eap->cookie,
6132 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6133#ifdef FEAT_EVAL
6134 current_SID = save_current_SID;
6135#endif
6136 vim_free(buf);
6137 vim_free(split_buf);
6138}
6139
6140# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6141 static char_u *
6142get_user_command_name(idx)
6143 int idx;
6144{
6145 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6146}
6147
6148/*
6149 * Function given to ExpandGeneric() to obtain the list of user command names.
6150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 char_u *
6152get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006153 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 int idx;
6155{
6156 if (idx < curbuf->b_ucmds.ga_len)
6157 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6158 idx -= curbuf->b_ucmds.ga_len;
6159 if (idx < ucmds.ga_len)
6160 return USER_CMD(idx)->uc_name;
6161 return NULL;
6162}
6163
6164/*
6165 * Function given to ExpandGeneric() to obtain the list of user command
6166 * attributes.
6167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 char_u *
6169get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006170 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 int idx;
6172{
6173 static char *user_cmd_flags[] =
6174 {"bang", "bar", "buffer", "complete", "count",
6175 "nargs", "range", "register"};
6176
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006177 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 return NULL;
6179 return (char_u *)user_cmd_flags[idx];
6180}
6181
6182/*
6183 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 char_u *
6186get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006187 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 int idx;
6189{
6190 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6191
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006192 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 return NULL;
6194 return (char_u *)user_cmd_nargs[idx];
6195}
6196
6197/*
6198 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 char_u *
6201get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006202 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int idx;
6204{
6205 return (char_u *)command_complete[idx].name;
6206}
6207# endif /* FEAT_CMDL_COMPL */
6208
6209#endif /* FEAT_USR_CMDS */
6210
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006211#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6212/*
6213 * Parse a completion argument "value[vallen]".
6214 * The detected completion goes in "*complp", argument type in "*argt".
6215 * When there is an argument, for function and user defined completion, it's
6216 * copied to allocated memory and stored in "*compl_arg".
6217 * Returns FAIL if something is wrong.
6218 */
6219 int
6220parse_compl_arg(value, vallen, complp, argt, compl_arg)
6221 char_u *value;
6222 int vallen;
6223 int *complp;
6224 long *argt;
6225 char_u **compl_arg;
6226{
6227 char_u *arg = NULL;
6228 size_t arglen = 0;
6229 int i;
6230 int valend = vallen;
6231
6232 /* Look for any argument part - which is the part after any ',' */
6233 for (i = 0; i < vallen; ++i)
6234 {
6235 if (value[i] == ',')
6236 {
6237 arg = &value[i + 1];
6238 arglen = vallen - i - 1;
6239 valend = i;
6240 break;
6241 }
6242 }
6243
6244 for (i = 0; command_complete[i].expand != 0; ++i)
6245 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006246 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006247 && STRNCMP(value, command_complete[i].name, valend) == 0)
6248 {
6249 *complp = command_complete[i].expand;
6250 if (command_complete[i].expand == EXPAND_BUFFERS)
6251 *argt |= BUFNAME;
6252 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6253 || command_complete[i].expand == EXPAND_FILES)
6254 *argt |= XFILE;
6255 break;
6256 }
6257 }
6258
6259 if (command_complete[i].expand == 0)
6260 {
6261 EMSG2(_("E180: Invalid complete value: %s"), value);
6262 return FAIL;
6263 }
6264
6265# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6266 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6267 && arg != NULL)
6268# else
6269 if (arg != NULL)
6270# endif
6271 {
6272 EMSG(_("E468: Completion argument only allowed for custom completion"));
6273 return FAIL;
6274 }
6275
6276# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6277 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6278 && arg == NULL)
6279 {
6280 EMSG(_("E467: Custom completion requires a function argument"));
6281 return FAIL;
6282 }
6283
6284 if (arg != NULL)
6285 *compl_arg = vim_strnsave(arg, (int)arglen);
6286# endif
6287 return OK;
6288}
6289#endif
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 static void
6292ex_colorscheme(eap)
6293 exarg_T *eap;
6294{
Bram Moolenaare6850792010-05-14 15:28:44 +02006295 if (*eap->arg == NUL)
6296 {
6297#ifdef FEAT_EVAL
6298 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6299 char_u *p = NULL;
6300
6301 if (expr != NULL)
6302 {
6303 ++emsg_off;
6304 p = eval_to_string(expr, NULL, FALSE);
6305 --emsg_off;
6306 vim_free(expr);
6307 }
6308 if (p != NULL)
6309 {
6310 MSG(p);
6311 vim_free(p);
6312 }
6313 else
6314 MSG("default");
6315#else
6316 MSG(_("unknown"));
6317#endif
6318 }
6319 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6321}
6322
6323 static void
6324ex_highlight(eap)
6325 exarg_T *eap;
6326{
6327 if (*eap->arg == NUL && eap->cmd[2] == '!')
6328 MSG(_("Greetings, Vim user!"));
6329 do_highlight(eap->arg, eap->forceit, FALSE);
6330}
6331
6332
6333/*
6334 * Call this function if we thought we were going to exit, but we won't
6335 * (because of an error). May need to restore the terminal mode.
6336 */
6337 void
6338not_exiting()
6339{
6340 exiting = FALSE;
6341 settmode(TMODE_RAW);
6342}
6343
6344/*
6345 * ":quit": quit current window, quit Vim if closed the last window.
6346 */
6347 static void
6348ex_quit(eap)
6349 exarg_T *eap;
6350{
6351#ifdef FEAT_CMDWIN
6352 if (cmdwin_type != 0)
6353 {
6354 cmdwin_result = Ctrl_C;
6355 return;
6356 }
6357#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006358 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006359 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006360 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006361 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006362 return;
6363 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006364#ifdef FEAT_AUTOCMD
6365 if (curbuf_locked())
6366 return;
6367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368
6369#ifdef FEAT_NETBEANS_INTG
6370 netbeansForcedQuit = eap->forceit;
6371#endif
6372
6373 /*
6374 * If there are more files or windows we won't exit.
6375 */
6376 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6377 exiting = TRUE;
6378 if ((!P_HID(curbuf)
6379 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6380 || check_more(TRUE, eap->forceit) == FAIL
6381 || (only_one_window() && check_changed_any(eap->forceit)))
6382 {
6383 not_exiting();
6384 }
6385 else
6386 {
6387#ifdef FEAT_WINDOWS
6388 if (only_one_window()) /* quit last window */
6389#endif
6390 getout(0);
6391#ifdef FEAT_WINDOWS
6392# ifdef FEAT_GUI
6393 need_mouse_correct = TRUE;
6394# endif
6395 /* close window; may free buffer */
6396 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6397#endif
6398 }
6399}
6400
6401/*
6402 * ":cquit".
6403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 static void
6405ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006406 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408 getout(1); /* this does not always pass on the exit code to the Manx
6409 compiler. why? */
6410}
6411
6412/*
6413 * ":qall": try to quit all windows
6414 */
6415 static void
6416ex_quit_all(eap)
6417 exarg_T *eap;
6418{
6419# ifdef FEAT_CMDWIN
6420 if (cmdwin_type != 0)
6421 {
6422 if (eap->forceit)
6423 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6424 else
6425 cmdwin_result = K_XF2;
6426 return;
6427 }
6428# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006429
6430 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006431 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006432 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006433 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006434 return;
6435 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006436#ifdef FEAT_AUTOCMD
6437 if (curbuf_locked())
6438 return;
6439#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 exiting = TRUE;
6442 if (eap->forceit || !check_changed_any(FALSE))
6443 getout(0);
6444 not_exiting();
6445}
6446
Bram Moolenaard9967712006-03-11 21:18:15 +00006447#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448/*
6449 * ":close": close current window, unless it is the last one
6450 */
6451 static void
6452ex_close(eap)
6453 exarg_T *eap;
6454{
6455# ifdef FEAT_CMDWIN
6456 if (cmdwin_type != 0)
6457 cmdwin_result = K_IGNORE;
6458 else
6459# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460 if (!text_locked()
6461#ifdef FEAT_AUTOCMD
6462 && !curbuf_locked()
6463#endif
6464 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006465 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466}
6467
Bram Moolenaard9967712006-03-11 21:18:15 +00006468# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006469/*
6470 * ":pclose": Close any preview window.
6471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475{
6476 win_T *win;
6477
6478 for (win = firstwin; win != NULL; win = win->w_next)
6479 if (win->w_p_pvw)
6480 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006481 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482 break;
6483 }
6484}
Bram Moolenaard9967712006-03-11 21:18:15 +00006485# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486
Bram Moolenaarf740b292006-02-16 22:11:02 +00006487/*
6488 * Close window "win" and take care of handling closing the last window for a
6489 * modified buffer.
6490 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006492ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006495 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 int need_hide;
6498 buf_T *buf = win->w_buffer;
6499
6500 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006501 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006503# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 if ((p_confirm || cmdmod.confirm) && p_write)
6505 {
6506 dialog_changed(buf, FALSE);
6507 if (buf_valid(buf) && bufIsChanged(buf))
6508 return;
6509 need_hide = FALSE;
6510 }
6511 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
6514 EMSG(_(e_nowrtmsg));
6515 return;
6516 }
6517 }
6518
Bram Moolenaard9967712006-03-11 21:18:15 +00006519# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006521# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524 if (tp == NULL)
6525 win_close(win, !need_hide && !P_HID(buf));
6526 else
6527 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006531 * ":tabclose": close current tab page, unless it is the last one.
6532 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 */
6534 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006535ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 exarg_T *eap;
6537{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006538 tabpage_T *tp;
6539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540# ifdef FEAT_CMDWIN
6541 if (cmdwin_type != 0)
6542 cmdwin_result = K_IGNORE;
6543 else
6544# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006545 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006546 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006547 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006550 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 tp = find_tabpage((int)eap->line2);
6552 if (tp == NULL)
6553 {
6554 beep_flush();
6555 return;
6556 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006557 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006558 {
6559 tabpage_close_other(tp, eap->forceit);
6560 return;
6561 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006562 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006563 if (!text_locked()
6564#ifdef FEAT_AUTOCMD
6565 && !curbuf_locked()
6566#endif
6567 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006570}
6571
6572/*
6573 * ":tabonly": close all tab pages except the current one
6574 */
6575 static void
6576ex_tabonly(eap)
6577 exarg_T *eap;
6578{
6579 tabpage_T *tp;
6580 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006581
6582# ifdef FEAT_CMDWIN
6583 if (cmdwin_type != 0)
6584 cmdwin_result = K_IGNORE;
6585 else
6586# endif
6587 if (first_tabpage->tp_next == NULL)
6588 MSG(_("Already only one tab page"));
6589 else
6590 {
6591 /* Repeat this up to a 1000 times, because autocommands may mess
6592 * up the lists. */
6593 for (done = 0; done < 1000; ++done)
6594 {
6595 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6596 if (tp->tp_topframe != topframe)
6597 {
6598 tabpage_close_other(tp, eap->forceit);
6599 /* if we failed to close it quit */
6600 if (valid_tabpage(tp))
6601 done = 1000;
6602 /* start over, "tp" is now invalid */
6603 break;
6604 }
6605 if (first_tabpage->tp_next == NULL)
6606 break;
6607 }
6608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
6611/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006612 * Close the current tab page.
6613 */
6614 void
6615tabpage_close(forceit)
6616 int forceit;
6617{
6618 /* First close all the windows but the current one. If that worked then
6619 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006620 if (lastwin != firstwin)
6621 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006622 if (lastwin == firstwin)
6623 ex_win_close(forceit, curwin, NULL);
6624# ifdef FEAT_GUI
6625 need_mouse_correct = TRUE;
6626# endif
6627}
6628
6629/*
6630 * Close tab page "tp", which is not the current tab page.
6631 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006632 * Also takes care of the tab pages line disappearing when closing the
6633 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006634 */
6635 void
6636tabpage_close_other(tp, forceit)
6637 tabpage_T *tp;
6638 int forceit;
6639{
6640 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006641 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006642 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006643
6644 /* Limit to 1000 windows, autocommands may add a window while we close
6645 * one. OK, so I'm paranoid... */
6646 while (++done < 1000)
6647 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006648 wp = tp->tp_firstwin;
6649 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006650
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006651 /* Autocommands may delete the tab page under our fingers and we may
6652 * fail to close a window with a modified buffer. */
6653 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006654 break;
6655 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006656
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006657 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006658 if (h != tabline_height())
6659 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006660}
6661
6662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 * ":only".
6664 */
6665 static void
6666ex_only(eap)
6667 exarg_T *eap;
6668{
6669# ifdef FEAT_GUI
6670 need_mouse_correct = TRUE;
6671# endif
6672 close_others(TRUE, eap->forceit);
6673}
6674
6675/*
6676 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006677 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006679 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680ex_all(eap)
6681 exarg_T *eap;
6682{
6683 if (eap->addr_count == 0)
6684 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006685 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686}
6687#endif /* FEAT_WINDOWS */
6688
6689 static void
6690ex_hide(eap)
6691 exarg_T *eap;
6692{
6693 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6694 eap->errmsg = e_invarg;
6695 else
6696 {
6697 /* ":hide" or ":hide | cmd": hide current window */
6698 eap->nextcmd = check_nextcmd(eap->arg);
6699#ifdef FEAT_WINDOWS
6700 if (!eap->skip)
6701 {
6702# ifdef FEAT_GUI
6703 need_mouse_correct = TRUE;
6704# endif
6705 win_close(curwin, FALSE); /* don't free buffer */
6706 }
6707#endif
6708 }
6709}
6710
6711/*
6712 * ":stop" and ":suspend": Suspend Vim.
6713 */
6714 static void
6715ex_stop(eap)
6716 exarg_T *eap;
6717{
6718 /*
6719 * Disallow suspending for "rvim".
6720 */
6721 if (!check_restricted()
6722#ifdef WIN3264
6723 /*
6724 * Check if external commands are allowed now.
6725 */
6726 && can_end_termcap_mode(TRUE)
6727#endif
6728 )
6729 {
6730 if (!eap->forceit)
6731 autowrite_all();
6732 windgoto((int)Rows - 1, 0);
6733 out_char('\n');
6734 out_flush();
6735 stoptermcap();
6736 out_flush(); /* needed for SUN to restore xterm buffer */
6737#ifdef FEAT_TITLE
6738 mch_restore_title(3); /* restore window titles */
6739#endif
6740 ui_suspend(); /* call machine specific function */
6741#ifdef FEAT_TITLE
6742 maketitle();
6743 resettitle(); /* force updating the title */
6744#endif
6745 starttermcap();
6746 scroll_start(); /* scroll screen before redrawing */
6747 redraw_later_clear();
6748 shell_resized(); /* may have resized window */
6749 }
6750}
6751
6752/*
6753 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6754 */
6755 static void
6756ex_exit(eap)
6757 exarg_T *eap;
6758{
6759#ifdef FEAT_CMDWIN
6760 if (cmdwin_type != 0)
6761 {
6762 cmdwin_result = Ctrl_C;
6763 return;
6764 }
6765#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006766 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006767 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006768 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006769 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006770 return;
6771 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006772#ifdef FEAT_AUTOCMD
6773 if (curbuf_locked())
6774 return;
6775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777 /*
6778 * if more files or windows we won't exit
6779 */
6780 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6781 exiting = TRUE;
6782 if ( ((eap->cmdidx == CMD_wq
6783 || curbufIsChanged())
6784 && do_write(eap) == FAIL)
6785 || check_more(TRUE, eap->forceit) == FAIL
6786 || (only_one_window() && check_changed_any(eap->forceit)))
6787 {
6788 not_exiting();
6789 }
6790 else
6791 {
6792#ifdef FEAT_WINDOWS
6793 if (only_one_window()) /* quit last window, exit Vim */
6794#endif
6795 getout(0);
6796#ifdef FEAT_WINDOWS
6797# ifdef FEAT_GUI
6798 need_mouse_correct = TRUE;
6799# endif
6800 /* quit current window, may free buffer */
6801 win_close(curwin, !P_HID(curwin->w_buffer));
6802#endif
6803 }
6804}
6805
6806/*
6807 * ":print", ":list", ":number".
6808 */
6809 static void
6810ex_print(eap)
6811 exarg_T *eap;
6812{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6814 EMSG(_(e_emptybuf));
6815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006817 for ( ;!got_int; ui_breakcheck())
6818 {
6819 print_line(eap->line1,
6820 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6821 || (eap->flags & EXFLAG_NR)),
6822 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6823 if (++eap->line1 > eap->line2)
6824 break;
6825 out_flush(); /* show one line at a time */
6826 }
6827 setpcmark();
6828 /* put cursor at last line */
6829 curwin->w_cursor.lnum = eap->line2;
6830 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834}
6835
6836#ifdef FEAT_BYTEOFF
6837 static void
6838ex_goto(eap)
6839 exarg_T *eap;
6840{
6841 goto_byte(eap->line2);
6842}
6843#endif
6844
6845/*
6846 * ":shell".
6847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 static void
6849ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006850 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 do_shell(NULL, 0);
6853}
6854
6855#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6856 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006857 || defined(FEAT_GUI_MSWIN) \
6858 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 || defined(PROTO)
6860
6861/*
6862 * Handle a file drop. The code is here because a drop is *nearly* like an
6863 * :args command, but not quite (we have a list of exact filenames, so we
6864 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6865 * code is very similar to :args and hence needs access to a lot of the static
6866 * functions in this file.
6867 *
6868 * The list should be allocated using alloc(), as should each item in the
6869 * list. This function takes over responsibility for freeing the list.
6870 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006871 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6873 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6874 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6875 * file functionality is (currently) not in EMX this is not presently a
6876 * problem.
6877 */
6878 void
6879handle_drop(filec, filev, split)
6880 int filec; /* the number of files dropped */
6881 char_u **filev; /* the list of files dropped */
6882 int split; /* force splitting the window */
6883{
6884 exarg_T ea;
6885 int save_msg_scroll = msg_scroll;
6886
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006887 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006888 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006890#ifdef FEAT_AUTOCMD
6891 if (curbuf_locked())
6892 return;
6893#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006894 /* When the screen is being updated we should not change buffers and
6895 * windows structures, it may cause freed memory to be used. */
6896 if (updating_screen)
6897 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
6899 /* Check whether the current buffer is changed. If so, we will need
6900 * to split the current window or data could be lost.
6901 * We don't need to check if the 'hidden' option is set, as in this
6902 * case the buffer won't be lost.
6903 */
6904 if (!P_HID(curbuf) && !split)
6905 {
6906 ++emsg_off;
6907 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6908 --emsg_off;
6909 }
6910 if (split)
6911 {
6912# ifdef FEAT_WINDOWS
6913 if (win_split(0, 0) == FAIL)
6914 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006915 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
6917 /* When splitting the window, create a new alist. Otherwise the
6918 * existing one is overwritten. */
6919 alist_unlink(curwin->w_alist);
6920 alist_new();
6921# else
6922 return; /* can't split, always fail */
6923# endif
6924 }
6925
6926 /*
6927 * Set up the new argument list.
6928 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006929 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
6931 /*
6932 * Move to the first file.
6933 */
6934 /* Fake up a minimal "next" command for do_argfile() */
6935 vim_memset(&ea, 0, sizeof(ea));
6936 ea.cmd = (char_u *)"next";
6937 do_argfile(&ea, 0);
6938
6939 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6940 * mode that is not needed here. */
6941 need_start_insertmode = FALSE;
6942
6943 /* Restore msg_scroll, otherwise a following command may cause scrolling
6944 * unexpectedly. The screen will be redrawn by the caller, thus
6945 * msg_scroll being set by displaying a message is irrelevant. */
6946 msg_scroll = save_msg_scroll;
6947}
6948#endif
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950/*
6951 * Clear an argument list: free all file names and reset it to zero entries.
6952 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006953 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954alist_clear(al)
6955 alist_T *al;
6956{
6957 while (--al->al_ga.ga_len >= 0)
6958 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6959 ga_clear(&al->al_ga);
6960}
6961
6962/*
6963 * Init an argument list.
6964 */
6965 void
6966alist_init(al)
6967 alist_T *al;
6968{
6969 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6970}
6971
6972#if defined(FEAT_WINDOWS) || defined(PROTO)
6973
6974/*
6975 * Remove a reference from an argument list.
6976 * Ignored when the argument list is the global one.
6977 * If the argument list is no longer used by any window, free it.
6978 */
6979 void
6980alist_unlink(al)
6981 alist_T *al;
6982{
6983 if (al != &global_alist && --al->al_refcount <= 0)
6984 {
6985 alist_clear(al);
6986 vim_free(al);
6987 }
6988}
6989
6990# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6991/*
6992 * Create a new argument list and use it for the current window.
6993 */
6994 void
6995alist_new()
6996{
6997 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6998 if (curwin->w_alist == NULL)
6999 {
7000 curwin->w_alist = &global_alist;
7001 ++global_alist.al_refcount;
7002 }
7003 else
7004 {
7005 curwin->w_alist->al_refcount = 1;
7006 alist_init(curwin->w_alist);
7007 }
7008}
7009# endif
7010#endif
7011
7012#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7013/*
7014 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007015 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7016 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 */
7018 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007019alist_expand(fnum_list, fnum_len)
7020 int *fnum_list;
7021 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007024 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 char_u **new_arg_files;
7026 int new_arg_file_count;
7027 char_u *save_p_su = p_su;
7028 int i;
7029
7030 /* Don't use 'suffixes' here. This should work like the shell did the
7031 * expansion. Also, the vimrc file isn't read yet, thus the user
7032 * can't set the options. */
7033 p_su = empty_option;
7034 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7035 if (old_arg_files != NULL)
7036 {
7037 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007038 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7039 old_arg_count = GARGCOUNT;
7040 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 &new_arg_file_count, &new_arg_files,
7042 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7043 && new_arg_file_count > 0)
7044 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007045 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7046 TRUE, fnum_list, fnum_len);
7047 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050 p_su = save_p_su;
7051}
7052#endif
7053
7054/*
7055 * Set the argument list for the current window.
7056 * Takes over the allocated files[] and the allocated fnames in it.
7057 */
7058 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007059alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 alist_T *al;
7061 int count;
7062 char_u **files;
7063 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007064 int *fnum_list;
7065 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 int i;
7068
7069 alist_clear(al);
7070 if (ga_grow(&al->al_ga, count) == OK)
7071 {
7072 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007073 {
7074 if (got_int)
7075 {
7076 /* When adding many buffers this can take a long time. Allow
7077 * interrupting here. */
7078 while (i < count)
7079 vim_free(files[i++]);
7080 break;
7081 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007082
7083 /* May set buffer name of a buffer previously used for the
7084 * argument list, so that it's re-used by alist_add. */
7085 if (fnum_list != NULL && i < fnum_len)
7086 buf_set_name(fnum_list[i], files[i]);
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007089 ui_breakcheck();
7090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 vim_free(files);
7092 }
7093 else
7094 FreeWild(count, files);
7095#ifdef FEAT_WINDOWS
7096 if (al == &global_alist)
7097#endif
7098 arg_had_last = FALSE;
7099}
7100
7101/*
7102 * Add file "fname" to argument list "al".
7103 * "fname" must have been allocated and "al" must have been checked for room.
7104 */
7105 void
7106alist_add(al, fname, set_fnum)
7107 alist_T *al;
7108 char_u *fname;
7109 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7110{
7111 if (fname == NULL) /* don't add NULL file names */
7112 return;
7113#ifdef BACKSLASH_IN_FILENAME
7114 slash_adjust(fname);
7115#endif
7116 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7117 if (set_fnum > 0)
7118 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7119 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7120 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121}
7122
7123#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7124/*
7125 * Adjust slashes in file names. Called after 'shellslash' was set.
7126 */
7127 void
7128alist_slash_adjust()
7129{
7130 int i;
7131# ifdef FEAT_WINDOWS
7132 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007133 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134# endif
7135
7136 for (i = 0; i < GARGCOUNT; ++i)
7137 if (GARGLIST[i].ae_fname != NULL)
7138 slash_adjust(GARGLIST[i].ae_fname);
7139# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007140 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (wp->w_alist != &global_alist)
7142 for (i = 0; i < WARGCOUNT(wp); ++i)
7143 if (WARGLIST(wp)[i].ae_fname != NULL)
7144 slash_adjust(WARGLIST(wp)[i].ae_fname);
7145# endif
7146}
7147#endif
7148
7149/*
7150 * ":preserve".
7151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 static void
7153ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007154 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007156 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 ml_preserve(curbuf, TRUE);
7158}
7159
7160/*
7161 * ":recover".
7162 */
7163 static void
7164ex_recover(eap)
7165 exarg_T *eap;
7166{
7167 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7168 recoverymode = TRUE;
7169 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7170 && (*eap->arg == NUL
7171 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7172 ml_recover();
7173 recoverymode = FALSE;
7174}
7175
7176/*
7177 * Command modifier used in a wrong way.
7178 */
7179 static void
7180ex_wrongmodifier(eap)
7181 exarg_T *eap;
7182{
7183 eap->errmsg = e_invcmd;
7184}
7185
7186#ifdef FEAT_WINDOWS
7187/*
7188 * :sview [+command] file split window with new file, read-only
7189 * :split [[+command] file] split window with current or new file
7190 * :vsplit [[+command] file] split window vertically with current or new file
7191 * :new [[+command] file] split window with no or new file
7192 * :vnew [[+command] file] split vertically window with no or new file
7193 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007194 *
7195 * :tabedit open new Tab page with empty window
7196 * :tabedit [+command] file open new Tab page and edit "file"
7197 * :tabnew [[+command] file] just like :tabedit
7198 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 */
7200 void
7201ex_splitview(eap)
7202 exarg_T *eap;
7203{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007204 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007205# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207# endif
7208# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007212# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7214 {
7215 ex_ni(eap);
7216 return;
7217 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007220# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007224# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007226 * quickfix windows. But it's OK when doing ":tab split". */
7227 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
7229 if (eap->cmdidx == CMD_split)
7230 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007231# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 if (eap->cmdidx == CMD_vsplit)
7233 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007238# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007239 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
7241 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7242 FNAME_MESS, TRUE, curbuf->b_ffname);
7243 if (fname == NULL)
7244 goto theend;
7245 eap->arg = fname;
7246 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007247# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007251# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007253# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 && eap->cmdidx != CMD_new)
7257 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007258# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007259 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007260# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007261 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007262# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007263 au_has_group((char_u *)"FileExplorer"))
7264 {
7265 /* No browsing supported but we do have the file explorer:
7266 * Edit the directory. */
7267 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7268 eap->arg = (char_u *)".";
7269 }
7270 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007271# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007272 {
7273 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007275 if (fname == NULL)
7276 goto theend;
7277 eap->arg = fname;
7278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007283 /*
7284 * Either open new tab page or split the window.
7285 */
7286 if (eap->cmdidx == CMD_tabedit
7287 || eap->cmdidx == CMD_tabfind
7288 || eap->cmdidx == CMD_tabnew)
7289 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007290 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7291 : eap->addr_count == 0 ? 0
7292 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007293 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007294 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007295
7296 /* set the alternate buffer for the window we came from */
7297 if (curwin != old_curwin
7298 && win_valid(old_curwin)
7299 && old_curwin->w_buffer != curbuf
7300 && !cmdmod.keepalt)
7301 old_curwin->w_alt_fnum = curbuf->b_fnum;
7302 }
7303 }
7304 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7306 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007307# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 /* Reset 'scrollbind' when editing another file, but keep it when
7309 * doing ":split" without arguments. */
7310 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007315 {
7316 RESET_BINDING(curwin);
7317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 else
7319 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 do_exedit(eap, old_curwin);
7322 }
7323
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007324# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007328# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329theend:
7330 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007333
7334/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007335 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007336 */
7337 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007338tabpage_new()
7339{
7340 exarg_T ea;
7341
7342 vim_memset(&ea, 0, sizeof(ea));
7343 ea.cmdidx = CMD_tabnew;
7344 ea.cmd = (char_u *)"tabn";
7345 ea.arg = (char_u *)"";
7346 ex_splitview(&ea);
7347}
7348
7349/*
7350 * :tabnext command
7351 */
7352 static void
7353ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354 exarg_T *eap;
7355{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007356 switch (eap->cmdidx)
7357 {
7358 case CMD_tabfirst:
7359 case CMD_tabrewind:
7360 goto_tabpage(1);
7361 break;
7362 case CMD_tablast:
7363 goto_tabpage(9999);
7364 break;
7365 case CMD_tabprevious:
7366 case CMD_tabNext:
7367 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7368 break;
7369 default: /* CMD_tabnext */
7370 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7371 break;
7372 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007373}
7374
7375/*
7376 * :tabmove command
7377 */
7378 static void
7379ex_tabmove(eap)
7380 exarg_T *eap;
7381{
7382 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383}
7384
7385/*
7386 * :tabs command: List tabs and their contents.
7387 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388 static void
7389ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007390 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007391{
7392 tabpage_T *tp;
7393 win_T *wp;
7394 int tabcount = 1;
7395
7396 msg_start();
7397 msg_scroll = TRUE;
7398 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7399 {
7400 msg_putchar('\n');
7401 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7402 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7403 out_flush(); /* output one line at a time */
7404 ui_breakcheck();
7405
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007406 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407 wp = firstwin;
7408 else
7409 wp = tp->tp_firstwin;
7410 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7411 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007412 msg_putchar('\n');
7413 msg_putchar(wp == curwin ? '>' : ' ');
7414 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007415 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7416 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417 if (buf_spname(wp->w_buffer) != NULL)
7418 STRCPY(IObuff, buf_spname(wp->w_buffer));
7419 else
7420 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7421 IObuff, IOSIZE, TRUE);
7422 msg_outtrans(IObuff);
7423 out_flush(); /* output one line at a time */
7424 ui_breakcheck();
7425 }
7426 }
7427}
7428
7429#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431/*
7432 * ":mode": Set screen mode.
7433 * If no argument given, just get the screen size and redraw.
7434 */
7435 static void
7436ex_mode(eap)
7437 exarg_T *eap;
7438{
7439 if (*eap->arg == NUL)
7440 shell_resized();
7441 else
7442 mch_screenmode(eap->arg);
7443}
7444
7445#ifdef FEAT_WINDOWS
7446/*
7447 * ":resize".
7448 * set, increment or decrement current window height
7449 */
7450 static void
7451ex_resize(eap)
7452 exarg_T *eap;
7453{
7454 int n;
7455 win_T *wp = curwin;
7456
7457 if (eap->addr_count > 0)
7458 {
7459 n = eap->line2;
7460 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7461 ;
7462 }
7463
7464#ifdef FEAT_GUI
7465 need_mouse_correct = TRUE;
7466#endif
7467 n = atol((char *)eap->arg);
7468#ifdef FEAT_VERTSPLIT
7469 if (cmdmod.split & WSP_VERT)
7470 {
7471 if (*eap->arg == '-' || *eap->arg == '+')
7472 n += W_WIDTH(curwin);
7473 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7474 n = 9999;
7475 win_setwidth_win((int)n, wp);
7476 }
7477 else
7478#endif
7479 {
7480 if (*eap->arg == '-' || *eap->arg == '+')
7481 n += curwin->w_height;
7482 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7483 n = 9999;
7484 win_setheight_win((int)n, wp);
7485 }
7486}
7487#endif
7488
7489/*
7490 * ":find [+command] <file>" command.
7491 */
7492 static void
7493ex_find(eap)
7494 exarg_T *eap;
7495{
7496#ifdef FEAT_SEARCHPATH
7497 char_u *fname;
7498 int count;
7499
7500 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7501 TRUE, curbuf->b_ffname);
7502 if (eap->addr_count > 0)
7503 {
7504 /* Repeat finding the file "count" times. This matters when it
7505 * appears several times in the path. */
7506 count = eap->line2;
7507 while (fname != NULL && --count > 0)
7508 {
7509 vim_free(fname);
7510 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7511 FALSE, curbuf->b_ffname);
7512 }
7513 }
7514
7515 if (fname != NULL)
7516 {
7517 eap->arg = fname;
7518#endif
7519 do_exedit(eap, NULL);
7520#ifdef FEAT_SEARCHPATH
7521 vim_free(fname);
7522 }
7523#endif
7524}
7525
7526/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007527 * ":open" simulation: for now just work like ":visual".
7528 */
7529 static void
7530ex_open(eap)
7531 exarg_T *eap;
7532{
7533 regmatch_T regmatch;
7534 char_u *p;
7535
7536 curwin->w_cursor.lnum = eap->line2;
7537 beginline(BL_SOL | BL_FIX);
7538 if (*eap->arg == '/')
7539 {
7540 /* ":open /pattern/": put cursor in column found with pattern */
7541 ++eap->arg;
7542 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7543 *p = NUL;
7544 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7545 if (regmatch.regprog != NULL)
7546 {
7547 regmatch.rm_ic = p_ic;
7548 p = ml_get_curline();
7549 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007550 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007551 else
7552 EMSG(_(e_nomatch));
7553 vim_free(regmatch.regprog);
7554 }
7555 /* Move to the NUL, ignore any other arguments. */
7556 eap->arg += STRLEN(eap->arg);
7557 }
7558 check_cursor();
7559
7560 eap->cmdidx = CMD_visual;
7561 do_exedit(eap, NULL);
7562}
7563
7564/*
7565 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 */
7567 static void
7568ex_edit(eap)
7569 exarg_T *eap;
7570{
7571 do_exedit(eap, NULL);
7572}
7573
7574/*
7575 * ":edit <file>" command and alikes.
7576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 void
7578do_exedit(eap, old_curwin)
7579 exarg_T *eap;
7580 win_T *old_curwin; /* curwin before doing a split or NULL */
7581{
7582 int n;
7583#ifdef FEAT_WINDOWS
7584 int need_hide;
7585#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007586 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587
7588 /*
7589 * ":vi" command ends Ex mode.
7590 */
7591 if (exmode_active && (eap->cmdidx == CMD_visual
7592 || eap->cmdidx == CMD_view))
7593 {
7594 exmode_active = FALSE;
7595 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007596 {
7597 /* Special case: ":global/pat/visual\NLvi-commands" */
7598 if (global_busy)
7599 {
7600 int rd = RedrawingDisabled;
7601 int nwr = no_wait_return;
7602 int ms = msg_scroll;
7603#ifdef FEAT_GUI
7604 int he = hold_gui_events;
7605#endif
7606
7607 if (eap->nextcmd != NULL)
7608 {
7609 stuffReadbuff(eap->nextcmd);
7610 eap->nextcmd = NULL;
7611 }
7612
7613 if (exmode_was != EXMODE_VIM)
7614 settmode(TMODE_RAW);
7615 RedrawingDisabled = 0;
7616 no_wait_return = 0;
7617 need_wait_return = FALSE;
7618 msg_scroll = 0;
7619#ifdef FEAT_GUI
7620 hold_gui_events = 0;
7621#endif
7622 must_redraw = CLEAR;
7623
7624 main_loop(FALSE, TRUE);
7625
7626 RedrawingDisabled = rd;
7627 no_wait_return = nwr;
7628 msg_scroll = ms;
7629#ifdef FEAT_GUI
7630 hold_gui_events = he;
7631#endif
7632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636
7637 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007638 || eap->cmdidx == CMD_tabnew
7639 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640#ifdef FEAT_VERTSPLIT
7641 || eap->cmdidx == CMD_vnew
7642#endif
7643 ) && *eap->arg == NUL)
7644 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007645 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 setpcmark();
7647 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007648 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7649 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651 else if ((eap->cmdidx != CMD_split
7652#ifdef FEAT_VERTSPLIT
7653 && eap->cmdidx != CMD_vsplit
7654#endif
7655 )
7656 || *eap->arg != NUL
7657#ifdef FEAT_BROWSE
7658 || cmdmod.browse
7659#endif
7660 )
7661 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007662#ifdef FEAT_AUTOCMD
7663 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7664 * can bring us here, others are stopped earlier. */
7665 if (*eap->arg != NUL && curbuf_locked())
7666 return;
7667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 n = readonlymode;
7669 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7670 readonlymode = TRUE;
7671 else if (eap->cmdidx == CMD_enew)
7672 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7673 empty buffer */
7674 setpcmark();
7675 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7676 NULL, eap,
7677 /* ":edit" goes to first line if Vi compatible */
7678 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7679 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7680 ? ECMD_ONE : eap->do_ecmd_lnum,
7681 (P_HID(curbuf) ? ECMD_HIDE : 0)
7682 + (eap->forceit ? ECMD_FORCEIT : 0)
7683#ifdef FEAT_LISTCMDS
7684 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7685#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007686 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 /* Editing the file failed. If the window was split, close it. */
7689#ifdef FEAT_WINDOWS
7690 if (old_curwin != NULL)
7691 {
7692 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7693 if (!need_hide || P_HID(curbuf))
7694 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007695# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7696 cleanup_T cs;
7697
7698 /* Reset the error/interrupt/exception state here so that
7699 * aborting() returns FALSE when closing a window. */
7700 enter_cleanup(&cs);
7701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702# ifdef FEAT_GUI
7703 need_mouse_correct = TRUE;
7704# endif
7705 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007706
7707# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7708 /* Restore the error/interrupt/exception state if not
7709 * discarded by a new aborting error, interrupt, or
7710 * uncaught exception. */
7711 leave_cleanup(&cs);
7712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714 }
7715#endif
7716 }
7717 else if (readonlymode && curbuf->b_nwindows == 1)
7718 {
7719 /* When editing an already visited buffer, 'readonly' won't be set
7720 * but the previous value is kept. With ":view" and ":sview" we
7721 * want the file to be readonly, except when another window is
7722 * editing the same buffer. */
7723 curbuf->b_p_ro = TRUE;
7724 }
7725 readonlymode = n;
7726 }
7727 else
7728 {
7729 if (eap->do_ecmd_cmd != NULL)
7730 do_cmdline_cmd(eap->do_ecmd_cmd);
7731#ifdef FEAT_TITLE
7732 n = curwin->w_arg_idx_invalid;
7733#endif
7734 check_arg_idx(curwin);
7735#ifdef FEAT_TITLE
7736 if (n != curwin->w_arg_idx_invalid)
7737 maketitle();
7738#endif
7739 }
7740
7741#ifdef FEAT_WINDOWS
7742 /*
7743 * if ":split file" worked, set alternate file name in old window to new
7744 * file
7745 */
7746 if (old_curwin != NULL
7747 && *eap->arg != NUL
7748 && curwin != old_curwin
7749 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007750 && old_curwin->w_buffer != curbuf
7751 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 old_curwin->w_alt_fnum = curbuf->b_fnum;
7753#endif
7754
7755 ex_no_reprint = TRUE;
7756}
7757
7758#ifndef FEAT_GUI
7759/*
7760 * ":gui" and ":gvim" when there is no GUI.
7761 */
7762 static void
7763ex_nogui(eap)
7764 exarg_T *eap;
7765{
7766 eap->errmsg = e_nogvim;
7767}
7768#endif
7769
7770#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7771 static void
7772ex_tearoff(eap)
7773 exarg_T *eap;
7774{
7775 gui_make_tearoff(eap->arg);
7776}
7777#endif
7778
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007779#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 static void
7781ex_popup(eap)
7782 exarg_T *eap;
7783{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007784 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786#endif
7787
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 static void
7789ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007790 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7793 MSG(_("No swap file"));
7794 else
7795 msg(curbuf->b_ml.ml_mfp->mf_fname);
7796}
7797
7798/*
7799 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7800 * offset.
7801 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 static void
7804ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007805 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807#ifdef FEAT_SCROLLBIND
7808 win_T *wp;
7809 long topline;
7810 long y;
7811 linenr_T old_linenr = curwin->w_cursor.lnum;
7812
7813 setpcmark();
7814
7815 /*
7816 * determine max topline
7817 */
7818 if (curwin->w_p_scb)
7819 {
7820 topline = curwin->w_topline;
7821 for (wp = firstwin; wp; wp = wp->w_next)
7822 {
7823 if (wp->w_p_scb && wp->w_buffer)
7824 {
7825 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7826 if (topline > y)
7827 topline = y;
7828 }
7829 }
7830 if (topline < 1)
7831 topline = 1;
7832 }
7833 else
7834 {
7835 topline = 1;
7836 }
7837
7838
7839 /*
7840 * set all scrollbind windows to the same topline
7841 */
7842 wp = curwin;
7843 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7844 {
7845 if (curwin->w_p_scb)
7846 {
7847 y = topline - curwin->w_topline;
7848 if (y > 0)
7849 scrollup(y, TRUE);
7850 else
7851 scrolldown(-y, TRUE);
7852 curwin->w_scbind_pos = topline;
7853 redraw_later(VALID);
7854 cursor_correct();
7855#ifdef FEAT_WINDOWS
7856 curwin->w_redr_status = TRUE;
7857#endif
7858 }
7859 }
7860 curwin = wp;
7861 if (curwin->w_p_scb)
7862 {
7863 did_syncbind = TRUE;
7864 checkpcmark();
7865 if (old_linenr != curwin->w_cursor.lnum)
7866 {
7867 char_u ctrl_o[2];
7868
7869 ctrl_o[0] = Ctrl_O;
7870 ctrl_o[1] = 0;
7871 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7872 }
7873 }
7874#endif
7875}
7876
7877
7878 static void
7879ex_read(eap)
7880 exarg_T *eap;
7881{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007882 int i;
7883 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7884 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885
7886 if (eap->usefilter) /* :r!cmd */
7887 do_bang(1, eap, FALSE, FALSE, TRUE);
7888 else
7889 {
7890 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7891 return;
7892
7893#ifdef FEAT_BROWSE
7894 if (cmdmod.browse)
7895 {
7896 char_u *browseFile;
7897
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007898 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 NULL, NULL, NULL, curbuf);
7900 if (browseFile != NULL)
7901 {
7902 i = readfile(browseFile, NULL,
7903 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7904 vim_free(browseFile);
7905 }
7906 else
7907 i = OK;
7908 }
7909 else
7910#endif
7911 if (*eap->arg == NUL)
7912 {
7913 if (check_fname() == FAIL) /* check for no file name */
7914 return;
7915 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7916 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7917 }
7918 else
7919 {
7920 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7921 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7922 i = readfile(eap->arg, NULL,
7923 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7924
7925 }
7926 if (i == FAIL)
7927 {
7928#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7929 if (!aborting())
7930#endif
7931 EMSG2(_(e_notopen), eap->arg);
7932 }
7933 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007934 {
7935 if (empty && exmode_active)
7936 {
7937 /* Delete the empty line that remains. Historically ex does
7938 * this but vi doesn't. */
7939 if (eap->line2 == 0)
7940 lnum = curbuf->b_ml.ml_line_count;
7941 else
7942 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007943 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007944 {
7945 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007946 if (curwin->w_cursor.lnum > 1
7947 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007948 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007949 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 }
7951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
7955}
7956
Bram Moolenaarea408852005-06-25 22:49:46 +00007957static char_u *prev_dir = NULL;
7958
7959#if defined(EXITFREE) || defined(PROTO)
7960 void
7961free_cd_dir()
7962{
7963 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007964 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007965
7966 vim_free(globaldir);
7967 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007968}
7969#endif
7970
7971
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972/*
7973 * ":cd", ":lcd", ":chdir" and ":lchdir".
7974 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007975 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976ex_cd(eap)
7977 exarg_T *eap;
7978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 char_u *new_dir;
7980 char_u *tofree;
7981
7982 new_dir = eap->arg;
7983#if !defined(UNIX) && !defined(VMS)
7984 /* for non-UNIX ":cd" means: print current directory */
7985 if (*new_dir == NUL)
7986 ex_pwd(NULL);
7987 else
7988#endif
7989 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007990#ifdef FEAT_AUTOCMD
7991 if (allbuf_locked())
7992 return;
7993#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007994 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7995 && !eap->forceit)
7996 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007997 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007998 return;
7999 }
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 /* ":cd -": Change to previous directory */
8002 if (STRCMP(new_dir, "-") == 0)
8003 {
8004 if (prev_dir == NULL)
8005 {
8006 EMSG(_("E186: No previous directory"));
8007 return;
8008 }
8009 new_dir = prev_dir;
8010 }
8011
8012 /* Save current directory for next ":cd -" */
8013 tofree = prev_dir;
8014 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8015 prev_dir = vim_strsave(NameBuff);
8016 else
8017 prev_dir = NULL;
8018
8019#if defined(UNIX) || defined(VMS)
8020 /* for UNIX ":cd" means: go to home directory */
8021 if (*new_dir == NUL)
8022 {
8023 /* use NameBuff for home directory name */
8024# ifdef VMS
8025 char_u *p;
8026
8027 p = mch_getenv((char_u *)"SYS$LOGIN");
8028 if (p == NULL || *p == NUL) /* empty is the same as not set */
8029 NameBuff[0] = NUL;
8030 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008031 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032# else
8033 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8034# endif
8035 new_dir = NameBuff;
8036 }
8037#endif
8038 if (new_dir == NULL || vim_chdir(new_dir))
8039 EMSG(_(e_failed));
8040 else
8041 {
8042 vim_free(curwin->w_localdir);
8043 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8044 {
8045 /* If still in global directory, need to remember current
8046 * directory as global directory. */
8047 if (globaldir == NULL && prev_dir != NULL)
8048 globaldir = vim_strsave(prev_dir);
8049 /* Remember this local directory for the window. */
8050 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8051 curwin->w_localdir = vim_strsave(NameBuff);
8052 }
8053 else
8054 {
8055 /* We are now in the global directory, no need to remember its
8056 * name. */
8057 vim_free(globaldir);
8058 globaldir = NULL;
8059 curwin->w_localdir = NULL;
8060 }
8061
8062 shorten_fnames(TRUE);
8063
8064 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008065 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 ex_pwd(eap);
8067 }
8068 vim_free(tofree);
8069 }
8070}
8071
8072/*
8073 * ":pwd".
8074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 static void
8076ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008077 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8080 {
8081#ifdef BACKSLASH_IN_FILENAME
8082 slash_adjust(NameBuff);
8083#endif
8084 msg(NameBuff);
8085 }
8086 else
8087 EMSG(_("E187: Unknown"));
8088}
8089
8090/*
8091 * ":=".
8092 */
8093 static void
8094ex_equal(eap)
8095 exarg_T *eap;
8096{
8097 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008098 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099}
8100
8101 static void
8102ex_sleep(eap)
8103 exarg_T *eap;
8104{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008105 int n;
8106 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
8108 if (cursor_valid())
8109 {
8110 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8111 if (n >= 0)
8112 windgoto((int)n, curwin->w_wcol);
8113 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008114
8115 len = eap->line2;
8116 switch (*eap->arg)
8117 {
8118 case 'm': break;
8119 case NUL: len *= 1000L; break;
8120 default: EMSG2(_(e_invarg2), eap->arg); return;
8121 }
8122 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123}
8124
8125/*
8126 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8127 */
8128 void
8129do_sleep(msec)
8130 long msec;
8131{
8132 long done;
8133
8134 cursor_on();
8135 out_flush();
8136 for (done = 0; !got_int && done < msec; done += 1000L)
8137 {
8138 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8139 ui_breakcheck();
8140 }
8141}
8142
8143 static void
8144do_exmap(eap, isabbrev)
8145 exarg_T *eap;
8146 int isabbrev;
8147{
8148 int mode;
8149 char_u *cmdp;
8150
8151 cmdp = eap->cmd;
8152 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8153
8154 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8155 eap->arg, mode, isabbrev))
8156 {
8157 case 1: EMSG(_(e_invarg));
8158 break;
8159 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8160 break;
8161 }
8162}
8163
8164/*
8165 * ":winsize" command (obsolete).
8166 */
8167 static void
8168ex_winsize(eap)
8169 exarg_T *eap;
8170{
8171 int w, h;
8172 char_u *arg = eap->arg;
8173 char_u *p;
8174
8175 w = getdigits(&arg);
8176 arg = skipwhite(arg);
8177 p = arg;
8178 h = getdigits(&arg);
8179 if (*p != NUL && *arg == NUL)
8180 set_shellsize(w, h, TRUE);
8181 else
8182 EMSG(_("E465: :winsize requires two number arguments"));
8183}
8184
8185#ifdef FEAT_WINDOWS
8186 static void
8187ex_wincmd(eap)
8188 exarg_T *eap;
8189{
8190 int xchar = NUL;
8191 char_u *p;
8192
8193 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8194 {
8195 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8196 if (eap->arg[1] == NUL)
8197 {
8198 EMSG(_(e_invarg));
8199 return;
8200 }
8201 xchar = eap->arg[1];
8202 p = eap->arg + 2;
8203 }
8204 else
8205 p = eap->arg + 1;
8206
8207 eap->nextcmd = check_nextcmd(p);
8208 p = skipwhite(p);
8209 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8210 EMSG(_(e_invarg));
8211 else
8212 {
8213 /* Pass flags on for ":vertical wincmd ]". */
8214 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008215 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8217 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008218 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220}
8221#endif
8222
Bram Moolenaar843ee412004-06-30 16:16:41 +00008223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224/*
8225 * ":winpos".
8226 */
8227 static void
8228ex_winpos(eap)
8229 exarg_T *eap;
8230{
8231 int x, y;
8232 char_u *arg = eap->arg;
8233 char_u *p;
8234
8235 if (*arg == NUL)
8236 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008237# if defined(FEAT_GUI) || defined(MSWIN)
8238# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008240# else
8241 if (mch_get_winpos(&x, &y) != FAIL)
8242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8245 msg(IObuff);
8246 }
8247 else
8248# endif
8249 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8250 }
8251 else
8252 {
8253 x = getdigits(&arg);
8254 arg = skipwhite(arg);
8255 p = arg;
8256 y = getdigits(&arg);
8257 if (*p == NUL || *arg != NUL)
8258 {
8259 EMSG(_("E466: :winpos requires two number arguments"));
8260 return;
8261 }
8262# ifdef FEAT_GUI
8263 if (gui.in_use)
8264 gui_mch_set_winpos(x, y);
8265 else if (gui.starting)
8266 {
8267 /* Remember the coordinates for when the window is opened. */
8268 gui_win_x = x;
8269 gui_win_y = y;
8270 }
8271# ifdef HAVE_TGETENT
8272 else
8273# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008274# else
8275# ifdef MSWIN
8276 mch_set_winpos(x, y);
8277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278# endif
8279# ifdef HAVE_TGETENT
8280 if (*T_CWP)
8281 term_set_winpos(x, y);
8282# endif
8283 }
8284}
8285#endif
8286
8287/*
8288 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8289 */
8290 static void
8291ex_operators(eap)
8292 exarg_T *eap;
8293{
8294 oparg_T oa;
8295
8296 clear_oparg(&oa);
8297 oa.regname = eap->regname;
8298 oa.start.lnum = eap->line1;
8299 oa.end.lnum = eap->line2;
8300 oa.line_count = eap->line2 - eap->line1 + 1;
8301 oa.motion_type = MLINE;
8302#ifdef FEAT_VIRTUALEDIT
8303 virtual_op = FALSE;
8304#endif
8305 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8306 {
8307 setpcmark();
8308 curwin->w_cursor.lnum = eap->line1;
8309 beginline(BL_SOL | BL_FIX);
8310 }
8311
8312 switch (eap->cmdidx)
8313 {
8314 case CMD_delete:
8315 oa.op_type = OP_DELETE;
8316 op_delete(&oa);
8317 break;
8318
8319 case CMD_yank:
8320 oa.op_type = OP_YANK;
8321 (void)op_yank(&oa, FALSE, TRUE);
8322 break;
8323
8324 default: /* CMD_rshift or CMD_lshift */
8325 if ((eap->cmdidx == CMD_rshift)
8326#ifdef FEAT_RIGHTLEFT
8327 ^ curwin->w_p_rl
8328#endif
8329 )
8330 oa.op_type = OP_RSHIFT;
8331 else
8332 oa.op_type = OP_LSHIFT;
8333 op_shift(&oa, FALSE, eap->amount);
8334 break;
8335 }
8336#ifdef FEAT_VIRTUALEDIT
8337 virtual_op = MAYBE;
8338#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008339 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
8342/*
8343 * ":put".
8344 */
8345 static void
8346ex_put(eap)
8347 exarg_T *eap;
8348{
8349 /* ":0put" works like ":1put!". */
8350 if (eap->line2 == 0)
8351 {
8352 eap->line2 = 1;
8353 eap->forceit = TRUE;
8354 }
8355 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008356 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8357 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
8359
8360/*
8361 * Handle ":copy" and ":move".
8362 */
8363 static void
8364ex_copymove(eap)
8365 exarg_T *eap;
8366{
8367 long n;
8368
8369 n = get_address(&eap->arg, FALSE, FALSE);
8370 if (eap->arg == NULL) /* error detected */
8371 {
8372 eap->nextcmd = NULL;
8373 return;
8374 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008375 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376
8377 /*
8378 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8379 */
8380 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8381 {
8382 EMSG(_(e_invaddr));
8383 return;
8384 }
8385
8386 if (eap->cmdidx == CMD_move)
8387 {
8388 if (do_move(eap->line1, eap->line2, n) == FAIL)
8389 return;
8390 }
8391 else
8392 ex_copy(eap->line1, eap->line2, n);
8393 u_clearline();
8394 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008395 ex_may_print(eap);
8396}
8397
8398/*
8399 * Print the current line if flags were given to the Ex command.
8400 */
8401 static void
8402ex_may_print(eap)
8403 exarg_T *eap;
8404{
8405 if (eap->flags != 0)
8406 {
8407 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8408 (eap->flags & EXFLAG_LIST));
8409 ex_no_reprint = TRUE;
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413/*
8414 * ":smagic" and ":snomagic".
8415 */
8416 static void
8417ex_submagic(eap)
8418 exarg_T *eap;
8419{
8420 int magic_save = p_magic;
8421
8422 p_magic = (eap->cmdidx == CMD_smagic);
8423 do_sub(eap);
8424 p_magic = magic_save;
8425}
8426
8427/*
8428 * ":join".
8429 */
8430 static void
8431ex_join(eap)
8432 exarg_T *eap;
8433{
8434 curwin->w_cursor.lnum = eap->line1;
8435 if (eap->line1 == eap->line2)
8436 {
8437 if (eap->addr_count >= 2) /* :2,2join does nothing */
8438 return;
8439 if (eap->line2 == curbuf->b_ml.ml_line_count)
8440 {
8441 beep_flush();
8442 return;
8443 }
8444 ++eap->line2;
8445 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008446 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008448 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449}
8450
8451/*
8452 * ":[addr]@r" or ":[addr]*r": execute register
8453 */
8454 static void
8455ex_at(eap)
8456 exarg_T *eap;
8457{
8458 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008459 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
8461 curwin->w_cursor.lnum = eap->line2;
8462
8463#ifdef USE_ON_FLY_SCROLL
8464 dont_scroll = TRUE; /* disallow scrolling here */
8465#endif
8466
8467 /* get the register name. No name means to use the previous one */
8468 c = *eap->arg;
8469 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8470 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008471 /* Put the register in the typeahead buffer with the "silent" flag. */
8472 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8473 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 else
8478 {
8479 int save_efr = exec_from_reg;
8480
8481 exec_from_reg = TRUE;
8482
8483 /*
8484 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008485 * Continue until the stuff buffer is empty and all added characters
8486 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008488 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8490
8491 exec_from_reg = save_efr;
8492 }
8493}
8494
8495/*
8496 * ":!".
8497 */
8498 static void
8499ex_bang(eap)
8500 exarg_T *eap;
8501{
8502 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8503}
8504
8505/*
8506 * ":undo".
8507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 static void
8509ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008510 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008512 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008513 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008514 else
8515 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516}
8517
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008518#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008519 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008520ex_wundo(eap)
8521 exarg_T *eap;
8522{
8523 char_u hash[UNDO_HASH_SIZE];
8524
8525 u_compute_hash(hash);
8526 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8527}
8528
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008529 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008530ex_rundo(eap)
8531 exarg_T *eap;
8532{
8533 char_u hash[UNDO_HASH_SIZE];
8534
8535 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008536 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008537}
8538#endif
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540/*
8541 * ":redo".
8542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 static void
8544ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008545 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 u_redo(1);
8548}
8549
8550/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008551 * ":earlier" and ":later".
8552 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008553 static void
8554ex_later(eap)
8555 exarg_T *eap;
8556{
8557 long count = 0;
8558 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008559 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008560 char_u *p = eap->arg;
8561
8562 if (*p == NUL)
8563 count = 1;
8564 else if (isdigit(*p))
8565 {
8566 count = getdigits(&p);
8567 switch (*p)
8568 {
8569 case 's': ++p; sec = TRUE; break;
8570 case 'm': ++p; sec = TRUE; count *= 60; break;
8571 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008572 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8573 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008574 }
8575 }
8576
8577 if (*p != NUL)
8578 EMSG2(_(e_invarg2), eap->arg);
8579 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008580 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8581 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008582}
8583
8584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 * ":redir": start/stop redirection.
8586 */
8587 static void
8588ex_redir(eap)
8589 exarg_T *eap;
8590{
8591 char *mode;
8592 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008593 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
8595 if (STRICMP(eap->arg, "END") == 0)
8596 close_redir();
8597 else
8598 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008599 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008601 ++arg;
8602 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008604 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 mode = "a";
8606 }
8607 else
8608 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008609 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
8611 close_redir();
8612
8613 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008614 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (fname == NULL)
8616 return;
8617#ifdef FEAT_BROWSE
8618 if (cmdmod.browse)
8619 {
8620 char_u *browseFile;
8621
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008622 browseFile = do_browse(BROWSE_SAVE,
8623 (char_u *)_("Save Redirection"),
8624 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 if (browseFile == NULL)
8626 return; /* operation cancelled */
8627 vim_free(fname);
8628 fname = browseFile;
8629 eap->forceit = TRUE; /* since dialog already asked */
8630 }
8631#endif
8632
8633 redir_fd = open_exfile(fname, eap->forceit, mode);
8634 vim_free(fname);
8635 }
8636#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008637 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {
8639 /* redirect to a register a-z (resp. A-Z for appending) */
8640 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008641 ++arg;
8642 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008644 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008645 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008647 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008649 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008650 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008651 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008654 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008655 if (*arg == '>')
8656 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008657 /* Make register empty when not using @A-@Z and the
8658 * command is valid. */
8659 if (*arg == NUL && !isupper(redir_reg))
8660 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662 }
8663 if (*arg != NUL)
8664 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008665 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008666 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667 }
8668 }
8669 else if (*arg == '=' && arg[1] == '>')
8670 {
8671 int append;
8672
8673 /* redirect to a variable */
8674 close_redir();
8675 arg += 2;
8676
8677 if (*arg == '>')
8678 {
8679 ++arg;
8680 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 }
8682 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683 append = FALSE;
8684
8685 if (var_redir_start(skipwhite(arg), append) == OK)
8686 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688#endif
8689
8690 /* TODO: redirect to a buffer */
8691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008693 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008695
8696 /* Make sure redirection is not off. Can happen for cmdline completion
8697 * that indirectly invokes a command to catch its output. */
8698 if (redir_fd != NULL
8699#ifdef FEAT_EVAL
8700 || redir_reg || redir_vname
8701#endif
8702 )
8703 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
8706/*
8707 * ":redraw": force redraw
8708 */
8709 static void
8710ex_redraw(eap)
8711 exarg_T *eap;
8712{
8713 int r = RedrawingDisabled;
8714 int p = p_lz;
8715
8716 RedrawingDisabled = 0;
8717 p_lz = FALSE;
8718 update_topline();
8719 update_screen(eap->forceit ? CLEAR :
8720#ifdef FEAT_VISUAL
8721 VIsual_active ? INVERTED :
8722#endif
8723 0);
8724#ifdef FEAT_TITLE
8725 if (need_maketitle)
8726 maketitle();
8727#endif
8728 RedrawingDisabled = r;
8729 p_lz = p;
8730
8731 /* Reset msg_didout, so that a message that's there is overwritten. */
8732 msg_didout = FALSE;
8733 msg_col = 0;
8734
8735 out_flush();
8736}
8737
8738/*
8739 * ":redrawstatus": force redraw of status line(s)
8740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 static void
8742ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008743 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
8745#if defined(FEAT_WINDOWS)
8746 int r = RedrawingDisabled;
8747 int p = p_lz;
8748
8749 RedrawingDisabled = 0;
8750 p_lz = FALSE;
8751 if (eap->forceit)
8752 status_redraw_all();
8753 else
8754 status_redraw_curbuf();
8755 update_screen(
8756# ifdef FEAT_VISUAL
8757 VIsual_active ? INVERTED :
8758# endif
8759 0);
8760 RedrawingDisabled = r;
8761 p_lz = p;
8762 out_flush();
8763#endif
8764}
8765
8766 static void
8767close_redir()
8768{
8769 if (redir_fd != NULL)
8770 {
8771 fclose(redir_fd);
8772 redir_fd = NULL;
8773 }
8774#ifdef FEAT_EVAL
8775 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008776 if (redir_vname)
8777 {
8778 var_redir_stop();
8779 redir_vname = 0;
8780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#endif
8782}
8783
8784#if defined(FEAT_SESSION) && defined(USE_CRNL)
8785# define MKSESSION_NL
8786static int mksession_nl = FALSE; /* use NL only in put_eol() */
8787#endif
8788
8789/*
8790 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8791 */
8792 static void
8793ex_mkrc(eap)
8794 exarg_T *eap;
8795{
8796 FILE *fd;
8797 int failed = FALSE;
8798 char_u *fname;
8799#ifdef FEAT_BROWSE
8800 char_u *browseFile = NULL;
8801#endif
8802#ifdef FEAT_SESSION
8803 int view_session = FALSE;
8804 int using_vdir = FALSE; /* using 'viewdir'? */
8805 char_u *viewFile = NULL;
8806 unsigned *flagp;
8807#endif
8808
8809 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8810 {
8811#ifdef FEAT_SESSION
8812 view_session = TRUE;
8813#else
8814 ex_ni(eap);
8815 return;
8816#endif
8817 }
8818
8819#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008820 /* Use the short file name until ":lcd" is used. We also don't use the
8821 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008822 did_lcd = FALSE;
8823
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8825 if (eap->cmdidx == CMD_mkview
8826 && (*eap->arg == NUL
8827 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8828 {
8829 eap->forceit = TRUE;
8830 fname = get_view_file(*eap->arg);
8831 if (fname == NULL)
8832 return;
8833 viewFile = fname;
8834 using_vdir = TRUE;
8835 }
8836 else
8837#endif
8838 if (*eap->arg != NUL)
8839 fname = eap->arg;
8840 else if (eap->cmdidx == CMD_mkvimrc)
8841 fname = (char_u *)VIMRC_FILE;
8842#ifdef FEAT_SESSION
8843 else if (eap->cmdidx == CMD_mksession)
8844 fname = (char_u *)SESSION_FILE;
8845#endif
8846 else
8847 fname = (char_u *)EXRC_FILE;
8848
8849#ifdef FEAT_BROWSE
8850 if (cmdmod.browse)
8851 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008852 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853# ifdef FEAT_SESSION
8854 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8855 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8856# endif
8857 (char_u *)_("Save Setup"),
8858 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8859 if (browseFile == NULL)
8860 goto theend;
8861 fname = browseFile;
8862 eap->forceit = TRUE; /* since dialog already asked */
8863 }
8864#endif
8865
8866#if defined(FEAT_SESSION) && defined(vim_mkdir)
8867 /* When using 'viewdir' may have to create the directory. */
8868 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008869 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870#endif
8871
8872 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8873 if (fd != NULL)
8874 {
8875#ifdef FEAT_SESSION
8876 if (eap->cmdidx == CMD_mkview)
8877 flagp = &vop_flags;
8878 else
8879 flagp = &ssop_flags;
8880#endif
8881
8882#ifdef MKSESSION_NL
8883 /* "unix" in 'sessionoptions': use NL line separator */
8884 if (view_session && (*flagp & SSOP_UNIX))
8885 mksession_nl = TRUE;
8886#endif
8887
8888 /* Write the version command for :mkvimrc */
8889 if (eap->cmdidx == CMD_mkvimrc)
8890 (void)put_line(fd, "version 6.0");
8891
8892#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008893 if (eap->cmdidx == CMD_mksession)
8894 {
8895 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8896 failed = TRUE;
8897 }
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 if (eap->cmdidx != CMD_mkview)
8900#endif
8901 {
8902 /* Write setting 'compatible' first, because it has side effects.
8903 * For that same reason only do it when needed. */
8904 if (p_cp)
8905 (void)put_line(fd, "if !&cp | set cp | endif");
8906 else
8907 (void)put_line(fd, "if &cp | set nocp | endif");
8908 }
8909
8910#ifdef FEAT_SESSION
8911 if (!view_session
8912 || (eap->cmdidx == CMD_mksession
8913 && (*flagp & SSOP_OPTIONS)))
8914#endif
8915 failed |= (makemap(fd, NULL) == FAIL
8916 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8917
8918#ifdef FEAT_SESSION
8919 if (!failed && view_session)
8920 {
8921 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8922 failed = TRUE;
8923 if (eap->cmdidx == CMD_mksession)
8924 {
8925 char_u dirnow[MAXPATHL]; /* current directory */
8926
8927 /*
8928 * Change to session file's dir.
8929 */
8930 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8931 || mch_chdir((char *)dirnow) != 0)
8932 *dirnow = NUL;
8933 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8934 {
8935 if (vim_chdirfile(fname) == OK)
8936 shorten_fnames(TRUE);
8937 }
8938 else if (*dirnow != NUL
8939 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8940 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008941 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008942 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 }
8944
8945 failed |= (makeopens(fd, dirnow) == FAIL);
8946
8947 /* restore original dir */
8948 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8949 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8950 {
8951 if (mch_chdir((char *)dirnow) != 0)
8952 EMSG(_(e_prev_dir));
8953 shorten_fnames(TRUE);
8954 }
8955 }
8956 else
8957 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008958 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8959 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8962 == FAIL)
8963 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008964 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8965 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008966 if (eap->cmdidx == CMD_mksession)
8967 {
8968 if (put_line(fd, "unlet SessionLoad") == FAIL)
8969 failed = TRUE;
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
8972#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008973 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8974 failed = TRUE;
8975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 failed |= fclose(fd);
8977
8978 if (failed)
8979 EMSG(_(e_write));
8980#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8981 else if (eap->cmdidx == CMD_mksession)
8982 {
8983 /* successful session write - set this_session var */
8984 char_u tbuf[MAXPATHL];
8985
8986 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8987 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8988 }
8989#endif
8990#ifdef MKSESSION_NL
8991 mksession_nl = FALSE;
8992#endif
8993 }
8994
8995#ifdef FEAT_BROWSE
8996theend:
8997 vim_free(browseFile);
8998#endif
8999#ifdef FEAT_SESSION
9000 vim_free(viewFile);
9001#endif
9002}
9003
Bram Moolenaardf177f62005-02-22 08:39:57 +00009004#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9005 || defined(PROTO)
9006 int
9007vim_mkdir_emsg(name, prot)
9008 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009009 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010{
9011 if (vim_mkdir(name, prot) != 0)
9012 {
9013 EMSG2(_("E739: Cannot create directory: %s"), name);
9014 return FAIL;
9015 }
9016 return OK;
9017}
9018#endif
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020/*
9021 * Open a file for writing for an Ex command, with some checks.
9022 * Return file descriptor, or NULL on failure.
9023 */
9024 FILE *
9025open_exfile(fname, forceit, mode)
9026 char_u *fname;
9027 int forceit;
9028 char *mode; /* "w" for create new file or "a" for append */
9029{
9030 FILE *fd;
9031
9032#ifdef UNIX
9033 /* with Unix it is possible to open a directory */
9034 if (mch_isdir(fname))
9035 {
9036 EMSG2(_(e_isadir2), fname);
9037 return NULL;
9038 }
9039#endif
9040 if (!forceit && *mode != 'a' && vim_fexists(fname))
9041 {
9042 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9043 return NULL;
9044 }
9045
9046 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9047 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9048
9049 return fd;
9050}
9051
9052/*
9053 * ":mark" and ":k".
9054 */
9055 static void
9056ex_mark(eap)
9057 exarg_T *eap;
9058{
9059 pos_T pos;
9060
9061 if (*eap->arg == NUL) /* No argument? */
9062 EMSG(_(e_argreq));
9063 else if (eap->arg[1] != NUL) /* more than one character? */
9064 EMSG(_(e_trailing));
9065 else
9066 {
9067 pos = curwin->w_cursor; /* save curwin->w_cursor */
9068 curwin->w_cursor.lnum = eap->line2;
9069 beginline(BL_WHITE | BL_FIX);
9070 if (setmark(*eap->arg) == FAIL) /* set mark */
9071 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9072 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9073 }
9074}
9075
9076/*
9077 * Update w_topline, w_leftcol and the cursor position.
9078 */
9079 void
9080update_topline_cursor()
9081{
9082 check_cursor(); /* put cursor on valid line */
9083 update_topline();
9084 if (!curwin->w_p_wrap)
9085 validate_cursor();
9086 update_curswant();
9087}
9088
9089#ifdef FEAT_EX_EXTRA
9090/*
9091 * ":normal[!] {commands}": Execute normal mode commands.
9092 */
9093 static void
9094ex_normal(eap)
9095 exarg_T *eap;
9096{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 int save_msg_scroll = msg_scroll;
9098 int save_restart_edit = restart_edit;
9099 int save_msg_didout = msg_didout;
9100 int save_State = State;
9101 tasave_T tabuf;
9102 int save_insertmode = p_im;
9103 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009104 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105#ifdef FEAT_MBYTE
9106 char_u *arg = NULL;
9107 int l;
9108 char_u *p;
9109#endif
9110
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009111 if (ex_normal_lock > 0)
9112 {
9113 EMSG(_(e_secure));
9114 return;
9115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 if (ex_normal_busy >= p_mmd)
9117 {
9118 EMSG(_("E192: Recursive use of :normal too deep"));
9119 return;
9120 }
9121 ++ex_normal_busy;
9122
9123 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9124 restart_edit = 0; /* don't go to Insert mode */
9125 p_im = FALSE; /* don't use 'insertmode' */
9126
9127#ifdef FEAT_MBYTE
9128 /*
9129 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9130 * this for the K_SPECIAL leading byte, otherwise special keys will not
9131 * work.
9132 */
9133 if (has_mbyte)
9134 {
9135 int len = 0;
9136
9137 /* Count the number of characters to be escaped. */
9138 for (p = eap->arg; *p != NUL; ++p)
9139 {
9140# ifdef FEAT_GUI
9141 if (*p == CSI) /* leadbyte CSI */
9142 len += 2;
9143# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009144 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9146# ifdef FEAT_GUI
9147 || *p == CSI
9148# endif
9149 )
9150 len += 2;
9151 }
9152 if (len > 0)
9153 {
9154 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9155 if (arg != NULL)
9156 {
9157 len = 0;
9158 for (p = eap->arg; *p != NUL; ++p)
9159 {
9160 arg[len++] = *p;
9161# ifdef FEAT_GUI
9162 if (*p == CSI)
9163 {
9164 arg[len++] = KS_EXTRA;
9165 arg[len++] = (int)KE_CSI;
9166 }
9167# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009168 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 {
9170 arg[len++] = *++p;
9171 if (*p == K_SPECIAL)
9172 {
9173 arg[len++] = KS_SPECIAL;
9174 arg[len++] = KE_FILLER;
9175 }
9176# ifdef FEAT_GUI
9177 else if (*p == CSI)
9178 {
9179 arg[len++] = KS_EXTRA;
9180 arg[len++] = (int)KE_CSI;
9181 }
9182# endif
9183 }
9184 arg[len] = NUL;
9185 }
9186 }
9187 }
9188 }
9189#endif
9190
9191 /*
9192 * Save the current typeahead. This is required to allow using ":normal"
9193 * from an event handler and makes sure we don't hang when the argument
9194 * ends with half a command.
9195 */
9196 save_typeahead(&tabuf);
9197 if (tabuf.typebuf_valid)
9198 {
9199 /*
9200 * Repeat the :normal command for each line in the range. When no
9201 * range given, execute it just once, without positioning the cursor
9202 * first.
9203 */
9204 do
9205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 if (eap->addr_count != 0)
9207 {
9208 curwin->w_cursor.lnum = eap->line1++;
9209 curwin->w_cursor.col = 0;
9210 }
9211
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009212 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213#ifdef FEAT_MBYTE
9214 arg != NULL ? arg :
9215#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009216 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 }
9218 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9219 }
9220
9221 /* Might not return to the main loop when in an event handler. */
9222 update_topline_cursor();
9223
9224 /* Restore the previous typeahead. */
9225 restore_typeahead(&tabuf);
9226
9227 --ex_normal_busy;
9228 msg_scroll = save_msg_scroll;
9229 restart_edit = save_restart_edit;
9230 p_im = save_insertmode;
9231 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009232 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9234
9235 /* Restore the state (needed when called from a function executed for
9236 * 'indentexpr'). */
9237 State = save_State;
9238#ifdef FEAT_MBYTE
9239 vim_free(arg);
9240#endif
9241}
9242
9243/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009244 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 */
9246 static void
9247ex_startinsert(eap)
9248 exarg_T *eap;
9249{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009250 if (eap->forceit)
9251 {
9252 coladvance((colnr_T)MAXCOL);
9253 curwin->w_curswant = MAXCOL;
9254 curwin->w_set_curswant = FALSE;
9255 }
9256
Bram Moolenaara40c5002005-01-09 21:16:21 +00009257 /* Ignore the command when already in Insert mode. Inserting an
9258 * expression register that invokes a function can do this. */
9259 if (State & INSERT)
9260 return;
9261
Bram Moolenaar64969662005-12-14 21:59:55 +00009262 if (eap->cmdidx == CMD_startinsert)
9263 restart_edit = 'a';
9264 else if (eap->cmdidx == CMD_startreplace)
9265 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009267 restart_edit = 'V';
9268
9269 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009271 if (eap->cmdidx == CMD_startinsert)
9272 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 curwin->w_curswant = 0; /* avoid MAXCOL */
9274 }
9275}
9276
9277/*
9278 * ":stopinsert"
9279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 static void
9281ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009282 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
9284 restart_edit = 0;
9285 stop_insert_mode = TRUE;
9286}
9287#endif
9288
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009289#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9290/*
9291 * Execute normal mode command "cmd".
9292 * "remap" can be REMAP_NONE or REMAP_YES.
9293 */
9294 void
9295exec_normal_cmd(cmd, remap, silent)
9296 char_u *cmd;
9297 int remap;
9298 int silent;
9299{
9300 oparg_T oa;
9301
9302 /*
9303 * Stuff the argument into the typeahead buffer.
9304 * Execute normal_cmd() until there is no typeahead left.
9305 */
9306 clear_oparg(&oa);
9307 finish_op = FALSE;
9308 ins_typebuf(cmd, remap, 0, TRUE, silent);
9309 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9310 && !got_int)
9311 {
9312 update_topline_cursor();
9313 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9314 }
9315}
9316#endif
9317
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318#ifdef FEAT_FIND_ID
9319 static void
9320ex_checkpath(eap)
9321 exarg_T *eap;
9322{
9323 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9324 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9325 (linenr_T)1, (linenr_T)MAXLNUM);
9326}
9327
9328#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9329/*
9330 * ":psearch"
9331 */
9332 static void
9333ex_psearch(eap)
9334 exarg_T *eap;
9335{
9336 g_do_tagpreview = p_pvh;
9337 ex_findpat(eap);
9338 g_do_tagpreview = 0;
9339}
9340#endif
9341
9342 static void
9343ex_findpat(eap)
9344 exarg_T *eap;
9345{
9346 int whole = TRUE;
9347 long n;
9348 char_u *p;
9349 int action;
9350
9351 switch (cmdnames[eap->cmdidx].cmd_name[2])
9352 {
9353 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9354 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9355 action = ACTION_GOTO;
9356 else
9357 action = ACTION_SHOW;
9358 break;
9359 case 'i': /* ":ilist" and ":dlist" */
9360 action = ACTION_SHOW_ALL;
9361 break;
9362 case 'u': /* ":ijump" and ":djump" */
9363 action = ACTION_GOTO;
9364 break;
9365 default: /* ":isplit" and ":dsplit" */
9366 action = ACTION_SPLIT;
9367 break;
9368 }
9369
9370 n = 1;
9371 if (vim_isdigit(*eap->arg)) /* get count */
9372 {
9373 n = getdigits(&eap->arg);
9374 eap->arg = skipwhite(eap->arg);
9375 }
9376 if (*eap->arg == '/') /* Match regexp, not just whole words */
9377 {
9378 whole = FALSE;
9379 ++eap->arg;
9380 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9381 if (*p)
9382 {
9383 *p++ = NUL;
9384 p = skipwhite(p);
9385
9386 /* Check for trailing illegal characters */
9387 if (!ends_excmd(*p))
9388 eap->errmsg = e_trailing;
9389 else
9390 eap->nextcmd = check_nextcmd(p);
9391 }
9392 }
9393 if (!eap->skip)
9394 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9395 whole, !eap->forceit,
9396 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9397 n, action, eap->line1, eap->line2);
9398}
9399#endif
9400
9401#ifdef FEAT_WINDOWS
9402
9403# ifdef FEAT_QUICKFIX
9404/*
9405 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9406 */
9407 static void
9408ex_ptag(eap)
9409 exarg_T *eap;
9410{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009411 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9413}
9414
9415/*
9416 * ":pedit"
9417 */
9418 static void
9419ex_pedit(eap)
9420 exarg_T *eap;
9421{
9422 win_T *curwin_save = curwin;
9423
9424 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009425 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 keep_help_flag = curwin_save->w_buffer->b_help;
9427 do_exedit(eap, NULL);
9428 keep_help_flag = FALSE;
9429 if (curwin != curwin_save && win_valid(curwin_save))
9430 {
9431 /* Return cursor to where we were */
9432 validate_cursor();
9433 redraw_later(VALID);
9434 win_enter(curwin_save, TRUE);
9435 }
9436 g_do_tagpreview = 0;
9437}
9438# endif
9439
9440/*
9441 * ":stag", ":stselect" and ":stjump".
9442 */
9443 static void
9444ex_stag(eap)
9445 exarg_T *eap;
9446{
9447 postponed_split = -1;
9448 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009449 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9451 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009452 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454#endif
9455
9456/*
9457 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9458 */
9459 static void
9460ex_tag(eap)
9461 exarg_T *eap;
9462{
9463 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9464}
9465
9466 static void
9467ex_tag_cmd(eap, name)
9468 exarg_T *eap;
9469 char_u *name;
9470{
9471 int cmd;
9472
9473 switch (name[1])
9474 {
9475 case 'j': cmd = DT_JUMP; /* ":tjump" */
9476 break;
9477 case 's': cmd = DT_SELECT; /* ":tselect" */
9478 break;
9479 case 'p': cmd = DT_PREV; /* ":tprevious" */
9480 break;
9481 case 'N': cmd = DT_PREV; /* ":tNext" */
9482 break;
9483 case 'n': cmd = DT_NEXT; /* ":tnext" */
9484 break;
9485 case 'o': cmd = DT_POP; /* ":pop" */
9486 break;
9487 case 'f': /* ":tfirst" */
9488 case 'r': cmd = DT_FIRST; /* ":trewind" */
9489 break;
9490 case 'l': cmd = DT_LAST; /* ":tlast" */
9491 break;
9492 default: /* ":tag" */
9493#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009494 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 {
9496 do_cstag(eap);
9497 return;
9498 }
9499#endif
9500 cmd = DT_TAG;
9501 break;
9502 }
9503
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009504 if (name[0] == 'l')
9505 {
9506#ifndef FEAT_QUICKFIX
9507 ex_ni(eap);
9508 return;
9509#else
9510 cmd = DT_LTAG;
9511#endif
9512 }
9513
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9515 eap->forceit, TRUE);
9516}
9517
9518/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009519 * Check "str" for starting with a special cmdline variable.
9520 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9521 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9522 */
9523 int
9524find_cmdline_var(src, usedlen)
9525 char_u *src;
9526 int *usedlen;
9527{
9528 int len;
9529 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009530 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009531 "%",
9532#define SPEC_PERC 0
9533 "#",
9534#define SPEC_HASH 1
9535 "<cword>", /* cursor word */
9536#define SPEC_CWORD 2
9537 "<cWORD>", /* cursor WORD */
9538#define SPEC_CCWORD 3
9539 "<cfile>", /* cursor path name */
9540#define SPEC_CFILE 4
9541 "<sfile>", /* ":so" file name */
9542#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009543 "<slnum>", /* ":so" file line number */
9544#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009545#ifdef FEAT_AUTOCMD
9546 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009547# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009548 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009549# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009550 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009551# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009552#endif
9553#ifdef FEAT_CLIENTSERVER
9554 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009555# ifdef FEAT_AUTOCMD
9556# define SPEC_CLIENT 10
9557# else
9558# define SPEC_CLIENT 7
9559# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009560#endif
9561 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009562
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009563 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009564 {
9565 len = (int)STRLEN(spec_str[i]);
9566 if (STRNCMP(src, spec_str[i], len) == 0)
9567 {
9568 *usedlen = len;
9569 return i;
9570 }
9571 }
9572 return -1;
9573}
9574
9575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 * Evaluate cmdline variables.
9577 *
9578 * change '%' to curbuf->b_ffname
9579 * '#' to curwin->w_altfile
9580 * '<cword>' to word under the cursor
9581 * '<cWORD>' to WORD under the cursor
9582 * '<cfile>' to path name under the cursor
9583 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009584 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 * '<afile>' to file name for autocommand
9586 * '<abuf>' to buffer number for autocommand
9587 * '<amatch>' to matching name for autocommand
9588 *
9589 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9590 * "" for error without a message) and NULL is returned.
9591 * Returns an allocated string if a valid match was found.
9592 * Returns NULL if no match was found. "usedlen" then still contains the
9593 * number of characters to skip.
9594 */
9595 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009596eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009598 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 int *usedlen; /* characters after src that are used */
9600 linenr_T *lnump; /* line number for :e command, or NULL */
9601 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009602 int *escaped; /* return value has escaped white space (can
9603 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 int i;
9606 char_u *s;
9607 char_u *result;
9608 char_u *resultbuf = NULL;
9609 int resultlen;
9610 buf_T *buf;
9611 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9612 int spec_idx;
9613#ifdef FEAT_MODIFY_FNAME
9614 int skip_mod = FALSE;
9615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617
9618 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009619 if (escaped != NULL)
9620 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
9622 /*
9623 * Check if there is something to do.
9624 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009625 spec_idx = find_cmdline_var(src, usedlen);
9626 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 {
9628 *usedlen = 1;
9629 return NULL;
9630 }
9631
9632 /*
9633 * Skip when preceded with a backslash "\%" and "\#".
9634 * Note: In "\\%" the % is also not recognized!
9635 */
9636 if (src > srcstart && src[-1] == '\\')
9637 {
9638 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009639 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 return NULL;
9641 }
9642
9643 /*
9644 * word or WORD under cursor
9645 */
9646 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9647 {
9648 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9649 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9650 if (resultlen == 0)
9651 {
9652 *errormsg = (char_u *)"";
9653 return NULL;
9654 }
9655 }
9656
9657 /*
9658 * '#': Alternate file name
9659 * '%': Current file name
9660 * File name under the cursor
9661 * File name for autocommand
9662 * and following modifiers
9663 */
9664 else
9665 {
9666 switch (spec_idx)
9667 {
9668 case SPEC_PERC: /* '%': current file */
9669 if (curbuf->b_fname == NULL)
9670 {
9671 result = (char_u *)"";
9672 valid = 0; /* Must have ":p:h" to be valid */
9673 }
9674 else
9675#ifdef RISCOS
9676 /* Always use the full path for RISC OS if possible. */
9677 result = curbuf->b_ffname;
9678 if (result == NULL)
9679 result = curbuf->b_fname;
9680#else
9681 result = curbuf->b_fname;
9682#endif
9683 break;
9684
9685 case SPEC_HASH: /* '#' or "#99": alternate file */
9686 if (src[1] == '#') /* "##": the argument list */
9687 {
9688 result = arg_all();
9689 resultbuf = result;
9690 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009691 if (escaped != NULL)
9692 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#ifdef FEAT_MODIFY_FNAME
9694 skip_mod = TRUE;
9695#endif
9696 break;
9697 }
9698 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009699 if (*s == '<') /* "#<99" uses v:oldfiles */
9700 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 i = (int)getdigits(&s);
9702 *usedlen = (int)(s - src); /* length of what we expand */
9703
Bram Moolenaard812df62008-11-09 12:46:09 +00009704 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009706 if (*usedlen < 2)
9707 {
9708 /* Should we give an error message for #<text? */
9709 *usedlen = 1;
9710 return NULL;
9711 }
9712#ifdef FEAT_EVAL
9713 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9714 (long)i);
9715 if (result == NULL)
9716 {
9717 *errormsg = (char_u *)"";
9718 return NULL;
9719 }
9720#else
9721 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 }
9725 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009726 {
9727 buf = buflist_findnr(i);
9728 if (buf == NULL)
9729 {
9730 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9731 return NULL;
9732 }
9733 if (lnump != NULL)
9734 *lnump = ECMD_LAST;
9735 if (buf->b_fname == NULL)
9736 {
9737 result = (char_u *)"";
9738 valid = 0; /* Must have ":p:h" to be valid */
9739 }
9740 else
9741 result = buf->b_fname;
9742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 break;
9744
9745#ifdef FEAT_SEARCHPATH
9746 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009747 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (result == NULL)
9749 {
9750 *errormsg = (char_u *)"";
9751 return NULL;
9752 }
9753 resultbuf = result; /* remember allocated string */
9754 break;
9755#endif
9756
9757#ifdef FEAT_AUTOCMD
9758 case SPEC_AFILE: /* file name for autocommand */
9759 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009760 if (result != NULL && !autocmd_fname_full)
9761 {
9762 /* Still need to turn the fname into a full path. It is
9763 * postponed to avoid a delay when <afile> is not used. */
9764 autocmd_fname_full = TRUE;
9765 result = FullName_save(autocmd_fname, FALSE);
9766 vim_free(autocmd_fname);
9767 autocmd_fname = result;
9768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 if (result == NULL)
9770 {
9771 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9772 return NULL;
9773 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009774 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 break;
9776
9777 case SPEC_ABUF: /* buffer number for autocommand */
9778 if (autocmd_bufnr <= 0)
9779 {
9780 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9781 return NULL;
9782 }
9783 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9784 result = strbuf;
9785 break;
9786
9787 case SPEC_AMATCH: /* match name for autocommand */
9788 result = autocmd_match;
9789 if (result == NULL)
9790 {
9791 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9792 return NULL;
9793 }
9794 break;
9795
9796#endif
9797 case SPEC_SFILE: /* file name for ":so" command */
9798 result = sourcing_name;
9799 if (result == NULL)
9800 {
9801 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9802 return NULL;
9803 }
9804 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009805 case SPEC_SLNUM: /* line in file for ":so" command */
9806 if (sourcing_name == NULL || sourcing_lnum == 0)
9807 {
9808 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9809 return NULL;
9810 }
9811 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9812 result = strbuf;
9813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814#if defined(FEAT_CLIENTSERVER)
9815 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009816 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9817 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 result = strbuf;
9819 break;
9820#endif
9821 }
9822
9823 resultlen = (int)STRLEN(result); /* length of new string */
9824 if (src[*usedlen] == '<') /* remove the file name extension */
9825 {
9826 ++*usedlen;
9827#ifdef RISCOS
9828 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9829#else
9830 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9831#endif
9832 resultlen = (int)(s - result);
9833 }
9834#ifdef FEAT_MODIFY_FNAME
9835 else if (!skip_mod)
9836 {
9837 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9838 &resultlen);
9839 if (result == NULL)
9840 {
9841 *errormsg = (char_u *)"";
9842 return NULL;
9843 }
9844 }
9845#endif
9846 }
9847
9848 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9849 {
9850 if (valid != VALID_HEAD + VALID_PATH)
9851 /* xgettext:no-c-format */
9852 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9853 else
9854 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9855 result = NULL;
9856 }
9857 else
9858 result = vim_strnsave(result, resultlen);
9859 vim_free(resultbuf);
9860 return result;
9861}
9862
9863/*
9864 * Concatenate all files in the argument list, separated by spaces, and return
9865 * it in one allocated string.
9866 * Spaces and backslashes in the file names are escaped with a backslash.
9867 * Returns NULL when out of memory.
9868 */
9869 static char_u *
9870arg_all()
9871{
9872 int len;
9873 int idx;
9874 char_u *retval = NULL;
9875 char_u *p;
9876
9877 /*
9878 * Do this loop two times:
9879 * first time: compute the total length
9880 * second time: concatenate the names
9881 */
9882 for (;;)
9883 {
9884 len = 0;
9885 for (idx = 0; idx < ARGCOUNT; ++idx)
9886 {
9887 p = alist_name(&ARGLIST[idx]);
9888 if (p != NULL)
9889 {
9890 if (len > 0)
9891 {
9892 /* insert a space in between names */
9893 if (retval != NULL)
9894 retval[len] = ' ';
9895 ++len;
9896 }
9897 for ( ; *p != NUL; ++p)
9898 {
9899 if (*p == ' ' || *p == '\\')
9900 {
9901 /* insert a backslash */
9902 if (retval != NULL)
9903 retval[len] = '\\';
9904 ++len;
9905 }
9906 if (retval != NULL)
9907 retval[len] = *p;
9908 ++len;
9909 }
9910 }
9911 }
9912
9913 /* second time: break here */
9914 if (retval != NULL)
9915 {
9916 retval[len] = NUL;
9917 break;
9918 }
9919
9920 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009921 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (retval == NULL)
9923 break;
9924 }
9925
9926 return retval;
9927}
9928
9929#if defined(FEAT_AUTOCMD) || defined(PROTO)
9930/*
9931 * Expand the <sfile> string in "arg".
9932 *
9933 * Returns an allocated string, or NULL for any error.
9934 */
9935 char_u *
9936expand_sfile(arg)
9937 char_u *arg;
9938{
9939 char_u *errormsg;
9940 int len;
9941 char_u *result;
9942 char_u *newres;
9943 char_u *repl;
9944 int srclen;
9945 char_u *p;
9946
9947 result = vim_strsave(arg);
9948 if (result == NULL)
9949 return NULL;
9950
9951 for (p = result; *p; )
9952 {
9953 if (STRNCMP(p, "<sfile>", 7) != 0)
9954 ++p;
9955 else
9956 {
9957 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009958 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 if (errormsg != NULL)
9960 {
9961 if (*errormsg)
9962 emsg(errormsg);
9963 vim_free(result);
9964 return NULL;
9965 }
9966 if (repl == NULL) /* no match (cannot happen) */
9967 {
9968 p += srclen;
9969 continue;
9970 }
9971 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9972 newres = alloc(len);
9973 if (newres == NULL)
9974 {
9975 vim_free(repl);
9976 vim_free(result);
9977 return NULL;
9978 }
9979 mch_memmove(newres, result, (size_t)(p - result));
9980 STRCPY(newres + (p - result), repl);
9981 len = (int)STRLEN(newres);
9982 STRCAT(newres, p + srclen);
9983 vim_free(repl);
9984 vim_free(result);
9985 result = newres;
9986 p = newres + len; /* continue after the match */
9987 }
9988 }
9989
9990 return result;
9991}
9992#endif
9993
9994#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009995static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9996 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9998static frame_T *ses_skipframe __ARGS((frame_T *fr));
9999static int ses_do_frame __ARGS((frame_T *fr));
10000static int ses_do_win __ARGS((win_T *wp));
10001static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10002static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10003static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10004
10005/*
10006 * Write openfile commands for the current buffers to an .exrc file.
10007 * Return FAIL on error, OK otherwise.
10008 */
10009 static int
10010makeopens(fd, dirnow)
10011 FILE *fd;
10012 char_u *dirnow; /* Current directory name */
10013{
10014 buf_T *buf;
10015 int only_save_windows = TRUE;
10016 int nr;
10017 int cnr = 1;
10018 int restore_size = TRUE;
10019 win_T *wp;
10020 char_u *sname;
10021 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010022 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010023 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010024 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010025 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010026 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
10028 if (ssop_flags & SSOP_BUFFERS)
10029 only_save_windows = FALSE; /* Save ALL buffers */
10030
10031 /*
10032 * Begin by setting the this_session variable, and then other
10033 * sessionable variables.
10034 */
10035#ifdef FEAT_EVAL
10036 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10037 return FAIL;
10038 if (ssop_flags & SSOP_GLOBALS)
10039 if (store_session_globals(fd) == FAIL)
10040 return FAIL;
10041#endif
10042
10043 /*
10044 * Close all windows but one.
10045 */
10046 if (put_line(fd, "silent only") == FAIL)
10047 return FAIL;
10048
10049 /*
10050 * Now a :cd command to the session directory or the current directory
10051 */
10052 if (ssop_flags & SSOP_SESDIR)
10053 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010054 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10055 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 return FAIL;
10057 }
10058 else if (ssop_flags & SSOP_CURDIR)
10059 {
10060 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10061 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010062 || fputs("cd ", fd) < 0
10063 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10064 || put_eol(fd) == FAIL)
10065 {
10066 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 vim_free(sname);
10070 }
10071
10072 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010073 * If there is an empty, unnamed buffer we will wipe it out later.
10074 * Remember the buffer number.
10075 */
10076 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10077 return FAIL;
10078 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10079 return FAIL;
10080 if (put_line(fd, "endif") == FAIL)
10081 return FAIL;
10082
10083 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * Now save the current files, current buffer first.
10085 */
10086 if (put_line(fd, "set shortmess=aoO") == FAIL)
10087 return FAIL;
10088
10089 /* Now put the other buffers into the buffer list */
10090 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10091 {
10092 if (!(only_save_windows && buf->b_nwindows == 0)
10093 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10094 && buf->b_fname != NULL
10095 && buf->b_p_bl)
10096 {
10097 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10098 : buf->b_wininfo->wi_fpos.lnum) < 0
10099 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10100 return FAIL;
10101 }
10102 }
10103
10104 /* the global argument list */
10105 if (ses_arglist(fd, "args", &global_alist.al_ga,
10106 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10107 return FAIL;
10108
10109 if (ssop_flags & SSOP_RESIZE)
10110 {
10111 /* Note: after the restore we still check it worked!*/
10112 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10113 || put_eol(fd) == FAIL)
10114 return FAIL;
10115 }
10116
10117#ifdef FEAT_GUI
10118 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10119 {
10120 int x, y;
10121
10122 if (gui_mch_get_winpos(&x, &y) == OK)
10123 {
10124 /* Note: after the restore we still check it worked!*/
10125 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10126 return FAIL;
10127 }
10128 }
10129#endif
10130
10131 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010132 * May repeat putting Windows for each tab, when "tabpages" is in
10133 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010134 * Don't use goto_tabpage(), it may change directory and trigger
10135 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010137 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010138 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010139 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010141 int need_tabnew = FALSE;
10142
Bram Moolenaar18144c82006-04-12 21:52:12 +000010143 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010145 tabpage_T *tp = find_tabpage(tabnr);
10146
10147 if (tp == NULL)
10148 break; /* done all tab pages */
10149 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010150 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010151 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010152 tab_topframe = topframe;
10153 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010154 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010155 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010156 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010157 tab_topframe = tp->tp_topframe;
10158 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010159 if (tabnr > 1)
10160 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
Bram Moolenaar18144c82006-04-12 21:52:12 +000010163 /*
10164 * Before creating the window layout, try loading one file. If this
10165 * is aborted we don't end up with a number of useless windows.
10166 * This may have side effects! (e.g., compressed or network file).
10167 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010168 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010169 {
10170 if (ses_do_win(wp)
10171 && wp->w_buffer->b_ffname != NULL
10172 && !wp->w_buffer->b_help
10173#ifdef FEAT_QUICKFIX
10174 && !bt_nofile(wp->w_buffer)
10175#endif
10176 )
10177 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010178 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010179 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10180 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010181 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010182 if (!wp->w_arg_idx_invalid)
10183 edited_win = wp;
10184 break;
10185 }
10186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010188 /* If no file got edited create an empty tab page. */
10189 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10190 return FAIL;
10191
Bram Moolenaar18144c82006-04-12 21:52:12 +000010192 /*
10193 * Save current window layout.
10194 */
10195 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010197 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010199 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10200 return FAIL;
10201 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203
Bram Moolenaar18144c82006-04-12 21:52:12 +000010204 /*
10205 * Check if window sizes can be restored (no windows omitted).
10206 * Remember the window number of the current window after restoring.
10207 */
10208 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010209 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010210 {
10211 if (ses_do_win(wp))
10212 ++nr;
10213 else
10214 restore_size = FALSE;
10215 if (curwin == wp)
10216 cnr = nr;
10217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
Bram Moolenaar18144c82006-04-12 21:52:12 +000010219 /* Go to the first window. */
10220 if (put_line(fd, "wincmd t") == FAIL)
10221 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010222
Bram Moolenaar18144c82006-04-12 21:52:12 +000010223 /*
10224 * If more than one window, see if sizes can be restored.
10225 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10226 * resized when moving between windows.
10227 * Do this before restoring the view, so that the topline and the
10228 * cursor can be set. This is done again below.
10229 */
10230 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10231 return FAIL;
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
Bram Moolenaar18144c82006-04-12 21:52:12 +000010235 /*
10236 * Restore the view of the window (options, file, cursor, etc.).
10237 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010238 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010239 {
10240 if (!ses_do_win(wp))
10241 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010242 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10243 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010244 return FAIL;
10245 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10246 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010247 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010248 }
10249
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010250 /* The argument index in the first tab page is zero, need to set it in
10251 * each window. For further tab pages it's the window where we do
10252 * "tabedit". */
10253 cur_arg_idx = next_arg_idx;
10254
Bram Moolenaar18144c82006-04-12 21:52:12 +000010255 /*
10256 * Restore cursor to the current window if it's not the first one.
10257 */
10258 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10259 || put_eol(fd) == FAIL))
10260 return FAIL;
10261
10262 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010263 * Restore window sizes again after jumping around in windows, because
10264 * the current window has a minimum size while others may not.
10265 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010266 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010267 return FAIL;
10268
Bram Moolenaar18144c82006-04-12 21:52:12 +000010269 /* Don't continue in another tab page when doing only the current one
10270 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010271 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010272 break;
10273 }
10274
10275 if (ssop_flags & SSOP_TABPAGES)
10276 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010277 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10278 || put_eol(fd) == FAIL)
10279 return FAIL;
10280 }
10281
Bram Moolenaar9c102382006-05-03 21:26:49 +000010282 /*
10283 * Wipe out an empty unnamed buffer we started in.
10284 */
10285 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10286 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010287 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010288 return FAIL;
10289 if (put_line(fd, "endif") == FAIL)
10290 return FAIL;
10291 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10292 return FAIL;
10293
10294 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10295 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10296 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298
10299 /*
10300 * Lastly, execute the x.vim file if it exists.
10301 */
10302 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10303 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010304 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 || put_line(fd, "endif") == FAIL)
10306 return FAIL;
10307
10308 return OK;
10309}
10310
10311 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010312ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 FILE *fd;
10314 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010315 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
10317 int n = 0;
10318 win_T *wp;
10319
10320 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10321 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010322 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 {
10324 if (!ses_do_win(wp))
10325 continue;
10326 ++n;
10327
10328 /* restore height when not full height */
10329 if (wp->w_height + wp->w_status_height < topframe->fr_height
10330 && (fprintf(fd,
10331 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10332 n, (long)wp->w_height, Rows / 2, Rows) < 0
10333 || put_eol(fd) == FAIL))
10334 return FAIL;
10335
10336 /* restore width when not full width */
10337 if (wp->w_width < Columns && (fprintf(fd,
10338 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10339 n, (long)wp->w_width, Columns / 2, Columns) < 0
10340 || put_eol(fd) == FAIL))
10341 return FAIL;
10342 }
10343 }
10344 else
10345 {
10346 /* Just equalise window sizes */
10347 if (put_line(fd, "wincmd =") == FAIL)
10348 return FAIL;
10349 }
10350 return OK;
10351}
10352
10353/*
10354 * Write commands to "fd" to recursively create windows for frame "fr",
10355 * horizontally and vertically split.
10356 * After the commands the last window in the frame is the current window.
10357 * Returns FAIL when writing the commands to "fd" fails.
10358 */
10359 static int
10360ses_win_rec(fd, fr)
10361 FILE *fd;
10362 frame_T *fr;
10363{
10364 frame_T *frc;
10365 int count = 0;
10366
10367 if (fr->fr_layout != FR_LEAF)
10368 {
10369 /* Find first frame that's not skipped and then create a window for
10370 * each following one (first frame is already there). */
10371 frc = ses_skipframe(fr->fr_child);
10372 if (frc != NULL)
10373 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10374 {
10375 /* Make window as big as possible so that we have lots of room
10376 * to split. */
10377 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10378 || put_line(fd, fr->fr_layout == FR_COL
10379 ? "split" : "vsplit") == FAIL)
10380 return FAIL;
10381 ++count;
10382 }
10383
10384 /* Go back to the first window. */
10385 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10386 ? "%dwincmd k" : "%dwincmd h", count) < 0
10387 || put_eol(fd) == FAIL))
10388 return FAIL;
10389
10390 /* Recursively create frames/windows in each window of this column or
10391 * row. */
10392 frc = ses_skipframe(fr->fr_child);
10393 while (frc != NULL)
10394 {
10395 ses_win_rec(fd, frc);
10396 frc = ses_skipframe(frc->fr_next);
10397 /* Go to next window. */
10398 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10399 return FAIL;
10400 }
10401 }
10402 return OK;
10403}
10404
10405/*
10406 * Skip frames that don't contain windows we want to save in the Session.
10407 * Returns NULL when there none.
10408 */
10409 static frame_T *
10410ses_skipframe(fr)
10411 frame_T *fr;
10412{
10413 frame_T *frc;
10414
10415 for (frc = fr; frc != NULL; frc = frc->fr_next)
10416 if (ses_do_frame(frc))
10417 break;
10418 return frc;
10419}
10420
10421/*
10422 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10423 * the Session.
10424 */
10425 static int
10426ses_do_frame(fr)
10427 frame_T *fr;
10428{
10429 frame_T *frc;
10430
10431 if (fr->fr_layout == FR_LEAF)
10432 return ses_do_win(fr->fr_win);
10433 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10434 if (ses_do_frame(frc))
10435 return TRUE;
10436 return FALSE;
10437}
10438
10439/*
10440 * Return non-zero if window "wp" is to be stored in the Session.
10441 */
10442 static int
10443ses_do_win(wp)
10444 win_T *wp;
10445{
10446 if (wp->w_buffer->b_fname == NULL
10447#ifdef FEAT_QUICKFIX
10448 /* When 'buftype' is "nofile" can't restore the window contents. */
10449 || bt_nofile(wp->w_buffer)
10450#endif
10451 )
10452 return (ssop_flags & SSOP_BLANK);
10453 if (wp->w_buffer->b_help)
10454 return (ssop_flags & SSOP_HELP);
10455 return TRUE;
10456}
10457
10458/*
10459 * Write commands to "fd" to restore the view of a window.
10460 * Caller must make sure 'scrolloff' is zero.
10461 */
10462 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010463put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 FILE *fd;
10465 win_T *wp;
10466 int add_edit; /* add ":edit" command to view */
10467 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010468 int current_arg_idx; /* current argument index of the window, use
10469 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470{
10471 win_T *save_curwin;
10472 int f;
10473 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010474 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475
10476 /* Always restore cursor position for ":mksession". For ":mkview" only
10477 * when 'viewoptions' contains "cursor". */
10478 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10479
10480 /*
10481 * Local argument list.
10482 */
10483 if (wp->w_alist == &global_alist)
10484 {
10485 if (put_line(fd, "argglobal") == FAIL)
10486 return FAIL;
10487 }
10488 else
10489 {
10490 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10491 flagp == &vop_flags
10492 || !(*flagp & SSOP_CURDIR)
10493 || wp->w_localdir != NULL, flagp) == FAIL)
10494 return FAIL;
10495 }
10496
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010497 /* Only when part of a session: restore the argument index. Some
10498 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010499 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010500 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010502 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 || put_eol(fd) == FAIL)
10504 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010505 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 }
10507
10508 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010509 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 {
10511 /*
10512 * Load the file.
10513 */
10514 if (wp->w_buffer->b_ffname != NULL
10515#ifdef FEAT_QUICKFIX
10516 && !bt_nofile(wp->w_buffer)
10517#endif
10518 )
10519 {
10520 /*
10521 * Editing a file in this buffer: use ":edit file".
10522 * This may have side effects! (e.g., compressed or network file).
10523 */
10524 if (fputs("edit ", fd) < 0
10525 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10526 return FAIL;
10527 }
10528 else
10529 {
10530 /* No file in this buffer, just make it empty. */
10531 if (put_line(fd, "enew") == FAIL)
10532 return FAIL;
10533#ifdef FEAT_QUICKFIX
10534 if (wp->w_buffer->b_ffname != NULL)
10535 {
10536 /* The buffer does have a name, but it's not a file name. */
10537 if (fputs("file ", fd) < 0
10538 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10539 return FAIL;
10540 }
10541#endif
10542 do_cursor = FALSE;
10543 }
10544 }
10545
10546 /*
10547 * Local mappings and abbreviations.
10548 */
10549 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10550 && makemap(fd, wp->w_buffer) == FAIL)
10551 return FAIL;
10552
10553 /*
10554 * Local options. Need to go to the window temporarily.
10555 * Store only local values when using ":mkview" and when ":mksession" is
10556 * used and 'sessionoptions' doesn't include "options".
10557 * Some folding options are always stored when "folds" is included,
10558 * otherwise the folds would not be restored correctly.
10559 */
10560 save_curwin = curwin;
10561 curwin = wp;
10562 curbuf = curwin->w_buffer;
10563 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10564 f = makeset(fd, OPT_LOCAL,
10565 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10566#ifdef FEAT_FOLDING
10567 else if (*flagp & SSOP_FOLDS)
10568 f = makefoldset(fd);
10569#endif
10570 else
10571 f = OK;
10572 curwin = save_curwin;
10573 curbuf = curwin->w_buffer;
10574 if (f == FAIL)
10575 return FAIL;
10576
10577#ifdef FEAT_FOLDING
10578 /*
10579 * Save Folds when 'buftype' is empty and for help files.
10580 */
10581 if ((*flagp & SSOP_FOLDS)
10582 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010583# ifdef FEAT_QUICKFIX
10584 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10585# endif
10586 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 {
10588 if (put_folds(fd, wp) == FAIL)
10589 return FAIL;
10590 }
10591#endif
10592
10593 /*
10594 * Set the cursor after creating folds, since that moves the cursor.
10595 */
10596 if (do_cursor)
10597 {
10598
10599 /* Restore the cursor line in the file and relatively in the
10600 * window. Don't use "G", it changes the jumplist. */
10601 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10602 (long)wp->w_cursor.lnum,
10603 (long)(wp->w_cursor.lnum - wp->w_topline),
10604 (long)wp->w_height / 2, (long)wp->w_height) < 0
10605 || put_eol(fd) == FAIL
10606 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10607 || put_line(fd, "exe s:l") == FAIL
10608 || put_line(fd, "normal! zt") == FAIL
10609 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10610 || put_eol(fd) == FAIL)
10611 return FAIL;
10612 /* Restore the cursor column and left offset when not wrapping. */
10613 if (wp->w_cursor.col == 0)
10614 {
10615 if (put_line(fd, "normal! 0") == FAIL)
10616 return FAIL;
10617 }
10618 else
10619 {
10620 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10621 {
10622 if (fprintf(fd,
10623 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10624 (long)wp->w_cursor.col,
10625 (long)(wp->w_cursor.col - wp->w_leftcol),
10626 (long)wp->w_width / 2, (long)wp->w_width) < 0
10627 || put_eol(fd) == FAIL
10628 || put_line(fd, "if s:c > 0") == FAIL
10629 || fprintf(fd,
10630 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10631 (long)wp->w_cursor.col) < 0
10632 || put_eol(fd) == FAIL
10633 || put_line(fd, "else") == FAIL
10634 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10635 || put_eol(fd) == FAIL
10636 || put_line(fd, "endif") == FAIL)
10637 return FAIL;
10638 }
10639 else
10640 {
10641 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10642 || put_eol(fd) == FAIL)
10643 return FAIL;
10644 }
10645 }
10646 }
10647
10648 /*
10649 * Local directory.
10650 */
10651 if (wp->w_localdir != NULL)
10652 {
10653 if (fputs("lcd ", fd) < 0
10654 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10655 || put_eol(fd) == FAIL)
10656 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010657 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659
10660 return OK;
10661}
10662
10663/*
10664 * Write an argument list to the session file.
10665 * Returns FAIL if writing fails.
10666 */
10667 static int
10668ses_arglist(fd, cmd, gap, fullname, flagp)
10669 FILE *fd;
10670 char *cmd;
10671 garray_T *gap;
10672 int fullname; /* TRUE: use full path name */
10673 unsigned *flagp;
10674{
10675 int i;
10676 char_u buf[MAXPATHL];
10677 char_u *s;
10678
10679 if (gap->ga_len == 0)
10680 return put_line(fd, "silent! argdel *");
10681 if (fputs(cmd, fd) < 0)
10682 return FAIL;
10683 for (i = 0; i < gap->ga_len; ++i)
10684 {
10685 /* NULL file names are skipped (only happens when out of memory). */
10686 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10687 if (s != NULL)
10688 {
10689 if (fullname)
10690 {
10691 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10692 s = buf;
10693 }
10694 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10695 return FAIL;
10696 }
10697 }
10698 return put_eol(fd);
10699}
10700
10701/*
10702 * Write a buffer name to the session file.
10703 * Also ends the line.
10704 * Returns FAIL if writing fails.
10705 */
10706 static int
10707ses_fname(fd, buf, flagp)
10708 FILE *fd;
10709 buf_T *buf;
10710 unsigned *flagp;
10711{
10712 char_u *name;
10713
10714 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010715 * the session file will be sourced.
10716 * Don't do this for ":mkview", we don't know the current directory.
10717 * Don't do this after ":lcd", we don't keep track of what the current
10718 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (buf->b_sfname != NULL
10720 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010721 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010722#ifdef FEAT_AUTOCHDIR
10723 && !p_acd
10724#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010725 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 name = buf->b_sfname;
10727 else
10728 name = buf->b_ffname;
10729 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10730 return FAIL;
10731 return OK;
10732}
10733
10734/*
10735 * Write a file name to the session file.
10736 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10737 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010738 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 */
10740 static int
10741ses_put_fname(fd, name, flagp)
10742 FILE *fd;
10743 char_u *name;
10744 unsigned *flagp;
10745{
10746 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010747 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749
10750 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010751 if (sname == NULL)
10752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010754 if (*flagp & SSOP_SLASH)
10755 {
10756 /* change all backslashes to forward slashes */
10757 for (p = sname; *p != NUL; mb_ptr_adv(p))
10758 if (*p == '\\')
10759 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010761
10762 /* escapse special characters */
10763 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010765 if (p == NULL)
10766 return FAIL;
10767
10768 /* write the result */
10769 if (fputs((char *)p, fd) < 0)
10770 retval = FAIL;
10771
10772 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 return retval;
10774}
10775
10776/*
10777 * ":loadview [nr]"
10778 */
10779 static void
10780ex_loadview(eap)
10781 exarg_T *eap;
10782{
10783 char_u *fname;
10784
10785 fname = get_view_file(*eap->arg);
10786 if (fname != NULL)
10787 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010788 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 vim_free(fname);
10790 }
10791}
10792
10793/*
10794 * Get the name of the view file for the current buffer.
10795 */
10796 static char_u *
10797get_view_file(c)
10798 int c;
10799{
10800 int len = 0;
10801 char_u *p, *s;
10802 char_u *retval;
10803 char_u *sname;
10804
10805 if (curbuf->b_ffname == NULL)
10806 {
10807 EMSG(_(e_noname));
10808 return NULL;
10809 }
10810 sname = home_replace_save(NULL, curbuf->b_ffname);
10811 if (sname == NULL)
10812 return NULL;
10813
10814 /*
10815 * We want a file name without separators, because we're not going to make
10816 * a directory.
10817 * "normal" path separator -> "=+"
10818 * "=" -> "=="
10819 * ":" path separator -> "=-"
10820 */
10821 for (p = sname; *p; ++p)
10822 if (*p == '=' || vim_ispathsep(*p))
10823 ++len;
10824 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10825 if (retval != NULL)
10826 {
10827 STRCPY(retval, p_vdir);
10828 add_pathsep(retval);
10829 s = retval + STRLEN(retval);
10830 for (p = sname; *p; ++p)
10831 {
10832 if (*p == '=')
10833 {
10834 *s++ = '=';
10835 *s++ = '=';
10836 }
10837 else if (vim_ispathsep(*p))
10838 {
10839 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010840#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 || defined(VMS)
10842 if (*p == ':')
10843 *s++ = '-';
10844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010846 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 }
10848 else
10849 *s++ = *p;
10850 }
10851 *s++ = '=';
10852 *s++ = c;
10853 STRCPY(s, ".vim");
10854 }
10855
10856 vim_free(sname);
10857 return retval;
10858}
10859
10860#endif /* FEAT_SESSION */
10861
10862/*
10863 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10864 * Return FAIL for a write error.
10865 */
10866 int
10867put_eol(fd)
10868 FILE *fd;
10869{
10870 if (
10871#ifdef USE_CRNL
10872 (
10873# ifdef MKSESSION_NL
10874 !mksession_nl &&
10875# endif
10876 (putc('\r', fd) < 0)) ||
10877#endif
10878 (putc('\n', fd) < 0))
10879 return FAIL;
10880 return OK;
10881}
10882
10883/*
10884 * Write a line to "fd".
10885 * Return FAIL for a write error.
10886 */
10887 int
10888put_line(fd, s)
10889 FILE *fd;
10890 char *s;
10891{
10892 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10893 return FAIL;
10894 return OK;
10895}
10896
10897#ifdef FEAT_VIMINFO
10898/*
10899 * ":rviminfo" and ":wviminfo".
10900 */
10901 static void
10902ex_viminfo(eap)
10903 exarg_T *eap;
10904{
10905 char_u *save_viminfo;
10906
10907 save_viminfo = p_viminfo;
10908 if (*p_viminfo == NUL)
10909 p_viminfo = (char_u *)"'100";
10910 if (eap->cmdidx == CMD_rviminfo)
10911 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010912 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10913 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 EMSG(_("E195: Cannot open viminfo file for reading"));
10915 }
10916 else
10917 write_viminfo(eap->arg, eap->forceit);
10918 p_viminfo = save_viminfo;
10919}
10920#endif
10921
10922#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010923/*
10924 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010925 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 void
10928dialog_msg(buff, format, fname)
10929 char_u *buff;
10930 char *format;
10931 char_u *fname;
10932{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 if (fname == NULL)
10934 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010935 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936}
10937#endif
10938
10939/*
10940 * ":behave {mswin,xterm}"
10941 */
10942 static void
10943ex_behave(eap)
10944 exarg_T *eap;
10945{
10946 if (STRCMP(eap->arg, "mswin") == 0)
10947 {
10948 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10949 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10950 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10951 set_option_value((char_u *)"keymodel", 0L,
10952 (char_u *)"startsel,stopsel", 0);
10953 }
10954 else if (STRCMP(eap->arg, "xterm") == 0)
10955 {
10956 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10957 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10958 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10959 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10960 }
10961 else
10962 EMSG2(_(e_invarg2), eap->arg);
10963}
10964
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010965#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10966/*
10967 * Function given to ExpandGeneric() to obtain the possible arguments of the
10968 * ":behave {mswin,xterm}" command.
10969 */
10970 char_u *
10971get_behave_arg(xp, idx)
10972 expand_T *xp UNUSED;
10973 int idx;
10974{
10975 if (idx == 0)
10976 return (char_u *)"mswin";
10977 if (idx == 1)
10978 return (char_u *)"xterm";
10979 return NULL;
10980}
10981#endif
10982
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983#ifdef FEAT_AUTOCMD
10984static int filetype_detect = FALSE;
10985static int filetype_plugin = FALSE;
10986static int filetype_indent = FALSE;
10987
10988/*
10989 * ":filetype [plugin] [indent] {on,off,detect}"
10990 * on: Load the filetype.vim file to install autocommands for file types.
10991 * off: Load the ftoff.vim file to remove all autocommands for file types.
10992 * plugin on: load filetype.vim and ftplugin.vim
10993 * plugin off: load ftplugof.vim
10994 * indent on: load filetype.vim and indent.vim
10995 * indent off: load indoff.vim
10996 */
10997 static void
10998ex_filetype(eap)
10999 exarg_T *eap;
11000{
11001 char_u *arg = eap->arg;
11002 int plugin = FALSE;
11003 int indent = FALSE;
11004
11005 if (*eap->arg == NUL)
11006 {
11007 /* Print current status. */
11008 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11009 filetype_detect ? "ON" : "OFF",
11010 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11011 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11012 return;
11013 }
11014
11015 /* Accept "plugin" and "indent" in any order. */
11016 for (;;)
11017 {
11018 if (STRNCMP(arg, "plugin", 6) == 0)
11019 {
11020 plugin = TRUE;
11021 arg = skipwhite(arg + 6);
11022 continue;
11023 }
11024 if (STRNCMP(arg, "indent", 6) == 0)
11025 {
11026 indent = TRUE;
11027 arg = skipwhite(arg + 6);
11028 continue;
11029 }
11030 break;
11031 }
11032 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11033 {
11034 if (*arg == 'o' || !filetype_detect)
11035 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011036 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 filetype_detect = TRUE;
11038 if (plugin)
11039 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011040 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 filetype_plugin = TRUE;
11042 }
11043 if (indent)
11044 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011045 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 filetype_indent = TRUE;
11047 }
11048 }
11049 if (*arg == 'd')
11050 {
11051 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011052 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054 }
11055 else if (STRCMP(arg, "off") == 0)
11056 {
11057 if (plugin || indent)
11058 {
11059 if (plugin)
11060 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011061 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 filetype_plugin = FALSE;
11063 }
11064 if (indent)
11065 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011066 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 filetype_indent = FALSE;
11068 }
11069 }
11070 else
11071 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011072 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 filetype_detect = FALSE;
11074 }
11075 }
11076 else
11077 EMSG2(_(e_invarg2), arg);
11078}
11079
11080/*
11081 * ":setfiletype {name}"
11082 */
11083 static void
11084ex_setfiletype(eap)
11085 exarg_T *eap;
11086{
11087 if (!did_filetype)
11088 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11089}
11090#endif
11091
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 static void
11093ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011094 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
11096#ifdef FEAT_DIGRAPHS
11097 if (*eap->arg != NUL)
11098 putdigraph(eap->arg);
11099 else
11100 listdigraphs();
11101#else
11102 EMSG(_("E196: No digraphs in this version"));
11103#endif
11104}
11105
11106 static void
11107ex_set(eap)
11108 exarg_T *eap;
11109{
11110 int flags = 0;
11111
11112 if (eap->cmdidx == CMD_setlocal)
11113 flags = OPT_LOCAL;
11114 else if (eap->cmdidx == CMD_setglobal)
11115 flags = OPT_GLOBAL;
11116#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11117 if (cmdmod.browse && flags == 0)
11118 ex_options(eap);
11119 else
11120#endif
11121 (void)do_set(eap->arg, flags);
11122}
11123
11124#ifdef FEAT_SEARCH_EXTRA
11125/*
11126 * ":nohlsearch"
11127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 static void
11129ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011130 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
11132 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011133 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134}
11135
11136/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011137 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 * Sets nextcmd to the start of the next command, if any. Also called when
11139 * skipping commands to find the next command.
11140 */
11141 static void
11142ex_match(eap)
11143 exarg_T *eap;
11144{
11145 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011146 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 char_u *end;
11148 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011149 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011150
11151 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011152 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011153 else
11154 {
11155 EMSG(e_invcmd);
11156 return;
11157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
11159 /* First clear any old pattern. */
11160 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011161 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
11163 if (ends_excmd(*eap->arg))
11164 end = eap->arg;
11165 else if ((STRNICMP(eap->arg, "none", 4) == 0
11166 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11167 end = eap->arg + 4;
11168 else
11169 {
11170 p = skiptowhite(eap->arg);
11171 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011172 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 p = skipwhite(p);
11174 if (*p == NUL)
11175 {
11176 /* There must be two arguments. */
11177 EMSG2(_(e_invarg2), eap->arg);
11178 return;
11179 }
11180 end = skip_regexp(p + 1, *p, TRUE, NULL);
11181 if (!eap->skip)
11182 {
11183 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11184 {
11185 eap->errmsg = e_trailing;
11186 return;
11187 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011188 if (*end != *p)
11189 {
11190 EMSG2(_(e_invarg2), p);
11191 return;
11192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193
11194 c = *end;
11195 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011196 match_add(curwin, g, p + 1, 10, id);
11197 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011198 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 }
11200 }
11201 eap->nextcmd = find_nextcmd(end);
11202}
11203#endif
11204
11205#ifdef FEAT_CRYPT
11206/*
11207 * ":X": Get crypt key
11208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 static void
11210ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011211 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011213 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011214 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215}
11216#endif
11217
11218#ifdef FEAT_FOLDING
11219 static void
11220ex_fold(eap)
11221 exarg_T *eap;
11222{
11223 if (foldManualAllowed(TRUE))
11224 foldCreate(eap->line1, eap->line2);
11225}
11226
11227 static void
11228ex_foldopen(eap)
11229 exarg_T *eap;
11230{
11231 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11232 eap->forceit, FALSE);
11233}
11234
11235 static void
11236ex_folddo(eap)
11237 exarg_T *eap;
11238{
11239 linenr_T lnum;
11240
11241 /* First set the marks for all lines closed/open. */
11242 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11243 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11244 ml_setmarked(lnum);
11245
11246 /* Execute the command on the marked lines. */
11247 global_exe(eap->arg);
11248 ml_clearmarked(); /* clear rest of the marks */
11249}
11250#endif