blob: ae9bb0fcf3f987824a386907a65a8dacbeaa2b88 [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)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005099 vim_strncpy(buff,
5100 (char_u *)_("1 more file to edit. Quit anyway?"),
5101 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005103 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 _("%d more files to edit. Quit anyway?"), n);
5105 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5106 return OK;
5107 return FAIL;
5108 }
5109#endif
5110 if (n == 1)
5111 EMSG(_("E173: 1 more file to edit"));
5112 else
5113 EMSGN(_("E173: %ld more files to edit"), n);
5114 quitmore = 2; /* next try to quit is allowed */
5115 }
5116 return FAIL;
5117 }
5118 return OK;
5119}
5120
5121#ifdef FEAT_CMDL_COMPL
5122/*
5123 * Function given to ExpandGeneric() to obtain the list of command names.
5124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 char_u *
5126get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005127 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 int idx;
5129{
5130 if (idx >= (int)CMD_SIZE)
5131# ifdef FEAT_USR_CMDS
5132 return get_user_command_name(idx);
5133# else
5134 return NULL;
5135# endif
5136 return cmdnames[idx].cmd_name;
5137}
5138#endif
5139
5140#if defined(FEAT_USR_CMDS) || defined(PROTO)
5141static 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));
5142static void uc_list __ARGS((char_u *name, size_t name_len));
5143static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5144static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5145static 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));
5146
5147 static int
5148uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5149 char_u *name;
5150 size_t name_len;
5151 char_u *rep;
5152 long argt;
5153 long def;
5154 int flags;
5155 int compl;
5156 char_u *compl_arg;
5157 int force;
5158{
5159 ucmd_T *cmd = NULL;
5160 char_u *p;
5161 int i;
5162 int cmp = 1;
5163 char_u *rep_buf = NULL;
5164 garray_T *gap;
5165
Bram Moolenaar9c102382006-05-03 21:26:49 +00005166 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (rep_buf == NULL)
5168 {
5169 /* Can't replace termcodes - try using the string as is */
5170 rep_buf = vim_strsave(rep);
5171
5172 /* Give up if out of memory */
5173 if (rep_buf == NULL)
5174 return FAIL;
5175 }
5176
5177 /* get address of growarray: global or in curbuf */
5178 if (flags & UC_BUFFER)
5179 {
5180 gap = &curbuf->b_ucmds;
5181 if (gap->ga_itemsize == 0)
5182 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5183 }
5184 else
5185 gap = &ucmds;
5186
5187 /* Search for the command in the already defined commands. */
5188 for (i = 0; i < gap->ga_len; ++i)
5189 {
5190 size_t len;
5191
5192 cmd = USER_CMD_GA(gap, i);
5193 len = STRLEN(cmd->uc_name);
5194 cmp = STRNCMP(name, cmd->uc_name, name_len);
5195 if (cmp == 0)
5196 {
5197 if (name_len < len)
5198 cmp = -1;
5199 else if (name_len > len)
5200 cmp = 1;
5201 }
5202
5203 if (cmp == 0)
5204 {
5205 if (!force)
5206 {
5207 EMSG(_("E174: Command already exists: add ! to replace it"));
5208 goto fail;
5209 }
5210
5211 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005212 cmd->uc_rep = NULL;
5213#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5214 vim_free(cmd->uc_compl_arg);
5215 cmd->uc_compl_arg = NULL;
5216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218 }
5219
5220 /* Stop as soon as we pass the name to add */
5221 if (cmp < 0)
5222 break;
5223 }
5224
5225 /* Extend the array unless we're replacing an existing command */
5226 if (cmp != 0)
5227 {
5228 if (ga_grow(gap, 1) != OK)
5229 goto fail;
5230 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5231 goto fail;
5232
5233 cmd = USER_CMD_GA(gap, i);
5234 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5235
5236 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
5238 cmd->uc_name = p;
5239 }
5240
5241 cmd->uc_rep = rep_buf;
5242 cmd->uc_argt = argt;
5243 cmd->uc_def = def;
5244 cmd->uc_compl = compl;
5245#ifdef FEAT_EVAL
5246 cmd->uc_scriptID = current_SID;
5247# ifdef FEAT_CMDL_COMPL
5248 cmd->uc_compl_arg = compl_arg;
5249# endif
5250#endif
5251
5252 return OK;
5253
5254fail:
5255 vim_free(rep_buf);
5256#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5257 vim_free(compl_arg);
5258#endif
5259 return FAIL;
5260}
5261
5262/*
5263 * List of names for completion for ":command" with the EXPAND_ flag.
5264 * Must be alphabetical for completion.
5265 */
5266static struct
5267{
5268 int expand;
5269 char *name;
5270} command_complete[] =
5271{
5272 {EXPAND_AUGROUP, "augroup"},
5273 {EXPAND_BUFFERS, "buffer"},
5274 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005275#if defined(FEAT_CSCOPE)
5276 {EXPAND_CSCOPE, "cscope"},
5277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5279 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005280 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#endif
5282 {EXPAND_DIRECTORIES, "dir"},
5283 {EXPAND_ENV_VARS, "environment"},
5284 {EXPAND_EVENTS, "event"},
5285 {EXPAND_EXPRESSION, "expression"},
5286 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005287 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {EXPAND_FUNCTIONS, "function"},
5289 {EXPAND_HELP, "help"},
5290 {EXPAND_HIGHLIGHT, "highlight"},
5291 {EXPAND_MAPPINGS, "mapping"},
5292 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005293 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005295 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005296#if defined(FEAT_SIGNS)
5297 {EXPAND_SIGN, "sign"},
5298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {EXPAND_TAGS, "tag"},
5300 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5301 {EXPAND_USER_VARS, "var"},
5302 {0, NULL}
5303};
5304
5305 static void
5306uc_list(name, name_len)
5307 char_u *name;
5308 size_t name_len;
5309{
5310 int i, j;
5311 int found = FALSE;
5312 ucmd_T *cmd;
5313 int len;
5314 long a;
5315 garray_T *gap;
5316
5317 gap = &curbuf->b_ucmds;
5318 for (;;)
5319 {
5320 for (i = 0; i < gap->ga_len; ++i)
5321 {
5322 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005323 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
5325 /* Skip commands which don't match the requested prefix */
5326 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5327 continue;
5328
5329 /* Put out the title first time */
5330 if (!found)
5331 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5332 found = TRUE;
5333 msg_putchar('\n');
5334 if (got_int)
5335 break;
5336
5337 /* Special cases */
5338 msg_putchar(a & BANG ? '!' : ' ');
5339 msg_putchar(a & REGSTR ? '"' : ' ');
5340 msg_putchar(gap != &ucmds ? 'b' : ' ');
5341 msg_putchar(' ');
5342
5343 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5344 len = (int)STRLEN(cmd->uc_name) + 4;
5345
5346 do {
5347 msg_putchar(' ');
5348 ++len;
5349 } while (len < 16);
5350
5351 len = 0;
5352
5353 /* Arguments */
5354 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5355 {
5356 case 0: IObuff[len++] = '0'; break;
5357 case (EXTRA): IObuff[len++] = '*'; break;
5358 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5359 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5360 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5361 }
5362
5363 do {
5364 IObuff[len++] = ' ';
5365 } while (len < 5);
5366
5367 /* Range */
5368 if (a & (RANGE|COUNT))
5369 {
5370 if (a & COUNT)
5371 {
5372 /* -count=N */
5373 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5374 len += (int)STRLEN(IObuff + len);
5375 }
5376 else if (a & DFLALL)
5377 IObuff[len++] = '%';
5378 else if (cmd->uc_def >= 0)
5379 {
5380 /* -range=N */
5381 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5382 len += (int)STRLEN(IObuff + len);
5383 }
5384 else
5385 IObuff[len++] = '.';
5386 }
5387
5388 do {
5389 IObuff[len++] = ' ';
5390 } while (len < 11);
5391
5392 /* Completion */
5393 for (j = 0; command_complete[j].expand != 0; ++j)
5394 if (command_complete[j].expand == cmd->uc_compl)
5395 {
5396 STRCPY(IObuff + len, command_complete[j].name);
5397 len += (int)STRLEN(IObuff + len);
5398 break;
5399 }
5400
5401 do {
5402 IObuff[len++] = ' ';
5403 } while (len < 21);
5404
5405 IObuff[len] = '\0';
5406 msg_outtrans(IObuff);
5407
5408 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005409#ifdef FEAT_EVAL
5410 if (p_verbose > 0)
5411 last_set_msg(cmd->uc_scriptID);
5412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 out_flush();
5414 ui_breakcheck();
5415 if (got_int)
5416 break;
5417 }
5418 if (gap == &ucmds || i < gap->ga_len)
5419 break;
5420 gap = &ucmds;
5421 }
5422
5423 if (!found)
5424 MSG(_("No user-defined commands found"));
5425}
5426
5427 static char_u *
5428uc_fun_cmd()
5429{
5430 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5431 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5432 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5433 0xb9, 0x7f, 0};
5434 int i;
5435
5436 for (i = 0; fcmd[i]; ++i)
5437 IObuff[i] = fcmd[i] - 0x40;
5438 IObuff[i] = 0;
5439 return IObuff;
5440}
5441
5442 static int
5443uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5444 char_u *attr;
5445 size_t len;
5446 long *argt;
5447 long *def;
5448 int *flags;
5449 int *compl;
5450 char_u **compl_arg;
5451{
5452 char_u *p;
5453
5454 if (len == 0)
5455 {
5456 EMSG(_("E175: No attribute specified"));
5457 return FAIL;
5458 }
5459
5460 /* First, try the simple attributes (no arguments) */
5461 if (STRNICMP(attr, "bang", len) == 0)
5462 *argt |= BANG;
5463 else if (STRNICMP(attr, "buffer", len) == 0)
5464 *flags |= UC_BUFFER;
5465 else if (STRNICMP(attr, "register", len) == 0)
5466 *argt |= REGSTR;
5467 else if (STRNICMP(attr, "bar", len) == 0)
5468 *argt |= TRLBAR;
5469 else
5470 {
5471 int i;
5472 char_u *val = NULL;
5473 size_t vallen = 0;
5474 size_t attrlen = len;
5475
5476 /* Look for the attribute name - which is the part before any '=' */
5477 for (i = 0; i < (int)len; ++i)
5478 {
5479 if (attr[i] == '=')
5480 {
5481 val = &attr[i + 1];
5482 vallen = len - i - 1;
5483 attrlen = i;
5484 break;
5485 }
5486 }
5487
5488 if (STRNICMP(attr, "nargs", attrlen) == 0)
5489 {
5490 if (vallen == 1)
5491 {
5492 if (*val == '0')
5493 /* Do nothing - this is the default */;
5494 else if (*val == '1')
5495 *argt |= (EXTRA | NOSPC | NEEDARG);
5496 else if (*val == '*')
5497 *argt |= EXTRA;
5498 else if (*val == '?')
5499 *argt |= (EXTRA | NOSPC);
5500 else if (*val == '+')
5501 *argt |= (EXTRA | NEEDARG);
5502 else
5503 goto wrong_nargs;
5504 }
5505 else
5506 {
5507wrong_nargs:
5508 EMSG(_("E176: Invalid number of arguments"));
5509 return FAIL;
5510 }
5511 }
5512 else if (STRNICMP(attr, "range", attrlen) == 0)
5513 {
5514 *argt |= RANGE;
5515 if (vallen == 1 && *val == '%')
5516 *argt |= DFLALL;
5517 else if (val != NULL)
5518 {
5519 p = val;
5520 if (*def >= 0)
5521 {
5522two_count:
5523 EMSG(_("E177: Count cannot be specified twice"));
5524 return FAIL;
5525 }
5526
5527 *def = getdigits(&p);
5528 *argt |= (ZEROR | NOTADR);
5529
5530 if (p != val + vallen || vallen == 0)
5531 {
5532invalid_count:
5533 EMSG(_("E178: Invalid default value for count"));
5534 return FAIL;
5535 }
5536 }
5537 }
5538 else if (STRNICMP(attr, "count", attrlen) == 0)
5539 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005540 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
5542 if (val != NULL)
5543 {
5544 p = val;
5545 if (*def >= 0)
5546 goto two_count;
5547
5548 *def = getdigits(&p);
5549
5550 if (p != val + vallen)
5551 goto invalid_count;
5552 }
5553
5554 if (*def < 0)
5555 *def = 0;
5556 }
5557 else if (STRNICMP(attr, "complete", attrlen) == 0)
5558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (val == NULL)
5560 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005561 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return FAIL;
5563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005565 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5566 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 else
5570 {
5571 char_u ch = attr[len];
5572 attr[len] = '\0';
5573 EMSG2(_("E181: Invalid attribute: %s"), attr);
5574 attr[len] = ch;
5575 return FAIL;
5576 }
5577 }
5578
5579 return OK;
5580}
5581
Bram Moolenaara850a712009-01-28 14:42:59 +00005582/*
5583 * ":command ..."
5584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 static void
5586ex_command(eap)
5587 exarg_T *eap;
5588{
5589 char_u *name;
5590 char_u *end;
5591 char_u *p;
5592 long argt = 0;
5593 long def = -1;
5594 int flags = 0;
5595 int compl = EXPAND_NOTHING;
5596 char_u *compl_arg = NULL;
5597 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005598 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
5600 p = eap->arg;
5601
5602 /* Check for attributes */
5603 while (*p == '-')
5604 {
5605 ++p;
5606 end = skiptowhite(p);
5607 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5608 == FAIL)
5609 return;
5610 p = skipwhite(end);
5611 }
5612
5613 /* Get the name (if any) and skip to the following argument */
5614 name = p;
5615 if (ASCII_ISALPHA(*p))
5616 while (ASCII_ISALNUM(*p))
5617 ++p;
5618 if (!ends_excmd(*p) && !vim_iswhite(*p))
5619 {
5620 EMSG(_("E182: Invalid command name"));
5621 return;
5622 }
5623 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005624 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
5626 /* If there is nothing after the name, and no attributes were specified,
5627 * we are listing commands
5628 */
5629 p = skipwhite(end);
5630 if (!has_attr && ends_excmd(*p))
5631 {
5632 uc_list(name, end - name);
5633 }
5634 else if (!ASCII_ISUPPER(*name))
5635 {
5636 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5637 return;
5638 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005639 else if ((name_len == 1 && *name == 'X')
5640 || (name_len <= 4
5641 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5642 {
5643 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5644 return;
5645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
5647 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5648 eap->forceit);
5649}
5650
5651/*
5652 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * Clear all user commands, global and for current buffer.
5654 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005655 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005657 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 uc_clear(&ucmds);
5660 uc_clear(&curbuf->b_ucmds);
5661}
5662
5663/*
5664 * Clear all user commands for "gap".
5665 */
5666 void
5667uc_clear(gap)
5668 garray_T *gap;
5669{
5670 int i;
5671 ucmd_T *cmd;
5672
5673 for (i = 0; i < gap->ga_len; ++i)
5674 {
5675 cmd = USER_CMD_GA(gap, i);
5676 vim_free(cmd->uc_name);
5677 vim_free(cmd->uc_rep);
5678# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5679 vim_free(cmd->uc_compl_arg);
5680# endif
5681 }
5682 ga_clear(gap);
5683}
5684
5685 static void
5686ex_delcommand(eap)
5687 exarg_T *eap;
5688{
5689 int i = 0;
5690 ucmd_T *cmd = NULL;
5691 int cmp = -1;
5692 garray_T *gap;
5693
5694 gap = &curbuf->b_ucmds;
5695 for (;;)
5696 {
5697 for (i = 0; i < gap->ga_len; ++i)
5698 {
5699 cmd = USER_CMD_GA(gap, i);
5700 cmp = STRCMP(eap->arg, cmd->uc_name);
5701 if (cmp <= 0)
5702 break;
5703 }
5704 if (gap == &ucmds || cmp == 0)
5705 break;
5706 gap = &ucmds;
5707 }
5708
5709 if (cmp != 0)
5710 {
5711 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5712 return;
5713 }
5714
5715 vim_free(cmd->uc_name);
5716 vim_free(cmd->uc_rep);
5717# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5718 vim_free(cmd->uc_compl_arg);
5719# endif
5720
5721 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723 if (i < gap->ga_len)
5724 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5725}
5726
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005727/*
5728 * split and quote args for <f-args>
5729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 static char_u *
5731uc_split_args(arg, lenp)
5732 char_u *arg;
5733 size_t *lenp;
5734{
5735 char_u *buf;
5736 char_u *p;
5737 char_u *q;
5738 int len;
5739
5740 /* Precalculate length */
5741 p = arg;
5742 len = 2; /* Initial and final quotes */
5743
5744 while (*p)
5745 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005746 if (p[0] == '\\' && p[1] == '\\')
5747 {
5748 len += 2;
5749 p += 2;
5750 }
5751 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 len += 1;
5754 p += 2;
5755 }
5756 else if (*p == '\\' || *p == '"')
5757 {
5758 len += 2;
5759 p += 1;
5760 }
5761 else if (vim_iswhite(*p))
5762 {
5763 p = skipwhite(p);
5764 if (*p == NUL)
5765 break;
5766 len += 3; /* "," */
5767 }
5768 else
5769 {
5770 ++len;
5771 ++p;
5772 }
5773 }
5774
5775 buf = alloc(len + 1);
5776 if (buf == NULL)
5777 {
5778 *lenp = 0;
5779 return buf;
5780 }
5781
5782 p = arg;
5783 q = buf;
5784 *q++ = '"';
5785 while (*p)
5786 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005787 if (p[0] == '\\' && p[1] == '\\')
5788 {
5789 *q++ = '\\';
5790 *q++ = '\\';
5791 p += 2;
5792 }
5793 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 *q++ = p[1];
5796 p += 2;
5797 }
5798 else if (*p == '\\' || *p == '"')
5799 {
5800 *q++ = '\\';
5801 *q++ = *p++;
5802 }
5803 else if (vim_iswhite(*p))
5804 {
5805 p = skipwhite(p);
5806 if (*p == NUL)
5807 break;
5808 *q++ = '"';
5809 *q++ = ',';
5810 *q++ = '"';
5811 }
5812 else
5813 {
5814 *q++ = *p++;
5815 }
5816 }
5817 *q++ = '"';
5818 *q = 0;
5819
5820 *lenp = len;
5821 return buf;
5822}
5823
5824/*
5825 * Check for a <> code in a user command.
5826 * "code" points to the '<'. "len" the length of the <> (inclusive).
5827 * "buf" is where the result is to be added.
5828 * "split_buf" points to a buffer used for splitting, caller should free it.
5829 * "split_len" is the length of what "split_buf" contains.
5830 * Returns the length of the replacement, which has been added to "buf".
5831 * Returns -1 if there was no match, and only the "<" has been copied.
5832 */
5833 static size_t
5834uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5835 char_u *code;
5836 size_t len;
5837 char_u *buf;
5838 ucmd_T *cmd; /* the user command we're expanding */
5839 exarg_T *eap; /* ex arguments */
5840 char_u **split_buf;
5841 size_t *split_len;
5842{
5843 size_t result = 0;
5844 char_u *p = code + 1;
5845 size_t l = len - 2;
5846 int quote = 0;
5847 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5848 ct_LT, ct_NONE } type = ct_NONE;
5849
5850 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5851 {
5852 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5853 p += 2;
5854 l -= 2;
5855 }
5856
Bram Moolenaar371d5402006-03-20 21:47:49 +00005857 ++l;
5858 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005860 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005862 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005864 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005866 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005868 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005870 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005872 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 type = ct_REGISTER;
5874
5875 switch (type)
5876 {
5877 case ct_ARGS:
5878 /* Simple case first */
5879 if (*eap->arg == NUL)
5880 {
5881 if (quote == 1)
5882 {
5883 result = 2;
5884 if (buf != NULL)
5885 STRCPY(buf, "''");
5886 }
5887 else
5888 result = 0;
5889 break;
5890 }
5891
5892 /* When specified there is a single argument don't split it.
5893 * Works for ":Cmd %" when % is "a b c". */
5894 if ((eap->argt & NOSPC) && quote == 2)
5895 quote = 1;
5896
5897 switch (quote)
5898 {
5899 case 0: /* No quoting, no splitting */
5900 result = STRLEN(eap->arg);
5901 if (buf != NULL)
5902 STRCPY(buf, eap->arg);
5903 break;
5904 case 1: /* Quote, but don't split */
5905 result = STRLEN(eap->arg) + 2;
5906 for (p = eap->arg; *p; ++p)
5907 {
5908 if (*p == '\\' || *p == '"')
5909 ++result;
5910 }
5911
5912 if (buf != NULL)
5913 {
5914 *buf++ = '"';
5915 for (p = eap->arg; *p; ++p)
5916 {
5917 if (*p == '\\' || *p == '"')
5918 *buf++ = '\\';
5919 *buf++ = *p;
5920 }
5921 *buf = '"';
5922 }
5923
5924 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005925 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 /* This is hard, so only do it once, and cache the result */
5927 if (*split_buf == NULL)
5928 *split_buf = uc_split_args(eap->arg, split_len);
5929
5930 result = *split_len;
5931 if (buf != NULL && result != 0)
5932 STRCPY(buf, *split_buf);
5933
5934 break;
5935 }
5936 break;
5937
5938 case ct_BANG:
5939 result = eap->forceit ? 1 : 0;
5940 if (quote)
5941 result += 2;
5942 if (buf != NULL)
5943 {
5944 if (quote)
5945 *buf++ = '"';
5946 if (eap->forceit)
5947 *buf++ = '!';
5948 if (quote)
5949 *buf = '"';
5950 }
5951 break;
5952
5953 case ct_LINE1:
5954 case ct_LINE2:
5955 case ct_COUNT:
5956 {
5957 char num_buf[20];
5958 long num = (type == ct_LINE1) ? eap->line1 :
5959 (type == ct_LINE2) ? eap->line2 :
5960 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5961 size_t num_len;
5962
5963 sprintf(num_buf, "%ld", num);
5964 num_len = STRLEN(num_buf);
5965 result = num_len;
5966
5967 if (quote)
5968 result += 2;
5969
5970 if (buf != NULL)
5971 {
5972 if (quote)
5973 *buf++ = '"';
5974 STRCPY(buf, num_buf);
5975 buf += num_len;
5976 if (quote)
5977 *buf = '"';
5978 }
5979
5980 break;
5981 }
5982
5983 case ct_REGISTER:
5984 result = eap->regname ? 1 : 0;
5985 if (quote)
5986 result += 2;
5987 if (buf != NULL)
5988 {
5989 if (quote)
5990 *buf++ = '\'';
5991 if (eap->regname)
5992 *buf++ = eap->regname;
5993 if (quote)
5994 *buf = '\'';
5995 }
5996 break;
5997
5998 case ct_LT:
5999 result = 1;
6000 if (buf != NULL)
6001 *buf = '<';
6002 break;
6003
6004 default:
6005 /* Not recognized: just copy the '<' and return -1. */
6006 result = (size_t)-1;
6007 if (buf != NULL)
6008 *buf = '<';
6009 break;
6010 }
6011
6012 return result;
6013}
6014
6015 static void
6016do_ucmd(eap)
6017 exarg_T *eap;
6018{
6019 char_u *buf;
6020 char_u *p;
6021 char_u *q;
6022
6023 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006024 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006025 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 size_t len, totlen;
6027
6028 size_t split_len = 0;
6029 char_u *split_buf = NULL;
6030 ucmd_T *cmd;
6031#ifdef FEAT_EVAL
6032 scid_T save_current_SID = current_SID;
6033#endif
6034
6035 if (eap->cmdidx == CMD_USER)
6036 cmd = USER_CMD(eap->useridx);
6037 else
6038 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6039
6040 /*
6041 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006042 * First round: "buf" is NULL, compute length, allocate "buf".
6043 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 */
6045 buf = NULL;
6046 for (;;)
6047 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006048 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006049 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006051
6052 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006054 start = vim_strchr(p, '<');
6055 if (start != NULL)
6056 end = vim_strchr(start + 1, '>');
6057 if (buf != NULL)
6058 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006059 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6060 ;
6061 if (*ksp == K_SPECIAL
6062 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006063 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6064# ifdef FEAT_GUI
6065 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6066# endif
6067 ))
6068 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006069 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006070 * KS_SPECIAL KE_FILLER, like for mappings, but
6071 * do_cmdline() doesn't handle that, so convert it back.
6072 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6073 len = ksp - p;
6074 if (len > 0)
6075 {
6076 mch_memmove(q, p, len);
6077 q += len;
6078 }
6079 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6080 p = ksp + 3;
6081 continue;
6082 }
6083 }
6084
6085 /* break if there no <item> is found */
6086 if (start == NULL || end == NULL)
6087 break;
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 /* Include the '>' */
6090 ++end;
6091
6092 /* Take everything up to the '<' */
6093 len = start - p;
6094 if (buf == NULL)
6095 totlen += len;
6096 else
6097 {
6098 mch_memmove(q, p, len);
6099 q += len;
6100 }
6101
6102 len = uc_check_code(start, end - start, q, cmd, eap,
6103 &split_buf, &split_len);
6104 if (len == (size_t)-1)
6105 {
6106 /* no match, continue after '<' */
6107 p = start + 1;
6108 len = 1;
6109 }
6110 else
6111 p = end;
6112 if (buf == NULL)
6113 totlen += len;
6114 else
6115 q += len;
6116 }
6117 if (buf != NULL) /* second time here, finished */
6118 {
6119 STRCPY(q, p);
6120 break;
6121 }
6122
6123 totlen += STRLEN(p); /* Add on the trailing characters */
6124 buf = alloc((unsigned)(totlen + 1));
6125 if (buf == NULL)
6126 {
6127 vim_free(split_buf);
6128 return;
6129 }
6130 }
6131
6132#ifdef FEAT_EVAL
6133 current_SID = cmd->uc_scriptID;
6134#endif
6135 (void)do_cmdline(buf, eap->getline, eap->cookie,
6136 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6137#ifdef FEAT_EVAL
6138 current_SID = save_current_SID;
6139#endif
6140 vim_free(buf);
6141 vim_free(split_buf);
6142}
6143
6144# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6145 static char_u *
6146get_user_command_name(idx)
6147 int idx;
6148{
6149 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6150}
6151
6152/*
6153 * Function given to ExpandGeneric() to obtain the list of user command names.
6154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 char_u *
6156get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006157 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 int idx;
6159{
6160 if (idx < curbuf->b_ucmds.ga_len)
6161 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6162 idx -= curbuf->b_ucmds.ga_len;
6163 if (idx < ucmds.ga_len)
6164 return USER_CMD(idx)->uc_name;
6165 return NULL;
6166}
6167
6168/*
6169 * Function given to ExpandGeneric() to obtain the list of user command
6170 * attributes.
6171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 char_u *
6173get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006174 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 int idx;
6176{
6177 static char *user_cmd_flags[] =
6178 {"bang", "bar", "buffer", "complete", "count",
6179 "nargs", "range", "register"};
6180
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006181 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 return NULL;
6183 return (char_u *)user_cmd_flags[idx];
6184}
6185
6186/*
6187 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 char_u *
6190get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006191 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 int idx;
6193{
6194 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6195
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006196 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 return NULL;
6198 return (char_u *)user_cmd_nargs[idx];
6199}
6200
6201/*
6202 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 char_u *
6205get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006206 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 int idx;
6208{
6209 return (char_u *)command_complete[idx].name;
6210}
6211# endif /* FEAT_CMDL_COMPL */
6212
6213#endif /* FEAT_USR_CMDS */
6214
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006215#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6216/*
6217 * Parse a completion argument "value[vallen]".
6218 * The detected completion goes in "*complp", argument type in "*argt".
6219 * When there is an argument, for function and user defined completion, it's
6220 * copied to allocated memory and stored in "*compl_arg".
6221 * Returns FAIL if something is wrong.
6222 */
6223 int
6224parse_compl_arg(value, vallen, complp, argt, compl_arg)
6225 char_u *value;
6226 int vallen;
6227 int *complp;
6228 long *argt;
6229 char_u **compl_arg;
6230{
6231 char_u *arg = NULL;
6232 size_t arglen = 0;
6233 int i;
6234 int valend = vallen;
6235
6236 /* Look for any argument part - which is the part after any ',' */
6237 for (i = 0; i < vallen; ++i)
6238 {
6239 if (value[i] == ',')
6240 {
6241 arg = &value[i + 1];
6242 arglen = vallen - i - 1;
6243 valend = i;
6244 break;
6245 }
6246 }
6247
6248 for (i = 0; command_complete[i].expand != 0; ++i)
6249 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006250 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006251 && STRNCMP(value, command_complete[i].name, valend) == 0)
6252 {
6253 *complp = command_complete[i].expand;
6254 if (command_complete[i].expand == EXPAND_BUFFERS)
6255 *argt |= BUFNAME;
6256 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6257 || command_complete[i].expand == EXPAND_FILES)
6258 *argt |= XFILE;
6259 break;
6260 }
6261 }
6262
6263 if (command_complete[i].expand == 0)
6264 {
6265 EMSG2(_("E180: Invalid complete value: %s"), value);
6266 return FAIL;
6267 }
6268
6269# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6270 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6271 && arg != NULL)
6272# else
6273 if (arg != NULL)
6274# endif
6275 {
6276 EMSG(_("E468: Completion argument only allowed for custom completion"));
6277 return FAIL;
6278 }
6279
6280# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6281 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6282 && arg == NULL)
6283 {
6284 EMSG(_("E467: Custom completion requires a function argument"));
6285 return FAIL;
6286 }
6287
6288 if (arg != NULL)
6289 *compl_arg = vim_strnsave(arg, (int)arglen);
6290# endif
6291 return OK;
6292}
6293#endif
6294
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 static void
6296ex_colorscheme(eap)
6297 exarg_T *eap;
6298{
Bram Moolenaare6850792010-05-14 15:28:44 +02006299 if (*eap->arg == NUL)
6300 {
6301#ifdef FEAT_EVAL
6302 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6303 char_u *p = NULL;
6304
6305 if (expr != NULL)
6306 {
6307 ++emsg_off;
6308 p = eval_to_string(expr, NULL, FALSE);
6309 --emsg_off;
6310 vim_free(expr);
6311 }
6312 if (p != NULL)
6313 {
6314 MSG(p);
6315 vim_free(p);
6316 }
6317 else
6318 MSG("default");
6319#else
6320 MSG(_("unknown"));
6321#endif
6322 }
6323 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6325}
6326
6327 static void
6328ex_highlight(eap)
6329 exarg_T *eap;
6330{
6331 if (*eap->arg == NUL && eap->cmd[2] == '!')
6332 MSG(_("Greetings, Vim user!"));
6333 do_highlight(eap->arg, eap->forceit, FALSE);
6334}
6335
6336
6337/*
6338 * Call this function if we thought we were going to exit, but we won't
6339 * (because of an error). May need to restore the terminal mode.
6340 */
6341 void
6342not_exiting()
6343{
6344 exiting = FALSE;
6345 settmode(TMODE_RAW);
6346}
6347
6348/*
6349 * ":quit": quit current window, quit Vim if closed the last window.
6350 */
6351 static void
6352ex_quit(eap)
6353 exarg_T *eap;
6354{
6355#ifdef FEAT_CMDWIN
6356 if (cmdwin_type != 0)
6357 {
6358 cmdwin_result = Ctrl_C;
6359 return;
6360 }
6361#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006362 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006363 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006364 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006365 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006366 return;
6367 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006368#ifdef FEAT_AUTOCMD
6369 if (curbuf_locked())
6370 return;
6371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373#ifdef FEAT_NETBEANS_INTG
6374 netbeansForcedQuit = eap->forceit;
6375#endif
6376
6377 /*
6378 * If there are more files or windows we won't exit.
6379 */
6380 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6381 exiting = TRUE;
6382 if ((!P_HID(curbuf)
6383 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6384 || check_more(TRUE, eap->forceit) == FAIL
6385 || (only_one_window() && check_changed_any(eap->forceit)))
6386 {
6387 not_exiting();
6388 }
6389 else
6390 {
6391#ifdef FEAT_WINDOWS
6392 if (only_one_window()) /* quit last window */
6393#endif
6394 getout(0);
6395#ifdef FEAT_WINDOWS
6396# ifdef FEAT_GUI
6397 need_mouse_correct = TRUE;
6398# endif
6399 /* close window; may free buffer */
6400 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6401#endif
6402 }
6403}
6404
6405/*
6406 * ":cquit".
6407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 static void
6409ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006410 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 getout(1); /* this does not always pass on the exit code to the Manx
6413 compiler. why? */
6414}
6415
6416/*
6417 * ":qall": try to quit all windows
6418 */
6419 static void
6420ex_quit_all(eap)
6421 exarg_T *eap;
6422{
6423# ifdef FEAT_CMDWIN
6424 if (cmdwin_type != 0)
6425 {
6426 if (eap->forceit)
6427 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6428 else
6429 cmdwin_result = K_XF2;
6430 return;
6431 }
6432# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006433
6434 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006435 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006436 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006437 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006438 return;
6439 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006440#ifdef FEAT_AUTOCMD
6441 if (curbuf_locked())
6442 return;
6443#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 exiting = TRUE;
6446 if (eap->forceit || !check_changed_any(FALSE))
6447 getout(0);
6448 not_exiting();
6449}
6450
Bram Moolenaard9967712006-03-11 21:18:15 +00006451#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452/*
6453 * ":close": close current window, unless it is the last one
6454 */
6455 static void
6456ex_close(eap)
6457 exarg_T *eap;
6458{
6459# ifdef FEAT_CMDWIN
6460 if (cmdwin_type != 0)
6461 cmdwin_result = K_IGNORE;
6462 else
6463# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006464 if (!text_locked()
6465#ifdef FEAT_AUTOCMD
6466 && !curbuf_locked()
6467#endif
6468 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006469 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470}
6471
Bram Moolenaard9967712006-03-11 21:18:15 +00006472# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473/*
6474 * ":pclose": Close any preview window.
6475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479{
6480 win_T *win;
6481
6482 for (win = firstwin; win != NULL; win = win->w_next)
6483 if (win->w_p_pvw)
6484 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006485 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486 break;
6487 }
6488}
Bram Moolenaard9967712006-03-11 21:18:15 +00006489# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490
Bram Moolenaarf740b292006-02-16 22:11:02 +00006491/*
6492 * Close window "win" and take care of handling closing the last window for a
6493 * modified buffer.
6494 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006496ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006499 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
6501 int need_hide;
6502 buf_T *buf = win->w_buffer;
6503
6504 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006507# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if ((p_confirm || cmdmod.confirm) && p_write)
6509 {
6510 dialog_changed(buf, FALSE);
6511 if (buf_valid(buf) && bufIsChanged(buf))
6512 return;
6513 need_hide = FALSE;
6514 }
6515 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
6518 EMSG(_(e_nowrtmsg));
6519 return;
6520 }
6521 }
6522
Bram Moolenaard9967712006-03-11 21:18:15 +00006523# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006525# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006528 if (tp == NULL)
6529 win_close(win, !need_hide && !P_HID(buf));
6530 else
6531 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532}
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006535 * ":tabclose": close current tab page, unless it is the last one.
6536 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 */
6538 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006539ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 exarg_T *eap;
6541{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006542 tabpage_T *tp;
6543
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006544# ifdef FEAT_CMDWIN
6545 if (cmdwin_type != 0)
6546 cmdwin_result = K_IGNORE;
6547 else
6548# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006550 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006553 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006554 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006555 tp = find_tabpage((int)eap->line2);
6556 if (tp == NULL)
6557 {
6558 beep_flush();
6559 return;
6560 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006561 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006562 {
6563 tabpage_close_other(tp, eap->forceit);
6564 return;
6565 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006566 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006567 if (!text_locked()
6568#ifdef FEAT_AUTOCMD
6569 && !curbuf_locked()
6570#endif
6571 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006572 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006574}
6575
6576/*
6577 * ":tabonly": close all tab pages except the current one
6578 */
6579 static void
6580ex_tabonly(eap)
6581 exarg_T *eap;
6582{
6583 tabpage_T *tp;
6584 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006585
6586# ifdef FEAT_CMDWIN
6587 if (cmdwin_type != 0)
6588 cmdwin_result = K_IGNORE;
6589 else
6590# endif
6591 if (first_tabpage->tp_next == NULL)
6592 MSG(_("Already only one tab page"));
6593 else
6594 {
6595 /* Repeat this up to a 1000 times, because autocommands may mess
6596 * up the lists. */
6597 for (done = 0; done < 1000; ++done)
6598 {
6599 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6600 if (tp->tp_topframe != topframe)
6601 {
6602 tabpage_close_other(tp, eap->forceit);
6603 /* if we failed to close it quit */
6604 if (valid_tabpage(tp))
6605 done = 1000;
6606 /* start over, "tp" is now invalid */
6607 break;
6608 }
6609 if (first_tabpage->tp_next == NULL)
6610 break;
6611 }
6612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
6615/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006616 * Close the current tab page.
6617 */
6618 void
6619tabpage_close(forceit)
6620 int forceit;
6621{
6622 /* First close all the windows but the current one. If that worked then
6623 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006624 if (lastwin != firstwin)
6625 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006626 if (lastwin == firstwin)
6627 ex_win_close(forceit, curwin, NULL);
6628# ifdef FEAT_GUI
6629 need_mouse_correct = TRUE;
6630# endif
6631}
6632
6633/*
6634 * Close tab page "tp", which is not the current tab page.
6635 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006636 * Also takes care of the tab pages line disappearing when closing the
6637 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006638 */
6639 void
6640tabpage_close_other(tp, forceit)
6641 tabpage_T *tp;
6642 int forceit;
6643{
6644 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006645 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006646 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006647
6648 /* Limit to 1000 windows, autocommands may add a window while we close
6649 * one. OK, so I'm paranoid... */
6650 while (++done < 1000)
6651 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006652 wp = tp->tp_firstwin;
6653 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006654
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006655 /* Autocommands may delete the tab page under our fingers and we may
6656 * fail to close a window with a modified buffer. */
6657 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006658 break;
6659 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006660
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006661 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006662 if (h != tabline_height())
6663 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006664}
6665
6666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 * ":only".
6668 */
6669 static void
6670ex_only(eap)
6671 exarg_T *eap;
6672{
6673# ifdef FEAT_GUI
6674 need_mouse_correct = TRUE;
6675# endif
6676 close_others(TRUE, eap->forceit);
6677}
6678
6679/*
6680 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006681 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006683 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684ex_all(eap)
6685 exarg_T *eap;
6686{
6687 if (eap->addr_count == 0)
6688 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006689 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690}
6691#endif /* FEAT_WINDOWS */
6692
6693 static void
6694ex_hide(eap)
6695 exarg_T *eap;
6696{
6697 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6698 eap->errmsg = e_invarg;
6699 else
6700 {
6701 /* ":hide" or ":hide | cmd": hide current window */
6702 eap->nextcmd = check_nextcmd(eap->arg);
6703#ifdef FEAT_WINDOWS
6704 if (!eap->skip)
6705 {
6706# ifdef FEAT_GUI
6707 need_mouse_correct = TRUE;
6708# endif
6709 win_close(curwin, FALSE); /* don't free buffer */
6710 }
6711#endif
6712 }
6713}
6714
6715/*
6716 * ":stop" and ":suspend": Suspend Vim.
6717 */
6718 static void
6719ex_stop(eap)
6720 exarg_T *eap;
6721{
6722 /*
6723 * Disallow suspending for "rvim".
6724 */
6725 if (!check_restricted()
6726#ifdef WIN3264
6727 /*
6728 * Check if external commands are allowed now.
6729 */
6730 && can_end_termcap_mode(TRUE)
6731#endif
6732 )
6733 {
6734 if (!eap->forceit)
6735 autowrite_all();
6736 windgoto((int)Rows - 1, 0);
6737 out_char('\n');
6738 out_flush();
6739 stoptermcap();
6740 out_flush(); /* needed for SUN to restore xterm buffer */
6741#ifdef FEAT_TITLE
6742 mch_restore_title(3); /* restore window titles */
6743#endif
6744 ui_suspend(); /* call machine specific function */
6745#ifdef FEAT_TITLE
6746 maketitle();
6747 resettitle(); /* force updating the title */
6748#endif
6749 starttermcap();
6750 scroll_start(); /* scroll screen before redrawing */
6751 redraw_later_clear();
6752 shell_resized(); /* may have resized window */
6753 }
6754}
6755
6756/*
6757 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6758 */
6759 static void
6760ex_exit(eap)
6761 exarg_T *eap;
6762{
6763#ifdef FEAT_CMDWIN
6764 if (cmdwin_type != 0)
6765 {
6766 cmdwin_result = Ctrl_C;
6767 return;
6768 }
6769#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006770 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006771 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006772 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006773 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006774 return;
6775 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006776#ifdef FEAT_AUTOCMD
6777 if (curbuf_locked())
6778 return;
6779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
6781 /*
6782 * if more files or windows we won't exit
6783 */
6784 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6785 exiting = TRUE;
6786 if ( ((eap->cmdidx == CMD_wq
6787 || curbufIsChanged())
6788 && do_write(eap) == FAIL)
6789 || check_more(TRUE, eap->forceit) == FAIL
6790 || (only_one_window() && check_changed_any(eap->forceit)))
6791 {
6792 not_exiting();
6793 }
6794 else
6795 {
6796#ifdef FEAT_WINDOWS
6797 if (only_one_window()) /* quit last window, exit Vim */
6798#endif
6799 getout(0);
6800#ifdef FEAT_WINDOWS
6801# ifdef FEAT_GUI
6802 need_mouse_correct = TRUE;
6803# endif
6804 /* quit current window, may free buffer */
6805 win_close(curwin, !P_HID(curwin->w_buffer));
6806#endif
6807 }
6808}
6809
6810/*
6811 * ":print", ":list", ":number".
6812 */
6813 static void
6814ex_print(eap)
6815 exarg_T *eap;
6816{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006817 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6818 EMSG(_(e_emptybuf));
6819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006821 for ( ;!got_int; ui_breakcheck())
6822 {
6823 print_line(eap->line1,
6824 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6825 || (eap->flags & EXFLAG_NR)),
6826 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6827 if (++eap->line1 > eap->line2)
6828 break;
6829 out_flush(); /* show one line at a time */
6830 }
6831 setpcmark();
6832 /* put cursor at last line */
6833 curwin->w_cursor.lnum = eap->line2;
6834 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838}
6839
6840#ifdef FEAT_BYTEOFF
6841 static void
6842ex_goto(eap)
6843 exarg_T *eap;
6844{
6845 goto_byte(eap->line2);
6846}
6847#endif
6848
6849/*
6850 * ":shell".
6851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 static void
6853ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006854 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 do_shell(NULL, 0);
6857}
6858
6859#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6860 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006861 || defined(FEAT_GUI_MSWIN) \
6862 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 || defined(PROTO)
6864
6865/*
6866 * Handle a file drop. The code is here because a drop is *nearly* like an
6867 * :args command, but not quite (we have a list of exact filenames, so we
6868 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6869 * code is very similar to :args and hence needs access to a lot of the static
6870 * functions in this file.
6871 *
6872 * The list should be allocated using alloc(), as should each item in the
6873 * list. This function takes over responsibility for freeing the list.
6874 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006875 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6877 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6878 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6879 * file functionality is (currently) not in EMX this is not presently a
6880 * problem.
6881 */
6882 void
6883handle_drop(filec, filev, split)
6884 int filec; /* the number of files dropped */
6885 char_u **filev; /* the list of files dropped */
6886 int split; /* force splitting the window */
6887{
6888 exarg_T ea;
6889 int save_msg_scroll = msg_scroll;
6890
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006891 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006892 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#ifdef FEAT_AUTOCMD
6895 if (curbuf_locked())
6896 return;
6897#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006898 /* When the screen is being updated we should not change buffers and
6899 * windows structures, it may cause freed memory to be used. */
6900 if (updating_screen)
6901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903 /* Check whether the current buffer is changed. If so, we will need
6904 * to split the current window or data could be lost.
6905 * We don't need to check if the 'hidden' option is set, as in this
6906 * case the buffer won't be lost.
6907 */
6908 if (!P_HID(curbuf) && !split)
6909 {
6910 ++emsg_off;
6911 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6912 --emsg_off;
6913 }
6914 if (split)
6915 {
6916# ifdef FEAT_WINDOWS
6917 if (win_split(0, 0) == FAIL)
6918 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006919 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /* When splitting the window, create a new alist. Otherwise the
6922 * existing one is overwritten. */
6923 alist_unlink(curwin->w_alist);
6924 alist_new();
6925# else
6926 return; /* can't split, always fail */
6927# endif
6928 }
6929
6930 /*
6931 * Set up the new argument list.
6932 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006933 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
6935 /*
6936 * Move to the first file.
6937 */
6938 /* Fake up a minimal "next" command for do_argfile() */
6939 vim_memset(&ea, 0, sizeof(ea));
6940 ea.cmd = (char_u *)"next";
6941 do_argfile(&ea, 0);
6942
6943 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6944 * mode that is not needed here. */
6945 need_start_insertmode = FALSE;
6946
6947 /* Restore msg_scroll, otherwise a following command may cause scrolling
6948 * unexpectedly. The screen will be redrawn by the caller, thus
6949 * msg_scroll being set by displaying a message is irrelevant. */
6950 msg_scroll = save_msg_scroll;
6951}
6952#endif
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954/*
6955 * Clear an argument list: free all file names and reset it to zero entries.
6956 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006957 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958alist_clear(al)
6959 alist_T *al;
6960{
6961 while (--al->al_ga.ga_len >= 0)
6962 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6963 ga_clear(&al->al_ga);
6964}
6965
6966/*
6967 * Init an argument list.
6968 */
6969 void
6970alist_init(al)
6971 alist_T *al;
6972{
6973 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6974}
6975
6976#if defined(FEAT_WINDOWS) || defined(PROTO)
6977
6978/*
6979 * Remove a reference from an argument list.
6980 * Ignored when the argument list is the global one.
6981 * If the argument list is no longer used by any window, free it.
6982 */
6983 void
6984alist_unlink(al)
6985 alist_T *al;
6986{
6987 if (al != &global_alist && --al->al_refcount <= 0)
6988 {
6989 alist_clear(al);
6990 vim_free(al);
6991 }
6992}
6993
6994# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6995/*
6996 * Create a new argument list and use it for the current window.
6997 */
6998 void
6999alist_new()
7000{
7001 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7002 if (curwin->w_alist == NULL)
7003 {
7004 curwin->w_alist = &global_alist;
7005 ++global_alist.al_refcount;
7006 }
7007 else
7008 {
7009 curwin->w_alist->al_refcount = 1;
7010 alist_init(curwin->w_alist);
7011 }
7012}
7013# endif
7014#endif
7015
7016#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7017/*
7018 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007019 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7020 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 */
7022 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007023alist_expand(fnum_list, fnum_len)
7024 int *fnum_list;
7025 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007028 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 char_u **new_arg_files;
7030 int new_arg_file_count;
7031 char_u *save_p_su = p_su;
7032 int i;
7033
7034 /* Don't use 'suffixes' here. This should work like the shell did the
7035 * expansion. Also, the vimrc file isn't read yet, thus the user
7036 * can't set the options. */
7037 p_su = empty_option;
7038 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7039 if (old_arg_files != NULL)
7040 {
7041 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007042 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7043 old_arg_count = GARGCOUNT;
7044 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 &new_arg_file_count, &new_arg_files,
7046 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7047 && new_arg_file_count > 0)
7048 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007049 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7050 TRUE, fnum_list, fnum_len);
7051 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
7054 p_su = save_p_su;
7055}
7056#endif
7057
7058/*
7059 * Set the argument list for the current window.
7060 * Takes over the allocated files[] and the allocated fnames in it.
7061 */
7062 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007063alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 alist_T *al;
7065 int count;
7066 char_u **files;
7067 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007068 int *fnum_list;
7069 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int i;
7072
7073 alist_clear(al);
7074 if (ga_grow(&al->al_ga, count) == OK)
7075 {
7076 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007077 {
7078 if (got_int)
7079 {
7080 /* When adding many buffers this can take a long time. Allow
7081 * interrupting here. */
7082 while (i < count)
7083 vim_free(files[i++]);
7084 break;
7085 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007086
7087 /* May set buffer name of a buffer previously used for the
7088 * argument list, so that it's re-used by alist_add. */
7089 if (fnum_list != NULL && i < fnum_len)
7090 buf_set_name(fnum_list[i], files[i]);
7091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007093 ui_breakcheck();
7094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 vim_free(files);
7096 }
7097 else
7098 FreeWild(count, files);
7099#ifdef FEAT_WINDOWS
7100 if (al == &global_alist)
7101#endif
7102 arg_had_last = FALSE;
7103}
7104
7105/*
7106 * Add file "fname" to argument list "al".
7107 * "fname" must have been allocated and "al" must have been checked for room.
7108 */
7109 void
7110alist_add(al, fname, set_fnum)
7111 alist_T *al;
7112 char_u *fname;
7113 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7114{
7115 if (fname == NULL) /* don't add NULL file names */
7116 return;
7117#ifdef BACKSLASH_IN_FILENAME
7118 slash_adjust(fname);
7119#endif
7120 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7121 if (set_fnum > 0)
7122 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7123 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7124 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125}
7126
7127#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7128/*
7129 * Adjust slashes in file names. Called after 'shellslash' was set.
7130 */
7131 void
7132alist_slash_adjust()
7133{
7134 int i;
7135# ifdef FEAT_WINDOWS
7136 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007137 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138# endif
7139
7140 for (i = 0; i < GARGCOUNT; ++i)
7141 if (GARGLIST[i].ae_fname != NULL)
7142 slash_adjust(GARGLIST[i].ae_fname);
7143# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007144 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (wp->w_alist != &global_alist)
7146 for (i = 0; i < WARGCOUNT(wp); ++i)
7147 if (WARGLIST(wp)[i].ae_fname != NULL)
7148 slash_adjust(WARGLIST(wp)[i].ae_fname);
7149# endif
7150}
7151#endif
7152
7153/*
7154 * ":preserve".
7155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 static void
7157ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007158 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007160 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 ml_preserve(curbuf, TRUE);
7162}
7163
7164/*
7165 * ":recover".
7166 */
7167 static void
7168ex_recover(eap)
7169 exarg_T *eap;
7170{
7171 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7172 recoverymode = TRUE;
7173 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7174 && (*eap->arg == NUL
7175 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7176 ml_recover();
7177 recoverymode = FALSE;
7178}
7179
7180/*
7181 * Command modifier used in a wrong way.
7182 */
7183 static void
7184ex_wrongmodifier(eap)
7185 exarg_T *eap;
7186{
7187 eap->errmsg = e_invcmd;
7188}
7189
7190#ifdef FEAT_WINDOWS
7191/*
7192 * :sview [+command] file split window with new file, read-only
7193 * :split [[+command] file] split window with current or new file
7194 * :vsplit [[+command] file] split window vertically with current or new file
7195 * :new [[+command] file] split window with no or new file
7196 * :vnew [[+command] file] split vertically window with no or new file
7197 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007198 *
7199 * :tabedit open new Tab page with empty window
7200 * :tabedit [+command] file open new Tab page and edit "file"
7201 * :tabnew [[+command] file] just like :tabedit
7202 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 */
7204 void
7205ex_splitview(eap)
7206 exarg_T *eap;
7207{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007208 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007209# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007211# endif
7212# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007216# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7218 {
7219 ex_ni(eap);
7220 return;
7221 }
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_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007230 * quickfix windows. But it's OK when doing ":tab split". */
7231 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
7233 if (eap->cmdidx == CMD_split)
7234 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007235# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 if (eap->cmdidx == CMD_vsplit)
7237 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007242# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007243 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {
7245 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7246 FNAME_MESS, TRUE, curbuf->b_ffname);
7247 if (fname == NULL)
7248 goto theend;
7249 eap->arg = fname;
7250 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007251# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007255# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007257# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 && eap->cmdidx != CMD_new)
7261 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007262# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007263 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007264# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007265 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007266# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007267 au_has_group((char_u *)"FileExplorer"))
7268 {
7269 /* No browsing supported but we do have the file explorer:
7270 * Edit the directory. */
7271 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7272 eap->arg = (char_u *)".";
7273 }
7274 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007275# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007276 {
7277 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007279 if (fname == NULL)
7280 goto theend;
7281 eap->arg = fname;
7282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007287 /*
7288 * Either open new tab page or split the window.
7289 */
7290 if (eap->cmdidx == CMD_tabedit
7291 || eap->cmdidx == CMD_tabfind
7292 || eap->cmdidx == CMD_tabnew)
7293 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007294 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7295 : eap->addr_count == 0 ? 0
7296 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007297 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007298 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007299
7300 /* set the alternate buffer for the window we came from */
7301 if (curwin != old_curwin
7302 && win_valid(old_curwin)
7303 && old_curwin->w_buffer != curbuf
7304 && !cmdmod.keepalt)
7305 old_curwin->w_alt_fnum = curbuf->b_fnum;
7306 }
7307 }
7308 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7310 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 /* Reset 'scrollbind' when editing another file, but keep it when
7313 * doing ":split" without arguments. */
7314 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007315# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007319 {
7320 RESET_BINDING(curwin);
7321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 else
7323 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 do_exedit(eap, old_curwin);
7326 }
7327
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007328# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007332# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333theend:
7334 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007337
7338/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007339 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007340 */
7341 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007342tabpage_new()
7343{
7344 exarg_T ea;
7345
7346 vim_memset(&ea, 0, sizeof(ea));
7347 ea.cmdidx = CMD_tabnew;
7348 ea.cmd = (char_u *)"tabn";
7349 ea.arg = (char_u *)"";
7350 ex_splitview(&ea);
7351}
7352
7353/*
7354 * :tabnext command
7355 */
7356 static void
7357ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007358 exarg_T *eap;
7359{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007360 switch (eap->cmdidx)
7361 {
7362 case CMD_tabfirst:
7363 case CMD_tabrewind:
7364 goto_tabpage(1);
7365 break;
7366 case CMD_tablast:
7367 goto_tabpage(9999);
7368 break;
7369 case CMD_tabprevious:
7370 case CMD_tabNext:
7371 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7372 break;
7373 default: /* CMD_tabnext */
7374 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7375 break;
7376 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007377}
7378
7379/*
7380 * :tabmove command
7381 */
7382 static void
7383ex_tabmove(eap)
7384 exarg_T *eap;
7385{
7386 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387}
7388
7389/*
7390 * :tabs command: List tabs and their contents.
7391 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392 static void
7393ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007394 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007395{
7396 tabpage_T *tp;
7397 win_T *wp;
7398 int tabcount = 1;
7399
7400 msg_start();
7401 msg_scroll = TRUE;
7402 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7403 {
7404 msg_putchar('\n');
7405 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7406 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7407 out_flush(); /* output one line at a time */
7408 ui_breakcheck();
7409
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007410 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 wp = firstwin;
7412 else
7413 wp = tp->tp_firstwin;
7414 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7415 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007416 msg_putchar('\n');
7417 msg_putchar(wp == curwin ? '>' : ' ');
7418 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007419 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7420 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421 if (buf_spname(wp->w_buffer) != NULL)
7422 STRCPY(IObuff, buf_spname(wp->w_buffer));
7423 else
7424 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7425 IObuff, IOSIZE, TRUE);
7426 msg_outtrans(IObuff);
7427 out_flush(); /* output one line at a time */
7428 ui_breakcheck();
7429 }
7430 }
7431}
7432
7433#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435/*
7436 * ":mode": Set screen mode.
7437 * If no argument given, just get the screen size and redraw.
7438 */
7439 static void
7440ex_mode(eap)
7441 exarg_T *eap;
7442{
7443 if (*eap->arg == NUL)
7444 shell_resized();
7445 else
7446 mch_screenmode(eap->arg);
7447}
7448
7449#ifdef FEAT_WINDOWS
7450/*
7451 * ":resize".
7452 * set, increment or decrement current window height
7453 */
7454 static void
7455ex_resize(eap)
7456 exarg_T *eap;
7457{
7458 int n;
7459 win_T *wp = curwin;
7460
7461 if (eap->addr_count > 0)
7462 {
7463 n = eap->line2;
7464 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7465 ;
7466 }
7467
7468#ifdef FEAT_GUI
7469 need_mouse_correct = TRUE;
7470#endif
7471 n = atol((char *)eap->arg);
7472#ifdef FEAT_VERTSPLIT
7473 if (cmdmod.split & WSP_VERT)
7474 {
7475 if (*eap->arg == '-' || *eap->arg == '+')
7476 n += W_WIDTH(curwin);
7477 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7478 n = 9999;
7479 win_setwidth_win((int)n, wp);
7480 }
7481 else
7482#endif
7483 {
7484 if (*eap->arg == '-' || *eap->arg == '+')
7485 n += curwin->w_height;
7486 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7487 n = 9999;
7488 win_setheight_win((int)n, wp);
7489 }
7490}
7491#endif
7492
7493/*
7494 * ":find [+command] <file>" command.
7495 */
7496 static void
7497ex_find(eap)
7498 exarg_T *eap;
7499{
7500#ifdef FEAT_SEARCHPATH
7501 char_u *fname;
7502 int count;
7503
7504 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7505 TRUE, curbuf->b_ffname);
7506 if (eap->addr_count > 0)
7507 {
7508 /* Repeat finding the file "count" times. This matters when it
7509 * appears several times in the path. */
7510 count = eap->line2;
7511 while (fname != NULL && --count > 0)
7512 {
7513 vim_free(fname);
7514 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7515 FALSE, curbuf->b_ffname);
7516 }
7517 }
7518
7519 if (fname != NULL)
7520 {
7521 eap->arg = fname;
7522#endif
7523 do_exedit(eap, NULL);
7524#ifdef FEAT_SEARCHPATH
7525 vim_free(fname);
7526 }
7527#endif
7528}
7529
7530/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 * ":open" simulation: for now just work like ":visual".
7532 */
7533 static void
7534ex_open(eap)
7535 exarg_T *eap;
7536{
7537 regmatch_T regmatch;
7538 char_u *p;
7539
7540 curwin->w_cursor.lnum = eap->line2;
7541 beginline(BL_SOL | BL_FIX);
7542 if (*eap->arg == '/')
7543 {
7544 /* ":open /pattern/": put cursor in column found with pattern */
7545 ++eap->arg;
7546 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7547 *p = NUL;
7548 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7549 if (regmatch.regprog != NULL)
7550 {
7551 regmatch.rm_ic = p_ic;
7552 p = ml_get_curline();
7553 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007554 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007555 else
7556 EMSG(_(e_nomatch));
7557 vim_free(regmatch.regprog);
7558 }
7559 /* Move to the NUL, ignore any other arguments. */
7560 eap->arg += STRLEN(eap->arg);
7561 }
7562 check_cursor();
7563
7564 eap->cmdidx = CMD_visual;
7565 do_exedit(eap, NULL);
7566}
7567
7568/*
7569 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 */
7571 static void
7572ex_edit(eap)
7573 exarg_T *eap;
7574{
7575 do_exedit(eap, NULL);
7576}
7577
7578/*
7579 * ":edit <file>" command and alikes.
7580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 void
7582do_exedit(eap, old_curwin)
7583 exarg_T *eap;
7584 win_T *old_curwin; /* curwin before doing a split or NULL */
7585{
7586 int n;
7587#ifdef FEAT_WINDOWS
7588 int need_hide;
7589#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007590 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
7592 /*
7593 * ":vi" command ends Ex mode.
7594 */
7595 if (exmode_active && (eap->cmdidx == CMD_visual
7596 || eap->cmdidx == CMD_view))
7597 {
7598 exmode_active = FALSE;
7599 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007600 {
7601 /* Special case: ":global/pat/visual\NLvi-commands" */
7602 if (global_busy)
7603 {
7604 int rd = RedrawingDisabled;
7605 int nwr = no_wait_return;
7606 int ms = msg_scroll;
7607#ifdef FEAT_GUI
7608 int he = hold_gui_events;
7609#endif
7610
7611 if (eap->nextcmd != NULL)
7612 {
7613 stuffReadbuff(eap->nextcmd);
7614 eap->nextcmd = NULL;
7615 }
7616
7617 if (exmode_was != EXMODE_VIM)
7618 settmode(TMODE_RAW);
7619 RedrawingDisabled = 0;
7620 no_wait_return = 0;
7621 need_wait_return = FALSE;
7622 msg_scroll = 0;
7623#ifdef FEAT_GUI
7624 hold_gui_events = 0;
7625#endif
7626 must_redraw = CLEAR;
7627
7628 main_loop(FALSE, TRUE);
7629
7630 RedrawingDisabled = rd;
7631 no_wait_return = nwr;
7632 msg_scroll = ms;
7633#ifdef FEAT_GUI
7634 hold_gui_events = he;
7635#endif
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640
7641 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007642 || eap->cmdidx == CMD_tabnew
7643 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644#ifdef FEAT_VERTSPLIT
7645 || eap->cmdidx == CMD_vnew
7646#endif
7647 ) && *eap->arg == NUL)
7648 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007649 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 setpcmark();
7651 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007652 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7653 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 else if ((eap->cmdidx != CMD_split
7656#ifdef FEAT_VERTSPLIT
7657 && eap->cmdidx != CMD_vsplit
7658#endif
7659 )
7660 || *eap->arg != NUL
7661#ifdef FEAT_BROWSE
7662 || cmdmod.browse
7663#endif
7664 )
7665 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007666#ifdef FEAT_AUTOCMD
7667 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7668 * can bring us here, others are stopped earlier. */
7669 if (*eap->arg != NUL && curbuf_locked())
7670 return;
7671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 n = readonlymode;
7673 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7674 readonlymode = TRUE;
7675 else if (eap->cmdidx == CMD_enew)
7676 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7677 empty buffer */
7678 setpcmark();
7679 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7680 NULL, eap,
7681 /* ":edit" goes to first line if Vi compatible */
7682 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7683 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7684 ? ECMD_ONE : eap->do_ecmd_lnum,
7685 (P_HID(curbuf) ? ECMD_HIDE : 0)
7686 + (eap->forceit ? ECMD_FORCEIT : 0)
7687#ifdef FEAT_LISTCMDS
7688 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7689#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007690 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
7692 /* Editing the file failed. If the window was split, close it. */
7693#ifdef FEAT_WINDOWS
7694 if (old_curwin != NULL)
7695 {
7696 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7697 if (!need_hide || P_HID(curbuf))
7698 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007699# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7700 cleanup_T cs;
7701
7702 /* Reset the error/interrupt/exception state here so that
7703 * aborting() returns FALSE when closing a window. */
7704 enter_cleanup(&cs);
7705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706# ifdef FEAT_GUI
7707 need_mouse_correct = TRUE;
7708# endif
7709 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007710
7711# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7712 /* Restore the error/interrupt/exception state if not
7713 * discarded by a new aborting error, interrupt, or
7714 * uncaught exception. */
7715 leave_cleanup(&cs);
7716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718 }
7719#endif
7720 }
7721 else if (readonlymode && curbuf->b_nwindows == 1)
7722 {
7723 /* When editing an already visited buffer, 'readonly' won't be set
7724 * but the previous value is kept. With ":view" and ":sview" we
7725 * want the file to be readonly, except when another window is
7726 * editing the same buffer. */
7727 curbuf->b_p_ro = TRUE;
7728 }
7729 readonlymode = n;
7730 }
7731 else
7732 {
7733 if (eap->do_ecmd_cmd != NULL)
7734 do_cmdline_cmd(eap->do_ecmd_cmd);
7735#ifdef FEAT_TITLE
7736 n = curwin->w_arg_idx_invalid;
7737#endif
7738 check_arg_idx(curwin);
7739#ifdef FEAT_TITLE
7740 if (n != curwin->w_arg_idx_invalid)
7741 maketitle();
7742#endif
7743 }
7744
7745#ifdef FEAT_WINDOWS
7746 /*
7747 * if ":split file" worked, set alternate file name in old window to new
7748 * file
7749 */
7750 if (old_curwin != NULL
7751 && *eap->arg != NUL
7752 && curwin != old_curwin
7753 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007754 && old_curwin->w_buffer != curbuf
7755 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 old_curwin->w_alt_fnum = curbuf->b_fnum;
7757#endif
7758
7759 ex_no_reprint = TRUE;
7760}
7761
7762#ifndef FEAT_GUI
7763/*
7764 * ":gui" and ":gvim" when there is no GUI.
7765 */
7766 static void
7767ex_nogui(eap)
7768 exarg_T *eap;
7769{
7770 eap->errmsg = e_nogvim;
7771}
7772#endif
7773
7774#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7775 static void
7776ex_tearoff(eap)
7777 exarg_T *eap;
7778{
7779 gui_make_tearoff(eap->arg);
7780}
7781#endif
7782
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007783#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 static void
7785ex_popup(eap)
7786 exarg_T *eap;
7787{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007788 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790#endif
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 static void
7793ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007794 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7797 MSG(_("No swap file"));
7798 else
7799 msg(curbuf->b_ml.ml_mfp->mf_fname);
7800}
7801
7802/*
7803 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7804 * offset.
7805 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 static void
7808ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007809 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811#ifdef FEAT_SCROLLBIND
7812 win_T *wp;
7813 long topline;
7814 long y;
7815 linenr_T old_linenr = curwin->w_cursor.lnum;
7816
7817 setpcmark();
7818
7819 /*
7820 * determine max topline
7821 */
7822 if (curwin->w_p_scb)
7823 {
7824 topline = curwin->w_topline;
7825 for (wp = firstwin; wp; wp = wp->w_next)
7826 {
7827 if (wp->w_p_scb && wp->w_buffer)
7828 {
7829 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7830 if (topline > y)
7831 topline = y;
7832 }
7833 }
7834 if (topline < 1)
7835 topline = 1;
7836 }
7837 else
7838 {
7839 topline = 1;
7840 }
7841
7842
7843 /*
7844 * set all scrollbind windows to the same topline
7845 */
7846 wp = curwin;
7847 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7848 {
7849 if (curwin->w_p_scb)
7850 {
7851 y = topline - curwin->w_topline;
7852 if (y > 0)
7853 scrollup(y, TRUE);
7854 else
7855 scrolldown(-y, TRUE);
7856 curwin->w_scbind_pos = topline;
7857 redraw_later(VALID);
7858 cursor_correct();
7859#ifdef FEAT_WINDOWS
7860 curwin->w_redr_status = TRUE;
7861#endif
7862 }
7863 }
7864 curwin = wp;
7865 if (curwin->w_p_scb)
7866 {
7867 did_syncbind = TRUE;
7868 checkpcmark();
7869 if (old_linenr != curwin->w_cursor.lnum)
7870 {
7871 char_u ctrl_o[2];
7872
7873 ctrl_o[0] = Ctrl_O;
7874 ctrl_o[1] = 0;
7875 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7876 }
7877 }
7878#endif
7879}
7880
7881
7882 static void
7883ex_read(eap)
7884 exarg_T *eap;
7885{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007886 int i;
7887 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7888 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
7890 if (eap->usefilter) /* :r!cmd */
7891 do_bang(1, eap, FALSE, FALSE, TRUE);
7892 else
7893 {
7894 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7895 return;
7896
7897#ifdef FEAT_BROWSE
7898 if (cmdmod.browse)
7899 {
7900 char_u *browseFile;
7901
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007902 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 NULL, NULL, NULL, curbuf);
7904 if (browseFile != NULL)
7905 {
7906 i = readfile(browseFile, NULL,
7907 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7908 vim_free(browseFile);
7909 }
7910 else
7911 i = OK;
7912 }
7913 else
7914#endif
7915 if (*eap->arg == NUL)
7916 {
7917 if (check_fname() == FAIL) /* check for no file name */
7918 return;
7919 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7920 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7921 }
7922 else
7923 {
7924 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7925 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7926 i = readfile(eap->arg, NULL,
7927 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7928
7929 }
7930 if (i == FAIL)
7931 {
7932#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7933 if (!aborting())
7934#endif
7935 EMSG2(_(e_notopen), eap->arg);
7936 }
7937 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007938 {
7939 if (empty && exmode_active)
7940 {
7941 /* Delete the empty line that remains. Historically ex does
7942 * this but vi doesn't. */
7943 if (eap->line2 == 0)
7944 lnum = curbuf->b_ml.ml_line_count;
7945 else
7946 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007947 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007948 {
7949 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007950 if (curwin->w_cursor.lnum > 1
7951 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007953 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007954 }
7955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959}
7960
Bram Moolenaarea408852005-06-25 22:49:46 +00007961static char_u *prev_dir = NULL;
7962
7963#if defined(EXITFREE) || defined(PROTO)
7964 void
7965free_cd_dir()
7966{
7967 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007968 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007969
7970 vim_free(globaldir);
7971 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007972}
7973#endif
7974
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976/*
7977 * ":cd", ":lcd", ":chdir" and ":lchdir".
7978 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007979 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980ex_cd(eap)
7981 exarg_T *eap;
7982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 char_u *new_dir;
7984 char_u *tofree;
7985
7986 new_dir = eap->arg;
7987#if !defined(UNIX) && !defined(VMS)
7988 /* for non-UNIX ":cd" means: print current directory */
7989 if (*new_dir == NUL)
7990 ex_pwd(NULL);
7991 else
7992#endif
7993 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007994#ifdef FEAT_AUTOCMD
7995 if (allbuf_locked())
7996 return;
7997#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007998 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7999 && !eap->forceit)
8000 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008001 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008002 return;
8003 }
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 /* ":cd -": Change to previous directory */
8006 if (STRCMP(new_dir, "-") == 0)
8007 {
8008 if (prev_dir == NULL)
8009 {
8010 EMSG(_("E186: No previous directory"));
8011 return;
8012 }
8013 new_dir = prev_dir;
8014 }
8015
8016 /* Save current directory for next ":cd -" */
8017 tofree = prev_dir;
8018 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8019 prev_dir = vim_strsave(NameBuff);
8020 else
8021 prev_dir = NULL;
8022
8023#if defined(UNIX) || defined(VMS)
8024 /* for UNIX ":cd" means: go to home directory */
8025 if (*new_dir == NUL)
8026 {
8027 /* use NameBuff for home directory name */
8028# ifdef VMS
8029 char_u *p;
8030
8031 p = mch_getenv((char_u *)"SYS$LOGIN");
8032 if (p == NULL || *p == NUL) /* empty is the same as not set */
8033 NameBuff[0] = NUL;
8034 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008035 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036# else
8037 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8038# endif
8039 new_dir = NameBuff;
8040 }
8041#endif
8042 if (new_dir == NULL || vim_chdir(new_dir))
8043 EMSG(_(e_failed));
8044 else
8045 {
8046 vim_free(curwin->w_localdir);
8047 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8048 {
8049 /* If still in global directory, need to remember current
8050 * directory as global directory. */
8051 if (globaldir == NULL && prev_dir != NULL)
8052 globaldir = vim_strsave(prev_dir);
8053 /* Remember this local directory for the window. */
8054 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8055 curwin->w_localdir = vim_strsave(NameBuff);
8056 }
8057 else
8058 {
8059 /* We are now in the global directory, no need to remember its
8060 * name. */
8061 vim_free(globaldir);
8062 globaldir = NULL;
8063 curwin->w_localdir = NULL;
8064 }
8065
8066 shorten_fnames(TRUE);
8067
8068 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008069 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 ex_pwd(eap);
8071 }
8072 vim_free(tofree);
8073 }
8074}
8075
8076/*
8077 * ":pwd".
8078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 static void
8080ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008081 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8084 {
8085#ifdef BACKSLASH_IN_FILENAME
8086 slash_adjust(NameBuff);
8087#endif
8088 msg(NameBuff);
8089 }
8090 else
8091 EMSG(_("E187: Unknown"));
8092}
8093
8094/*
8095 * ":=".
8096 */
8097 static void
8098ex_equal(eap)
8099 exarg_T *eap;
8100{
8101 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008102 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103}
8104
8105 static void
8106ex_sleep(eap)
8107 exarg_T *eap;
8108{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008109 int n;
8110 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 if (cursor_valid())
8113 {
8114 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8115 if (n >= 0)
8116 windgoto((int)n, curwin->w_wcol);
8117 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008118
8119 len = eap->line2;
8120 switch (*eap->arg)
8121 {
8122 case 'm': break;
8123 case NUL: len *= 1000L; break;
8124 default: EMSG2(_(e_invarg2), eap->arg); return;
8125 }
8126 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127}
8128
8129/*
8130 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8131 */
8132 void
8133do_sleep(msec)
8134 long msec;
8135{
8136 long done;
8137
8138 cursor_on();
8139 out_flush();
8140 for (done = 0; !got_int && done < msec; done += 1000L)
8141 {
8142 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8143 ui_breakcheck();
8144 }
8145}
8146
8147 static void
8148do_exmap(eap, isabbrev)
8149 exarg_T *eap;
8150 int isabbrev;
8151{
8152 int mode;
8153 char_u *cmdp;
8154
8155 cmdp = eap->cmd;
8156 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8157
8158 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8159 eap->arg, mode, isabbrev))
8160 {
8161 case 1: EMSG(_(e_invarg));
8162 break;
8163 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8164 break;
8165 }
8166}
8167
8168/*
8169 * ":winsize" command (obsolete).
8170 */
8171 static void
8172ex_winsize(eap)
8173 exarg_T *eap;
8174{
8175 int w, h;
8176 char_u *arg = eap->arg;
8177 char_u *p;
8178
8179 w = getdigits(&arg);
8180 arg = skipwhite(arg);
8181 p = arg;
8182 h = getdigits(&arg);
8183 if (*p != NUL && *arg == NUL)
8184 set_shellsize(w, h, TRUE);
8185 else
8186 EMSG(_("E465: :winsize requires two number arguments"));
8187}
8188
8189#ifdef FEAT_WINDOWS
8190 static void
8191ex_wincmd(eap)
8192 exarg_T *eap;
8193{
8194 int xchar = NUL;
8195 char_u *p;
8196
8197 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8198 {
8199 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8200 if (eap->arg[1] == NUL)
8201 {
8202 EMSG(_(e_invarg));
8203 return;
8204 }
8205 xchar = eap->arg[1];
8206 p = eap->arg + 2;
8207 }
8208 else
8209 p = eap->arg + 1;
8210
8211 eap->nextcmd = check_nextcmd(p);
8212 p = skipwhite(p);
8213 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8214 EMSG(_(e_invarg));
8215 else
8216 {
8217 /* Pass flags on for ":vertical wincmd ]". */
8218 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008219 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8221 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008222 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
8224}
8225#endif
8226
Bram Moolenaar843ee412004-06-30 16:16:41 +00008227#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228/*
8229 * ":winpos".
8230 */
8231 static void
8232ex_winpos(eap)
8233 exarg_T *eap;
8234{
8235 int x, y;
8236 char_u *arg = eap->arg;
8237 char_u *p;
8238
8239 if (*arg == NUL)
8240 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008241# if defined(FEAT_GUI) || defined(MSWIN)
8242# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008244# else
8245 if (mch_get_winpos(&x, &y) != FAIL)
8246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
8248 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8249 msg(IObuff);
8250 }
8251 else
8252# endif
8253 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8254 }
8255 else
8256 {
8257 x = getdigits(&arg);
8258 arg = skipwhite(arg);
8259 p = arg;
8260 y = getdigits(&arg);
8261 if (*p == NUL || *arg != NUL)
8262 {
8263 EMSG(_("E466: :winpos requires two number arguments"));
8264 return;
8265 }
8266# ifdef FEAT_GUI
8267 if (gui.in_use)
8268 gui_mch_set_winpos(x, y);
8269 else if (gui.starting)
8270 {
8271 /* Remember the coordinates for when the window is opened. */
8272 gui_win_x = x;
8273 gui_win_y = y;
8274 }
8275# ifdef HAVE_TGETENT
8276 else
8277# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008278# else
8279# ifdef MSWIN
8280 mch_set_winpos(x, y);
8281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282# endif
8283# ifdef HAVE_TGETENT
8284 if (*T_CWP)
8285 term_set_winpos(x, y);
8286# endif
8287 }
8288}
8289#endif
8290
8291/*
8292 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8293 */
8294 static void
8295ex_operators(eap)
8296 exarg_T *eap;
8297{
8298 oparg_T oa;
8299
8300 clear_oparg(&oa);
8301 oa.regname = eap->regname;
8302 oa.start.lnum = eap->line1;
8303 oa.end.lnum = eap->line2;
8304 oa.line_count = eap->line2 - eap->line1 + 1;
8305 oa.motion_type = MLINE;
8306#ifdef FEAT_VIRTUALEDIT
8307 virtual_op = FALSE;
8308#endif
8309 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8310 {
8311 setpcmark();
8312 curwin->w_cursor.lnum = eap->line1;
8313 beginline(BL_SOL | BL_FIX);
8314 }
8315
8316 switch (eap->cmdidx)
8317 {
8318 case CMD_delete:
8319 oa.op_type = OP_DELETE;
8320 op_delete(&oa);
8321 break;
8322
8323 case CMD_yank:
8324 oa.op_type = OP_YANK;
8325 (void)op_yank(&oa, FALSE, TRUE);
8326 break;
8327
8328 default: /* CMD_rshift or CMD_lshift */
8329 if ((eap->cmdidx == CMD_rshift)
8330#ifdef FEAT_RIGHTLEFT
8331 ^ curwin->w_p_rl
8332#endif
8333 )
8334 oa.op_type = OP_RSHIFT;
8335 else
8336 oa.op_type = OP_LSHIFT;
8337 op_shift(&oa, FALSE, eap->amount);
8338 break;
8339 }
8340#ifdef FEAT_VIRTUALEDIT
8341 virtual_op = MAYBE;
8342#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008343 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
8346/*
8347 * ":put".
8348 */
8349 static void
8350ex_put(eap)
8351 exarg_T *eap;
8352{
8353 /* ":0put" works like ":1put!". */
8354 if (eap->line2 == 0)
8355 {
8356 eap->line2 = 1;
8357 eap->forceit = TRUE;
8358 }
8359 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8361 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362}
8363
8364/*
8365 * Handle ":copy" and ":move".
8366 */
8367 static void
8368ex_copymove(eap)
8369 exarg_T *eap;
8370{
8371 long n;
8372
8373 n = get_address(&eap->arg, FALSE, FALSE);
8374 if (eap->arg == NULL) /* error detected */
8375 {
8376 eap->nextcmd = NULL;
8377 return;
8378 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008379 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
8381 /*
8382 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8383 */
8384 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8385 {
8386 EMSG(_(e_invaddr));
8387 return;
8388 }
8389
8390 if (eap->cmdidx == CMD_move)
8391 {
8392 if (do_move(eap->line1, eap->line2, n) == FAIL)
8393 return;
8394 }
8395 else
8396 ex_copy(eap->line1, eap->line2, n);
8397 u_clearline();
8398 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008399 ex_may_print(eap);
8400}
8401
8402/*
8403 * Print the current line if flags were given to the Ex command.
8404 */
8405 static void
8406ex_may_print(eap)
8407 exarg_T *eap;
8408{
8409 if (eap->flags != 0)
8410 {
8411 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8412 (eap->flags & EXFLAG_LIST));
8413 ex_no_reprint = TRUE;
8414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415}
8416
8417/*
8418 * ":smagic" and ":snomagic".
8419 */
8420 static void
8421ex_submagic(eap)
8422 exarg_T *eap;
8423{
8424 int magic_save = p_magic;
8425
8426 p_magic = (eap->cmdidx == CMD_smagic);
8427 do_sub(eap);
8428 p_magic = magic_save;
8429}
8430
8431/*
8432 * ":join".
8433 */
8434 static void
8435ex_join(eap)
8436 exarg_T *eap;
8437{
8438 curwin->w_cursor.lnum = eap->line1;
8439 if (eap->line1 == eap->line2)
8440 {
8441 if (eap->addr_count >= 2) /* :2,2join does nothing */
8442 return;
8443 if (eap->line2 == curbuf->b_ml.ml_line_count)
8444 {
8445 beep_flush();
8446 return;
8447 }
8448 ++eap->line2;
8449 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008450 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008452 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453}
8454
8455/*
8456 * ":[addr]@r" or ":[addr]*r": execute register
8457 */
8458 static void
8459ex_at(eap)
8460 exarg_T *eap;
8461{
8462 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008463 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
8465 curwin->w_cursor.lnum = eap->line2;
8466
8467#ifdef USE_ON_FLY_SCROLL
8468 dont_scroll = TRUE; /* disallow scrolling here */
8469#endif
8470
8471 /* get the register name. No name means to use the previous one */
8472 c = *eap->arg;
8473 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8474 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008475 /* Put the register in the typeahead buffer with the "silent" flag. */
8476 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8477 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 else
8482 {
8483 int save_efr = exec_from_reg;
8484
8485 exec_from_reg = TRUE;
8486
8487 /*
8488 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008489 * Continue until the stuff buffer is empty and all added characters
8490 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008492 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8494
8495 exec_from_reg = save_efr;
8496 }
8497}
8498
8499/*
8500 * ":!".
8501 */
8502 static void
8503ex_bang(eap)
8504 exarg_T *eap;
8505{
8506 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8507}
8508
8509/*
8510 * ":undo".
8511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 static void
8513ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008514 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008516 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008517 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008518 else
8519 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520}
8521
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008522#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008523 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008524ex_wundo(eap)
8525 exarg_T *eap;
8526{
8527 char_u hash[UNDO_HASH_SIZE];
8528
8529 u_compute_hash(hash);
8530 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8531}
8532
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008533 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008534ex_rundo(eap)
8535 exarg_T *eap;
8536{
8537 char_u hash[UNDO_HASH_SIZE];
8538
8539 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008540 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008541}
8542#endif
8543
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544/*
8545 * ":redo".
8546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 static void
8548ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008549 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 u_redo(1);
8552}
8553
8554/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008555 * ":earlier" and ":later".
8556 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008557 static void
8558ex_later(eap)
8559 exarg_T *eap;
8560{
8561 long count = 0;
8562 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008563 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008564 char_u *p = eap->arg;
8565
8566 if (*p == NUL)
8567 count = 1;
8568 else if (isdigit(*p))
8569 {
8570 count = getdigits(&p);
8571 switch (*p)
8572 {
8573 case 's': ++p; sec = TRUE; break;
8574 case 'm': ++p; sec = TRUE; count *= 60; break;
8575 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008576 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8577 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008578 }
8579 }
8580
8581 if (*p != NUL)
8582 EMSG2(_(e_invarg2), eap->arg);
8583 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008584 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8585 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008586}
8587
8588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 * ":redir": start/stop redirection.
8590 */
8591 static void
8592ex_redir(eap)
8593 exarg_T *eap;
8594{
8595 char *mode;
8596 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008597 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 if (STRICMP(eap->arg, "END") == 0)
8600 close_redir();
8601 else
8602 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008603 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008605 ++arg;
8606 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008608 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 mode = "a";
8610 }
8611 else
8612 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008613 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
8615 close_redir();
8616
8617 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008618 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (fname == NULL)
8620 return;
8621#ifdef FEAT_BROWSE
8622 if (cmdmod.browse)
8623 {
8624 char_u *browseFile;
8625
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008626 browseFile = do_browse(BROWSE_SAVE,
8627 (char_u *)_("Save Redirection"),
8628 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 if (browseFile == NULL)
8630 return; /* operation cancelled */
8631 vim_free(fname);
8632 fname = browseFile;
8633 eap->forceit = TRUE; /* since dialog already asked */
8634 }
8635#endif
8636
8637 redir_fd = open_exfile(fname, eap->forceit, mode);
8638 vim_free(fname);
8639 }
8640#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008641 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {
8643 /* redirect to a register a-z (resp. A-Z for appending) */
8644 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008645 ++arg;
8646 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008648 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008649 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008651 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008653 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008654 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008655 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008656 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008658 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008659 if (*arg == '>')
8660 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008661 /* Make register empty when not using @A-@Z and the
8662 * command is valid. */
8663 if (*arg == NUL && !isupper(redir_reg))
8664 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 }
8667 if (*arg != NUL)
8668 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008670 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671 }
8672 }
8673 else if (*arg == '=' && arg[1] == '>')
8674 {
8675 int append;
8676
8677 /* redirect to a variable */
8678 close_redir();
8679 arg += 2;
8680
8681 if (*arg == '>')
8682 {
8683 ++arg;
8684 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 }
8686 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008687 append = FALSE;
8688
8689 if (var_redir_start(skipwhite(arg), append) == OK)
8690 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 }
8692#endif
8693
8694 /* TODO: redirect to a buffer */
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008697 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008699
8700 /* Make sure redirection is not off. Can happen for cmdline completion
8701 * that indirectly invokes a command to catch its output. */
8702 if (redir_fd != NULL
8703#ifdef FEAT_EVAL
8704 || redir_reg || redir_vname
8705#endif
8706 )
8707 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708}
8709
8710/*
8711 * ":redraw": force redraw
8712 */
8713 static void
8714ex_redraw(eap)
8715 exarg_T *eap;
8716{
8717 int r = RedrawingDisabled;
8718 int p = p_lz;
8719
8720 RedrawingDisabled = 0;
8721 p_lz = FALSE;
8722 update_topline();
8723 update_screen(eap->forceit ? CLEAR :
8724#ifdef FEAT_VISUAL
8725 VIsual_active ? INVERTED :
8726#endif
8727 0);
8728#ifdef FEAT_TITLE
8729 if (need_maketitle)
8730 maketitle();
8731#endif
8732 RedrawingDisabled = r;
8733 p_lz = p;
8734
8735 /* Reset msg_didout, so that a message that's there is overwritten. */
8736 msg_didout = FALSE;
8737 msg_col = 0;
8738
8739 out_flush();
8740}
8741
8742/*
8743 * ":redrawstatus": force redraw of status line(s)
8744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 static void
8746ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008747 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749#if defined(FEAT_WINDOWS)
8750 int r = RedrawingDisabled;
8751 int p = p_lz;
8752
8753 RedrawingDisabled = 0;
8754 p_lz = FALSE;
8755 if (eap->forceit)
8756 status_redraw_all();
8757 else
8758 status_redraw_curbuf();
8759 update_screen(
8760# ifdef FEAT_VISUAL
8761 VIsual_active ? INVERTED :
8762# endif
8763 0);
8764 RedrawingDisabled = r;
8765 p_lz = p;
8766 out_flush();
8767#endif
8768}
8769
8770 static void
8771close_redir()
8772{
8773 if (redir_fd != NULL)
8774 {
8775 fclose(redir_fd);
8776 redir_fd = NULL;
8777 }
8778#ifdef FEAT_EVAL
8779 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008780 if (redir_vname)
8781 {
8782 var_redir_stop();
8783 redir_vname = 0;
8784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785#endif
8786}
8787
8788#if defined(FEAT_SESSION) && defined(USE_CRNL)
8789# define MKSESSION_NL
8790static int mksession_nl = FALSE; /* use NL only in put_eol() */
8791#endif
8792
8793/*
8794 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8795 */
8796 static void
8797ex_mkrc(eap)
8798 exarg_T *eap;
8799{
8800 FILE *fd;
8801 int failed = FALSE;
8802 char_u *fname;
8803#ifdef FEAT_BROWSE
8804 char_u *browseFile = NULL;
8805#endif
8806#ifdef FEAT_SESSION
8807 int view_session = FALSE;
8808 int using_vdir = FALSE; /* using 'viewdir'? */
8809 char_u *viewFile = NULL;
8810 unsigned *flagp;
8811#endif
8812
8813 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8814 {
8815#ifdef FEAT_SESSION
8816 view_session = TRUE;
8817#else
8818 ex_ni(eap);
8819 return;
8820#endif
8821 }
8822
8823#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008824 /* Use the short file name until ":lcd" is used. We also don't use the
8825 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008826 did_lcd = FALSE;
8827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8829 if (eap->cmdidx == CMD_mkview
8830 && (*eap->arg == NUL
8831 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8832 {
8833 eap->forceit = TRUE;
8834 fname = get_view_file(*eap->arg);
8835 if (fname == NULL)
8836 return;
8837 viewFile = fname;
8838 using_vdir = TRUE;
8839 }
8840 else
8841#endif
8842 if (*eap->arg != NUL)
8843 fname = eap->arg;
8844 else if (eap->cmdidx == CMD_mkvimrc)
8845 fname = (char_u *)VIMRC_FILE;
8846#ifdef FEAT_SESSION
8847 else if (eap->cmdidx == CMD_mksession)
8848 fname = (char_u *)SESSION_FILE;
8849#endif
8850 else
8851 fname = (char_u *)EXRC_FILE;
8852
8853#ifdef FEAT_BROWSE
8854 if (cmdmod.browse)
8855 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008856 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857# ifdef FEAT_SESSION
8858 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8859 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8860# endif
8861 (char_u *)_("Save Setup"),
8862 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8863 if (browseFile == NULL)
8864 goto theend;
8865 fname = browseFile;
8866 eap->forceit = TRUE; /* since dialog already asked */
8867 }
8868#endif
8869
8870#if defined(FEAT_SESSION) && defined(vim_mkdir)
8871 /* When using 'viewdir' may have to create the directory. */
8872 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008873 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874#endif
8875
8876 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8877 if (fd != NULL)
8878 {
8879#ifdef FEAT_SESSION
8880 if (eap->cmdidx == CMD_mkview)
8881 flagp = &vop_flags;
8882 else
8883 flagp = &ssop_flags;
8884#endif
8885
8886#ifdef MKSESSION_NL
8887 /* "unix" in 'sessionoptions': use NL line separator */
8888 if (view_session && (*flagp & SSOP_UNIX))
8889 mksession_nl = TRUE;
8890#endif
8891
8892 /* Write the version command for :mkvimrc */
8893 if (eap->cmdidx == CMD_mkvimrc)
8894 (void)put_line(fd, "version 6.0");
8895
8896#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008897 if (eap->cmdidx == CMD_mksession)
8898 {
8899 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8900 failed = TRUE;
8901 }
8902
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (eap->cmdidx != CMD_mkview)
8904#endif
8905 {
8906 /* Write setting 'compatible' first, because it has side effects.
8907 * For that same reason only do it when needed. */
8908 if (p_cp)
8909 (void)put_line(fd, "if !&cp | set cp | endif");
8910 else
8911 (void)put_line(fd, "if &cp | set nocp | endif");
8912 }
8913
8914#ifdef FEAT_SESSION
8915 if (!view_session
8916 || (eap->cmdidx == CMD_mksession
8917 && (*flagp & SSOP_OPTIONS)))
8918#endif
8919 failed |= (makemap(fd, NULL) == FAIL
8920 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8921
8922#ifdef FEAT_SESSION
8923 if (!failed && view_session)
8924 {
8925 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8926 failed = TRUE;
8927 if (eap->cmdidx == CMD_mksession)
8928 {
8929 char_u dirnow[MAXPATHL]; /* current directory */
8930
8931 /*
8932 * Change to session file's dir.
8933 */
8934 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8935 || mch_chdir((char *)dirnow) != 0)
8936 *dirnow = NUL;
8937 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8938 {
8939 if (vim_chdirfile(fname) == OK)
8940 shorten_fnames(TRUE);
8941 }
8942 else if (*dirnow != NUL
8943 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8944 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008945 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008946 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
8948
8949 failed |= (makeopens(fd, dirnow) == FAIL);
8950
8951 /* restore original dir */
8952 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8953 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8954 {
8955 if (mch_chdir((char *)dirnow) != 0)
8956 EMSG(_(e_prev_dir));
8957 shorten_fnames(TRUE);
8958 }
8959 }
8960 else
8961 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008962 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8963 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8966 == FAIL)
8967 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008968 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8969 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008970 if (eap->cmdidx == CMD_mksession)
8971 {
8972 if (put_line(fd, "unlet SessionLoad") == FAIL)
8973 failed = TRUE;
8974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 }
8976#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008977 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8978 failed = TRUE;
8979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 failed |= fclose(fd);
8981
8982 if (failed)
8983 EMSG(_(e_write));
8984#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8985 else if (eap->cmdidx == CMD_mksession)
8986 {
8987 /* successful session write - set this_session var */
8988 char_u tbuf[MAXPATHL];
8989
8990 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8991 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8992 }
8993#endif
8994#ifdef MKSESSION_NL
8995 mksession_nl = FALSE;
8996#endif
8997 }
8998
8999#ifdef FEAT_BROWSE
9000theend:
9001 vim_free(browseFile);
9002#endif
9003#ifdef FEAT_SESSION
9004 vim_free(viewFile);
9005#endif
9006}
9007
Bram Moolenaardf177f62005-02-22 08:39:57 +00009008#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9009 || defined(PROTO)
9010 int
9011vim_mkdir_emsg(name, prot)
9012 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009013 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009014{
9015 if (vim_mkdir(name, prot) != 0)
9016 {
9017 EMSG2(_("E739: Cannot create directory: %s"), name);
9018 return FAIL;
9019 }
9020 return OK;
9021}
9022#endif
9023
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024/*
9025 * Open a file for writing for an Ex command, with some checks.
9026 * Return file descriptor, or NULL on failure.
9027 */
9028 FILE *
9029open_exfile(fname, forceit, mode)
9030 char_u *fname;
9031 int forceit;
9032 char *mode; /* "w" for create new file or "a" for append */
9033{
9034 FILE *fd;
9035
9036#ifdef UNIX
9037 /* with Unix it is possible to open a directory */
9038 if (mch_isdir(fname))
9039 {
9040 EMSG2(_(e_isadir2), fname);
9041 return NULL;
9042 }
9043#endif
9044 if (!forceit && *mode != 'a' && vim_fexists(fname))
9045 {
9046 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9047 return NULL;
9048 }
9049
9050 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9051 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9052
9053 return fd;
9054}
9055
9056/*
9057 * ":mark" and ":k".
9058 */
9059 static void
9060ex_mark(eap)
9061 exarg_T *eap;
9062{
9063 pos_T pos;
9064
9065 if (*eap->arg == NUL) /* No argument? */
9066 EMSG(_(e_argreq));
9067 else if (eap->arg[1] != NUL) /* more than one character? */
9068 EMSG(_(e_trailing));
9069 else
9070 {
9071 pos = curwin->w_cursor; /* save curwin->w_cursor */
9072 curwin->w_cursor.lnum = eap->line2;
9073 beginline(BL_WHITE | BL_FIX);
9074 if (setmark(*eap->arg) == FAIL) /* set mark */
9075 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9076 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9077 }
9078}
9079
9080/*
9081 * Update w_topline, w_leftcol and the cursor position.
9082 */
9083 void
9084update_topline_cursor()
9085{
9086 check_cursor(); /* put cursor on valid line */
9087 update_topline();
9088 if (!curwin->w_p_wrap)
9089 validate_cursor();
9090 update_curswant();
9091}
9092
9093#ifdef FEAT_EX_EXTRA
9094/*
9095 * ":normal[!] {commands}": Execute normal mode commands.
9096 */
9097 static void
9098ex_normal(eap)
9099 exarg_T *eap;
9100{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 int save_msg_scroll = msg_scroll;
9102 int save_restart_edit = restart_edit;
9103 int save_msg_didout = msg_didout;
9104 int save_State = State;
9105 tasave_T tabuf;
9106 int save_insertmode = p_im;
9107 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009108 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109#ifdef FEAT_MBYTE
9110 char_u *arg = NULL;
9111 int l;
9112 char_u *p;
9113#endif
9114
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009115 if (ex_normal_lock > 0)
9116 {
9117 EMSG(_(e_secure));
9118 return;
9119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (ex_normal_busy >= p_mmd)
9121 {
9122 EMSG(_("E192: Recursive use of :normal too deep"));
9123 return;
9124 }
9125 ++ex_normal_busy;
9126
9127 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9128 restart_edit = 0; /* don't go to Insert mode */
9129 p_im = FALSE; /* don't use 'insertmode' */
9130
9131#ifdef FEAT_MBYTE
9132 /*
9133 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9134 * this for the K_SPECIAL leading byte, otherwise special keys will not
9135 * work.
9136 */
9137 if (has_mbyte)
9138 {
9139 int len = 0;
9140
9141 /* Count the number of characters to be escaped. */
9142 for (p = eap->arg; *p != NUL; ++p)
9143 {
9144# ifdef FEAT_GUI
9145 if (*p == CSI) /* leadbyte CSI */
9146 len += 2;
9147# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009148 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9150# ifdef FEAT_GUI
9151 || *p == CSI
9152# endif
9153 )
9154 len += 2;
9155 }
9156 if (len > 0)
9157 {
9158 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9159 if (arg != NULL)
9160 {
9161 len = 0;
9162 for (p = eap->arg; *p != NUL; ++p)
9163 {
9164 arg[len++] = *p;
9165# ifdef FEAT_GUI
9166 if (*p == CSI)
9167 {
9168 arg[len++] = KS_EXTRA;
9169 arg[len++] = (int)KE_CSI;
9170 }
9171# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009172 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 {
9174 arg[len++] = *++p;
9175 if (*p == K_SPECIAL)
9176 {
9177 arg[len++] = KS_SPECIAL;
9178 arg[len++] = KE_FILLER;
9179 }
9180# ifdef FEAT_GUI
9181 else if (*p == CSI)
9182 {
9183 arg[len++] = KS_EXTRA;
9184 arg[len++] = (int)KE_CSI;
9185 }
9186# endif
9187 }
9188 arg[len] = NUL;
9189 }
9190 }
9191 }
9192 }
9193#endif
9194
9195 /*
9196 * Save the current typeahead. This is required to allow using ":normal"
9197 * from an event handler and makes sure we don't hang when the argument
9198 * ends with half a command.
9199 */
9200 save_typeahead(&tabuf);
9201 if (tabuf.typebuf_valid)
9202 {
9203 /*
9204 * Repeat the :normal command for each line in the range. When no
9205 * range given, execute it just once, without positioning the cursor
9206 * first.
9207 */
9208 do
9209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 if (eap->addr_count != 0)
9211 {
9212 curwin->w_cursor.lnum = eap->line1++;
9213 curwin->w_cursor.col = 0;
9214 }
9215
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009216 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217#ifdef FEAT_MBYTE
9218 arg != NULL ? arg :
9219#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009220 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 }
9222 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9223 }
9224
9225 /* Might not return to the main loop when in an event handler. */
9226 update_topline_cursor();
9227
9228 /* Restore the previous typeahead. */
9229 restore_typeahead(&tabuf);
9230
9231 --ex_normal_busy;
9232 msg_scroll = save_msg_scroll;
9233 restart_edit = save_restart_edit;
9234 p_im = save_insertmode;
9235 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009236 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9238
9239 /* Restore the state (needed when called from a function executed for
9240 * 'indentexpr'). */
9241 State = save_State;
9242#ifdef FEAT_MBYTE
9243 vim_free(arg);
9244#endif
9245}
9246
9247/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009248 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 */
9250 static void
9251ex_startinsert(eap)
9252 exarg_T *eap;
9253{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009254 if (eap->forceit)
9255 {
9256 coladvance((colnr_T)MAXCOL);
9257 curwin->w_curswant = MAXCOL;
9258 curwin->w_set_curswant = FALSE;
9259 }
9260
Bram Moolenaara40c5002005-01-09 21:16:21 +00009261 /* Ignore the command when already in Insert mode. Inserting an
9262 * expression register that invokes a function can do this. */
9263 if (State & INSERT)
9264 return;
9265
Bram Moolenaar64969662005-12-14 21:59:55 +00009266 if (eap->cmdidx == CMD_startinsert)
9267 restart_edit = 'a';
9268 else if (eap->cmdidx == CMD_startreplace)
9269 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009271 restart_edit = 'V';
9272
9273 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009275 if (eap->cmdidx == CMD_startinsert)
9276 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 curwin->w_curswant = 0; /* avoid MAXCOL */
9278 }
9279}
9280
9281/*
9282 * ":stopinsert"
9283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 static void
9285ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009286 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 restart_edit = 0;
9289 stop_insert_mode = TRUE;
9290}
9291#endif
9292
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009293#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9294/*
9295 * Execute normal mode command "cmd".
9296 * "remap" can be REMAP_NONE or REMAP_YES.
9297 */
9298 void
9299exec_normal_cmd(cmd, remap, silent)
9300 char_u *cmd;
9301 int remap;
9302 int silent;
9303{
9304 oparg_T oa;
9305
9306 /*
9307 * Stuff the argument into the typeahead buffer.
9308 * Execute normal_cmd() until there is no typeahead left.
9309 */
9310 clear_oparg(&oa);
9311 finish_op = FALSE;
9312 ins_typebuf(cmd, remap, 0, TRUE, silent);
9313 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9314 && !got_int)
9315 {
9316 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009317 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009318 }
9319}
9320#endif
9321
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322#ifdef FEAT_FIND_ID
9323 static void
9324ex_checkpath(eap)
9325 exarg_T *eap;
9326{
9327 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9328 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9329 (linenr_T)1, (linenr_T)MAXLNUM);
9330}
9331
9332#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9333/*
9334 * ":psearch"
9335 */
9336 static void
9337ex_psearch(eap)
9338 exarg_T *eap;
9339{
9340 g_do_tagpreview = p_pvh;
9341 ex_findpat(eap);
9342 g_do_tagpreview = 0;
9343}
9344#endif
9345
9346 static void
9347ex_findpat(eap)
9348 exarg_T *eap;
9349{
9350 int whole = TRUE;
9351 long n;
9352 char_u *p;
9353 int action;
9354
9355 switch (cmdnames[eap->cmdidx].cmd_name[2])
9356 {
9357 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9358 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9359 action = ACTION_GOTO;
9360 else
9361 action = ACTION_SHOW;
9362 break;
9363 case 'i': /* ":ilist" and ":dlist" */
9364 action = ACTION_SHOW_ALL;
9365 break;
9366 case 'u': /* ":ijump" and ":djump" */
9367 action = ACTION_GOTO;
9368 break;
9369 default: /* ":isplit" and ":dsplit" */
9370 action = ACTION_SPLIT;
9371 break;
9372 }
9373
9374 n = 1;
9375 if (vim_isdigit(*eap->arg)) /* get count */
9376 {
9377 n = getdigits(&eap->arg);
9378 eap->arg = skipwhite(eap->arg);
9379 }
9380 if (*eap->arg == '/') /* Match regexp, not just whole words */
9381 {
9382 whole = FALSE;
9383 ++eap->arg;
9384 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9385 if (*p)
9386 {
9387 *p++ = NUL;
9388 p = skipwhite(p);
9389
9390 /* Check for trailing illegal characters */
9391 if (!ends_excmd(*p))
9392 eap->errmsg = e_trailing;
9393 else
9394 eap->nextcmd = check_nextcmd(p);
9395 }
9396 }
9397 if (!eap->skip)
9398 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9399 whole, !eap->forceit,
9400 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9401 n, action, eap->line1, eap->line2);
9402}
9403#endif
9404
9405#ifdef FEAT_WINDOWS
9406
9407# ifdef FEAT_QUICKFIX
9408/*
9409 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9410 */
9411 static void
9412ex_ptag(eap)
9413 exarg_T *eap;
9414{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009415 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9417}
9418
9419/*
9420 * ":pedit"
9421 */
9422 static void
9423ex_pedit(eap)
9424 exarg_T *eap;
9425{
9426 win_T *curwin_save = curwin;
9427
9428 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009429 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 keep_help_flag = curwin_save->w_buffer->b_help;
9431 do_exedit(eap, NULL);
9432 keep_help_flag = FALSE;
9433 if (curwin != curwin_save && win_valid(curwin_save))
9434 {
9435 /* Return cursor to where we were */
9436 validate_cursor();
9437 redraw_later(VALID);
9438 win_enter(curwin_save, TRUE);
9439 }
9440 g_do_tagpreview = 0;
9441}
9442# endif
9443
9444/*
9445 * ":stag", ":stselect" and ":stjump".
9446 */
9447 static void
9448ex_stag(eap)
9449 exarg_T *eap;
9450{
9451 postponed_split = -1;
9452 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009453 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9455 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009456 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457}
9458#endif
9459
9460/*
9461 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9462 */
9463 static void
9464ex_tag(eap)
9465 exarg_T *eap;
9466{
9467 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9468}
9469
9470 static void
9471ex_tag_cmd(eap, name)
9472 exarg_T *eap;
9473 char_u *name;
9474{
9475 int cmd;
9476
9477 switch (name[1])
9478 {
9479 case 'j': cmd = DT_JUMP; /* ":tjump" */
9480 break;
9481 case 's': cmd = DT_SELECT; /* ":tselect" */
9482 break;
9483 case 'p': cmd = DT_PREV; /* ":tprevious" */
9484 break;
9485 case 'N': cmd = DT_PREV; /* ":tNext" */
9486 break;
9487 case 'n': cmd = DT_NEXT; /* ":tnext" */
9488 break;
9489 case 'o': cmd = DT_POP; /* ":pop" */
9490 break;
9491 case 'f': /* ":tfirst" */
9492 case 'r': cmd = DT_FIRST; /* ":trewind" */
9493 break;
9494 case 'l': cmd = DT_LAST; /* ":tlast" */
9495 break;
9496 default: /* ":tag" */
9497#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009498 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 {
9500 do_cstag(eap);
9501 return;
9502 }
9503#endif
9504 cmd = DT_TAG;
9505 break;
9506 }
9507
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009508 if (name[0] == 'l')
9509 {
9510#ifndef FEAT_QUICKFIX
9511 ex_ni(eap);
9512 return;
9513#else
9514 cmd = DT_LTAG;
9515#endif
9516 }
9517
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9519 eap->forceit, TRUE);
9520}
9521
9522/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009523 * Check "str" for starting with a special cmdline variable.
9524 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9525 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9526 */
9527 int
9528find_cmdline_var(src, usedlen)
9529 char_u *src;
9530 int *usedlen;
9531{
9532 int len;
9533 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009534 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009535 "%",
9536#define SPEC_PERC 0
9537 "#",
9538#define SPEC_HASH 1
9539 "<cword>", /* cursor word */
9540#define SPEC_CWORD 2
9541 "<cWORD>", /* cursor WORD */
9542#define SPEC_CCWORD 3
9543 "<cfile>", /* cursor path name */
9544#define SPEC_CFILE 4
9545 "<sfile>", /* ":so" file name */
9546#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009547 "<slnum>", /* ":so" file line number */
9548#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009549#ifdef FEAT_AUTOCMD
9550 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009551# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009552 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009553# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009554 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009555# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009556#endif
9557#ifdef FEAT_CLIENTSERVER
9558 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009559# ifdef FEAT_AUTOCMD
9560# define SPEC_CLIENT 10
9561# else
9562# define SPEC_CLIENT 7
9563# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009564#endif
9565 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009566
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009567 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009568 {
9569 len = (int)STRLEN(spec_str[i]);
9570 if (STRNCMP(src, spec_str[i], len) == 0)
9571 {
9572 *usedlen = len;
9573 return i;
9574 }
9575 }
9576 return -1;
9577}
9578
9579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 * Evaluate cmdline variables.
9581 *
9582 * change '%' to curbuf->b_ffname
9583 * '#' to curwin->w_altfile
9584 * '<cword>' to word under the cursor
9585 * '<cWORD>' to WORD under the cursor
9586 * '<cfile>' to path name under the cursor
9587 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009588 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 * '<afile>' to file name for autocommand
9590 * '<abuf>' to buffer number for autocommand
9591 * '<amatch>' to matching name for autocommand
9592 *
9593 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9594 * "" for error without a message) and NULL is returned.
9595 * Returns an allocated string if a valid match was found.
9596 * Returns NULL if no match was found. "usedlen" then still contains the
9597 * number of characters to skip.
9598 */
9599 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009600eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009602 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 int *usedlen; /* characters after src that are used */
9604 linenr_T *lnump; /* line number for :e command, or NULL */
9605 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009606 int *escaped; /* return value has escaped white space (can
9607 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 int i;
9610 char_u *s;
9611 char_u *result;
9612 char_u *resultbuf = NULL;
9613 int resultlen;
9614 buf_T *buf;
9615 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9616 int spec_idx;
9617#ifdef FEAT_MODIFY_FNAME
9618 int skip_mod = FALSE;
9619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
9622 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009623 if (escaped != NULL)
9624 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
9626 /*
9627 * Check if there is something to do.
9628 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009629 spec_idx = find_cmdline_var(src, usedlen);
9630 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 {
9632 *usedlen = 1;
9633 return NULL;
9634 }
9635
9636 /*
9637 * Skip when preceded with a backslash "\%" and "\#".
9638 * Note: In "\\%" the % is also not recognized!
9639 */
9640 if (src > srcstart && src[-1] == '\\')
9641 {
9642 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009643 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 return NULL;
9645 }
9646
9647 /*
9648 * word or WORD under cursor
9649 */
9650 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9651 {
9652 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9653 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9654 if (resultlen == 0)
9655 {
9656 *errormsg = (char_u *)"";
9657 return NULL;
9658 }
9659 }
9660
9661 /*
9662 * '#': Alternate file name
9663 * '%': Current file name
9664 * File name under the cursor
9665 * File name for autocommand
9666 * and following modifiers
9667 */
9668 else
9669 {
9670 switch (spec_idx)
9671 {
9672 case SPEC_PERC: /* '%': current file */
9673 if (curbuf->b_fname == NULL)
9674 {
9675 result = (char_u *)"";
9676 valid = 0; /* Must have ":p:h" to be valid */
9677 }
9678 else
9679#ifdef RISCOS
9680 /* Always use the full path for RISC OS if possible. */
9681 result = curbuf->b_ffname;
9682 if (result == NULL)
9683 result = curbuf->b_fname;
9684#else
9685 result = curbuf->b_fname;
9686#endif
9687 break;
9688
9689 case SPEC_HASH: /* '#' or "#99": alternate file */
9690 if (src[1] == '#') /* "##": the argument list */
9691 {
9692 result = arg_all();
9693 resultbuf = result;
9694 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009695 if (escaped != NULL)
9696 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697#ifdef FEAT_MODIFY_FNAME
9698 skip_mod = TRUE;
9699#endif
9700 break;
9701 }
9702 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009703 if (*s == '<') /* "#<99" uses v:oldfiles */
9704 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 i = (int)getdigits(&s);
9706 *usedlen = (int)(s - src); /* length of what we expand */
9707
Bram Moolenaard812df62008-11-09 12:46:09 +00009708 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009710 if (*usedlen < 2)
9711 {
9712 /* Should we give an error message for #<text? */
9713 *usedlen = 1;
9714 return NULL;
9715 }
9716#ifdef FEAT_EVAL
9717 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9718 (long)i);
9719 if (result == NULL)
9720 {
9721 *errormsg = (char_u *)"";
9722 return NULL;
9723 }
9724#else
9725 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 }
9729 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009730 {
9731 buf = buflist_findnr(i);
9732 if (buf == NULL)
9733 {
9734 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9735 return NULL;
9736 }
9737 if (lnump != NULL)
9738 *lnump = ECMD_LAST;
9739 if (buf->b_fname == NULL)
9740 {
9741 result = (char_u *)"";
9742 valid = 0; /* Must have ":p:h" to be valid */
9743 }
9744 else
9745 result = buf->b_fname;
9746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 break;
9748
9749#ifdef FEAT_SEARCHPATH
9750 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009751 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (result == NULL)
9753 {
9754 *errormsg = (char_u *)"";
9755 return NULL;
9756 }
9757 resultbuf = result; /* remember allocated string */
9758 break;
9759#endif
9760
9761#ifdef FEAT_AUTOCMD
9762 case SPEC_AFILE: /* file name for autocommand */
9763 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009764 if (result != NULL && !autocmd_fname_full)
9765 {
9766 /* Still need to turn the fname into a full path. It is
9767 * postponed to avoid a delay when <afile> is not used. */
9768 autocmd_fname_full = TRUE;
9769 result = FullName_save(autocmd_fname, FALSE);
9770 vim_free(autocmd_fname);
9771 autocmd_fname = result;
9772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (result == NULL)
9774 {
9775 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9776 return NULL;
9777 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009778 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 break;
9780
9781 case SPEC_ABUF: /* buffer number for autocommand */
9782 if (autocmd_bufnr <= 0)
9783 {
9784 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9785 return NULL;
9786 }
9787 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9788 result = strbuf;
9789 break;
9790
9791 case SPEC_AMATCH: /* match name for autocommand */
9792 result = autocmd_match;
9793 if (result == NULL)
9794 {
9795 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9796 return NULL;
9797 }
9798 break;
9799
9800#endif
9801 case SPEC_SFILE: /* file name for ":so" command */
9802 result = sourcing_name;
9803 if (result == NULL)
9804 {
9805 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9806 return NULL;
9807 }
9808 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009809 case SPEC_SLNUM: /* line in file for ":so" command */
9810 if (sourcing_name == NULL || sourcing_lnum == 0)
9811 {
9812 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9813 return NULL;
9814 }
9815 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9816 result = strbuf;
9817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#if defined(FEAT_CLIENTSERVER)
9819 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009820 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9821 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 result = strbuf;
9823 break;
9824#endif
9825 }
9826
9827 resultlen = (int)STRLEN(result); /* length of new string */
9828 if (src[*usedlen] == '<') /* remove the file name extension */
9829 {
9830 ++*usedlen;
9831#ifdef RISCOS
9832 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9833#else
9834 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9835#endif
9836 resultlen = (int)(s - result);
9837 }
9838#ifdef FEAT_MODIFY_FNAME
9839 else if (!skip_mod)
9840 {
9841 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9842 &resultlen);
9843 if (result == NULL)
9844 {
9845 *errormsg = (char_u *)"";
9846 return NULL;
9847 }
9848 }
9849#endif
9850 }
9851
9852 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9853 {
9854 if (valid != VALID_HEAD + VALID_PATH)
9855 /* xgettext:no-c-format */
9856 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9857 else
9858 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9859 result = NULL;
9860 }
9861 else
9862 result = vim_strnsave(result, resultlen);
9863 vim_free(resultbuf);
9864 return result;
9865}
9866
9867/*
9868 * Concatenate all files in the argument list, separated by spaces, and return
9869 * it in one allocated string.
9870 * Spaces and backslashes in the file names are escaped with a backslash.
9871 * Returns NULL when out of memory.
9872 */
9873 static char_u *
9874arg_all()
9875{
9876 int len;
9877 int idx;
9878 char_u *retval = NULL;
9879 char_u *p;
9880
9881 /*
9882 * Do this loop two times:
9883 * first time: compute the total length
9884 * second time: concatenate the names
9885 */
9886 for (;;)
9887 {
9888 len = 0;
9889 for (idx = 0; idx < ARGCOUNT; ++idx)
9890 {
9891 p = alist_name(&ARGLIST[idx]);
9892 if (p != NULL)
9893 {
9894 if (len > 0)
9895 {
9896 /* insert a space in between names */
9897 if (retval != NULL)
9898 retval[len] = ' ';
9899 ++len;
9900 }
9901 for ( ; *p != NUL; ++p)
9902 {
9903 if (*p == ' ' || *p == '\\')
9904 {
9905 /* insert a backslash */
9906 if (retval != NULL)
9907 retval[len] = '\\';
9908 ++len;
9909 }
9910 if (retval != NULL)
9911 retval[len] = *p;
9912 ++len;
9913 }
9914 }
9915 }
9916
9917 /* second time: break here */
9918 if (retval != NULL)
9919 {
9920 retval[len] = NUL;
9921 break;
9922 }
9923
9924 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009925 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (retval == NULL)
9927 break;
9928 }
9929
9930 return retval;
9931}
9932
9933#if defined(FEAT_AUTOCMD) || defined(PROTO)
9934/*
9935 * Expand the <sfile> string in "arg".
9936 *
9937 * Returns an allocated string, or NULL for any error.
9938 */
9939 char_u *
9940expand_sfile(arg)
9941 char_u *arg;
9942{
9943 char_u *errormsg;
9944 int len;
9945 char_u *result;
9946 char_u *newres;
9947 char_u *repl;
9948 int srclen;
9949 char_u *p;
9950
9951 result = vim_strsave(arg);
9952 if (result == NULL)
9953 return NULL;
9954
9955 for (p = result; *p; )
9956 {
9957 if (STRNCMP(p, "<sfile>", 7) != 0)
9958 ++p;
9959 else
9960 {
9961 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009962 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 if (errormsg != NULL)
9964 {
9965 if (*errormsg)
9966 emsg(errormsg);
9967 vim_free(result);
9968 return NULL;
9969 }
9970 if (repl == NULL) /* no match (cannot happen) */
9971 {
9972 p += srclen;
9973 continue;
9974 }
9975 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9976 newres = alloc(len);
9977 if (newres == NULL)
9978 {
9979 vim_free(repl);
9980 vim_free(result);
9981 return NULL;
9982 }
9983 mch_memmove(newres, result, (size_t)(p - result));
9984 STRCPY(newres + (p - result), repl);
9985 len = (int)STRLEN(newres);
9986 STRCAT(newres, p + srclen);
9987 vim_free(repl);
9988 vim_free(result);
9989 result = newres;
9990 p = newres + len; /* continue after the match */
9991 }
9992 }
9993
9994 return result;
9995}
9996#endif
9997
9998#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009999static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10000 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10002static frame_T *ses_skipframe __ARGS((frame_T *fr));
10003static int ses_do_frame __ARGS((frame_T *fr));
10004static int ses_do_win __ARGS((win_T *wp));
10005static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10006static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10007static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10008
10009/*
10010 * Write openfile commands for the current buffers to an .exrc file.
10011 * Return FAIL on error, OK otherwise.
10012 */
10013 static int
10014makeopens(fd, dirnow)
10015 FILE *fd;
10016 char_u *dirnow; /* Current directory name */
10017{
10018 buf_T *buf;
10019 int only_save_windows = TRUE;
10020 int nr;
10021 int cnr = 1;
10022 int restore_size = TRUE;
10023 win_T *wp;
10024 char_u *sname;
10025 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010026 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010027 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010028 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010029 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010030 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
10032 if (ssop_flags & SSOP_BUFFERS)
10033 only_save_windows = FALSE; /* Save ALL buffers */
10034
10035 /*
10036 * Begin by setting the this_session variable, and then other
10037 * sessionable variables.
10038 */
10039#ifdef FEAT_EVAL
10040 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10041 return FAIL;
10042 if (ssop_flags & SSOP_GLOBALS)
10043 if (store_session_globals(fd) == FAIL)
10044 return FAIL;
10045#endif
10046
10047 /*
10048 * Close all windows but one.
10049 */
10050 if (put_line(fd, "silent only") == FAIL)
10051 return FAIL;
10052
10053 /*
10054 * Now a :cd command to the session directory or the current directory
10055 */
10056 if (ssop_flags & SSOP_SESDIR)
10057 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010058 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10059 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 return FAIL;
10061 }
10062 else if (ssop_flags & SSOP_CURDIR)
10063 {
10064 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10065 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010066 || fputs("cd ", fd) < 0
10067 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10068 || put_eol(fd) == FAIL)
10069 {
10070 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 vim_free(sname);
10074 }
10075
10076 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010077 * If there is an empty, unnamed buffer we will wipe it out later.
10078 * Remember the buffer number.
10079 */
10080 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10081 return FAIL;
10082 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10083 return FAIL;
10084 if (put_line(fd, "endif") == FAIL)
10085 return FAIL;
10086
10087 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 * Now save the current files, current buffer first.
10089 */
10090 if (put_line(fd, "set shortmess=aoO") == FAIL)
10091 return FAIL;
10092
10093 /* Now put the other buffers into the buffer list */
10094 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10095 {
10096 if (!(only_save_windows && buf->b_nwindows == 0)
10097 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10098 && buf->b_fname != NULL
10099 && buf->b_p_bl)
10100 {
10101 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10102 : buf->b_wininfo->wi_fpos.lnum) < 0
10103 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10104 return FAIL;
10105 }
10106 }
10107
10108 /* the global argument list */
10109 if (ses_arglist(fd, "args", &global_alist.al_ga,
10110 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10111 return FAIL;
10112
10113 if (ssop_flags & SSOP_RESIZE)
10114 {
10115 /* Note: after the restore we still check it worked!*/
10116 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10117 || put_eol(fd) == FAIL)
10118 return FAIL;
10119 }
10120
10121#ifdef FEAT_GUI
10122 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10123 {
10124 int x, y;
10125
10126 if (gui_mch_get_winpos(&x, &y) == OK)
10127 {
10128 /* Note: after the restore we still check it worked!*/
10129 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10130 return FAIL;
10131 }
10132 }
10133#endif
10134
10135 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010136 * May repeat putting Windows for each tab, when "tabpages" is in
10137 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010138 * Don't use goto_tabpage(), it may change directory and trigger
10139 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010141 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010142 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010143 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010145 int need_tabnew = FALSE;
10146
Bram Moolenaar18144c82006-04-12 21:52:12 +000010147 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010149 tabpage_T *tp = find_tabpage(tabnr);
10150
10151 if (tp == NULL)
10152 break; /* done all tab pages */
10153 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010154 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010155 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010156 tab_topframe = topframe;
10157 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010158 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010159 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010160 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010161 tab_topframe = tp->tp_topframe;
10162 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010163 if (tabnr > 1)
10164 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166
Bram Moolenaar18144c82006-04-12 21:52:12 +000010167 /*
10168 * Before creating the window layout, try loading one file. If this
10169 * is aborted we don't end up with a number of useless windows.
10170 * This may have side effects! (e.g., compressed or network file).
10171 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010172 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010173 {
10174 if (ses_do_win(wp)
10175 && wp->w_buffer->b_ffname != NULL
10176 && !wp->w_buffer->b_help
10177#ifdef FEAT_QUICKFIX
10178 && !bt_nofile(wp->w_buffer)
10179#endif
10180 )
10181 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010182 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010183 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10184 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010185 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010186 if (!wp->w_arg_idx_invalid)
10187 edited_win = wp;
10188 break;
10189 }
10190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010192 /* If no file got edited create an empty tab page. */
10193 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10194 return FAIL;
10195
Bram Moolenaar18144c82006-04-12 21:52:12 +000010196 /*
10197 * Save current window layout.
10198 */
10199 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010201 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010203 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10204 return FAIL;
10205 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207
Bram Moolenaar18144c82006-04-12 21:52:12 +000010208 /*
10209 * Check if window sizes can be restored (no windows omitted).
10210 * Remember the window number of the current window after restoring.
10211 */
10212 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010213 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010214 {
10215 if (ses_do_win(wp))
10216 ++nr;
10217 else
10218 restore_size = FALSE;
10219 if (curwin == wp)
10220 cnr = nr;
10221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
Bram Moolenaar18144c82006-04-12 21:52:12 +000010223 /* Go to the first window. */
10224 if (put_line(fd, "wincmd t") == FAIL)
10225 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010226
Bram Moolenaar18144c82006-04-12 21:52:12 +000010227 /*
10228 * If more than one window, see if sizes can be restored.
10229 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10230 * resized when moving between windows.
10231 * Do this before restoring the view, so that the topline and the
10232 * cursor can be set. This is done again below.
10233 */
10234 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10235 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010236 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
Bram Moolenaar18144c82006-04-12 21:52:12 +000010239 /*
10240 * Restore the view of the window (options, file, cursor, etc.).
10241 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010242 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010243 {
10244 if (!ses_do_win(wp))
10245 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010246 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10247 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010248 return FAIL;
10249 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10250 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010251 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010252 }
10253
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010254 /* The argument index in the first tab page is zero, need to set it in
10255 * each window. For further tab pages it's the window where we do
10256 * "tabedit". */
10257 cur_arg_idx = next_arg_idx;
10258
Bram Moolenaar18144c82006-04-12 21:52:12 +000010259 /*
10260 * Restore cursor to the current window if it's not the first one.
10261 */
10262 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10263 || put_eol(fd) == FAIL))
10264 return FAIL;
10265
10266 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010267 * Restore window sizes again after jumping around in windows, because
10268 * the current window has a minimum size while others may not.
10269 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010270 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010271 return FAIL;
10272
Bram Moolenaar18144c82006-04-12 21:52:12 +000010273 /* Don't continue in another tab page when doing only the current one
10274 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010275 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010276 break;
10277 }
10278
10279 if (ssop_flags & SSOP_TABPAGES)
10280 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010281 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10282 || put_eol(fd) == FAIL)
10283 return FAIL;
10284 }
10285
Bram Moolenaar9c102382006-05-03 21:26:49 +000010286 /*
10287 * Wipe out an empty unnamed buffer we started in.
10288 */
10289 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10290 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010291 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010292 return FAIL;
10293 if (put_line(fd, "endif") == FAIL)
10294 return FAIL;
10295 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10296 return FAIL;
10297
10298 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10299 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10300 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
10303 /*
10304 * Lastly, execute the x.vim file if it exists.
10305 */
10306 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10307 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010308 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 || put_line(fd, "endif") == FAIL)
10310 return FAIL;
10311
10312 return OK;
10313}
10314
10315 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010316ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 FILE *fd;
10318 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010319 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320{
10321 int n = 0;
10322 win_T *wp;
10323
10324 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10325 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010326 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 {
10328 if (!ses_do_win(wp))
10329 continue;
10330 ++n;
10331
10332 /* restore height when not full height */
10333 if (wp->w_height + wp->w_status_height < topframe->fr_height
10334 && (fprintf(fd,
10335 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10336 n, (long)wp->w_height, Rows / 2, Rows) < 0
10337 || put_eol(fd) == FAIL))
10338 return FAIL;
10339
10340 /* restore width when not full width */
10341 if (wp->w_width < Columns && (fprintf(fd,
10342 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10343 n, (long)wp->w_width, Columns / 2, Columns) < 0
10344 || put_eol(fd) == FAIL))
10345 return FAIL;
10346 }
10347 }
10348 else
10349 {
10350 /* Just equalise window sizes */
10351 if (put_line(fd, "wincmd =") == FAIL)
10352 return FAIL;
10353 }
10354 return OK;
10355}
10356
10357/*
10358 * Write commands to "fd" to recursively create windows for frame "fr",
10359 * horizontally and vertically split.
10360 * After the commands the last window in the frame is the current window.
10361 * Returns FAIL when writing the commands to "fd" fails.
10362 */
10363 static int
10364ses_win_rec(fd, fr)
10365 FILE *fd;
10366 frame_T *fr;
10367{
10368 frame_T *frc;
10369 int count = 0;
10370
10371 if (fr->fr_layout != FR_LEAF)
10372 {
10373 /* Find first frame that's not skipped and then create a window for
10374 * each following one (first frame is already there). */
10375 frc = ses_skipframe(fr->fr_child);
10376 if (frc != NULL)
10377 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10378 {
10379 /* Make window as big as possible so that we have lots of room
10380 * to split. */
10381 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10382 || put_line(fd, fr->fr_layout == FR_COL
10383 ? "split" : "vsplit") == FAIL)
10384 return FAIL;
10385 ++count;
10386 }
10387
10388 /* Go back to the first window. */
10389 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10390 ? "%dwincmd k" : "%dwincmd h", count) < 0
10391 || put_eol(fd) == FAIL))
10392 return FAIL;
10393
10394 /* Recursively create frames/windows in each window of this column or
10395 * row. */
10396 frc = ses_skipframe(fr->fr_child);
10397 while (frc != NULL)
10398 {
10399 ses_win_rec(fd, frc);
10400 frc = ses_skipframe(frc->fr_next);
10401 /* Go to next window. */
10402 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10403 return FAIL;
10404 }
10405 }
10406 return OK;
10407}
10408
10409/*
10410 * Skip frames that don't contain windows we want to save in the Session.
10411 * Returns NULL when there none.
10412 */
10413 static frame_T *
10414ses_skipframe(fr)
10415 frame_T *fr;
10416{
10417 frame_T *frc;
10418
10419 for (frc = fr; frc != NULL; frc = frc->fr_next)
10420 if (ses_do_frame(frc))
10421 break;
10422 return frc;
10423}
10424
10425/*
10426 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10427 * the Session.
10428 */
10429 static int
10430ses_do_frame(fr)
10431 frame_T *fr;
10432{
10433 frame_T *frc;
10434
10435 if (fr->fr_layout == FR_LEAF)
10436 return ses_do_win(fr->fr_win);
10437 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10438 if (ses_do_frame(frc))
10439 return TRUE;
10440 return FALSE;
10441}
10442
10443/*
10444 * Return non-zero if window "wp" is to be stored in the Session.
10445 */
10446 static int
10447ses_do_win(wp)
10448 win_T *wp;
10449{
10450 if (wp->w_buffer->b_fname == NULL
10451#ifdef FEAT_QUICKFIX
10452 /* When 'buftype' is "nofile" can't restore the window contents. */
10453 || bt_nofile(wp->w_buffer)
10454#endif
10455 )
10456 return (ssop_flags & SSOP_BLANK);
10457 if (wp->w_buffer->b_help)
10458 return (ssop_flags & SSOP_HELP);
10459 return TRUE;
10460}
10461
10462/*
10463 * Write commands to "fd" to restore the view of a window.
10464 * Caller must make sure 'scrolloff' is zero.
10465 */
10466 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010467put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 FILE *fd;
10469 win_T *wp;
10470 int add_edit; /* add ":edit" command to view */
10471 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010472 int current_arg_idx; /* current argument index of the window, use
10473 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474{
10475 win_T *save_curwin;
10476 int f;
10477 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010478 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479
10480 /* Always restore cursor position for ":mksession". For ":mkview" only
10481 * when 'viewoptions' contains "cursor". */
10482 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10483
10484 /*
10485 * Local argument list.
10486 */
10487 if (wp->w_alist == &global_alist)
10488 {
10489 if (put_line(fd, "argglobal") == FAIL)
10490 return FAIL;
10491 }
10492 else
10493 {
10494 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10495 flagp == &vop_flags
10496 || !(*flagp & SSOP_CURDIR)
10497 || wp->w_localdir != NULL, flagp) == FAIL)
10498 return FAIL;
10499 }
10500
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010501 /* Only when part of a session: restore the argument index. Some
10502 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010503 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010504 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010506 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 || put_eol(fd) == FAIL)
10508 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010509 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511
10512 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010513 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 {
10515 /*
10516 * Load the file.
10517 */
10518 if (wp->w_buffer->b_ffname != NULL
10519#ifdef FEAT_QUICKFIX
10520 && !bt_nofile(wp->w_buffer)
10521#endif
10522 )
10523 {
10524 /*
10525 * Editing a file in this buffer: use ":edit file".
10526 * This may have side effects! (e.g., compressed or network file).
10527 */
10528 if (fputs("edit ", fd) < 0
10529 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10530 return FAIL;
10531 }
10532 else
10533 {
10534 /* No file in this buffer, just make it empty. */
10535 if (put_line(fd, "enew") == FAIL)
10536 return FAIL;
10537#ifdef FEAT_QUICKFIX
10538 if (wp->w_buffer->b_ffname != NULL)
10539 {
10540 /* The buffer does have a name, but it's not a file name. */
10541 if (fputs("file ", fd) < 0
10542 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10543 return FAIL;
10544 }
10545#endif
10546 do_cursor = FALSE;
10547 }
10548 }
10549
10550 /*
10551 * Local mappings and abbreviations.
10552 */
10553 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10554 && makemap(fd, wp->w_buffer) == FAIL)
10555 return FAIL;
10556
10557 /*
10558 * Local options. Need to go to the window temporarily.
10559 * Store only local values when using ":mkview" and when ":mksession" is
10560 * used and 'sessionoptions' doesn't include "options".
10561 * Some folding options are always stored when "folds" is included,
10562 * otherwise the folds would not be restored correctly.
10563 */
10564 save_curwin = curwin;
10565 curwin = wp;
10566 curbuf = curwin->w_buffer;
10567 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10568 f = makeset(fd, OPT_LOCAL,
10569 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10570#ifdef FEAT_FOLDING
10571 else if (*flagp & SSOP_FOLDS)
10572 f = makefoldset(fd);
10573#endif
10574 else
10575 f = OK;
10576 curwin = save_curwin;
10577 curbuf = curwin->w_buffer;
10578 if (f == FAIL)
10579 return FAIL;
10580
10581#ifdef FEAT_FOLDING
10582 /*
10583 * Save Folds when 'buftype' is empty and for help files.
10584 */
10585 if ((*flagp & SSOP_FOLDS)
10586 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010587# ifdef FEAT_QUICKFIX
10588 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10589# endif
10590 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 {
10592 if (put_folds(fd, wp) == FAIL)
10593 return FAIL;
10594 }
10595#endif
10596
10597 /*
10598 * Set the cursor after creating folds, since that moves the cursor.
10599 */
10600 if (do_cursor)
10601 {
10602
10603 /* Restore the cursor line in the file and relatively in the
10604 * window. Don't use "G", it changes the jumplist. */
10605 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10606 (long)wp->w_cursor.lnum,
10607 (long)(wp->w_cursor.lnum - wp->w_topline),
10608 (long)wp->w_height / 2, (long)wp->w_height) < 0
10609 || put_eol(fd) == FAIL
10610 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10611 || put_line(fd, "exe s:l") == FAIL
10612 || put_line(fd, "normal! zt") == FAIL
10613 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10614 || put_eol(fd) == FAIL)
10615 return FAIL;
10616 /* Restore the cursor column and left offset when not wrapping. */
10617 if (wp->w_cursor.col == 0)
10618 {
10619 if (put_line(fd, "normal! 0") == FAIL)
10620 return FAIL;
10621 }
10622 else
10623 {
10624 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10625 {
10626 if (fprintf(fd,
10627 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10628 (long)wp->w_cursor.col,
10629 (long)(wp->w_cursor.col - wp->w_leftcol),
10630 (long)wp->w_width / 2, (long)wp->w_width) < 0
10631 || put_eol(fd) == FAIL
10632 || put_line(fd, "if s:c > 0") == FAIL
10633 || fprintf(fd,
10634 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10635 (long)wp->w_cursor.col) < 0
10636 || put_eol(fd) == FAIL
10637 || put_line(fd, "else") == FAIL
10638 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10639 || put_eol(fd) == FAIL
10640 || put_line(fd, "endif") == FAIL)
10641 return FAIL;
10642 }
10643 else
10644 {
10645 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10646 || put_eol(fd) == FAIL)
10647 return FAIL;
10648 }
10649 }
10650 }
10651
10652 /*
10653 * Local directory.
10654 */
10655 if (wp->w_localdir != NULL)
10656 {
10657 if (fputs("lcd ", fd) < 0
10658 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10659 || put_eol(fd) == FAIL)
10660 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010661 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663
10664 return OK;
10665}
10666
10667/*
10668 * Write an argument list to the session file.
10669 * Returns FAIL if writing fails.
10670 */
10671 static int
10672ses_arglist(fd, cmd, gap, fullname, flagp)
10673 FILE *fd;
10674 char *cmd;
10675 garray_T *gap;
10676 int fullname; /* TRUE: use full path name */
10677 unsigned *flagp;
10678{
10679 int i;
10680 char_u buf[MAXPATHL];
10681 char_u *s;
10682
10683 if (gap->ga_len == 0)
10684 return put_line(fd, "silent! argdel *");
10685 if (fputs(cmd, fd) < 0)
10686 return FAIL;
10687 for (i = 0; i < gap->ga_len; ++i)
10688 {
10689 /* NULL file names are skipped (only happens when out of memory). */
10690 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10691 if (s != NULL)
10692 {
10693 if (fullname)
10694 {
10695 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10696 s = buf;
10697 }
10698 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10699 return FAIL;
10700 }
10701 }
10702 return put_eol(fd);
10703}
10704
10705/*
10706 * Write a buffer name to the session file.
10707 * Also ends the line.
10708 * Returns FAIL if writing fails.
10709 */
10710 static int
10711ses_fname(fd, buf, flagp)
10712 FILE *fd;
10713 buf_T *buf;
10714 unsigned *flagp;
10715{
10716 char_u *name;
10717
10718 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010719 * the session file will be sourced.
10720 * Don't do this for ":mkview", we don't know the current directory.
10721 * Don't do this after ":lcd", we don't keep track of what the current
10722 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (buf->b_sfname != NULL
10724 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010725 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010726#ifdef FEAT_AUTOCHDIR
10727 && !p_acd
10728#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010729 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 name = buf->b_sfname;
10731 else
10732 name = buf->b_ffname;
10733 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10734 return FAIL;
10735 return OK;
10736}
10737
10738/*
10739 * Write a file name to the session file.
10740 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10741 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010742 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 */
10744 static int
10745ses_put_fname(fd, name, flagp)
10746 FILE *fd;
10747 char_u *name;
10748 unsigned *flagp;
10749{
10750 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010751 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
10754 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010755 if (sname == NULL)
10756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010758 if (*flagp & SSOP_SLASH)
10759 {
10760 /* change all backslashes to forward slashes */
10761 for (p = sname; *p != NUL; mb_ptr_adv(p))
10762 if (*p == '\\')
10763 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010765
10766 /* escapse special characters */
10767 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010769 if (p == NULL)
10770 return FAIL;
10771
10772 /* write the result */
10773 if (fputs((char *)p, fd) < 0)
10774 retval = FAIL;
10775
10776 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 return retval;
10778}
10779
10780/*
10781 * ":loadview [nr]"
10782 */
10783 static void
10784ex_loadview(eap)
10785 exarg_T *eap;
10786{
10787 char_u *fname;
10788
10789 fname = get_view_file(*eap->arg);
10790 if (fname != NULL)
10791 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010792 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 vim_free(fname);
10794 }
10795}
10796
10797/*
10798 * Get the name of the view file for the current buffer.
10799 */
10800 static char_u *
10801get_view_file(c)
10802 int c;
10803{
10804 int len = 0;
10805 char_u *p, *s;
10806 char_u *retval;
10807 char_u *sname;
10808
10809 if (curbuf->b_ffname == NULL)
10810 {
10811 EMSG(_(e_noname));
10812 return NULL;
10813 }
10814 sname = home_replace_save(NULL, curbuf->b_ffname);
10815 if (sname == NULL)
10816 return NULL;
10817
10818 /*
10819 * We want a file name without separators, because we're not going to make
10820 * a directory.
10821 * "normal" path separator -> "=+"
10822 * "=" -> "=="
10823 * ":" path separator -> "=-"
10824 */
10825 for (p = sname; *p; ++p)
10826 if (*p == '=' || vim_ispathsep(*p))
10827 ++len;
10828 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10829 if (retval != NULL)
10830 {
10831 STRCPY(retval, p_vdir);
10832 add_pathsep(retval);
10833 s = retval + STRLEN(retval);
10834 for (p = sname; *p; ++p)
10835 {
10836 if (*p == '=')
10837 {
10838 *s++ = '=';
10839 *s++ = '=';
10840 }
10841 else if (vim_ispathsep(*p))
10842 {
10843 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010844#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 || defined(VMS)
10846 if (*p == ':')
10847 *s++ = '-';
10848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010850 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 }
10852 else
10853 *s++ = *p;
10854 }
10855 *s++ = '=';
10856 *s++ = c;
10857 STRCPY(s, ".vim");
10858 }
10859
10860 vim_free(sname);
10861 return retval;
10862}
10863
10864#endif /* FEAT_SESSION */
10865
10866/*
10867 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10868 * Return FAIL for a write error.
10869 */
10870 int
10871put_eol(fd)
10872 FILE *fd;
10873{
10874 if (
10875#ifdef USE_CRNL
10876 (
10877# ifdef MKSESSION_NL
10878 !mksession_nl &&
10879# endif
10880 (putc('\r', fd) < 0)) ||
10881#endif
10882 (putc('\n', fd) < 0))
10883 return FAIL;
10884 return OK;
10885}
10886
10887/*
10888 * Write a line to "fd".
10889 * Return FAIL for a write error.
10890 */
10891 int
10892put_line(fd, s)
10893 FILE *fd;
10894 char *s;
10895{
10896 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10897 return FAIL;
10898 return OK;
10899}
10900
10901#ifdef FEAT_VIMINFO
10902/*
10903 * ":rviminfo" and ":wviminfo".
10904 */
10905 static void
10906ex_viminfo(eap)
10907 exarg_T *eap;
10908{
10909 char_u *save_viminfo;
10910
10911 save_viminfo = p_viminfo;
10912 if (*p_viminfo == NUL)
10913 p_viminfo = (char_u *)"'100";
10914 if (eap->cmdidx == CMD_rviminfo)
10915 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010916 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10917 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 EMSG(_("E195: Cannot open viminfo file for reading"));
10919 }
10920 else
10921 write_viminfo(eap->arg, eap->forceit);
10922 p_viminfo = save_viminfo;
10923}
10924#endif
10925
10926#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010927/*
10928 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010929 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 void
10932dialog_msg(buff, format, fname)
10933 char_u *buff;
10934 char *format;
10935 char_u *fname;
10936{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 if (fname == NULL)
10938 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010939 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940}
10941#endif
10942
10943/*
10944 * ":behave {mswin,xterm}"
10945 */
10946 static void
10947ex_behave(eap)
10948 exarg_T *eap;
10949{
10950 if (STRCMP(eap->arg, "mswin") == 0)
10951 {
10952 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10953 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10954 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10955 set_option_value((char_u *)"keymodel", 0L,
10956 (char_u *)"startsel,stopsel", 0);
10957 }
10958 else if (STRCMP(eap->arg, "xterm") == 0)
10959 {
10960 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10961 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10962 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10963 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10964 }
10965 else
10966 EMSG2(_(e_invarg2), eap->arg);
10967}
10968
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010969#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10970/*
10971 * Function given to ExpandGeneric() to obtain the possible arguments of the
10972 * ":behave {mswin,xterm}" command.
10973 */
10974 char_u *
10975get_behave_arg(xp, idx)
10976 expand_T *xp UNUSED;
10977 int idx;
10978{
10979 if (idx == 0)
10980 return (char_u *)"mswin";
10981 if (idx == 1)
10982 return (char_u *)"xterm";
10983 return NULL;
10984}
10985#endif
10986
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987#ifdef FEAT_AUTOCMD
10988static int filetype_detect = FALSE;
10989static int filetype_plugin = FALSE;
10990static int filetype_indent = FALSE;
10991
10992/*
10993 * ":filetype [plugin] [indent] {on,off,detect}"
10994 * on: Load the filetype.vim file to install autocommands for file types.
10995 * off: Load the ftoff.vim file to remove all autocommands for file types.
10996 * plugin on: load filetype.vim and ftplugin.vim
10997 * plugin off: load ftplugof.vim
10998 * indent on: load filetype.vim and indent.vim
10999 * indent off: load indoff.vim
11000 */
11001 static void
11002ex_filetype(eap)
11003 exarg_T *eap;
11004{
11005 char_u *arg = eap->arg;
11006 int plugin = FALSE;
11007 int indent = FALSE;
11008
11009 if (*eap->arg == NUL)
11010 {
11011 /* Print current status. */
11012 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11013 filetype_detect ? "ON" : "OFF",
11014 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11015 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11016 return;
11017 }
11018
11019 /* Accept "plugin" and "indent" in any order. */
11020 for (;;)
11021 {
11022 if (STRNCMP(arg, "plugin", 6) == 0)
11023 {
11024 plugin = TRUE;
11025 arg = skipwhite(arg + 6);
11026 continue;
11027 }
11028 if (STRNCMP(arg, "indent", 6) == 0)
11029 {
11030 indent = TRUE;
11031 arg = skipwhite(arg + 6);
11032 continue;
11033 }
11034 break;
11035 }
11036 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11037 {
11038 if (*arg == 'o' || !filetype_detect)
11039 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011040 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 filetype_detect = TRUE;
11042 if (plugin)
11043 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011044 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 filetype_plugin = TRUE;
11046 }
11047 if (indent)
11048 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011049 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 filetype_indent = TRUE;
11051 }
11052 }
11053 if (*arg == 'd')
11054 {
11055 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011056 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 }
11058 }
11059 else if (STRCMP(arg, "off") == 0)
11060 {
11061 if (plugin || indent)
11062 {
11063 if (plugin)
11064 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011065 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 filetype_plugin = FALSE;
11067 }
11068 if (indent)
11069 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011070 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 filetype_indent = FALSE;
11072 }
11073 }
11074 else
11075 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011076 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 filetype_detect = FALSE;
11078 }
11079 }
11080 else
11081 EMSG2(_(e_invarg2), arg);
11082}
11083
11084/*
11085 * ":setfiletype {name}"
11086 */
11087 static void
11088ex_setfiletype(eap)
11089 exarg_T *eap;
11090{
11091 if (!did_filetype)
11092 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11093}
11094#endif
11095
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 static void
11097ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011098 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099{
11100#ifdef FEAT_DIGRAPHS
11101 if (*eap->arg != NUL)
11102 putdigraph(eap->arg);
11103 else
11104 listdigraphs();
11105#else
11106 EMSG(_("E196: No digraphs in this version"));
11107#endif
11108}
11109
11110 static void
11111ex_set(eap)
11112 exarg_T *eap;
11113{
11114 int flags = 0;
11115
11116 if (eap->cmdidx == CMD_setlocal)
11117 flags = OPT_LOCAL;
11118 else if (eap->cmdidx == CMD_setglobal)
11119 flags = OPT_GLOBAL;
11120#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11121 if (cmdmod.browse && flags == 0)
11122 ex_options(eap);
11123 else
11124#endif
11125 (void)do_set(eap->arg, flags);
11126}
11127
11128#ifdef FEAT_SEARCH_EXTRA
11129/*
11130 * ":nohlsearch"
11131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 static void
11133ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011134 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
11136 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011137 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138}
11139
11140/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011141 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 * Sets nextcmd to the start of the next command, if any. Also called when
11143 * skipping commands to find the next command.
11144 */
11145 static void
11146ex_match(eap)
11147 exarg_T *eap;
11148{
11149 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011150 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 char_u *end;
11152 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011153 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011154
11155 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011156 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011157 else
11158 {
11159 EMSG(e_invcmd);
11160 return;
11161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
11163 /* First clear any old pattern. */
11164 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011165 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166
11167 if (ends_excmd(*eap->arg))
11168 end = eap->arg;
11169 else if ((STRNICMP(eap->arg, "none", 4) == 0
11170 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11171 end = eap->arg + 4;
11172 else
11173 {
11174 p = skiptowhite(eap->arg);
11175 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011176 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 p = skipwhite(p);
11178 if (*p == NUL)
11179 {
11180 /* There must be two arguments. */
11181 EMSG2(_(e_invarg2), eap->arg);
11182 return;
11183 }
11184 end = skip_regexp(p + 1, *p, TRUE, NULL);
11185 if (!eap->skip)
11186 {
11187 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11188 {
11189 eap->errmsg = e_trailing;
11190 return;
11191 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011192 if (*end != *p)
11193 {
11194 EMSG2(_(e_invarg2), p);
11195 return;
11196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197
11198 c = *end;
11199 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011200 match_add(curwin, g, p + 1, 10, id);
11201 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011202 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 }
11204 }
11205 eap->nextcmd = find_nextcmd(end);
11206}
11207#endif
11208
11209#ifdef FEAT_CRYPT
11210/*
11211 * ":X": Get crypt key
11212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 static void
11214ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011215 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011217 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011218 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219}
11220#endif
11221
11222#ifdef FEAT_FOLDING
11223 static void
11224ex_fold(eap)
11225 exarg_T *eap;
11226{
11227 if (foldManualAllowed(TRUE))
11228 foldCreate(eap->line1, eap->line2);
11229}
11230
11231 static void
11232ex_foldopen(eap)
11233 exarg_T *eap;
11234{
11235 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11236 eap->forceit, FALSE);
11237}
11238
11239 static void
11240ex_folddo(eap)
11241 exarg_T *eap;
11242{
11243 linenr_T lnum;
11244
11245 /* First set the marks for all lines closed/open. */
11246 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11247 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11248 ml_setmarked(lnum);
11249
11250 /* Execute the command on the marked lines. */
11251 global_exe(eap->arg);
11252 ml_clearmarked(); /* clear rest of the marks */
11253}
11254#endif