blob: d90e9644240083da846ae6561961a4c9871c1b42 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200132#if !defined(FEAT_PERL) \
133 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
134 || !defined(FEAT_TCL) \
135 || !defined(FEAT_RUBY) \
136 || !defined(FEAT_LUA) \
137 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000138# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void ex_script_ni __ARGS((exarg_T *eap));
140#endif
141static char_u *invalid_range __ARGS((exarg_T *eap));
142static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000143#ifdef FEAT_QUICKFIX
144static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
147static void ex_highlight __ARGS((exarg_T *eap));
148static void ex_colorscheme __ARGS((exarg_T *eap));
149static void ex_quit __ARGS((exarg_T *eap));
150static void ex_cquit __ARGS((exarg_T *eap));
151static void ex_quit_all __ARGS((exarg_T *eap));
152#ifdef FEAT_WINDOWS
153static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000154static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_resize __ARGS((exarg_T *eap));
157static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000159static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000160static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#else
164# define ex_close ex_ni
165# define ex_only ex_ni
166# define ex_all ex_ni
167# define ex_resize ex_ni
168# define ex_splitview ex_ni
169# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000170# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000172# define ex_tabs ex_ni
173# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000174# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
177static void ex_pclose __ARGS((exarg_T *eap));
178static void ex_ptag __ARGS((exarg_T *eap));
179static void ex_pedit __ARGS((exarg_T *eap));
180#else
181# define ex_pclose ex_ni
182# define ex_ptag ex_ni
183# define ex_pedit ex_ni
184#endif
185static void ex_hide __ARGS((exarg_T *eap));
186static void ex_stop __ARGS((exarg_T *eap));
187static void ex_exit __ARGS((exarg_T *eap));
188static void ex_print __ARGS((exarg_T *eap));
189#ifdef FEAT_BYTEOFF
190static void ex_goto __ARGS((exarg_T *eap));
191#else
192# define ex_goto ex_ni
193#endif
194static void ex_shell __ARGS((exarg_T *eap));
195static void ex_preserve __ARGS((exarg_T *eap));
196static void ex_recover __ARGS((exarg_T *eap));
197#ifndef FEAT_LISTCMDS
198# define ex_argedit ex_ni
199# define ex_argadd ex_ni
200# define ex_argdelete ex_ni
201# define ex_listdo ex_ni
202#endif
203static void ex_mode __ARGS((exarg_T *eap));
204static void ex_wrongmodifier __ARGS((exarg_T *eap));
205static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000206static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static void ex_edit __ARGS((exarg_T *eap));
208#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
209# define ex_drop ex_ni
210#endif
211#ifndef FEAT_GUI
212# define ex_gui ex_nogui
213static void ex_nogui __ARGS((exarg_T *eap));
214#endif
215#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
216static void ex_tearoff __ARGS((exarg_T *eap));
217#else
218# define ex_tearoff ex_ni
219#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000220#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221static void ex_popup __ARGS((exarg_T *eap));
222#else
223# define ex_popup ex_ni
224#endif
225#ifndef FEAT_GUI_MSWIN
226# define ex_simalt ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define gui_mch_find_dialog ex_ni
230# define gui_mch_replace_dialog ex_ni
231#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000232#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define ex_helpfind ex_ni
234#endif
235#ifndef FEAT_CSCOPE
236# define do_cscope ex_ni
237# define do_scscope ex_ni
238# define do_cstag ex_ni
239#endif
240#ifndef FEAT_SYN_HL
241# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200242# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#endif
244#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000245# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000247# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000248# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000249# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200251#ifndef FEAT_PERSISTENT_UNDO
252# define ex_rundo ex_ni
253# define ex_wundo ex_ni
254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200255#ifndef FEAT_LUA
256# define ex_lua ex_script_ni
257# define ex_luado ex_ni
258# define ex_luafile ex_ni
259#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000260#ifndef FEAT_MZSCHEME
261# define ex_mzscheme ex_script_ni
262# define ex_mzfile ex_ni
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_PERL
265# define ex_perl ex_script_ni
266# define ex_perldo ex_ni
267#endif
268#ifndef FEAT_PYTHON
269# define ex_python ex_script_ni
270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274# define ex_py3file ex_ni
275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#ifndef FEAT_TCL
277# define ex_tcl ex_script_ni
278# define ex_tcldo ex_ni
279# define ex_tclfile ex_ni
280#endif
281#ifndef FEAT_RUBY
282# define ex_ruby ex_script_ni
283# define ex_rubydo ex_ni
284# define ex_rubyfile ex_ni
285#endif
286#ifndef FEAT_SNIFF
287# define ex_sniff ex_ni
288#endif
289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
292static void ex_swapname __ARGS((exarg_T *eap));
293static void ex_syncbind __ARGS((exarg_T *eap));
294static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static void ex_pwd __ARGS((exarg_T *eap));
296static void ex_equal __ARGS((exarg_T *eap));
297static void ex_sleep __ARGS((exarg_T *eap));
298static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
299static void ex_winsize __ARGS((exarg_T *eap));
300#ifdef FEAT_WINDOWS
301static void ex_wincmd __ARGS((exarg_T *eap));
302#else
303# define ex_wincmd ex_ni
304#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000305#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_winpos __ARGS((exarg_T *eap));
307#else
308# define ex_winpos ex_ni
309#endif
310static void ex_operators __ARGS((exarg_T *eap));
311static void ex_put __ARGS((exarg_T *eap));
312static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000313static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static void ex_submagic __ARGS((exarg_T *eap));
315static void ex_join __ARGS((exarg_T *eap));
316static void ex_at __ARGS((exarg_T *eap));
317static void ex_bang __ARGS((exarg_T *eap));
318static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200319#ifdef FEAT_PERSISTENT_UNDO
320static void ex_wundo __ARGS((exarg_T *eap));
321static void ex_rundo __ARGS((exarg_T *eap));
322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000324static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static void ex_redir __ARGS((exarg_T *eap));
326static void ex_redraw __ARGS((exarg_T *eap));
327static void ex_redrawstatus __ARGS((exarg_T *eap));
328static void close_redir __ARGS((void));
329static void ex_mkrc __ARGS((exarg_T *eap));
330static void ex_mark __ARGS((exarg_T *eap));
331#ifdef FEAT_USR_CMDS
332static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000333static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#endif
335#ifdef FEAT_EX_EXTRA
336static void ex_normal __ARGS((exarg_T *eap));
337static void ex_startinsert __ARGS((exarg_T *eap));
338static void ex_stopinsert __ARGS((exarg_T *eap));
339#else
340# define ex_normal ex_ni
341# define ex_align ex_ni
342# define ex_retab ex_ni
343# define ex_startinsert ex_ni
344# define ex_stopinsert ex_ni
345# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000346# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
348#ifdef FEAT_FIND_ID
349static void ex_checkpath __ARGS((exarg_T *eap));
350static void ex_findpat __ARGS((exarg_T *eap));
351#else
352# define ex_findpat ex_ni
353# define ex_checkpath ex_ni
354#endif
355#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
356static void ex_psearch __ARGS((exarg_T *eap));
357#else
358# define ex_psearch ex_ni
359#endif
360static void ex_tag __ARGS((exarg_T *eap));
361static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
362#ifndef FEAT_EVAL
363# define ex_scriptnames ex_ni
364# define ex_finish ex_ni
365# define ex_echo ex_ni
366# define ex_echohl ex_ni
367# define ex_execute ex_ni
368# define ex_call ex_ni
369# define ex_if ex_ni
370# define ex_endif ex_ni
371# define ex_else ex_ni
372# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000373# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000377# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378# define ex_throw ex_ni
379# define ex_try ex_ni
380# define ex_catch ex_ni
381# define ex_finally ex_ni
382# define ex_endtry ex_ni
383# define ex_endfunction ex_ni
384# define ex_let ex_ni
385# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000386# define ex_lockvar ex_ni
387# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_function ex_ni
389# define ex_delfunction ex_ni
390# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000391# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393static char_u *arg_all __ARGS((void));
394#ifdef FEAT_SESSION
395static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000396static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void ex_loadview __ARGS((exarg_T *eap));
398static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000399static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#else
401# define ex_loadview ex_ni
402#endif
403#ifndef FEAT_EVAL
404# define ex_compiler ex_ni
405#endif
406#ifdef FEAT_VIMINFO
407static void ex_viminfo __ARGS((exarg_T *eap));
408#else
409# define ex_viminfo ex_ni
410#endif
411static void ex_behave __ARGS((exarg_T *eap));
412#ifdef FEAT_AUTOCMD
413static void ex_filetype __ARGS((exarg_T *eap));
414static void ex_setfiletype __ARGS((exarg_T *eap));
415#else
416# define ex_filetype ex_ni
417# define ex_setfiletype ex_ni
418#endif
419#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000420# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421# define ex_diffpatch ex_ni
422# define ex_diffgetput ex_ni
423# define ex_diffsplit ex_ni
424# define ex_diffthis ex_ni
425# define ex_diffupdate ex_ni
426#endif
427static void ex_digraphs __ARGS((exarg_T *eap));
428static void ex_set __ARGS((exarg_T *eap));
429#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
430# define ex_options ex_ni
431#endif
432#ifdef FEAT_SEARCH_EXTRA
433static void ex_nohlsearch __ARGS((exarg_T *eap));
434static void ex_match __ARGS((exarg_T *eap));
435#else
436# define ex_nohlsearch ex_ni
437# define ex_match ex_ni
438#endif
439#ifdef FEAT_CRYPT
440static void ex_X __ARGS((exarg_T *eap));
441#else
442# define ex_X ex_ni
443#endif
444#ifdef FEAT_FOLDING
445static void ex_fold __ARGS((exarg_T *eap));
446static void ex_foldopen __ARGS((exarg_T *eap));
447static void ex_folddo __ARGS((exarg_T *eap));
448#else
449# define ex_fold ex_ni
450# define ex_foldopen ex_ni
451# define ex_folddo ex_ni
452#endif
453#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
454 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
455# define ex_language ex_ni
456#endif
457#ifndef FEAT_SIGNS
458# define ex_sign ex_ni
459#endif
460#ifndef FEAT_SUN_WORKSHOP
461# define ex_wsverb ex_ni
462#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
469#ifndef FEAT_EVAL
470# define ex_debug ex_ni
471# define ex_breakadd ex_ni
472# define ex_debuggreedy ex_ni
473# define ex_breakdel ex_ni
474# define ex_breaklist ex_ni
475#endif
476
477#ifndef FEAT_CMDHIST
478# define ex_history ex_ni
479#endif
480#ifndef FEAT_JUMPLIST
481# define ex_jumps ex_ni
482# define ex_changes ex_ni
483#endif
484
Bram Moolenaar05159a02005-02-26 23:04:13 +0000485#ifndef FEAT_PROFILE
486# define ex_profile ex_ni
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Declare cmdnames[].
491 */
492#define DO_DECLARE_EXCMD
493#include "ex_cmds.h"
494
495/*
496 * Table used to quickly search for a command, based on its first character.
497 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000498static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 CMD_append,
501 CMD_buffer,
502 CMD_change,
503 CMD_delete,
504 CMD_edit,
505 CMD_file,
506 CMD_global,
507 CMD_help,
508 CMD_insert,
509 CMD_join,
510 CMD_k,
511 CMD_list,
512 CMD_move,
513 CMD_next,
514 CMD_open,
515 CMD_print,
516 CMD_quit,
517 CMD_read,
518 CMD_substitute,
519 CMD_t,
520 CMD_undo,
521 CMD_vglobal,
522 CMD_write,
523 CMD_xit,
524 CMD_yank,
525 CMD_z,
526 CMD_bang
527};
528
529static char_u dollar_command[2] = {'$', 0};
530
531
532#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534typedef struct
535{
536 char_u *line; /* command line */
537 linenr_T lnum; /* sourcing_lnum of the line */
538} wcmd_T;
539
540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 garray_T *lines_gap; /* growarray with line info */
548 int current_line; /* last read line from growarray */
549 int repeating; /* TRUE when looping a second time */
550 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
551 char_u *(*getline) __ARGS((int, void *, int));
552 void *cookie;
553};
554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
556static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000558
559/* Struct to save a few things while debugging. Used in do_cmdline() only. */
560struct dbg_stuff
561{
562 int trylevel;
563 int force_abort;
564 except_T *caught_stack;
565 char_u *vv_exception;
566 char_u *vv_throwpoint;
567 int did_emsg;
568 int got_int;
569 int did_throw;
570 int need_rethrow;
571 int check_cstack;
572 except_T *current_exception;
573};
574
575static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577
578 static void
579save_dbg_stuff(dsp)
580 struct dbg_stuff *dsp;
581{
582 dsp->trylevel = trylevel; trylevel = 0;
583 dsp->force_abort = force_abort; force_abort = FALSE;
584 dsp->caught_stack = caught_stack; caught_stack = NULL;
585 dsp->vv_exception = v_exception(NULL);
586 dsp->vv_throwpoint = v_throwpoint(NULL);
587
588 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
589 dsp->did_emsg = did_emsg; did_emsg = FALSE;
590 dsp->got_int = got_int; got_int = FALSE;
591 dsp->did_throw = did_throw; did_throw = FALSE;
592 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
593 dsp->check_cstack = check_cstack; check_cstack = FALSE;
594 dsp->current_exception = current_exception; current_exception = NULL;
595}
596
597 static void
598restore_dbg_stuff(dsp)
599 struct dbg_stuff *dsp;
600{
601 suppress_errthrow = FALSE;
602 trylevel = dsp->trylevel;
603 force_abort = dsp->force_abort;
604 caught_stack = dsp->caught_stack;
605 (void)v_exception(dsp->vv_exception);
606 (void)v_throwpoint(dsp->vv_throwpoint);
607 did_emsg = dsp->did_emsg;
608 got_int = dsp->got_int;
609 did_throw = dsp->did_throw;
610 need_rethrow = dsp->need_rethrow;
611 check_cstack = dsp->check_cstack;
612 current_exception = dsp->current_exception;
613}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615
616
617/*
618 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
619 * command is given.
620 */
621 void
622do_exmode(improved)
623 int improved; /* TRUE for "improved Ex" mode */
624{
625 int save_msg_scroll;
626 int prev_msg_row;
627 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 int changedtick;
629
630 if (improved)
631 exmode_active = EXMODE_VIM;
632 else
633 exmode_active = EXMODE_NORMAL;
634 State = NORMAL;
635
636 /* When using ":global /pat/ visual" and then "Q" we return to continue
637 * the :global command. */
638 if (global_busy)
639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 save_msg_scroll = msg_scroll;
642 ++RedrawingDisabled; /* don't redisplay the window */
643 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_GUI
645 /* Ignore scrollbar and mouse events in Ex mode */
646 ++hold_gui_events;
647#endif
648#ifdef FEAT_SNIFF
649 want_sniff_request = 0; /* No K_SNIFF wanted */
650#endif
651
652 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
653 while (exmode_active)
654 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000655#ifdef FEAT_EX_EXTRA
656 /* Check for a ":normal" command and no more characters left. */
657 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
658 {
659 exmode_active = FALSE;
660 break;
661 }
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 msg_scroll = TRUE;
664 need_wait_return = FALSE;
665 ex_pressedreturn = FALSE;
666 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 prev_msg_row = msg_row;
669 prev_line = curwin->w_cursor.lnum;
670#ifdef FEAT_SNIFF
671 ProcessSniffRequests();
672#endif
673 if (improved)
674 {
675 cmdline_row = msg_row;
676 do_cmdline(NULL, getexline, NULL, 0);
677 }
678 else
679 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
680 lines_left = Rows - 1;
681
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if ((prev_line != curwin->w_cursor.lnum
683 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (curbuf->b_ml.ml_flags & ML_EMPTY)
686 EMSG(_(e_emptybuf));
687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (ex_pressedreturn)
690 {
691 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000692 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 msg_row = prev_msg_row;
694 if (prev_msg_row == Rows - 1)
695 msg_row--;
696 }
697 msg_col = 0;
698 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
699 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
703 {
704 if (curbuf->b_ml.ml_flags & ML_EMPTY)
705 EMSG(_(e_emptybuf));
706 else
707 EMSG(_("E501: At end-of-file"));
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
711#ifdef FEAT_GUI
712 --hold_gui_events;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --RedrawingDisabled;
715 --no_wait_return;
716 update_screen(CLEAR);
717 need_wait_return = FALSE;
718 msg_scroll = save_msg_scroll;
719}
720
721/*
722 * Execute a simple command line. Used for translated commands like "*".
723 */
724 int
725do_cmdline_cmd(cmd)
726 char_u *cmd;
727{
728 return do_cmdline(cmd, NULL, NULL,
729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
730}
731
732/*
733 * do_cmdline(): execute one Ex command line
734 *
735 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * 2. Split up in parts separated with '|'.
738 *
739 * This function can be called recursively!
740 *
741 * flags:
742 * DOCMD_VERBOSE - The command will be included in the error message.
743 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * DOCMD_KEYTYPED - Don't reset KeyTyped.
746 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
747 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
748 *
749 * return FAIL if cmdline could not be executed, OK otherwise
750 */
751 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 char_u *(*fgetline) __ARGS((int, void *, int));
755 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 static int recursive = 0; /* recursive depth */
762 int msg_didout_before_start = 0;
763 int count = 0; /* line number count */
764 int did_inc = FALSE; /* incremented RedrawingDisabled */
765 int retval = OK;
766#ifdef FEAT_EVAL
767 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 int current_line = 0; /* active line in lines_ga */
770 char_u *fname = NULL; /* function or script name */
771 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
772 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000773 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int initial_trylevel;
775 struct msglist **saved_msg_list = NULL;
776 struct msglist *private_msg_list;
777
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 char_u *(*cmd_getline) __ARGS((int, void *, int));
780 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# define cmd_cookie cookie
787#endif
788 static int call_depth = 0; /* recursiveness */
789
790#ifdef FEAT_EVAL
791 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
792 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000793 * This ensures that the do_errthrow() call in do_one_cmd() does not
794 * combine the messages stored by an earlier invocation of do_one_cmd()
795 * with the command name of the later one. This would happen when
796 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 saved_msg_list = msg_list;
798 msg_list = &private_msg_list;
799 private_msg_list = NULL;
800#endif
801
802 /* It's possible to create an endless loop with ":execute", catch that
803 * here. The value of 200 allows nested function calls, ":source", etc. */
804 if (call_depth == 200)
805 {
806 EMSG(_("E169: Command too recursive"));
807#ifdef FEAT_EVAL
808 /* When converting to an exception, we do not include the command name
809 * since this is not an error of the specific command. */
810 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
811 msg_list = saved_msg_list;
812#endif
813 return FAIL;
814 }
815 ++call_depth;
816
817#ifdef FEAT_EVAL
818 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 cstack.cs_trylevel = 0;
821 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
824
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++ex_nesting_level;
831
832 /* Get the function or script name and the address where the next breakpoint
833 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = func_name(real_cookie);
837 breakpoint = func_breakpoint(real_cookie);
838 dbg_tick = func_dbg_tick(real_cookie);
839 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
842 fname = sourcing_name;
843 breakpoint = source_breakpoint(real_cookie);
844 dbg_tick = source_dbg_tick(real_cookie);
845 }
846
847 /*
848 * Initialize "force_abort" and "suppress_errthrow" at the top level.
849 */
850 if (!recursive)
851 {
852 force_abort = FALSE;
853 suppress_errthrow = FALSE;
854 }
855
856 /*
857 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000858 * exception handling (used when debugging). Otherwise clear it to avoid
859 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000861 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000862 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000863 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200864 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 initial_trylevel = trylevel;
867
868 /*
869 * "did_throw" will be set to TRUE when an exception is being thrown.
870 */
871 did_throw = FALSE;
872#endif
873 /*
874 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * If force_abort is set, we cancel everything.
877 */
878 did_emsg = FALSE;
879
880 /*
881 * KeyTyped is only set when calling vgetc(). Reset it here when not
882 * calling vgetc() (sourced command lines).
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (!(flags & DOCMD_KEYTYPED)
885 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 KeyTyped = FALSE;
887
888 /*
889 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000890 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * - for multiple commands on one line, separated with '|'
892 * - when repeating until there are no more lines (for ":source")
893 */
894 next_cmdline = cmdline;
895 do
896 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100898 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899#endif
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (next_cmdline == NULL
903#ifdef FEAT_EVAL
904 && !force_abort
905 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 )
909 did_emsg = FALSE;
910
911 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * 3. If a line is given: Make a copy, so we can mess with it.
915 */
916
917#ifdef FEAT_EVAL
918 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 /* Each '|' separated command is stored separately in lines_ga, to
922 * be able to jump to it. Don't use next_cmdline now. */
923 vim_free(cmdline_copy);
924 cmdline_copy = NULL;
925
926 /* Check if a function has returned or, unless it has an unclosed
927 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000931 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932 func_line_end(real_cookie);
933# endif
934 if (func_has_ended(real_cookie))
935 {
936 retval = FAIL;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 script_line_end();
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
952
953 /* If breakpoints have been added/deleted need to check for it. */
954 if (breakpoint != NULL && dbg_tick != NULL
955 && *dbg_tick != debug_tick)
956 {
957 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
962
963 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
964 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
965
966 /* Did we encounter a breakpoint? */
967 if (breakpoint != NULL && *breakpoint != 0
968 && *breakpoint <= sourcing_lnum)
969 {
970 dbg_breakpoint(fname, sourcing_lnum);
971 /* Find next breakpoint. */
972 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 fname, sourcing_lnum);
975 *dbg_tick = debug_tick;
976 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000978 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000979 {
980 if (getline_is_func)
981 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983 script_line_start();
984 }
985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * below, so that it stores lines in or reads them from
993 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 * while/for loop. */
995 cmd_getline = get_loop_line;
996 cmd_cookie = (void *)&cmd_loop_cookie;
997 cmd_loop_cookie.lines_gap = &lines_ga;
998 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 cmd_loop_cookie.cookie = cookie;
1001 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 else
1004 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 cmd_cookie = cookie;
1007 }
1008#endif
1009
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (next_cmdline == NULL)
1012 {
1013 /*
1014 * Need to set msg_didout for the first line after an ":if",
1015 * otherwise the ":if" will be overwritten.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_EVAL
1021 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1022#else
1023 0
1024#endif
1025 )) == NULL)
1026 {
1027 /* Don't call wait_return for aborted command line. The NULL
1028 * returned for the end of a sourced file or executed function
1029 * doesn't do this. */
1030 if (KeyTyped && !(flags & DOCMD_REPEAT))
1031 need_wait_return = FALSE;
1032 retval = FAIL;
1033 break;
1034 }
1035 used_getline = TRUE;
1036
1037 /*
1038 * Keep the first typed line. Clear it when more lines are typed.
1039 */
1040 if (flags & DOCMD_KEEPLINE)
1041 {
1042 vim_free(repeat_cmdline);
1043 if (count == 0)
1044 repeat_cmdline = vim_strsave(next_cmdline);
1045 else
1046 repeat_cmdline = NULL;
1047 }
1048 }
1049
1050 /* 3. Make a copy of the command so we can mess with it. */
1051 else if (cmdline_copy == NULL)
1052 {
1053 next_cmdline = vim_strsave(next_cmdline);
1054 if (next_cmdline == NULL)
1055 {
1056 EMSG(_(e_outofmem));
1057 retval = FAIL;
1058 break;
1059 }
1060 }
1061 cmdline_copy = next_cmdline;
1062
1063#ifdef FEAT_EVAL
1064 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 * Save the current line when inside a ":while" or ":for", and when
1066 * the command looks like a ":while" or ":for", because we may need it
1067 * later. When there is a '|' and another command, it is stored
1068 * separately, because we need to be able to jump back to it from an
1069 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (current_line == lines_ga.ga_len
1072 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 retval = FAIL;
1077 break;
1078 }
1079 }
1080 did_endif = FALSE;
1081#endif
1082
1083 if (count++ == 0)
1084 {
1085 /*
1086 * All output from the commands is put below each other, without
1087 * waiting for a return. Don't do this when executing commands
1088 * from a script or when being called recursive (e.g. for ":e
1089 * +command file").
1090 */
1091 if (!(flags & DOCMD_NOWAIT) && !recursive)
1092 {
1093 msg_didout_before_start = msg_didout;
1094 msg_didany = FALSE; /* no output yet */
1095 msg_start();
1096 msg_scroll = TRUE; /* put messages below each other */
1097 ++no_wait_return; /* dont wait for return until finished */
1098 ++RedrawingDisabled;
1099 did_inc = TRUE;
1100 }
1101 }
1102
1103 if (p_verbose >= 15 && sourcing_name != NULL)
1104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001106 verbose_enter_scroll();
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 smsg((char_u *)_("line %ld: %s"),
1109 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001110 if (msg_silent == 0)
1111 msg_puts((char_u *)"\n"); /* don't overwrite this */
1112
1113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 --no_wait_return;
1115 }
1116
1117 /*
1118 * 2. Execute one '|' separated command.
1119 * do_one_cmd() will return NULL if there is no trailing '|'.
1120 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1121 */
1122 ++recursive;
1123 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1124#ifdef FEAT_EVAL
1125 &cstack,
1126#endif
1127 cmd_getline, cmd_cookie);
1128 --recursive;
1129
1130#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cmd_cookie == (void *)&cmd_loop_cookie)
1132 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 if (next_cmdline == NULL)
1138 {
1139 vim_free(cmdline_copy);
1140 cmdline_copy = NULL;
1141#ifdef FEAT_CMDHIST
1142 /*
1143 * If the command was typed, remember it for the ':' register.
1144 * Do this AFTER executing the command to make :@: work.
1145 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001146 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 && new_last_cmdline != NULL)
1148 {
1149 vim_free(last_cmdline);
1150 last_cmdline = new_last_cmdline;
1151 new_last_cmdline = NULL;
1152 }
1153#endif
1154 }
1155 else
1156 {
1157 /* need to copy the command after the '|' to cmdline_copy, for the
1158 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001159 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 next_cmdline = cmdline_copy;
1161 }
1162
1163
1164#ifdef FEAT_EVAL
1165 /* reset did_emsg for a function that is not aborted by an error */
1166 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 && !func_has_abort(real_cookie))
1169 did_emsg = FALSE;
1170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 ++current_line;
1174
1175 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 * An ":endwhile", ":endfor" and ":continue" is handled here.
1177 * If we were executing commands, jump back to the ":while" or
1178 * ":for".
1179 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* Jump back to the matching ":while" or ":for". Be careful
1186 * not to use a cs_line[] from an entry that isn't a ":while"
1187 * or ":for": It would make "current_line" invalid and can
1188 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (!did_emsg && !got_int && !did_throw
1190 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 && (cstack.cs_flags[cstack.cs_idx]
1192 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && cstack.cs_line[cstack.cs_idx] >= 0
1194 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1195 {
1196 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 /* remember we jumped there */
1198 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 line_breakcheck(); /* check if CTRL-C typed */
1200
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* Check for the next breakpoint at or after the ":while"
1202 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (breakpoint != NULL)
1204 {
1205 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 fname,
1208 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1209 *dbg_tick = debug_tick;
1210 }
1211 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1217 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220
1221 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1228 }
1229 }
1230
1231 /*
1232 * When not inside any ":while" loop, clear remembered lines.
1233 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (lines_ga.ga_len > 0)
1237 {
1238 sourcing_lnum =
1239 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1240 free_cmdlines(&lines_ga);
1241 }
1242 current_line = 0;
1243 }
1244
1245 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1247 * being restored at the ":endtry". Reset them here and set the
1248 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1249 * This includes the case where a missing ":endif", ":endwhile" or
1250 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 cstack.cs_lflags &= ~CSL_HAD_FINA;
1255 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1256 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 did_throw ? (void *)current_exception : NULL);
1258 did_emsg = got_int = did_throw = FALSE;
1259 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1260 }
1261
1262 /* Update global "trylevel" for recursive calls to do_cmdline() from
1263 * within this loop. */
1264 trylevel = initial_trylevel + cstack.cs_trylevel;
1265
1266 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001267 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * files) is aborted because of an error, an interrupt, or an uncaught
1269 * exception, cancel everything. If it is left normally, reset
1270 * force_abort to get the non-EH compatible abortion behavior for
1271 * the rest of the script.
1272 */
1273 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1274 force_abort = FALSE;
1275
1276 /* Convert an interrupt to an exception if appropriate. */
1277 (void)do_intthrow(&cstack);
1278#endif /* FEAT_EVAL */
1279
1280 }
1281 /*
1282 * Continue executing command lines when:
1283 * - no CTRL-C typed, no aborting error, no exception thrown or try
1284 * conditionals need to be checked for executing finally clauses or
1285 * catching an interrupt exception
1286 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * looping for ":source" command or function call.
1289 */
1290 while (!((got_int
1291#ifdef FEAT_EVAL
1292 || (did_emsg && force_abort) || did_throw
1293#endif
1294 )
1295#ifdef FEAT_EVAL
1296 && cstack.cs_trylevel == 0
1297#endif
1298 )
1299 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001300 && (getline_equal(fgetline, cookie, getexmodeline)
1301 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (next_cmdline != NULL
1303#ifdef FEAT_EVAL
1304 || cstack.cs_idx >= 0
1305#endif
1306 || (flags & DOCMD_REPEAT)));
1307
1308 vim_free(cmdline_copy);
1309#ifdef FEAT_EVAL
1310 free_cmdlines(&lines_ga);
1311 ga_clear(&lines_ga);
1312
1313 if (cstack.cs_idx >= 0)
1314 {
1315 /*
1316 * If a sourced file or executed function ran to its end, report the
1317 * unclosed conditional.
1318 */
1319 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 && ((getline_equal(fgetline, cookie, getsourceline)
1321 && !source_finished(fgetline, cookie))
1322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && !func_has_ended(real_cookie))))
1324 {
1325 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1326 EMSG(_(e_endtry));
1327 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1328 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1330 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 EMSG(_(e_endif));
1333 }
1334
1335 /*
1336 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1337 * ":endtry" in a sourced file or executed function. If the try
1338 * conditional is in its finally clause, ignore anything pending.
1339 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001340 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 {
1344 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1345
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001346 if (idx >= 0)
1347 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1349 &cstack.cs_looplevel);
1350 }
1351 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 trylevel = initial_trylevel;
1353 }
1354
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001355 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1356 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001358 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ? (char_u *)"endfunction" : (char_u *)NULL);
1360
1361 if (trylevel == 0)
1362 {
1363 /*
1364 * When an exception is being thrown out of the outermost try
1365 * conditional, discard the uncaught exception, disable the conversion
1366 * of interrupts or errors to exceptions, and ensure that no more
1367 * commands are executed.
1368 */
1369 if (did_throw)
1370 {
1371 void *p = NULL;
1372 char_u *saved_sourcing_name;
1373 int saved_sourcing_lnum;
1374 struct msglist *messages = NULL, *next;
1375
1376 /*
1377 * If the uncaught exception is a user exception, report it as an
1378 * error. If it is an error exception, display the saved error
1379 * message now. For an interrupt exception, do nothing; the
1380 * interrupt message is given elsewhere.
1381 */
1382 switch (current_exception->type)
1383 {
1384 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001385 vim_snprintf((char *)IObuff, IOSIZE,
1386 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 current_exception->value);
1388 p = vim_strsave(IObuff);
1389 break;
1390 case ET_ERROR:
1391 messages = current_exception->messages;
1392 current_exception->messages = NULL;
1393 break;
1394 case ET_INTERRUPT:
1395 break;
1396 default:
1397 p = vim_strsave((char_u *)_(e_internal));
1398 }
1399
1400 saved_sourcing_name = sourcing_name;
1401 saved_sourcing_lnum = sourcing_lnum;
1402 sourcing_name = current_exception->throw_name;
1403 sourcing_lnum = current_exception->throw_lnum;
1404 current_exception->throw_name = NULL;
1405
1406 discard_current_exception(); /* uses IObuff if 'verbose' */
1407 suppress_errthrow = TRUE;
1408 force_abort = TRUE;
1409
1410 if (messages != NULL)
1411 {
1412 do
1413 {
1414 next = messages->next;
1415 emsg(messages->msg);
1416 vim_free(messages->msg);
1417 vim_free(messages);
1418 messages = next;
1419 }
1420 while (messages != NULL);
1421 }
1422 else if (p != NULL)
1423 {
1424 emsg(p);
1425 vim_free(p);
1426 }
1427 vim_free(sourcing_name);
1428 sourcing_name = saved_sourcing_name;
1429 sourcing_lnum = saved_sourcing_lnum;
1430 }
1431
1432 /*
1433 * On an interrupt or an aborting error not converted to an exception,
1434 * disable the conversion of errors to exceptions. (Interrupts are not
1435 * converted any more, here.) This enables also the interrupt message
1436 * when force_abort is set and did_emsg unset in case of an interrupt
1437 * from a finally clause after an error.
1438 */
1439 else if (got_int || (did_emsg && force_abort))
1440 suppress_errthrow = TRUE;
1441 }
1442
1443 /*
1444 * The current cstack will be freed when do_cmdline() returns. An uncaught
1445 * exception will have to be rethrown in the previous cstack. If a function
1446 * has just returned or a script file was just finished and the previous
1447 * cstack belongs to the same function or, respectively, script file, it
1448 * will have to be checked for finally clauses to be executed due to the
1449 * ":return" or ":finish". This is done in do_one_cmd().
1450 */
1451 if (did_throw)
1452 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001453 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001455 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 && ex_nesting_level > func_level(real_cookie) + 1))
1457 {
1458 if (!did_throw)
1459 check_cstack = TRUE;
1460 }
1461 else
1462 {
1463 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 --ex_nesting_level;
1466 /*
1467 * Go to debug mode when returning from a function in which we are
1468 * single-stepping.
1469 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 if ((getline_equal(fgetline, cookie, getsourceline)
1471 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ? (char_u *)_("End of sourced file")
1475 : (char_u *)_("End of function"));
1476 }
1477
1478 /*
1479 * Restore the exception environment (done after returning from the
1480 * debugger).
1481 */
1482 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001483 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 msg_list = saved_msg_list;
1486#endif /* FEAT_EVAL */
1487
1488 /*
1489 * If there was too much output to fit on the command line, ask the user to
1490 * hit return before redrawing the screen. With the ":global" command we do
1491 * this only once after the command is finished.
1492 */
1493 if (did_inc)
1494 {
1495 --RedrawingDisabled;
1496 --no_wait_return;
1497 msg_scroll = FALSE;
1498
1499 /*
1500 * When just finished an ":if"-":else" which was typed, no need to
1501 * wait for hit-return. Also for an error situation.
1502 */
1503 if (retval == FAIL
1504#ifdef FEAT_EVAL
1505 || (did_endif && KeyTyped && !did_emsg)
1506#endif
1507 )
1508 {
1509 need_wait_return = FALSE;
1510 msg_didany = FALSE; /* don't wait when restarting edit */
1511 }
1512 else if (need_wait_return)
1513 {
1514 /*
1515 * The msg_start() above clears msg_didout. The wait_return we do
1516 * here should not overwrite the command that may be shown before
1517 * doing that.
1518 */
1519 msg_didout |= msg_didout_before_start;
1520 wait_return(FALSE);
1521 }
1522 }
1523
1524#ifndef FEAT_EVAL
1525 /*
1526 * Reset if_level, in case a sourced script file contains more ":if" than
1527 * ":endif" (could be ":if x | foo | endif").
1528 */
1529 if_level = 0;
1530#endif
1531
1532 --call_depth;
1533 return retval;
1534}
1535
1536#ifdef FEAT_EVAL
1537/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int c;
1543 void *cookie;
1544 int indent;
1545{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 wcmd_T *wp;
1548 char_u *line;
1549
1550 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1551 {
1552 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001553 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (cp->getline == NULL)
1557 line = getcmdline(c, 0L, indent);
1558 else
1559 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001560 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 ++cp->current_line;
1562
1563 return line;
1564 }
1565
1566 KeyTyped = FALSE;
1567 ++cp->current_line;
1568 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1569 sourcing_lnum = wp->lnum;
1570 return vim_strsave(wp->line);
1571}
1572
1573/*
1574 * Store a line in "gap" so that a ":while" loop can execute it again.
1575 */
1576 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 garray_T *gap;
1579 char_u *line;
1580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
1593free_cmdlines(gap)
1594 garray_T *gap;
1595{
1596 while (gap->ga_len > 0)
1597 {
1598 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1599 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601}
1602#endif
1603
1604/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001605 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1606 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609getline_equal(fgetline, cookie, func)
1610 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001611 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 char_u *(*func) __ARGS((int, void *, int));
1613{
1614#ifdef FEAT_EVAL
1615 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001619 * function that's originally used to obtain the lines. This may be
1620 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 cp = (struct loop_cookie *)cookie;
1623 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 gp = cp->getline;
1626 cp = cp->cookie;
1627 }
1628 return gp == func;
1629#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#endif
1632}
1633
1634#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1635/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * getline function. Otherwise return "cookie".
1638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001641 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644# ifdef FEAT_EVAL
1645 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001646 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar89d40322006-08-29 15:30:07 +00001648 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001649 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001652 cp = (struct loop_cookie *)cookie;
1653 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655 gp = cp->getline;
1656 cp = cp->cookie;
1657 }
1658 return cp;
1659# else
1660 return cookie;
1661# endif
1662}
1663#endif
1664
1665/*
1666 * Execute one Ex command.
1667 *
1668 * If 'sourcing' is TRUE, the command will be included in the error message.
1669 *
1670 * 1. skip comment lines and leading space
1671 * 2. handle command modifiers
1672 * 3. parse range
1673 * 4. parse command
1674 * 5. parse arguments
1675 * 6. switch on command name
1676 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
1679 * This function may be called recursively!
1680 */
1681#if (_MSC_VER == 1200)
1682/*
Bram Moolenaared203462004-06-16 11:19:22 +00001683 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001685 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687 static char_u *
1688do_one_cmd(cmdlinep, sourcing,
1689#ifdef FEAT_EVAL
1690 cstack,
1691#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001692 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 char_u **cmdlinep;
1694 int sourcing;
1695#ifdef FEAT_EVAL
1696 struct condstack *cstack;
1697#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001698 char_u *(*fgetline) __ARGS((int, void *, int));
1699 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 char_u *p;
1702 linenr_T lnum;
1703 long n;
1704 char_u *errormsg = NULL; /* error message */
1705 exarg_T ea; /* Ex command arguments */
1706 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001707 int save_msg_scroll = msg_scroll;
1708 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001710#ifdef HAVE_SANDBOX
1711 int did_sandbox = FALSE;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 cmdmod_T save_cmdmod;
1714 int ni; /* set when Not Implemented */
1715
1716 vim_memset(&ea, 0, sizeof(ea));
1717 ea.line1 = 1;
1718 ea.line2 = 1;
1719#ifdef FEAT_EVAL
1720 ++ex_nesting_level;
1721#endif
1722
1723 /* when not editing the last file :q has to be typed twice */
1724 if (quitmore
1725#ifdef FEAT_EVAL
1726 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001727 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
1729 )
1730 --quitmore;
1731
1732 /*
1733 * Reset browse, confirm, etc.. They are restored when returning, for
1734 * recursive calls.
1735 */
1736 save_cmdmod = cmdmod;
1737 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1738
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001739 /* "#!anything" is handled like a comment. */
1740 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1741 goto doend;
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /*
1744 * Repeat until no more command modifiers are found.
1745 */
1746 ea.cmd = *cmdlinep;
1747 for (;;)
1748 {
1749/*
1750 * 1. skip comment lines and leading white space and colons
1751 */
1752 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1753 ++ea.cmd;
1754
1755 /* in ex mode, an empty line works like :+ */
1756 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 && (getline_equal(fgetline, cookie, getexmodeline)
1758 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001759 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
1761 ea.cmd = (char_u *)"+";
1762 ex_pressedreturn = TRUE;
1763 }
1764
1765 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001766 if (*ea.cmd == '"')
1767 goto doend;
1768 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001769 {
1770 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774/*
1775 * 2. handle command modifiers.
1776 */
1777 p = ea.cmd;
1778 if (VIM_ISDIGIT(*ea.cmd))
1779 p = skipwhite(skipdigits(ea.cmd));
1780 switch (*p)
1781 {
1782 /* When adding an entry, also modify cmd_exists(). */
1783 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1784 break;
1785#ifdef FEAT_WINDOWS
1786 cmdmod.split |= WSP_ABOVE;
1787#endif
1788 continue;
1789
1790 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1791 {
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_BELOW;
1794#endif
1795 continue;
1796 }
1797 if (checkforcmd(&ea.cmd, "browse", 3))
1798 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001799#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 cmdmod.browse = TRUE;
1801#endif
1802 continue;
1803 }
1804 if (!checkforcmd(&ea.cmd, "botright", 2))
1805 break;
1806#ifdef FEAT_WINDOWS
1807 cmdmod.split |= WSP_BOT;
1808#endif
1809 continue;
1810
1811 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1812 break;
1813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1814 cmdmod.confirm = TRUE;
1815#endif
1816 continue;
1817
1818 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1819 {
1820 cmdmod.keepmarks = TRUE;
1821 continue;
1822 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001823 if (checkforcmd(&ea.cmd, "keepalt", 5))
1824 {
1825 cmdmod.keepalt = TRUE;
1826 continue;
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1829 break;
1830 cmdmod.keepjumps = TRUE;
1831 continue;
1832
1833 /* ":hide" and ":hide | cmd" are not modifiers */
1834 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1835 || *p == NUL || ends_excmd(*p))
1836 break;
1837 ea.cmd = p;
1838 cmdmod.hide = TRUE;
1839 continue;
1840
1841 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1842 {
1843 cmdmod.lockmarks = TRUE;
1844 continue;
1845 }
1846
1847 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1848 break;
1849#ifdef FEAT_WINDOWS
1850 cmdmod.split |= WSP_ABOVE;
1851#endif
1852 continue;
1853
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001854 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1855 break;
1856#ifdef FEAT_AUTOCMD
1857 if (cmdmod.save_ei == NULL)
1858 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001859 /* Set 'eventignore' to "all". Restore the
1860 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001861 cmdmod.save_ei = vim_strsave(p_ei);
1862 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001863 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001864 }
1865#endif
1866 continue;
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1869 break;
1870#ifdef FEAT_WINDOWS
1871 cmdmod.split |= WSP_BELOW;
1872#endif
1873 continue;
1874
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001875 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1876 {
1877#ifdef HAVE_SANDBOX
1878 if (!did_sandbox)
1879 ++sandbox;
1880 did_sandbox = TRUE;
1881#endif
1882 continue;
1883 }
1884 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001886 if (save_msg_silent == -1)
1887 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1890 {
1891 /* ":silent!", but not "silent !cmd" */
1892 ea.cmd = skipwhite(ea.cmd + 1);
1893 ++emsg_silent;
1894 ++did_esilent;
1895 }
1896 continue;
1897
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001898 case 't': if (checkforcmd(&p, "tab", 3))
1899 {
1900#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001901 if (vim_isdigit(*ea.cmd))
1902 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1903 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001904 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001905 ea.cmd = p;
1906#endif
1907 continue;
1908 }
1909 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 break;
1911#ifdef FEAT_WINDOWS
1912 cmdmod.split |= WSP_TOP;
1913#endif
1914 continue;
1915
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001916 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1917 break;
1918 if (save_msg_silent == -1)
1919 save_msg_silent = msg_silent;
1920 msg_silent = 0;
1921 continue;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1924 {
1925#ifdef FEAT_VERTSPLIT
1926 cmdmod.split |= WSP_VERT;
1927#endif
1928 continue;
1929 }
1930 if (!checkforcmd(&p, "verbose", 4))
1931 break;
1932 if (verbose_save < 0)
1933 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001934 if (vim_isdigit(*ea.cmd))
1935 p_verbose = atoi((char *)ea.cmd);
1936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 p_verbose = 1;
1938 ea.cmd = p;
1939 continue;
1940 }
1941 break;
1942 }
1943
1944#ifdef FEAT_EVAL
1945 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1946 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1947#else
1948 ea.skip = (if_level > 0);
1949#endif
1950
1951#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001952# ifdef FEAT_PROFILE
1953 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001954 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001955 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001956 if (getline_equal(fgetline, cookie, get_func_line))
1957 func_line_exec(getline_cookie(fgetline, cookie));
1958 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 script_line_exec();
1960 }
1961#endif
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* May go to debug mode. If this happens and the ">quit" debug command is
1964 * used, throw an interrupt exception and skip the next command. */
1965 dbg_check_breakpoint(&ea);
1966 if (!ea.skip && got_int)
1967 {
1968 ea.skip = TRUE;
1969 (void)do_intthrow(cstack);
1970 }
1971#endif
1972
1973/*
1974 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1975 *
1976 * where 'addr' is:
1977 *
1978 * % (entire file)
1979 * $ [+-NUM]
1980 * 'x [+-NUM] (where x denotes a currently defined mark)
1981 * . [+-NUM]
1982 * [+-NUM]..
1983 * NUM
1984 *
1985 * The ea.cmd pointer is updated to point to the first character following the
1986 * range spec. If an initial address is found, but no second, the upper bound
1987 * is equal to the lower.
1988 */
1989
1990 /* repeat for all ',' or ';' separated addresses */
1991 for (;;)
1992 {
1993 ea.line1 = ea.line2;
1994 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1995 ea.cmd = skipwhite(ea.cmd);
1996 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1997 if (ea.cmd == NULL) /* error detected */
1998 goto doend;
1999 if (lnum == MAXLNUM)
2000 {
2001 if (*ea.cmd == '%') /* '%' - all lines */
2002 {
2003 ++ea.cmd;
2004 ea.line1 = 1;
2005 ea.line2 = curbuf->b_ml.ml_line_count;
2006 ++ea.addr_count;
2007 }
2008 /* '*' - visual area */
2009 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2010 {
2011 pos_T *fp;
2012
2013 ++ea.cmd;
2014 if (!ea.skip)
2015 {
2016 fp = getmark('<', FALSE);
2017 if (check_mark(fp) == FAIL)
2018 goto doend;
2019 ea.line1 = fp->lnum;
2020 fp = getmark('>', FALSE);
2021 if (check_mark(fp) == FAIL)
2022 goto doend;
2023 ea.line2 = fp->lnum;
2024 ++ea.addr_count;
2025 }
2026 }
2027 }
2028 else
2029 ea.line2 = lnum;
2030 ea.addr_count++;
2031
2032 if (*ea.cmd == ';')
2033 {
2034 if (!ea.skip)
2035 curwin->w_cursor.lnum = ea.line2;
2036 }
2037 else if (*ea.cmd != ',')
2038 break;
2039 ++ea.cmd;
2040 }
2041
2042 /* One address given: set start and end lines */
2043 if (ea.addr_count == 1)
2044 {
2045 ea.line1 = ea.line2;
2046 /* ... but only implicit: really no address given */
2047 if (lnum == MAXLNUM)
2048 ea.addr_count = 0;
2049 }
2050
2051 /* Don't leave the cursor on an illegal line (caused by ';') */
2052 check_cursor_lnum();
2053
2054/*
2055 * 4. parse command
2056 */
2057
2058 /*
2059 * Skip ':' and any white space
2060 */
2061 ea.cmd = skipwhite(ea.cmd);
2062 while (*ea.cmd == ':')
2063 ea.cmd = skipwhite(ea.cmd + 1);
2064
2065 /*
2066 * If we got a line, but no command, then go to the line.
2067 * If we find a '|' or '\n' we set ea.nextcmd.
2068 */
2069 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2070 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2071 {
2072 /*
2073 * strange vi behaviour:
2074 * ":3" jumps to line 3
2075 * ":3|..." prints line 3
2076 * ":|" prints current line
2077 */
2078 if (ea.skip) /* skip this if inside :if */
2079 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002080 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 ea.cmdidx = CMD_print;
2083 ea.argt = RANGE+COUNT+TRLBAR;
2084 if ((errormsg = invalid_range(&ea)) == NULL)
2085 {
2086 correct_range(&ea);
2087 ex_print(&ea);
2088 }
2089 }
2090 else if (ea.addr_count != 0)
2091 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002092 if (ea.line2 > curbuf->b_ml.ml_line_count)
2093 {
2094 /* With '-' in 'cpoptions' a line number past the file is an
2095 * error, otherwise put it at the end of the file. */
2096 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2097 ea.line2 = -1;
2098 else
2099 ea.line2 = curbuf->b_ml.ml_line_count;
2100 }
2101
2102 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002103 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 else
2105 {
2106 if (ea.line2 == 0)
2107 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
2109 curwin->w_cursor.lnum = ea.line2;
2110 beginline(BL_SOL | BL_FIX);
2111 }
2112 }
2113 goto doend;
2114 }
2115
2116 /* Find the command and let "p" point to after it. */
2117 p = find_command(&ea, NULL);
2118
2119#ifdef FEAT_USR_CMDS
2120 if (p == NULL)
2121 {
2122 if (!ea.skip)
2123 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2124 goto doend;
2125 }
2126 /* Check for wrong commands. */
2127 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2128 {
2129 errormsg = uc_fun_cmd();
2130 goto doend;
2131 }
2132#endif
2133 if (ea.cmdidx == CMD_SIZE)
2134 {
2135 if (!ea.skip)
2136 {
2137 STRCPY(IObuff, _("E492: Not an editor command"));
2138 if (!sourcing)
2139 {
2140 STRCAT(IObuff, ": ");
2141 STRNCAT(IObuff, *cmdlinep, 40);
2142 }
2143 errormsg = IObuff;
2144 }
2145 goto doend;
2146 }
2147
2148 ni = (
2149#ifdef FEAT_USR_CMDS
2150 !USER_CMDIDX(ea.cmdidx) &&
2151#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002152 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002153#ifdef HAVE_EX_SCRIPT_NI
2154 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2155#endif
2156 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158#ifndef FEAT_EVAL
2159 /*
2160 * When the expression evaluation is disabled, recognize the ":if" and
2161 * ":endif" commands and ignore everything in between it.
2162 */
2163 if (ea.cmdidx == CMD_if)
2164 ++if_level;
2165 if (if_level)
2166 {
2167 if (ea.cmdidx == CMD_endif)
2168 --if_level;
2169 goto doend;
2170 }
2171
2172#endif
2173
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002174 /* forced commands */
2175 if (*p == '!' && ea.cmdidx != CMD_substitute
2176 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 ++p;
2179 ea.forceit = TRUE;
2180 }
2181 else
2182 ea.forceit = FALSE;
2183
2184/*
2185 * 5. parse arguments
2186 */
2187#ifdef FEAT_USR_CMDS
2188 if (!USER_CMDIDX(ea.cmdidx))
2189#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002190 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 if (!ea.skip)
2193 {
2194#ifdef HAVE_SANDBOX
2195 if (sandbox != 0 && !(ea.argt & SBOXOK))
2196 {
2197 /* Command not allowed in sandbox. */
2198 errormsg = (char_u *)_(e_sandbox);
2199 goto doend;
2200 }
2201#endif
2202 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2203 {
2204 /* Command not allowed in non-'modifiable' buffer */
2205 errormsg = (char_u *)_(e_modifiable);
2206 goto doend;
2207 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002208
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002209 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# ifdef FEAT_USR_CMDS
2211 && !USER_CMDIDX(ea.cmdidx)
2212# endif
2213 )
2214 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002215 /* Command not allowed when editing the command line. */
2216#ifdef FEAT_CMDWIN
2217 if (cmdwin_type != 0)
2218 errormsg = (char_u *)_(e_cmdwin);
2219 else
2220#endif
2221 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto doend;
2223 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002225 /* Disallow editing another buffer when "curbuf_lock" is set.
2226 * Do allow ":edit" (check for argument later).
2227 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002228 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002229 && ea.cmdidx != CMD_edit
2230 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231# ifdef FEAT_USR_CMDS
2232 && !USER_CMDIDX(ea.cmdidx)
2233# endif
2234 && curbuf_locked())
2235 goto doend;
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2239 {
2240 /* no range allowed */
2241 errormsg = (char_u *)_(e_norange);
2242 goto doend;
2243 }
2244 }
2245
2246 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2247 {
2248 errormsg = (char_u *)_(e_nobang);
2249 goto doend;
2250 }
2251
2252 /*
2253 * Don't complain about the range if it is not used
2254 * (could happen if line_count is accidentally set to 0).
2255 */
2256 if (!ea.skip && !ni)
2257 {
2258 /*
2259 * If the range is backwards, ask for confirmation and, if given, swap
2260 * ea.line1 & ea.line2 so it's forwards again.
2261 * When global command is busy, don't ask, will fail below.
2262 */
2263 if (!global_busy && ea.line1 > ea.line2)
2264 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002265 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002267 if (sourcing || exmode_active)
2268 {
2269 errormsg = (char_u *)_("E493: Backwards range given");
2270 goto doend;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (ask_yesno((char_u *)
2273 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002274 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 lnum = ea.line1;
2277 ea.line1 = ea.line2;
2278 ea.line2 = lnum;
2279 }
2280 if ((errormsg = invalid_range(&ea)) != NULL)
2281 goto doend;
2282 }
2283
2284 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2285 ea.line2 = 1;
2286
2287 correct_range(&ea);
2288
2289#ifdef FEAT_FOLDING
2290 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2291 {
2292 /* Put the first line at the start of a closed fold, put the last line
2293 * at the end of a closed fold. */
2294 (void)hasFolding(ea.line1, &ea.line1, NULL);
2295 (void)hasFolding(ea.line2, NULL, &ea.line2);
2296 }
2297#endif
2298
2299#ifdef FEAT_QUICKFIX
2300 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002301 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * option here, so things like % get expanded.
2303 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002304 p = replace_makeprg(&ea, p, cmdlinep);
2305 if (p == NULL)
2306 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308
2309 /*
2310 * Skip to start of argument.
2311 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2312 */
2313 if (ea.cmdidx == CMD_bang)
2314 ea.arg = p;
2315 else
2316 ea.arg = skipwhite(p);
2317
2318 /*
2319 * Check for "++opt=val" argument.
2320 * Must be first, allow ":w ++enc=utf8 !cmd"
2321 */
2322 if (ea.argt & ARGOPT)
2323 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2324 if (getargopt(&ea) == FAIL && !ni)
2325 {
2326 errormsg = (char_u *)_(e_invarg);
2327 goto doend;
2328 }
2329
2330 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2331 {
2332 if (*ea.arg == '>') /* append */
2333 {
2334 if (*++ea.arg != '>') /* typed wrong */
2335 {
2336 errormsg = (char_u *)_("E494: Use w or w>>");
2337 goto doend;
2338 }
2339 ea.arg = skipwhite(ea.arg + 1);
2340 ea.append = TRUE;
2341 }
2342 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2343 {
2344 ++ea.arg;
2345 ea.usefilter = TRUE;
2346 }
2347 }
2348
2349 if (ea.cmdidx == CMD_read)
2350 {
2351 if (ea.forceit)
2352 {
2353 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2354 ea.forceit = FALSE;
2355 }
2356 else if (*ea.arg == '!') /* :r !filter */
2357 {
2358 ++ea.arg;
2359 ea.usefilter = TRUE;
2360 }
2361 }
2362
2363 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2364 {
2365 ea.amount = 1;
2366 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2367 {
2368 ++ea.arg;
2369 ++ea.amount;
2370 }
2371 ea.arg = skipwhite(ea.arg);
2372 }
2373
2374 /*
2375 * Check for "+command" argument, before checking for next command.
2376 * Don't do this for ":read !cmd" and ":write !cmd".
2377 */
2378 if ((ea.argt & EDITCMD) && !ea.usefilter)
2379 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2380
2381 /*
2382 * Check for '|' to separate commands and '"' to start comments.
2383 * Don't do this for ":read !cmd" and ":write !cmd".
2384 */
2385 if ((ea.argt & TRLBAR) && !ea.usefilter)
2386 separate_nextcmd(&ea);
2387
2388 /*
2389 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2391 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 else if (ea.cmdidx == CMD_bang
2394 || ea.cmdidx == CMD_global
2395 || ea.cmdidx == CMD_vglobal
2396 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 for (p = ea.arg; *p; ++p)
2399 {
2400 /* Remove one backslash before a newline, so that it's possible to
2401 * pass a newline to the shell and also a newline that is preceded
2402 * with a backslash. This makes it impossible to end a shell
2403 * command in a backslash, but that doesn't appear useful.
2404 * Halving the number of backslashes is incompatible with previous
2405 * versions. */
2406 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002407 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 else if (*p == '\n')
2409 {
2410 ea.nextcmd = p + 1;
2411 *p = NUL;
2412 break;
2413 }
2414 }
2415 }
2416
2417 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2418 {
2419 ea.line1 = 1;
2420 ea.line2 = curbuf->b_ml.ml_line_count;
2421 }
2422
2423 /* accept numbered register only when no count allowed (:put) */
2424 if ( (ea.argt & REGSTR)
2425 && *ea.arg != NUL
2426#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* Do not allow register = for user commands */
2428 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
2430 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2431 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002432#ifndef FEAT_CLIPBOARD
2433 /* check these explicitly for a more specific error message */
2434 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002436 errormsg = (char_u *)_(e_invalidreg);
2437 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002440 if (
2441#ifdef FEAT_USR_CMDS
2442 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2443 && USER_CMDIDX(ea.cmdidx)))
2444#else
2445 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2446#endif
2447 )
2448 {
2449 ea.regname = *ea.arg++;
2450#ifdef FEAT_EVAL
2451 /* for '=' register: accept the rest of the line as an expression */
2452 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2453 {
2454 set_expr_line(vim_strsave(ea.arg));
2455 ea.arg += STRLEN(ea.arg);
2456 }
2457#endif
2458 ea.arg = skipwhite(ea.arg);
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461
2462 /*
2463 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2464 * count, it's a buffer name.
2465 */
2466 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2467 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2468 || vim_iswhite(*p)))
2469 {
2470 n = getdigits(&ea.arg);
2471 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002472 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 errormsg = (char_u *)_(e_zerocount);
2475 goto doend;
2476 }
2477 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2478 {
2479 ea.line2 = n;
2480 if (ea.addr_count == 0)
2481 ea.addr_count = 1;
2482 }
2483 else
2484 {
2485 ea.line1 = ea.line2;
2486 ea.line2 += n - 1;
2487 ++ea.addr_count;
2488 /*
2489 * Be vi compatible: no error message for out of range.
2490 */
2491 if (ea.line2 > curbuf->b_ml.ml_line_count)
2492 ea.line2 = curbuf->b_ml.ml_line_count;
2493 }
2494 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002495
2496 /*
2497 * Check for flags: 'l', 'p' and '#'.
2498 */
2499 if (ea.argt & EXFLAGS)
2500 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002502 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002503 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 errormsg = (char_u *)_(e_trailing);
2506 goto doend;
2507 }
2508
2509 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2510 {
2511 errormsg = (char_u *)_(e_argreq);
2512 goto doend;
2513 }
2514
2515#ifdef FEAT_EVAL
2516 /*
2517 * Skip the command when it's not going to be executed.
2518 * The commands like :if, :endif, etc. always need to be executed.
2519 * Also make an exception for commands that handle a trailing command
2520 * themselves.
2521 */
2522 if (ea.skip)
2523 {
2524 switch (ea.cmdidx)
2525 {
2526 /* commands that need evaluation */
2527 case CMD_while:
2528 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002529 case CMD_for:
2530 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 case CMD_if:
2532 case CMD_elseif:
2533 case CMD_else:
2534 case CMD_endif:
2535 case CMD_try:
2536 case CMD_catch:
2537 case CMD_finally:
2538 case CMD_endtry:
2539 case CMD_function:
2540 break;
2541
2542 /* Commands that handle '|' themselves. Check: A command should
2543 * either have the TRLBAR flag, appear in this list or appear in
2544 * the list at ":help :bar". */
2545 case CMD_aboveleft:
2546 case CMD_and:
2547 case CMD_belowright:
2548 case CMD_botright:
2549 case CMD_browse:
2550 case CMD_call:
2551 case CMD_confirm:
2552 case CMD_delfunction:
2553 case CMD_djump:
2554 case CMD_dlist:
2555 case CMD_dsearch:
2556 case CMD_dsplit:
2557 case CMD_echo:
2558 case CMD_echoerr:
2559 case CMD_echomsg:
2560 case CMD_echon:
2561 case CMD_execute:
2562 case CMD_help:
2563 case CMD_hide:
2564 case CMD_ijump:
2565 case CMD_ilist:
2566 case CMD_isearch:
2567 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002568 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 case CMD_keepjumps:
2570 case CMD_keepmarks:
2571 case CMD_leftabove:
2572 case CMD_let:
2573 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002574 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002576 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 case CMD_perl:
2578 case CMD_psearch:
2579 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002580 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002581 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_return:
2583 case CMD_rightbelow:
2584 case CMD_ruby:
2585 case CMD_silent:
2586 case CMD_smagic:
2587 case CMD_snomagic:
2588 case CMD_substitute:
2589 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002590 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 case CMD_tcl:
2592 case CMD_throw:
2593 case CMD_tilde:
2594 case CMD_topleft:
2595 case CMD_unlet:
2596 case CMD_verbose:
2597 case CMD_vertical:
2598 break;
2599
2600 default: goto doend;
2601 }
2602 }
2603#endif
2604
2605 if (ea.argt & XFILE)
2606 {
2607 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2608 goto doend;
2609 }
2610
2611#ifdef FEAT_LISTCMDS
2612 /*
2613 * Accept buffer name. Cannot be used at the same time with a buffer
2614 * number. Don't do this for a user command.
2615 */
2616 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2617# ifdef FEAT_USR_CMDS
2618 && !USER_CMDIDX(ea.cmdidx)
2619# endif
2620 )
2621 {
2622 /*
2623 * :bdelete, :bwipeout and :bunload take several arguments, separated
2624 * by spaces: find next space (skipping over escaped characters).
2625 * The others take one argument: ignore trailing spaces.
2626 */
2627 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2628 || ea.cmdidx == CMD_bunload)
2629 p = skiptowhite_esc(ea.arg);
2630 else
2631 {
2632 p = ea.arg + STRLEN(ea.arg);
2633 while (p > ea.arg && vim_iswhite(p[-1]))
2634 --p;
2635 }
2636 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2637 if (ea.line2 < 0) /* failed */
2638 goto doend;
2639 ea.addr_count = 1;
2640 ea.arg = skipwhite(p);
2641 }
2642#endif
2643
2644/*
2645 * 6. switch on command name
2646 *
2647 * The "ea" structure holds the arguments that can be used.
2648 */
2649 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 ea.cookie = cookie;
2652#ifdef FEAT_EVAL
2653 ea.cstack = cstack;
2654#endif
2655
2656#ifdef FEAT_USR_CMDS
2657 if (USER_CMDIDX(ea.cmdidx))
2658 {
2659 /*
2660 * Execute a user-defined command.
2661 */
2662 do_ucmd(&ea);
2663 }
2664 else
2665#endif
2666 {
2667 /*
2668 * Call the function to execute the command.
2669 */
2670 ea.errmsg = NULL;
2671 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2672 if (ea.errmsg != NULL)
2673 errormsg = (char_u *)_(ea.errmsg);
2674 }
2675
2676#ifdef FEAT_EVAL
2677 /*
2678 * If the command just executed called do_cmdline(), any throw or ":return"
2679 * or ":finish" encountered there must also check the cstack of the still
2680 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2681 * exception, or reanimate a returned function or finished script file and
2682 * return or finish it again.
2683 */
2684 if (need_rethrow)
2685 do_throw(cstack);
2686 else if (check_cstack)
2687 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002688 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002690 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 && current_func_returned())
2692 do_return(&ea, TRUE, FALSE, NULL);
2693 }
2694 need_rethrow = check_cstack = FALSE;
2695#endif
2696
2697doend:
2698 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2699 curwin->w_cursor.lnum = 1;
2700
2701 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2702 {
2703 if (sourcing)
2704 {
2705 if (errormsg != IObuff)
2706 {
2707 STRCPY(IObuff, errormsg);
2708 errormsg = IObuff;
2709 }
2710 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002711 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 emsg(errormsg);
2714 }
2715#ifdef FEAT_EVAL
2716 do_errthrow(cstack,
2717 (ea.cmdidx != CMD_SIZE
2718# ifdef FEAT_USR_CMDS
2719 && !USER_CMDIDX(ea.cmdidx)
2720# endif
2721 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2722#endif
2723
2724 if (verbose_save >= 0)
2725 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002726#ifdef FEAT_AUTOCMD
2727 if (cmdmod.save_ei != NULL)
2728 {
2729 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002730 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2731 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002732 free_string_option(cmdmod.save_ei);
2733 }
2734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 cmdmod = save_cmdmod;
2737
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002738 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 /* messages could be enabled for a serious error, need to check if the
2741 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002742 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002743 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 emsg_silent -= did_esilent;
2745 if (emsg_silent < 0)
2746 emsg_silent = 0;
2747 /* Restore msg_scroll, it's set by file I/O commands, even when no
2748 * message is actually displayed. */
2749 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002750
2751 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2752 * somewhere in the line. Put it back in the first column. */
2753 if (redirecting())
2754 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002757#ifdef HAVE_SANDBOX
2758 if (did_sandbox)
2759 --sandbox;
2760#endif
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2763 ea.nextcmd = NULL;
2764
2765#ifdef FEAT_EVAL
2766 --ex_nesting_level;
2767#endif
2768
2769 return ea.nextcmd;
2770}
2771#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002772 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
2774
2775/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002776 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 * If there is a match advance "pp" to the argument and return TRUE.
2778 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002779 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002781 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 char *cmd; /* name of command */
2783 int len; /* required length */
2784{
2785 int i;
2786
2787 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002788 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 break;
2790 if (i >= len && !isalpha((*pp)[i]))
2791 {
2792 *pp = skipwhite(*pp + i);
2793 return TRUE;
2794 }
2795 return FALSE;
2796}
2797
2798/*
2799 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002800 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002802 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * Returns NULL for an ambiguous user command.
2804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 static char_u *
2806find_command(eap, full)
2807 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002808 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 int len;
2811 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002812 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814 /*
2815 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002816 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 * - the 'k' command can directly be followed by any character.
2818 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2819 * but :sre[wind] is another command, as are :scrip[tnames],
2820 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002821 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
2823 p = eap->cmd;
2824 if (*p == 'k')
2825 {
2826 eap->cmdidx = CMD_k;
2827 ++p;
2828 }
2829 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002830 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2831 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 || p[1] == 'g'
2833 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2834 || p[1] == 'I'
2835 || (p[1] == 'r' && p[2] != 'e')))
2836 {
2837 eap->cmdidx = CMD_substitute;
2838 ++p;
2839 }
2840 else
2841 {
2842 while (ASCII_ISALPHA(*p))
2843 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002844 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002845 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002846 while (ASCII_ISALNUM(*p))
2847 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 /* check for non-alpha command */
2850 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2851 ++p;
2852 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002853 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2854 {
2855 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2856 * :delete with the 'l' flag. Same for 'p'. */
2857 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002858 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002859 break;
2860 if (i == len - 1)
2861 {
2862 --len;
2863 if (p[-1] == 'l')
2864 eap->flags |= EXFLAG_LIST;
2865 else
2866 eap->flags |= EXFLAG_PRINT;
2867 }
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 if (ASCII_ISLOWER(*eap->cmd))
2871 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2872 else
2873 eap->cmdidx = cmdidxs[26];
2874
2875 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2876 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2877 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2878 (size_t)len) == 0)
2879 {
2880#ifdef FEAT_EVAL
2881 if (full != NULL
2882 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2883 *full = TRUE;
2884#endif
2885 break;
2886 }
2887
2888#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002889 /* Look for a user defined command as a last resort. Let ":Print" be
2890 * overruled by a user defined command. */
2891 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2892 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002894 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 while (ASCII_ISALNUM(*p))
2896 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002897 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002900 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 eap->cmdidx = CMD_SIZE;
2902 }
2903
2904 return p;
2905}
2906
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002907#ifdef FEAT_USR_CMDS
2908/*
2909 * Search for a user command that matches "eap->cmd".
2910 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2911 * Return a pointer to just after the command.
2912 * Return NULL if there is no matching command.
2913 */
2914 static char_u *
2915find_ucmd(eap, p, full, xp, compl)
2916 exarg_T *eap;
2917 char_u *p; /* end of the command (possibly including count) */
2918 int *full; /* set to TRUE for a full match */
2919 expand_T *xp; /* used for completion, NULL otherwise */
2920 int *compl; /* completion flags or NULL */
2921{
2922 int len = (int)(p - eap->cmd);
2923 int j, k, matchlen = 0;
2924 ucmd_T *uc;
2925 int found = FALSE;
2926 int possible = FALSE;
2927 char_u *cp, *np; /* Point into typed cmd and test name */
2928 garray_T *gap;
2929 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2930 only full match global is accepted. */
2931
2932 /*
2933 * Look for buffer-local user commands first, then global ones.
2934 */
2935 gap = &curbuf->b_ucmds;
2936 for (;;)
2937 {
2938 for (j = 0; j < gap->ga_len; ++j)
2939 {
2940 uc = USER_CMD_GA(gap, j);
2941 cp = eap->cmd;
2942 np = uc->uc_name;
2943 k = 0;
2944 while (k < len && *np != NUL && *cp++ == *np++)
2945 k++;
2946 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2947 {
2948 /* If finding a second match, the command is ambiguous. But
2949 * not if a buffer-local command wasn't a full match and a
2950 * global command is a full match. */
2951 if (k == len && found && *np != NUL)
2952 {
2953 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002954 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955 amb_local = TRUE;
2956 }
2957
2958 if (!found || (k == len && *np == NUL))
2959 {
2960 /* If we matched up to a digit, then there could
2961 * be another command including the digit that we
2962 * should use instead.
2963 */
2964 if (k == len)
2965 found = TRUE;
2966 else
2967 possible = TRUE;
2968
2969 if (gap == &ucmds)
2970 eap->cmdidx = CMD_USER;
2971 else
2972 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002973 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002974 eap->useridx = j;
2975
2976# ifdef FEAT_CMDL_COMPL
2977 if (compl != NULL)
2978 *compl = uc->uc_compl;
2979# ifdef FEAT_EVAL
2980 if (xp != NULL)
2981 {
2982 xp->xp_arg = uc->uc_compl_arg;
2983 xp->xp_scriptID = uc->uc_scriptID;
2984 }
2985# endif
2986# endif
2987 /* Do not search for further abbreviations
2988 * if this is an exact match. */
2989 matchlen = k;
2990 if (k == len && *np == NUL)
2991 {
2992 if (full != NULL)
2993 *full = TRUE;
2994 amb_local = FALSE;
2995 break;
2996 }
2997 }
2998 }
2999 }
3000
3001 /* Stop if we found a full match or searched all. */
3002 if (j < gap->ga_len || gap == &ucmds)
3003 break;
3004 gap = &ucmds;
3005 }
3006
3007 /* Only found ambiguous matches. */
3008 if (amb_local)
3009 {
3010 if (xp != NULL)
3011 xp->xp_context = EXPAND_UNSUCCESSFUL;
3012 return NULL;
3013 }
3014
3015 /* The match we found may be followed immediately by a number. Move "p"
3016 * back to point to it. */
3017 if (found || possible)
3018 return p + (matchlen - len);
3019 return p;
3020}
3021#endif
3022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003024static struct cmdmod
3025{
3026 char *name;
3027 int minlen;
3028 int has_count; /* :123verbose :3tab */
3029} cmdmods[] = {
3030 {"aboveleft", 3, FALSE},
3031 {"belowright", 3, FALSE},
3032 {"botright", 2, FALSE},
3033 {"browse", 3, FALSE},
3034 {"confirm", 4, FALSE},
3035 {"hide", 3, FALSE},
3036 {"keepalt", 5, FALSE},
3037 {"keepjumps", 5, FALSE},
3038 {"keepmarks", 3, FALSE},
3039 {"leftabove", 5, FALSE},
3040 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003041 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003042 {"rightbelow", 6, FALSE},
3043 {"sandbox", 3, FALSE},
3044 {"silent", 3, FALSE},
3045 {"tab", 3, TRUE},
3046 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003047 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003048 {"verbose", 4, TRUE},
3049 {"vertical", 4, FALSE},
3050};
3051
3052/*
3053 * Return length of a command modifier (including optional count).
3054 * Return zero when it's not a modifier.
3055 */
3056 int
3057modifier_len(cmd)
3058 char_u *cmd;
3059{
3060 int i, j;
3061 char_u *p = cmd;
3062
3063 if (VIM_ISDIGIT(*cmd))
3064 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003065 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003066 {
3067 for (j = 0; p[j] != NUL; ++j)
3068 if (p[j] != cmdmods[i].name[j])
3069 break;
3070 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3071 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003072 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003073 }
3074 return 0;
3075}
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077/*
3078 * Return > 0 if an Ex command "name" exists.
3079 * Return 2 if there is an exact match.
3080 * Return 3 if there is an ambiguous match.
3081 */
3082 int
3083cmd_exists(name)
3084 char_u *name;
3085{
3086 exarg_T ea;
3087 int full = FALSE;
3088 int i;
3089 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003090 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003093 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 for (j = 0; name[j] != NUL; ++j)
3096 if (name[j] != cmdmods[i].name[j])
3097 break;
3098 if (name[j] == NUL && j >= cmdmods[i].minlen)
3099 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3100 }
3101
Bram Moolenaara9587612006-05-04 21:47:50 +00003102 /* Check built-in commands and user defined commands.
3103 * For ":2match" and ":3match" we need to skip the number. */
3104 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003106 p = find_command(&ea, &full);
3107 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003109 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3110 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003111 if (*skipwhite(p) != NUL)
3112 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3114}
3115#endif
3116
3117/*
3118 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3119 * we don't need/want deleted. Maybe this could be done better if we didn't
3120 * repeat all this stuff. The only problem is that they may not stay
3121 * perfectly compatible with each other, but then the command line syntax
3122 * probably won't change that much -- webb.
3123 */
3124 char_u *
3125set_one_cmd_context(xp, buff)
3126 expand_T *xp;
3127 char_u *buff; /* buffer for command string */
3128{
3129 char_u *p;
3130 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 int len = 0;
3132 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3134 int compl = EXPAND_NOTHING;
3135#endif
3136#ifdef FEAT_CMDL_COMPL
3137 int delim;
3138#endif
3139 int forceit = FALSE;
3140 int usefilter = FALSE; /* filter instead of file name */
3141
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003142 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 xp->xp_pattern = buff;
3144 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003145 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147/*
3148 * 2. skip comment lines and leading space, colons or bars
3149 */
3150 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3151 ;
3152 xp->xp_pattern = cmd;
3153
3154 if (*cmd == NUL)
3155 return NULL;
3156 if (*cmd == '"') /* ignore comment lines */
3157 {
3158 xp->xp_context = EXPAND_NOTHING;
3159 return NULL;
3160 }
3161
3162/*
3163 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3164 */
3165 cmd = skip_range(cmd, &xp->xp_context);
3166
3167/*
3168 * 4. parse command
3169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 xp->xp_pattern = cmd;
3171 if (*cmd == NUL)
3172 return NULL;
3173 if (*cmd == '"')
3174 {
3175 xp->xp_context = EXPAND_NOTHING;
3176 return NULL;
3177 }
3178
3179 if (*cmd == '|' || *cmd == '\n')
3180 return cmd + 1; /* There's another command */
3181
3182 /*
3183 * Isolate the command and search for it in the command table.
3184 * Exceptions:
3185 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003186 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3188 */
3189 if (*cmd == 'k' && cmd[1] != 'e')
3190 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 p = cmd + 1;
3193 }
3194 else
3195 {
3196 p = cmd;
3197 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3198 ++p;
3199 /* check for non-alpha command */
3200 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3201 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003202 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003204 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 xp->xp_context = EXPAND_UNSUCCESSFUL;
3207 return NULL;
3208 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003209 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003210 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3211 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3212 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 break;
3214
3215#ifdef FEAT_USR_CMDS
3216 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3218 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220 }
3221
3222 /*
3223 * If the cursor is touching the command, and it ends in an alpha-numeric
3224 * character, complete the command name.
3225 */
3226 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3227 return NULL;
3228
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3232 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 p = cmd + 1;
3235 }
3236#ifdef FEAT_USR_CMDS
3237 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3238 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 ea.cmd = cmd;
3240 p = find_ucmd(&ea, p, NULL, xp,
3241# if defined(FEAT_CMDL_COMPL)
3242 &compl
3243# else
3244 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003247 if (p == NULL)
3248 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250#endif
3251 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 /* Not still touching the command and it was an illegal one */
3255 xp->xp_context = EXPAND_UNSUCCESSFUL;
3256 return NULL;
3257 }
3258
3259 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3260
3261 if (*p == '!') /* forced commands */
3262 {
3263 forceit = TRUE;
3264 ++p;
3265 }
3266
3267/*
3268 * 5. parse arguments
3269 */
3270#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003273 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 arg = skipwhite(p);
3276
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 if (*arg == '>') /* append */
3280 {
3281 if (*++arg == '>')
3282 ++arg;
3283 arg = skipwhite(arg);
3284 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 ++arg;
3288 usefilter = TRUE;
3289 }
3290 }
3291
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 usefilter = forceit; /* :r! filter if forced */
3295 if (*arg == '!') /* :r !filter */
3296 {
3297 ++arg;
3298 usefilter = TRUE;
3299 }
3300 }
3301
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 while (*arg == *cmd) /* allow any number of '>' or '<' */
3305 ++arg;
3306 arg = skipwhite(arg);
3307 }
3308
3309 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003310 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 /* Check if we're in the +command */
3313 p = arg + 1;
3314 arg = skip_cmd_arg(arg, FALSE);
3315
3316 /* Still touching the command after '+'? */
3317 if (*arg == NUL)
3318 return p;
3319
3320 /* Skip space(s) after +command to get to the real argument */
3321 arg = skipwhite(arg);
3322 }
3323
3324 /*
3325 * Check for '|' to separate commands and '"' to start comments.
3326 * Don't do this for ":read !cmd" and ":write !cmd".
3327 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
3330 p = arg;
3331 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003332 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 p += 2;
3334 while (*p)
3335 {
3336 if (*p == Ctrl_V)
3337 {
3338 if (p[1] != NUL)
3339 ++p;
3340 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 || *p == '|' || *p == '\n')
3343 {
3344 if (*(p - 1) != '\\')
3345 {
3346 if (*p == '|' || *p == '\n')
3347 return p + 1;
3348 return NULL; /* It's a comment */
3349 }
3350 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003351 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353 }
3354
3355 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003356 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 vim_strchr((char_u *)"|\"", *arg) == NULL)
3358 return NULL;
3359
3360 /* Find start of last argument (argument just before cursor): */
3361 p = buff + STRLEN(buff);
3362 while (p != arg && *p != ' ' && *p != TAB)
3363 p--;
3364 if (*p == ' ' || *p == TAB)
3365 p++;
3366 xp->xp_pattern = p;
3367
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003368 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370 int c;
3371 int in_quote = FALSE;
3372 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /*
3375 * Allow spaces within back-quotes to count as part of the argument
3376 * being expanded.
3377 */
3378 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003379 p = xp->xp_pattern;
3380 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003382#ifdef FEAT_MBYTE
3383 if (has_mbyte)
3384 c = mb_ptr2char(p);
3385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003387 c = *p;
3388 if (c == '\\' && p[1] != NUL)
3389 ++p;
3390 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
3392 if (!in_quote)
3393 {
3394 xp->xp_pattern = p;
3395 bow = p + 1;
3396 }
3397 in_quote = !in_quote;
3398 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003399 /* An argument can contain just about everything, except
3400 * characters that end the command and white space. */
3401 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003402#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003403 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003404#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003405 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003406 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003407 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003408 while (*p != NUL)
3409 {
3410#ifdef FEAT_MBYTE
3411 if (has_mbyte)
3412 c = mb_ptr2char(p);
3413 else
3414#endif
3415 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003416 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003417 break;
3418#ifdef FEAT_MBYTE
3419 if (has_mbyte)
3420 len = (*mb_ptr2len)(p);
3421 else
3422#endif
3423 len = 1;
3424 mb_ptr_adv(p);
3425 }
3426 if (in_quote)
3427 bow = p;
3428 else
3429 xp->xp_pattern = p;
3430 p -= len;
3431 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003432 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434
3435 /*
3436 * If we are still inside the quotes, and we passed a space, just
3437 * expand from there.
3438 */
3439 if (bow != NULL && in_quote)
3440 xp->xp_pattern = bow;
3441 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003442
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003443 /* For a shell command more chars need to be escaped. */
3444 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003445 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003446#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003447 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003448#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003449 /* When still after the command name expand executables. */
3450 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003451 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /* Check for environment variable */
3455 if (*xp->xp_pattern == '$'
3456#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3457 || *xp->xp_pattern == '%'
3458#endif
3459 )
3460 {
3461 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3462 if (!vim_isIDc(*p))
3463 break;
3464 if (*p == NUL)
3465 {
3466 xp->xp_context = EXPAND_ENV_VARS;
3467 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003468#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3469 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003470 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003471 compl = EXPAND_ENV_VARS;
3472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 }
3475 }
3476
3477/*
3478 * 6. switch on command name
3479 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003480 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003482 case CMD_find:
3483 case CMD_sfind:
3484 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003485 if (xp->xp_context == EXPAND_FILES)
3486 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 case CMD_cd:
3489 case CMD_chdir:
3490 case CMD_lcd:
3491 case CMD_lchdir:
3492 if (xp->xp_context == EXPAND_FILES)
3493 xp->xp_context = EXPAND_DIRECTORIES;
3494 break;
3495 case CMD_help:
3496 xp->xp_context = EXPAND_HELP;
3497 xp->xp_pattern = arg;
3498 break;
3499
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003500 /* Command modifiers: return the argument.
3501 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003503 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 case CMD_belowright:
3505 case CMD_botright:
3506 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003507 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003509 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 case CMD_folddoclosed:
3511 case CMD_folddoopen:
3512 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003513 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 case CMD_keepjumps:
3515 case CMD_keepmarks:
3516 case CMD_leftabove:
3517 case CMD_lockmarks:
3518 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003519 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003521 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 case CMD_topleft:
3523 case CMD_verbose:
3524 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003525 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 return arg;
3527
Bram Moolenaar4f688582007-07-24 12:34:30 +00003528#ifdef FEAT_CMDL_COMPL
3529# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 case CMD_match:
3531 if (*arg == NUL || !ends_excmd(*arg))
3532 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003533 /* also complete "None" */
3534 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 arg = skipwhite(skiptowhite(arg));
3536 if (*arg != NUL)
3537 {
3538 xp->xp_context = EXPAND_NOTHING;
3539 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3540 }
3541 }
3542 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
3546 * All completion for the +cmdline_compl feature goes here.
3547 */
3548
3549# ifdef FEAT_USR_CMDS
3550 case CMD_command:
3551 /* Check for attributes */
3552 while (*arg == '-')
3553 {
3554 arg++; /* Skip "-" */
3555 p = skiptowhite(arg);
3556 if (*p == NUL)
3557 {
3558 /* Cursor is still in the attribute */
3559 p = vim_strchr(arg, '=');
3560 if (p == NULL)
3561 {
3562 /* No "=", so complete attribute names */
3563 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3564 xp->xp_pattern = arg;
3565 return NULL;
3566 }
3567
3568 /* For the -complete and -nargs attributes, we complete
3569 * their arguments as well.
3570 */
3571 if (STRNICMP(arg, "complete", p - arg) == 0)
3572 {
3573 xp->xp_context = EXPAND_USER_COMPLETE;
3574 xp->xp_pattern = p + 1;
3575 return NULL;
3576 }
3577 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3578 {
3579 xp->xp_context = EXPAND_USER_NARGS;
3580 xp->xp_pattern = p + 1;
3581 return NULL;
3582 }
3583 return NULL;
3584 }
3585 arg = skipwhite(p);
3586 }
3587
3588 /* After the attributes comes the new command name */
3589 p = skiptowhite(arg);
3590 if (*p == NUL)
3591 {
3592 xp->xp_context = EXPAND_USER_COMMANDS;
3593 xp->xp_pattern = arg;
3594 break;
3595 }
3596
3597 /* And finally comes a normal command */
3598 return skipwhite(p);
3599
3600 case CMD_delcommand:
3601 xp->xp_context = EXPAND_USER_COMMANDS;
3602 xp->xp_pattern = arg;
3603 break;
3604# endif
3605
3606 case CMD_global:
3607 case CMD_vglobal:
3608 delim = *arg; /* get the delimiter */
3609 if (delim)
3610 ++arg; /* skip delimiter if there is one */
3611
3612 while (arg[0] != NUL && arg[0] != delim)
3613 {
3614 if (arg[0] == '\\' && arg[1] != NUL)
3615 ++arg;
3616 ++arg;
3617 }
3618 if (arg[0] != NUL)
3619 return arg + 1;
3620 break;
3621 case CMD_and:
3622 case CMD_substitute:
3623 delim = *arg;
3624 if (delim)
3625 {
3626 /* skip "from" part */
3627 ++arg;
3628 arg = skip_regexp(arg, delim, p_magic, NULL);
3629 }
3630 /* skip "to" part */
3631 while (arg[0] != NUL && arg[0] != delim)
3632 {
3633 if (arg[0] == '\\' && arg[1] != NUL)
3634 ++arg;
3635 ++arg;
3636 }
3637 if (arg[0] != NUL) /* skip delimiter */
3638 ++arg;
3639 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3640 ++arg;
3641 if (arg[0] != NUL)
3642 return arg;
3643 break;
3644 case CMD_isearch:
3645 case CMD_dsearch:
3646 case CMD_ilist:
3647 case CMD_dlist:
3648 case CMD_ijump:
3649 case CMD_psearch:
3650 case CMD_djump:
3651 case CMD_isplit:
3652 case CMD_dsplit:
3653 arg = skipwhite(skipdigits(arg)); /* skip count */
3654 if (*arg == '/') /* Match regexp, not just whole words */
3655 {
3656 for (++arg; *arg && *arg != '/'; arg++)
3657 if (*arg == '\\' && arg[1] != NUL)
3658 arg++;
3659 if (*arg)
3660 {
3661 arg = skipwhite(arg + 1);
3662
3663 /* Check for trailing illegal characters */
3664 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3665 xp->xp_context = EXPAND_NOTHING;
3666 else
3667 return arg;
3668 }
3669 }
3670 break;
3671#ifdef FEAT_AUTOCMD
3672 case CMD_autocmd:
3673 return set_context_in_autocmd(xp, arg, FALSE);
3674
3675 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003676 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return set_context_in_autocmd(xp, arg, TRUE);
3678#endif
3679 case CMD_set:
3680 set_context_in_set_cmd(xp, arg, 0);
3681 break;
3682 case CMD_setglobal:
3683 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3684 break;
3685 case CMD_setlocal:
3686 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3687 break;
3688 case CMD_tag:
3689 case CMD_stag:
3690 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003691 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 case CMD_tselect:
3693 case CMD_stselect:
3694 case CMD_ptselect:
3695 case CMD_tjump:
3696 case CMD_stjump:
3697 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003698 if (*p_wop != NUL)
3699 xp->xp_context = EXPAND_TAGS_LISTFILES;
3700 else
3701 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 xp->xp_pattern = arg;
3703 break;
3704 case CMD_augroup:
3705 xp->xp_context = EXPAND_AUGROUP;
3706 xp->xp_pattern = arg;
3707 break;
3708#ifdef FEAT_SYN_HL
3709 case CMD_syntax:
3710 set_context_in_syntax_cmd(xp, arg);
3711 break;
3712#endif
3713#ifdef FEAT_EVAL
3714 case CMD_let:
3715 case CMD_if:
3716 case CMD_elseif:
3717 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003718 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 case CMD_echo:
3720 case CMD_echon:
3721 case CMD_execute:
3722 case CMD_echomsg:
3723 case CMD_echoerr:
3724 case CMD_call:
3725 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003726 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 break;
3728
3729 case CMD_unlet:
3730 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3731 arg = xp->xp_pattern + 1;
3732 xp->xp_context = EXPAND_USER_VARS;
3733 xp->xp_pattern = arg;
3734 break;
3735
3736 case CMD_function:
3737 case CMD_delfunction:
3738 xp->xp_context = EXPAND_USER_FUNC;
3739 xp->xp_pattern = arg;
3740 break;
3741
3742 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003743 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 break;
3745#endif
3746 case CMD_highlight:
3747 set_context_in_highlight_cmd(xp, arg);
3748 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003749#ifdef FEAT_CSCOPE
3750 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003751 case CMD_lcscope:
3752 case CMD_scscope:
3753 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003754 break;
3755#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003756#ifdef FEAT_SIGNS
3757 case CMD_sign:
3758 set_context_in_sign_cmd(xp, arg);
3759 break;
3760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#ifdef FEAT_LISTCMDS
3762 case CMD_bdelete:
3763 case CMD_bwipeout:
3764 case CMD_bunload:
3765 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3766 arg = xp->xp_pattern + 1;
3767 /*FALLTHROUGH*/
3768 case CMD_buffer:
3769 case CMD_sbuffer:
3770 case CMD_checktime:
3771 xp->xp_context = EXPAND_BUFFERS;
3772 xp->xp_pattern = arg;
3773 break;
3774#endif
3775#ifdef FEAT_USR_CMDS
3776 case CMD_USER:
3777 case CMD_USER_BUF:
3778 if (compl != EXPAND_NOTHING)
3779 {
3780 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003781 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783# ifdef FEAT_MENU
3784 if (compl == EXPAND_MENUS)
3785 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3786# endif
3787 if (compl == EXPAND_COMMANDS)
3788 return arg;
3789 if (compl == EXPAND_MAPPINGS)
3790 return set_context_in_map_cmd(xp, (char_u *)"map",
3791 arg, forceit, FALSE, FALSE, CMD_map);
3792 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3793 arg = xp->xp_pattern + 1;
3794 xp->xp_pattern = arg;
3795 }
3796 xp->xp_context = compl;
3797 }
3798 break;
3799#endif
3800 case CMD_map: case CMD_noremap:
3801 case CMD_nmap: case CMD_nnoremap:
3802 case CMD_vmap: case CMD_vnoremap:
3803 case CMD_omap: case CMD_onoremap:
3804 case CMD_imap: case CMD_inoremap:
3805 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003806 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003808 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_unmap:
3810 case CMD_nunmap:
3811 case CMD_vunmap:
3812 case CMD_ounmap:
3813 case CMD_iunmap:
3814 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003815 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003817 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case CMD_abbreviate: case CMD_noreabbrev:
3819 case CMD_cabbrev: case CMD_cnoreabbrev:
3820 case CMD_iabbrev: case CMD_inoreabbrev:
3821 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003822 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_unabbreviate:
3824 case CMD_cunabbrev:
3825 case CMD_iunabbrev:
3826 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003827 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#ifdef FEAT_MENU
3829 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3830 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3831 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3832 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3833 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3834 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3835 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3836 case CMD_tmenu: case CMD_tunmenu:
3837 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3838 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3839#endif
3840
3841 case CMD_colorscheme:
3842 xp->xp_context = EXPAND_COLORS;
3843 xp->xp_pattern = arg;
3844 break;
3845
3846 case CMD_compiler:
3847 xp->xp_context = EXPAND_COMPILER;
3848 xp->xp_pattern = arg;
3849 break;
3850
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003851 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003852 xp->xp_context = EXPAND_OWNSYNTAX;
3853 xp->xp_pattern = arg;
3854 break;
3855
3856 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003857 xp->xp_context = EXPAND_FILETYPE;
3858 xp->xp_pattern = arg;
3859 break;
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3862 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3863 case CMD_language:
3864 if (*skiptowhite(arg) == NUL)
3865 {
3866 xp->xp_context = EXPAND_LANGUAGE;
3867 xp->xp_pattern = arg;
3868 }
3869 else
3870 xp->xp_context = EXPAND_NOTHING;
3871 break;
3872#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003873#if defined(FEAT_PROFILE)
3874 case CMD_profile:
3875 set_context_in_profile_cmd(xp, arg);
3876 break;
3877#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003878 case CMD_behave:
3879 xp->xp_context = EXPAND_BEHAVE;
3880 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
3882#endif /* FEAT_CMDL_COMPL */
3883
3884 default:
3885 break;
3886 }
3887 return NULL;
3888}
3889
3890/*
3891 * skip a range specifier of the form: addr [,addr] [;addr] ..
3892 *
3893 * Backslashed delimiters after / or ? will be skipped, and commands will
3894 * not be expanded between /'s and ?'s or after "'".
3895 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003896 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 * Returns the "cmd" pointer advanced to beyond the range.
3898 */
3899 char_u *
3900skip_range(cmd, ctx)
3901 char_u *cmd;
3902 int *ctx; /* pointer to xp_context or NULL */
3903{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003904 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaardf177f62005-02-22 08:39:57 +00003906 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 if (*cmd == '\'')
3909 {
3910 if (*++cmd == NUL && ctx != NULL)
3911 *ctx = EXPAND_NOTHING;
3912 }
3913 else if (*cmd == '/' || *cmd == '?')
3914 {
3915 delim = *cmd++;
3916 while (*cmd != NUL && *cmd != delim)
3917 if (*cmd++ == '\\' && *cmd != NUL)
3918 ++cmd;
3919 if (*cmd == NUL && ctx != NULL)
3920 *ctx = EXPAND_NOTHING;
3921 }
3922 if (*cmd != NUL)
3923 ++cmd;
3924 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003925
3926 /* Skip ":" and white space. */
3927 while (*cmd == ':')
3928 cmd = skipwhite(cmd + 1);
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 return cmd;
3931}
3932
3933/*
3934 * get a single EX address
3935 *
3936 * Set ptr to the next character after the part that was interpreted.
3937 * Set ptr to NULL when an error is encountered.
3938 *
3939 * Return MAXLNUM when no Ex address was found.
3940 */
3941 static linenr_T
3942get_address(ptr, skip, to_other_file)
3943 char_u **ptr;
3944 int skip; /* only skip the address, don't use it */
3945 int to_other_file; /* flag: may jump to other file */
3946{
3947 int c;
3948 int i;
3949 long n;
3950 char_u *cmd;
3951 pos_T pos;
3952 pos_T *fp;
3953 linenr_T lnum;
3954
3955 cmd = skipwhite(*ptr);
3956 lnum = MAXLNUM;
3957 do
3958 {
3959 switch (*cmd)
3960 {
3961 case '.': /* '.' - Cursor position */
3962 ++cmd;
3963 lnum = curwin->w_cursor.lnum;
3964 break;
3965
3966 case '$': /* '$' - last line */
3967 ++cmd;
3968 lnum = curbuf->b_ml.ml_line_count;
3969 break;
3970
3971 case '\'': /* ''' - mark */
3972 if (*++cmd == NUL)
3973 {
3974 cmd = NULL;
3975 goto error;
3976 }
3977 if (skip)
3978 ++cmd;
3979 else
3980 {
3981 /* Only accept a mark in another file when it is
3982 * used by itself: ":'M". */
3983 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3984 ++cmd;
3985 if (fp == (pos_T *)-1)
3986 /* Jumped to another file. */
3987 lnum = curwin->w_cursor.lnum;
3988 else
3989 {
3990 if (check_mark(fp) == FAIL)
3991 {
3992 cmd = NULL;
3993 goto error;
3994 }
3995 lnum = fp->lnum;
3996 }
3997 }
3998 break;
3999
4000 case '/':
4001 case '?': /* '/' or '?' - search */
4002 c = *cmd++;
4003 if (skip) /* skip "/pat/" */
4004 {
4005 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4006 if (*cmd == c)
4007 ++cmd;
4008 }
4009 else
4010 {
4011 pos = curwin->w_cursor; /* save curwin->w_cursor */
4012 /*
4013 * When '/' or '?' follows another address, start
4014 * from there.
4015 */
4016 if (lnum != MAXLNUM)
4017 curwin->w_cursor.lnum = lnum;
4018 /*
4019 * Start a forward search at the end of the line.
4020 * Start a backward search at the start of the line.
4021 * This makes sure we never match in the current
4022 * line, and can match anywhere in the
4023 * next/previous line.
4024 */
4025 if (c == '/')
4026 curwin->w_cursor.col = MAXCOL;
4027 else
4028 curwin->w_cursor.col = 0;
4029 searchcmdlen = 0;
4030 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004031 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
4033 curwin->w_cursor = pos;
4034 cmd = NULL;
4035 goto error;
4036 }
4037 lnum = curwin->w_cursor.lnum;
4038 curwin->w_cursor = pos;
4039 /* adjust command string pointer */
4040 cmd += searchcmdlen;
4041 }
4042 break;
4043
4044 case '\\': /* "\?", "\/" or "\&", repeat search */
4045 ++cmd;
4046 if (*cmd == '&')
4047 i = RE_SUBST;
4048 else if (*cmd == '?' || *cmd == '/')
4049 i = RE_SEARCH;
4050 else
4051 {
4052 EMSG(_(e_backslash));
4053 cmd = NULL;
4054 goto error;
4055 }
4056
4057 if (!skip)
4058 {
4059 /*
4060 * When search follows another address, start from
4061 * there.
4062 */
4063 if (lnum != MAXLNUM)
4064 pos.lnum = lnum;
4065 else
4066 pos.lnum = curwin->w_cursor.lnum;
4067
4068 /*
4069 * Start the search just like for the above
4070 * do_search().
4071 */
4072 if (*cmd != '?')
4073 pos.col = MAXCOL;
4074 else
4075 pos.col = 0;
4076 if (searchit(curwin, curbuf, &pos,
4077 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004078 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004079 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 lnum = pos.lnum;
4081 else
4082 {
4083 cmd = NULL;
4084 goto error;
4085 }
4086 }
4087 ++cmd;
4088 break;
4089
4090 default:
4091 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4092 lnum = getdigits(&cmd);
4093 }
4094
4095 for (;;)
4096 {
4097 cmd = skipwhite(cmd);
4098 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4099 break;
4100
4101 if (lnum == MAXLNUM)
4102 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4103 if (VIM_ISDIGIT(*cmd))
4104 i = '+'; /* "number" is same as "+number" */
4105 else
4106 i = *cmd++;
4107 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4108 n = 1;
4109 else
4110 n = getdigits(&cmd);
4111 if (i == '-')
4112 lnum -= n;
4113 else
4114 lnum += n;
4115 }
4116 } while (*cmd == '/' || *cmd == '?');
4117
4118error:
4119 *ptr = cmd;
4120 return lnum;
4121}
4122
4123/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004124 * Get flags from an Ex command argument.
4125 */
4126 static void
4127get_flags(eap)
4128 exarg_T *eap;
4129{
4130 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4131 {
4132 if (*eap->arg == 'l')
4133 eap->flags |= EXFLAG_LIST;
4134 else if (*eap->arg == 'p')
4135 eap->flags |= EXFLAG_PRINT;
4136 else
4137 eap->flags |= EXFLAG_NR;
4138 eap->arg = skipwhite(eap->arg + 1);
4139 }
4140}
4141
4142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * Function called for command which is Not Implemented. NI!
4144 */
4145 void
4146ex_ni(eap)
4147 exarg_T *eap;
4148{
4149 if (!eap->skip)
4150 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4151}
4152
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004153#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154/*
4155 * Function called for script command which is Not Implemented. NI!
4156 * Skips over ":perl <<EOF" constructs.
4157 */
4158 static void
4159ex_script_ni(eap)
4160 exarg_T *eap;
4161{
4162 if (!eap->skip)
4163 ex_ni(eap);
4164 else
4165 vim_free(script_get(eap, eap->arg));
4166}
4167#endif
4168
4169/*
4170 * Check range in Ex command for validity.
4171 * Return NULL when valid, error message when invalid.
4172 */
4173 static char_u *
4174invalid_range(eap)
4175 exarg_T *eap;
4176{
4177 if ( eap->line1 < 0
4178 || eap->line2 < 0
4179 || eap->line1 > eap->line2
4180 || ((eap->argt & RANGE)
4181 && !(eap->argt & NOTADR)
4182 && eap->line2 > curbuf->b_ml.ml_line_count
4183#ifdef FEAT_DIFF
4184 + (eap->cmdidx == CMD_diffget)
4185#endif
4186 ))
4187 return (char_u *)_(e_invrange);
4188 return NULL;
4189}
4190
4191/*
4192 * Correct the range for zero line number, if required.
4193 */
4194 static void
4195correct_range(eap)
4196 exarg_T *eap;
4197{
4198 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4199 {
4200 if (eap->line1 == 0)
4201 eap->line1 = 1;
4202 if (eap->line2 == 0)
4203 eap->line2 = 1;
4204 }
4205}
4206
Bram Moolenaar748bf032005-02-02 23:04:36 +00004207#ifdef FEAT_QUICKFIX
4208static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4209
4210/*
4211 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4212 * pattern. Otherwise return eap->arg.
4213 */
4214 static char_u *
4215skip_grep_pat(eap)
4216 exarg_T *eap;
4217{
4218 char_u *p = eap->arg;
4219
Bram Moolenaara37420f2006-02-04 22:37:47 +00004220 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4221 || eap->cmdidx == CMD_vimgrepadd
4222 || eap->cmdidx == CMD_lvimgrepadd
4223 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004224 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004225 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004226 if (p == NULL)
4227 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004228 }
4229 return p;
4230}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004231
4232/*
4233 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4234 * in the command line, so that things like % get expanded.
4235 */
4236 static char_u *
4237replace_makeprg(eap, p, cmdlinep)
4238 exarg_T *eap;
4239 char_u *p;
4240 char_u **cmdlinep;
4241{
4242 char_u *new_cmdline;
4243 char_u *program;
4244 char_u *pos;
4245 char_u *ptr;
4246 int len;
4247 int i;
4248
4249 /*
4250 * Don't do it when ":vimgrep" is used for ":grep".
4251 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004252 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4253 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4254 || eap->cmdidx == CMD_grepadd
4255 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004256 && !grep_internal(eap->cmdidx))
4257 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004258 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4259 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004260 {
4261 if (*curbuf->b_p_gp == NUL)
4262 program = p_gp;
4263 else
4264 program = curbuf->b_p_gp;
4265 }
4266 else
4267 {
4268 if (*curbuf->b_p_mp == NUL)
4269 program = p_mp;
4270 else
4271 program = curbuf->b_p_mp;
4272 }
4273
4274 p = skipwhite(p);
4275
4276 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4277 {
4278 /* replace $* by given arguments */
4279 i = 1;
4280 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4281 ++i;
4282 len = (int)STRLEN(p);
4283 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4284 if (new_cmdline == NULL)
4285 return NULL; /* out of memory */
4286 ptr = new_cmdline;
4287 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4288 {
4289 i = (int)(pos - program);
4290 STRNCPY(ptr, program, i);
4291 STRCPY(ptr += i, p);
4292 ptr += len;
4293 program = pos + 2;
4294 }
4295 STRCPY(ptr, program);
4296 }
4297 else
4298 {
4299 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4300 if (new_cmdline == NULL)
4301 return NULL; /* out of memory */
4302 STRCPY(new_cmdline, program);
4303 STRCAT(new_cmdline, " ");
4304 STRCAT(new_cmdline, p);
4305 }
4306 msg_make(p);
4307
4308 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4309 vim_free(*cmdlinep);
4310 *cmdlinep = new_cmdline;
4311 p = new_cmdline;
4312 }
4313 return p;
4314}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004315#endif
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317/*
4318 * Expand file name in Ex command argument.
4319 * Return FAIL for failure, OK otherwise.
4320 */
4321 int
4322expand_filename(eap, cmdlinep, errormsgp)
4323 exarg_T *eap;
4324 char_u **cmdlinep;
4325 char_u **errormsgp;
4326{
4327 int has_wildcards; /* need to expand wildcards */
4328 char_u *repl;
4329 int srclen;
4330 char_u *p;
4331 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004332 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaar748bf032005-02-02 23:04:36 +00004334#ifdef FEAT_QUICKFIX
4335 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4336 p = skip_grep_pat(eap);
4337#else
4338 p = eap->arg;
4339#endif
4340
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 /*
4342 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4343 * the file name contains a wildcard it should not cause expanding.
4344 * (it will be expanded anyway if there is a wildcard before replacing).
4345 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004346 has_wildcards = mch_has_wildcard(p);
4347 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004349#ifdef FEAT_EVAL
4350 /* Skip over `=expr`, wildcards in it are not expanded. */
4351 if (p[0] == '`' && p[1] == '=')
4352 {
4353 p += 2;
4354 (void)skip_expr(&p);
4355 if (*p == '`')
4356 ++p;
4357 continue;
4358 }
4359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /*
4361 * Quick check if this cannot be the start of a special string.
4362 * Also removes backslash before '%', '#' and '<'.
4363 */
4364 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4365 {
4366 ++p;
4367 continue;
4368 }
4369
4370 /*
4371 * Try to find a match at this position.
4372 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004373 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4374 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (*errormsgp != NULL) /* error detected */
4376 return FAIL;
4377 if (repl == NULL) /* no match found */
4378 {
4379 p += srclen;
4380 continue;
4381 }
4382
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004383 /* Wildcards won't be expanded below, the replacement is taken
4384 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4385 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4386 {
4387 char_u *l = repl;
4388
4389 repl = expand_env_save(repl);
4390 vim_free(l);
4391 }
4392
Bram Moolenaar63b92542007-03-27 14:57:09 +00004393 /* Need to escape white space et al. with a backslash.
4394 * Don't do this for:
4395 * - replacement that already has been escaped: "##"
4396 * - shell commands (may have to use quotes instead).
4397 * - non-unix systems when there is a single argument (spaces don't
4398 * separate arguments then).
4399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004401 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 && eap->cmdidx != CMD_bang
4403 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004404 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004406 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004408 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409#ifndef UNIX
4410 && !(eap->argt & NOSPC)
4411#endif
4412 )
4413 {
4414 char_u *l;
4415#ifdef BACKSLASH_IN_FILENAME
4416 /* Don't escape a backslash here, because rem_backslash() doesn't
4417 * remove it later. */
4418 static char_u *nobslash = (char_u *)" \t\"|";
4419# define ESCAPE_CHARS nobslash
4420#else
4421# define ESCAPE_CHARS escape_chars
4422#endif
4423
4424 for (l = repl; *l; ++l)
4425 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4426 {
4427 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4428 if (l != NULL)
4429 {
4430 vim_free(repl);
4431 repl = l;
4432 }
4433 break;
4434 }
4435 }
4436
4437 /* For a shell command a '!' must be escaped. */
4438 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004439 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 char_u *l;
4442
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004443 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if (l != NULL)
4445 {
4446 vim_free(repl);
4447 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004448 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 if (strstr((char *)p_sh, "sh") != NULL)
4450 {
4451 l = vim_strsave_escaped(repl, (char_u *)"!");
4452 if (l != NULL)
4453 {
4454 vim_free(repl);
4455 repl = l;
4456 }
4457 }
4458 }
4459 }
4460
4461 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4462 vim_free(repl);
4463 if (p == NULL)
4464 return FAIL;
4465 }
4466
4467 /*
4468 * One file argument: Expand wildcards.
4469 * Don't do this with ":r !command" or ":w !command".
4470 */
4471 if ((eap->argt & NOSPC) && !eap->usefilter)
4472 {
4473 /*
4474 * May do this twice:
4475 * 1. Replace environment variables.
4476 * 2. Replace any other wildcards, remove backslashes.
4477 */
4478 for (n = 1; n <= 2; ++n)
4479 {
4480 if (n == 2)
4481 {
4482#ifdef UNIX
4483 /*
4484 * Only for Unix we check for more than one file name.
4485 * For other systems spaces are considered to be part
4486 * of the file name.
4487 * Only check here if there is no wildcard, otherwise
4488 * ExpandOne() will check for errors. This allows
4489 * ":e `ls ve*.c`" on Unix.
4490 */
4491 if (!has_wildcards)
4492 for (p = eap->arg; *p; ++p)
4493 {
4494 /* skip escaped characters */
4495 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4496 ++p;
4497 else if (vim_iswhite(*p))
4498 {
4499 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4500 return FAIL;
4501 }
4502 }
4503#endif
4504
4505 /*
4506 * Halve the number of backslashes (this is Vi compatible).
4507 * For Unix and OS/2, when wildcards are expanded, this is
4508 * done by ExpandOne() below.
4509 */
4510#if defined(UNIX) || defined(OS2)
4511 if (!has_wildcards)
4512#endif
4513 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515
4516 if (has_wildcards)
4517 {
4518 if (n == 1)
4519 {
4520 /*
4521 * First loop: May expand environment variables. This
4522 * can be done much faster with expand_env() than with
4523 * something else (e.g., calling a shell).
4524 * After expanding environment variables, check again
4525 * if there are still wildcards present.
4526 */
4527 if (vim_strchr(eap->arg, '$') != NULL
4528 || vim_strchr(eap->arg, '~') != NULL)
4529 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004530 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004531 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 has_wildcards = mch_has_wildcard(NameBuff);
4533 p = NameBuff;
4534 }
4535 else
4536 p = NULL;
4537 }
4538 else /* n == 2 */
4539 {
4540 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004541 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 ExpandInit(&xpc);
4544 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004545 if (p_wic)
4546 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004548 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (p == NULL)
4550 return FAIL;
4551 }
4552 if (p != NULL)
4553 {
4554 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4555 p, cmdlinep);
4556 if (n == 2) /* p came from ExpandOne() */
4557 vim_free(p);
4558 }
4559 }
4560 }
4561 }
4562 return OK;
4563}
4564
4565/*
4566 * Replace part of the command line, keeping eap->cmd, eap->arg and
4567 * eap->nextcmd correct.
4568 * "src" points to the part that is to be replaced, of length "srclen".
4569 * "repl" is the replacement string.
4570 * Returns a pointer to the character after the replaced string.
4571 * Returns NULL for failure.
4572 */
4573 static char_u *
4574repl_cmdline(eap, src, srclen, repl, cmdlinep)
4575 exarg_T *eap;
4576 char_u *src;
4577 int srclen;
4578 char_u *repl;
4579 char_u **cmdlinep;
4580{
4581 int len;
4582 int i;
4583 char_u *new_cmdline;
4584
4585 /*
4586 * The new command line is build in new_cmdline[].
4587 * First allocate it.
4588 * Careful: a "+cmd" argument may have been NUL terminated.
4589 */
4590 len = (int)STRLEN(repl);
4591 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004592 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4594 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4595 return NULL; /* out of memory! */
4596
4597 /*
4598 * Copy the stuff before the expanded part.
4599 * Copy the expanded stuff.
4600 * Copy what came after the expanded part.
4601 * Copy the next commands, if there are any.
4602 */
4603 i = (int)(src - *cmdlinep); /* length of part before match */
4604 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 mch_memmove(new_cmdline + i, repl, (size_t)len);
4607 i += len; /* remember the end of the string */
4608 STRCPY(new_cmdline + i, src + srclen);
4609 src = new_cmdline + i; /* remember where to continue */
4610
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004611 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
4613 i = (int)STRLEN(new_cmdline) + 1;
4614 STRCPY(new_cmdline + i, eap->nextcmd);
4615 eap->nextcmd = new_cmdline + i;
4616 }
4617 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4618 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4619 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4620 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4621 vim_free(*cmdlinep);
4622 *cmdlinep = new_cmdline;
4623
4624 return src;
4625}
4626
4627/*
4628 * Check for '|' to separate commands and '"' to start comments.
4629 */
4630 void
4631separate_nextcmd(eap)
4632 exarg_T *eap;
4633{
4634 char_u *p;
4635
Bram Moolenaar86b68352004-12-27 21:59:20 +00004636#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004637 p = skip_grep_pat(eap);
4638#else
4639 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004640#endif
4641
4642 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 if (*p == Ctrl_V)
4645 {
4646 if (eap->argt & (USECTRLV | XFILE))
4647 ++p; /* skip CTRL-V and next char */
4648 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004649 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004650 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (*p == NUL) /* stop at NUL after CTRL-V */
4652 break;
4653 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004654
4655#ifdef FEAT_EVAL
4656 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004658 {
4659 p += 2;
4660 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004661 }
4662#endif
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /* Check for '"': start of comment or '|': next command */
4665 /* :@" and :*" do not start a comment!
4666 * :redir @" doesn't either. */
4667 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4668 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4669 || p != eap->arg)
4670 && (eap->cmdidx != CMD_redir
4671 || p != eap->arg + 1 || p[-1] != '@'))
4672 || *p == '|' || *p == '\n')
4673 {
4674 /*
4675 * We remove the '\' before the '|', unless USECTRLV is used
4676 * AND 'b' is present in 'cpoptions'.
4677 */
4678 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4679 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4680 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004681 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 --p;
4683 }
4684 else
4685 {
4686 eap->nextcmd = check_nextcmd(p);
4687 *p = NUL;
4688 break;
4689 }
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004692
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4694 del_trailing_spaces(eap->arg);
4695}
4696
4697/*
4698 * get + command from ex argument
4699 */
4700 static char_u *
4701getargcmd(argp)
4702 char_u **argp;
4703{
4704 char_u *arg = *argp;
4705 char_u *command = NULL;
4706
4707 if (*arg == '+') /* +[command] */
4708 {
4709 ++arg;
4710 if (vim_isspace(*arg))
4711 command = dollar_command;
4712 else
4713 {
4714 command = arg;
4715 arg = skip_cmd_arg(command, TRUE);
4716 if (*arg != NUL)
4717 *arg++ = NUL; /* terminate command with NUL */
4718 }
4719
4720 arg = skipwhite(arg); /* skip over spaces */
4721 *argp = arg;
4722 }
4723 return command;
4724}
4725
4726/*
4727 * Find end of "+command" argument. Skip over "\ " and "\\".
4728 */
4729 static char_u *
4730skip_cmd_arg(p, rembs)
4731 char_u *p;
4732 int rembs; /* TRUE to halve the number of backslashes */
4733{
4734 while (*p && !vim_isspace(*p))
4735 {
4736 if (*p == '\\' && p[1] != NUL)
4737 {
4738 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004739 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 else
4741 ++p;
4742 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004743 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 return p;
4746}
4747
4748/*
4749 * Get "++opt=arg" argument.
4750 * Return FAIL or OK.
4751 */
4752 static int
4753getargopt(eap)
4754 exarg_T *eap;
4755{
4756 char_u *arg = eap->arg + 2;
4757 int *pp = NULL;
4758#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004759 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 char_u *p;
4761#endif
4762
4763 /* ":edit ++[no]bin[ary] file" */
4764 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4765 {
4766 if (*arg == 'n')
4767 {
4768 arg += 2;
4769 eap->force_bin = FORCE_NOBIN;
4770 }
4771 else
4772 eap->force_bin = FORCE_BIN;
4773 if (!checkforcmd(&arg, "binary", 3))
4774 return FAIL;
4775 eap->arg = skipwhite(arg);
4776 return OK;
4777 }
4778
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004779 /* ":read ++edit file" */
4780 if (STRNCMP(arg, "edit", 4) == 0)
4781 {
4782 eap->read_edit = TRUE;
4783 eap->arg = skipwhite(arg + 4);
4784 return OK;
4785 }
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (STRNCMP(arg, "ff", 2) == 0)
4788 {
4789 arg += 2;
4790 pp = &eap->force_ff;
4791 }
4792 else if (STRNCMP(arg, "fileformat", 10) == 0)
4793 {
4794 arg += 10;
4795 pp = &eap->force_ff;
4796 }
4797#ifdef FEAT_MBYTE
4798 else if (STRNCMP(arg, "enc", 3) == 0)
4799 {
4800 arg += 3;
4801 pp = &eap->force_enc;
4802 }
4803 else if (STRNCMP(arg, "encoding", 8) == 0)
4804 {
4805 arg += 8;
4806 pp = &eap->force_enc;
4807 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004808 else if (STRNCMP(arg, "bad", 3) == 0)
4809 {
4810 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004811 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#endif
4814
4815 if (pp == NULL || *arg != '=')
4816 return FAIL;
4817
4818 ++arg;
4819 *pp = (int)(arg - eap->cmd);
4820 arg = skip_cmd_arg(arg, FALSE);
4821 eap->arg = skipwhite(arg);
4822 *arg = NUL;
4823
4824#ifdef FEAT_MBYTE
4825 if (pp == &eap->force_ff)
4826 {
4827#endif
4828 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4829 return FAIL;
4830#ifdef FEAT_MBYTE
4831 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004832 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 /* Make 'fileencoding' lower case. */
4835 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4836 *p = TOLOWER_ASC(*p);
4837 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004838 else
4839 {
4840 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4841 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004842 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004843 if (STRICMP(p, "keep") == 0)
4844 eap->bad_char = BAD_KEEP;
4845 else if (STRICMP(p, "drop") == 0)
4846 eap->bad_char = BAD_DROP;
4847 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4848 eap->bad_char = *p;
4849 else
4850 return FAIL;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#endif
4853
4854 return OK;
4855}
4856
4857/*
4858 * ":abbreviate" and friends.
4859 */
4860 static void
4861ex_abbreviate(eap)
4862 exarg_T *eap;
4863{
4864 do_exmap(eap, TRUE); /* almost the same as mapping */
4865}
4866
4867/*
4868 * ":map" and friends.
4869 */
4870 static void
4871ex_map(eap)
4872 exarg_T *eap;
4873{
4874 /*
4875 * If we are sourcing .exrc or .vimrc in current directory we
4876 * print the mappings for security reasons.
4877 */
4878 if (secure)
4879 {
4880 secure = 2;
4881 msg_outtrans(eap->cmd);
4882 msg_putchar('\n');
4883 }
4884 do_exmap(eap, FALSE);
4885}
4886
4887/*
4888 * ":unmap" and friends.
4889 */
4890 static void
4891ex_unmap(eap)
4892 exarg_T *eap;
4893{
4894 do_exmap(eap, FALSE);
4895}
4896
4897/*
4898 * ":mapclear" and friends.
4899 */
4900 static void
4901ex_mapclear(eap)
4902 exarg_T *eap;
4903{
4904 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4905}
4906
4907/*
4908 * ":abclear" and friends.
4909 */
4910 static void
4911ex_abclear(eap)
4912 exarg_T *eap;
4913{
4914 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4915}
4916
4917#ifdef FEAT_AUTOCMD
4918 static void
4919ex_autocmd(eap)
4920 exarg_T *eap;
4921{
4922 /*
4923 * Disallow auto commands from .exrc and .vimrc in current
4924 * directory for security reasons.
4925 */
4926 if (secure)
4927 {
4928 secure = 2;
4929 eap->errmsg = e_curdir;
4930 }
4931 else if (eap->cmdidx == CMD_autocmd)
4932 do_autocmd(eap->arg, eap->forceit);
4933 else
4934 do_augroup(eap->arg, eap->forceit);
4935}
4936
4937/*
4938 * ":doautocmd": Apply the automatic commands to the current buffer.
4939 */
4940 static void
4941ex_doautocmd(eap)
4942 exarg_T *eap;
4943{
4944 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004945 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946}
4947#endif
4948
4949#ifdef FEAT_LISTCMDS
4950/*
4951 * :[N]bunload[!] [N] [bufname] unload buffer
4952 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4953 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4954 */
4955 static void
4956ex_bunload(eap)
4957 exarg_T *eap;
4958{
4959 eap->errmsg = do_bufdel(
4960 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4961 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4962 : DOBUF_UNLOAD, eap->arg,
4963 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4964}
4965
4966/*
4967 * :[N]buffer [N] to buffer N
4968 * :[N]sbuffer [N] to buffer N
4969 */
4970 static void
4971ex_buffer(eap)
4972 exarg_T *eap;
4973{
4974 if (*eap->arg)
4975 eap->errmsg = e_trailing;
4976 else
4977 {
4978 if (eap->addr_count == 0) /* default is current buffer */
4979 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4980 else
4981 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4982 }
4983}
4984
4985/*
4986 * :[N]bmodified [N] to next mod. buffer
4987 * :[N]sbmodified [N] to next mod. buffer
4988 */
4989 static void
4990ex_bmodified(eap)
4991 exarg_T *eap;
4992{
4993 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4994}
4995
4996/*
4997 * :[N]bnext [N] to next buffer
4998 * :[N]sbnext [N] split and to next buffer
4999 */
5000 static void
5001ex_bnext(eap)
5002 exarg_T *eap;
5003{
5004 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5005}
5006
5007/*
5008 * :[N]bNext [N] to previous buffer
5009 * :[N]bprevious [N] to previous buffer
5010 * :[N]sbNext [N] split and to previous buffer
5011 * :[N]sbprevious [N] split and to previous buffer
5012 */
5013 static void
5014ex_bprevious(eap)
5015 exarg_T *eap;
5016{
5017 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5018}
5019
5020/*
5021 * :brewind to first buffer
5022 * :bfirst to first buffer
5023 * :sbrewind split and to first buffer
5024 * :sbfirst split and to first buffer
5025 */
5026 static void
5027ex_brewind(eap)
5028 exarg_T *eap;
5029{
5030 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5031}
5032
5033/*
5034 * :blast to last buffer
5035 * :sblast split and to last buffer
5036 */
5037 static void
5038ex_blast(eap)
5039 exarg_T *eap;
5040{
5041 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5042}
5043#endif
5044
5045 int
5046ends_excmd(c)
5047 int c;
5048{
5049 return (c == NUL || c == '|' || c == '"' || c == '\n');
5050}
5051
5052#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5053 || defined(PROTO)
5054/*
5055 * Return the next command, after the first '|' or '\n'.
5056 * Return NULL if not found.
5057 */
5058 char_u *
5059find_nextcmd(p)
5060 char_u *p;
5061{
5062 while (*p != '|' && *p != '\n')
5063 {
5064 if (*p == NUL)
5065 return NULL;
5066 ++p;
5067 }
5068 return (p + 1);
5069}
5070#endif
5071
5072/*
5073 * Check if *p is a separator between Ex commands.
5074 * Return NULL if it isn't, (p + 1) if it is.
5075 */
5076 char_u *
5077check_nextcmd(p)
5078 char_u *p;
5079{
5080 p = skipwhite(p);
5081 if (*p == '|' || *p == '\n')
5082 return (p + 1);
5083 else
5084 return NULL;
5085}
5086
5087/*
5088 * - if there are more files to edit
5089 * - and this is the last window
5090 * - and forceit not used
5091 * - and not repeated twice on a row
5092 * return FAIL and give error message if 'message' TRUE
5093 * return OK otherwise
5094 */
5095 static int
5096check_more(message, forceit)
5097 int message; /* when FALSE check only, no messages */
5098 int forceit;
5099{
5100 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5101
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005102 if (!forceit && only_one_window()
5103 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 if (message)
5106 {
5107#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5108 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5109 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005110 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005113 vim_strncpy(buff,
5114 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005115 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005117 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 _("%d more files to edit. Quit anyway?"), n);
5119 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5120 return OK;
5121 return FAIL;
5122 }
5123#endif
5124 if (n == 1)
5125 EMSG(_("E173: 1 more file to edit"));
5126 else
5127 EMSGN(_("E173: %ld more files to edit"), n);
5128 quitmore = 2; /* next try to quit is allowed */
5129 }
5130 return FAIL;
5131 }
5132 return OK;
5133}
5134
5135#ifdef FEAT_CMDL_COMPL
5136/*
5137 * Function given to ExpandGeneric() to obtain the list of command names.
5138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 char_u *
5140get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005141 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 int idx;
5143{
5144 if (idx >= (int)CMD_SIZE)
5145# ifdef FEAT_USR_CMDS
5146 return get_user_command_name(idx);
5147# else
5148 return NULL;
5149# endif
5150 return cmdnames[idx].cmd_name;
5151}
5152#endif
5153
5154#if defined(FEAT_USR_CMDS) || defined(PROTO)
5155static 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));
5156static void uc_list __ARGS((char_u *name, size_t name_len));
5157static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5158static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5159static 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));
5160
5161 static int
5162uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5163 char_u *name;
5164 size_t name_len;
5165 char_u *rep;
5166 long argt;
5167 long def;
5168 int flags;
5169 int compl;
5170 char_u *compl_arg;
5171 int force;
5172{
5173 ucmd_T *cmd = NULL;
5174 char_u *p;
5175 int i;
5176 int cmp = 1;
5177 char_u *rep_buf = NULL;
5178 garray_T *gap;
5179
Bram Moolenaar9c102382006-05-03 21:26:49 +00005180 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 if (rep_buf == NULL)
5182 {
5183 /* Can't replace termcodes - try using the string as is */
5184 rep_buf = vim_strsave(rep);
5185
5186 /* Give up if out of memory */
5187 if (rep_buf == NULL)
5188 return FAIL;
5189 }
5190
5191 /* get address of growarray: global or in curbuf */
5192 if (flags & UC_BUFFER)
5193 {
5194 gap = &curbuf->b_ucmds;
5195 if (gap->ga_itemsize == 0)
5196 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5197 }
5198 else
5199 gap = &ucmds;
5200
5201 /* Search for the command in the already defined commands. */
5202 for (i = 0; i < gap->ga_len; ++i)
5203 {
5204 size_t len;
5205
5206 cmd = USER_CMD_GA(gap, i);
5207 len = STRLEN(cmd->uc_name);
5208 cmp = STRNCMP(name, cmd->uc_name, name_len);
5209 if (cmp == 0)
5210 {
5211 if (name_len < len)
5212 cmp = -1;
5213 else if (name_len > len)
5214 cmp = 1;
5215 }
5216
5217 if (cmp == 0)
5218 {
5219 if (!force)
5220 {
5221 EMSG(_("E174: Command already exists: add ! to replace it"));
5222 goto fail;
5223 }
5224
5225 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005226 cmd->uc_rep = NULL;
5227#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5228 vim_free(cmd->uc_compl_arg);
5229 cmd->uc_compl_arg = NULL;
5230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 break;
5232 }
5233
5234 /* Stop as soon as we pass the name to add */
5235 if (cmp < 0)
5236 break;
5237 }
5238
5239 /* Extend the array unless we're replacing an existing command */
5240 if (cmp != 0)
5241 {
5242 if (ga_grow(gap, 1) != OK)
5243 goto fail;
5244 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5245 goto fail;
5246
5247 cmd = USER_CMD_GA(gap, i);
5248 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5249
5250 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
5252 cmd->uc_name = p;
5253 }
5254
5255 cmd->uc_rep = rep_buf;
5256 cmd->uc_argt = argt;
5257 cmd->uc_def = def;
5258 cmd->uc_compl = compl;
5259#ifdef FEAT_EVAL
5260 cmd->uc_scriptID = current_SID;
5261# ifdef FEAT_CMDL_COMPL
5262 cmd->uc_compl_arg = compl_arg;
5263# endif
5264#endif
5265
5266 return OK;
5267
5268fail:
5269 vim_free(rep_buf);
5270#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5271 vim_free(compl_arg);
5272#endif
5273 return FAIL;
5274}
5275
5276/*
5277 * List of names for completion for ":command" with the EXPAND_ flag.
5278 * Must be alphabetical for completion.
5279 */
5280static struct
5281{
5282 int expand;
5283 char *name;
5284} command_complete[] =
5285{
5286 {EXPAND_AUGROUP, "augroup"},
5287 {EXPAND_BUFFERS, "buffer"},
5288 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005289#if defined(FEAT_CSCOPE)
5290 {EXPAND_CSCOPE, "cscope"},
5291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5293 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005294 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
5296 {EXPAND_DIRECTORIES, "dir"},
5297 {EXPAND_ENV_VARS, "environment"},
5298 {EXPAND_EVENTS, "event"},
5299 {EXPAND_EXPRESSION, "expression"},
5300 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005301 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {EXPAND_FUNCTIONS, "function"},
5303 {EXPAND_HELP, "help"},
5304 {EXPAND_HIGHLIGHT, "highlight"},
5305 {EXPAND_MAPPINGS, "mapping"},
5306 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005307 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005309 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005310#if defined(FEAT_SIGNS)
5311 {EXPAND_SIGN, "sign"},
5312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {EXPAND_TAGS, "tag"},
5314 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5315 {EXPAND_USER_VARS, "var"},
5316 {0, NULL}
5317};
5318
5319 static void
5320uc_list(name, name_len)
5321 char_u *name;
5322 size_t name_len;
5323{
5324 int i, j;
5325 int found = FALSE;
5326 ucmd_T *cmd;
5327 int len;
5328 long a;
5329 garray_T *gap;
5330
5331 gap = &curbuf->b_ucmds;
5332 for (;;)
5333 {
5334 for (i = 0; i < gap->ga_len; ++i)
5335 {
5336 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005337 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 /* Skip commands which don't match the requested prefix */
5340 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5341 continue;
5342
5343 /* Put out the title first time */
5344 if (!found)
5345 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5346 found = TRUE;
5347 msg_putchar('\n');
5348 if (got_int)
5349 break;
5350
5351 /* Special cases */
5352 msg_putchar(a & BANG ? '!' : ' ');
5353 msg_putchar(a & REGSTR ? '"' : ' ');
5354 msg_putchar(gap != &ucmds ? 'b' : ' ');
5355 msg_putchar(' ');
5356
5357 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5358 len = (int)STRLEN(cmd->uc_name) + 4;
5359
5360 do {
5361 msg_putchar(' ');
5362 ++len;
5363 } while (len < 16);
5364
5365 len = 0;
5366
5367 /* Arguments */
5368 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5369 {
5370 case 0: IObuff[len++] = '0'; break;
5371 case (EXTRA): IObuff[len++] = '*'; break;
5372 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5373 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5374 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5375 }
5376
5377 do {
5378 IObuff[len++] = ' ';
5379 } while (len < 5);
5380
5381 /* Range */
5382 if (a & (RANGE|COUNT))
5383 {
5384 if (a & COUNT)
5385 {
5386 /* -count=N */
5387 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5388 len += (int)STRLEN(IObuff + len);
5389 }
5390 else if (a & DFLALL)
5391 IObuff[len++] = '%';
5392 else if (cmd->uc_def >= 0)
5393 {
5394 /* -range=N */
5395 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5396 len += (int)STRLEN(IObuff + len);
5397 }
5398 else
5399 IObuff[len++] = '.';
5400 }
5401
5402 do {
5403 IObuff[len++] = ' ';
5404 } while (len < 11);
5405
5406 /* Completion */
5407 for (j = 0; command_complete[j].expand != 0; ++j)
5408 if (command_complete[j].expand == cmd->uc_compl)
5409 {
5410 STRCPY(IObuff + len, command_complete[j].name);
5411 len += (int)STRLEN(IObuff + len);
5412 break;
5413 }
5414
5415 do {
5416 IObuff[len++] = ' ';
5417 } while (len < 21);
5418
5419 IObuff[len] = '\0';
5420 msg_outtrans(IObuff);
5421
5422 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005423#ifdef FEAT_EVAL
5424 if (p_verbose > 0)
5425 last_set_msg(cmd->uc_scriptID);
5426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 out_flush();
5428 ui_breakcheck();
5429 if (got_int)
5430 break;
5431 }
5432 if (gap == &ucmds || i < gap->ga_len)
5433 break;
5434 gap = &ucmds;
5435 }
5436
5437 if (!found)
5438 MSG(_("No user-defined commands found"));
5439}
5440
5441 static char_u *
5442uc_fun_cmd()
5443{
5444 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5445 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5446 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5447 0xb9, 0x7f, 0};
5448 int i;
5449
5450 for (i = 0; fcmd[i]; ++i)
5451 IObuff[i] = fcmd[i] - 0x40;
5452 IObuff[i] = 0;
5453 return IObuff;
5454}
5455
5456 static int
5457uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5458 char_u *attr;
5459 size_t len;
5460 long *argt;
5461 long *def;
5462 int *flags;
5463 int *compl;
5464 char_u **compl_arg;
5465{
5466 char_u *p;
5467
5468 if (len == 0)
5469 {
5470 EMSG(_("E175: No attribute specified"));
5471 return FAIL;
5472 }
5473
5474 /* First, try the simple attributes (no arguments) */
5475 if (STRNICMP(attr, "bang", len) == 0)
5476 *argt |= BANG;
5477 else if (STRNICMP(attr, "buffer", len) == 0)
5478 *flags |= UC_BUFFER;
5479 else if (STRNICMP(attr, "register", len) == 0)
5480 *argt |= REGSTR;
5481 else if (STRNICMP(attr, "bar", len) == 0)
5482 *argt |= TRLBAR;
5483 else
5484 {
5485 int i;
5486 char_u *val = NULL;
5487 size_t vallen = 0;
5488 size_t attrlen = len;
5489
5490 /* Look for the attribute name - which is the part before any '=' */
5491 for (i = 0; i < (int)len; ++i)
5492 {
5493 if (attr[i] == '=')
5494 {
5495 val = &attr[i + 1];
5496 vallen = len - i - 1;
5497 attrlen = i;
5498 break;
5499 }
5500 }
5501
5502 if (STRNICMP(attr, "nargs", attrlen) == 0)
5503 {
5504 if (vallen == 1)
5505 {
5506 if (*val == '0')
5507 /* Do nothing - this is the default */;
5508 else if (*val == '1')
5509 *argt |= (EXTRA | NOSPC | NEEDARG);
5510 else if (*val == '*')
5511 *argt |= EXTRA;
5512 else if (*val == '?')
5513 *argt |= (EXTRA | NOSPC);
5514 else if (*val == '+')
5515 *argt |= (EXTRA | NEEDARG);
5516 else
5517 goto wrong_nargs;
5518 }
5519 else
5520 {
5521wrong_nargs:
5522 EMSG(_("E176: Invalid number of arguments"));
5523 return FAIL;
5524 }
5525 }
5526 else if (STRNICMP(attr, "range", attrlen) == 0)
5527 {
5528 *argt |= RANGE;
5529 if (vallen == 1 && *val == '%')
5530 *argt |= DFLALL;
5531 else if (val != NULL)
5532 {
5533 p = val;
5534 if (*def >= 0)
5535 {
5536two_count:
5537 EMSG(_("E177: Count cannot be specified twice"));
5538 return FAIL;
5539 }
5540
5541 *def = getdigits(&p);
5542 *argt |= (ZEROR | NOTADR);
5543
5544 if (p != val + vallen || vallen == 0)
5545 {
5546invalid_count:
5547 EMSG(_("E178: Invalid default value for count"));
5548 return FAIL;
5549 }
5550 }
5551 }
5552 else if (STRNICMP(attr, "count", attrlen) == 0)
5553 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005554 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
5556 if (val != NULL)
5557 {
5558 p = val;
5559 if (*def >= 0)
5560 goto two_count;
5561
5562 *def = getdigits(&p);
5563
5564 if (p != val + vallen)
5565 goto invalid_count;
5566 }
5567
5568 if (*def < 0)
5569 *def = 0;
5570 }
5571 else if (STRNICMP(attr, "complete", attrlen) == 0)
5572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (val == NULL)
5574 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005575 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 return FAIL;
5577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005579 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5580 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 else
5584 {
5585 char_u ch = attr[len];
5586 attr[len] = '\0';
5587 EMSG2(_("E181: Invalid attribute: %s"), attr);
5588 attr[len] = ch;
5589 return FAIL;
5590 }
5591 }
5592
5593 return OK;
5594}
5595
Bram Moolenaara850a712009-01-28 14:42:59 +00005596/*
5597 * ":command ..."
5598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 static void
5600ex_command(eap)
5601 exarg_T *eap;
5602{
5603 char_u *name;
5604 char_u *end;
5605 char_u *p;
5606 long argt = 0;
5607 long def = -1;
5608 int flags = 0;
5609 int compl = EXPAND_NOTHING;
5610 char_u *compl_arg = NULL;
5611 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005612 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 p = eap->arg;
5615
5616 /* Check for attributes */
5617 while (*p == '-')
5618 {
5619 ++p;
5620 end = skiptowhite(p);
5621 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5622 == FAIL)
5623 return;
5624 p = skipwhite(end);
5625 }
5626
5627 /* Get the name (if any) and skip to the following argument */
5628 name = p;
5629 if (ASCII_ISALPHA(*p))
5630 while (ASCII_ISALNUM(*p))
5631 ++p;
5632 if (!ends_excmd(*p) && !vim_iswhite(*p))
5633 {
5634 EMSG(_("E182: Invalid command name"));
5635 return;
5636 }
5637 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005638 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 /* If there is nothing after the name, and no attributes were specified,
5641 * we are listing commands
5642 */
5643 p = skipwhite(end);
5644 if (!has_attr && ends_excmd(*p))
5645 {
5646 uc_list(name, end - name);
5647 }
5648 else if (!ASCII_ISUPPER(*name))
5649 {
5650 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5651 return;
5652 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005653 else if ((name_len == 1 && *name == 'X')
5654 || (name_len <= 4
5655 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5656 {
5657 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5658 return;
5659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
5661 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5662 eap->forceit);
5663}
5664
5665/*
5666 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 * Clear all user commands, global and for current buffer.
5668 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005669 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005671 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 uc_clear(&ucmds);
5674 uc_clear(&curbuf->b_ucmds);
5675}
5676
5677/*
5678 * Clear all user commands for "gap".
5679 */
5680 void
5681uc_clear(gap)
5682 garray_T *gap;
5683{
5684 int i;
5685 ucmd_T *cmd;
5686
5687 for (i = 0; i < gap->ga_len; ++i)
5688 {
5689 cmd = USER_CMD_GA(gap, i);
5690 vim_free(cmd->uc_name);
5691 vim_free(cmd->uc_rep);
5692# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5693 vim_free(cmd->uc_compl_arg);
5694# endif
5695 }
5696 ga_clear(gap);
5697}
5698
5699 static void
5700ex_delcommand(eap)
5701 exarg_T *eap;
5702{
5703 int i = 0;
5704 ucmd_T *cmd = NULL;
5705 int cmp = -1;
5706 garray_T *gap;
5707
5708 gap = &curbuf->b_ucmds;
5709 for (;;)
5710 {
5711 for (i = 0; i < gap->ga_len; ++i)
5712 {
5713 cmd = USER_CMD_GA(gap, i);
5714 cmp = STRCMP(eap->arg, cmd->uc_name);
5715 if (cmp <= 0)
5716 break;
5717 }
5718 if (gap == &ucmds || cmp == 0)
5719 break;
5720 gap = &ucmds;
5721 }
5722
5723 if (cmp != 0)
5724 {
5725 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5726 return;
5727 }
5728
5729 vim_free(cmd->uc_name);
5730 vim_free(cmd->uc_rep);
5731# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5732 vim_free(cmd->uc_compl_arg);
5733# endif
5734
5735 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 if (i < gap->ga_len)
5738 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5739}
5740
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005741/*
5742 * split and quote args for <f-args>
5743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 static char_u *
5745uc_split_args(arg, lenp)
5746 char_u *arg;
5747 size_t *lenp;
5748{
5749 char_u *buf;
5750 char_u *p;
5751 char_u *q;
5752 int len;
5753
5754 /* Precalculate length */
5755 p = arg;
5756 len = 2; /* Initial and final quotes */
5757
5758 while (*p)
5759 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005760 if (p[0] == '\\' && p[1] == '\\')
5761 {
5762 len += 2;
5763 p += 2;
5764 }
5765 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 len += 1;
5768 p += 2;
5769 }
5770 else if (*p == '\\' || *p == '"')
5771 {
5772 len += 2;
5773 p += 1;
5774 }
5775 else if (vim_iswhite(*p))
5776 {
5777 p = skipwhite(p);
5778 if (*p == NUL)
5779 break;
5780 len += 3; /* "," */
5781 }
5782 else
5783 {
5784 ++len;
5785 ++p;
5786 }
5787 }
5788
5789 buf = alloc(len + 1);
5790 if (buf == NULL)
5791 {
5792 *lenp = 0;
5793 return buf;
5794 }
5795
5796 p = arg;
5797 q = buf;
5798 *q++ = '"';
5799 while (*p)
5800 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005801 if (p[0] == '\\' && p[1] == '\\')
5802 {
5803 *q++ = '\\';
5804 *q++ = '\\';
5805 p += 2;
5806 }
5807 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 *q++ = p[1];
5810 p += 2;
5811 }
5812 else if (*p == '\\' || *p == '"')
5813 {
5814 *q++ = '\\';
5815 *q++ = *p++;
5816 }
5817 else if (vim_iswhite(*p))
5818 {
5819 p = skipwhite(p);
5820 if (*p == NUL)
5821 break;
5822 *q++ = '"';
5823 *q++ = ',';
5824 *q++ = '"';
5825 }
5826 else
5827 {
5828 *q++ = *p++;
5829 }
5830 }
5831 *q++ = '"';
5832 *q = 0;
5833
5834 *lenp = len;
5835 return buf;
5836}
5837
5838/*
5839 * Check for a <> code in a user command.
5840 * "code" points to the '<'. "len" the length of the <> (inclusive).
5841 * "buf" is where the result is to be added.
5842 * "split_buf" points to a buffer used for splitting, caller should free it.
5843 * "split_len" is the length of what "split_buf" contains.
5844 * Returns the length of the replacement, which has been added to "buf".
5845 * Returns -1 if there was no match, and only the "<" has been copied.
5846 */
5847 static size_t
5848uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5849 char_u *code;
5850 size_t len;
5851 char_u *buf;
5852 ucmd_T *cmd; /* the user command we're expanding */
5853 exarg_T *eap; /* ex arguments */
5854 char_u **split_buf;
5855 size_t *split_len;
5856{
5857 size_t result = 0;
5858 char_u *p = code + 1;
5859 size_t l = len - 2;
5860 int quote = 0;
5861 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5862 ct_LT, ct_NONE } type = ct_NONE;
5863
5864 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5865 {
5866 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5867 p += 2;
5868 l -= 2;
5869 }
5870
Bram Moolenaar371d5402006-03-20 21:47:49 +00005871 ++l;
5872 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005874 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005876 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005878 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005880 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005882 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005884 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005886 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 type = ct_REGISTER;
5888
5889 switch (type)
5890 {
5891 case ct_ARGS:
5892 /* Simple case first */
5893 if (*eap->arg == NUL)
5894 {
5895 if (quote == 1)
5896 {
5897 result = 2;
5898 if (buf != NULL)
5899 STRCPY(buf, "''");
5900 }
5901 else
5902 result = 0;
5903 break;
5904 }
5905
5906 /* When specified there is a single argument don't split it.
5907 * Works for ":Cmd %" when % is "a b c". */
5908 if ((eap->argt & NOSPC) && quote == 2)
5909 quote = 1;
5910
5911 switch (quote)
5912 {
5913 case 0: /* No quoting, no splitting */
5914 result = STRLEN(eap->arg);
5915 if (buf != NULL)
5916 STRCPY(buf, eap->arg);
5917 break;
5918 case 1: /* Quote, but don't split */
5919 result = STRLEN(eap->arg) + 2;
5920 for (p = eap->arg; *p; ++p)
5921 {
5922 if (*p == '\\' || *p == '"')
5923 ++result;
5924 }
5925
5926 if (buf != NULL)
5927 {
5928 *buf++ = '"';
5929 for (p = eap->arg; *p; ++p)
5930 {
5931 if (*p == '\\' || *p == '"')
5932 *buf++ = '\\';
5933 *buf++ = *p;
5934 }
5935 *buf = '"';
5936 }
5937
5938 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005939 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 /* This is hard, so only do it once, and cache the result */
5941 if (*split_buf == NULL)
5942 *split_buf = uc_split_args(eap->arg, split_len);
5943
5944 result = *split_len;
5945 if (buf != NULL && result != 0)
5946 STRCPY(buf, *split_buf);
5947
5948 break;
5949 }
5950 break;
5951
5952 case ct_BANG:
5953 result = eap->forceit ? 1 : 0;
5954 if (quote)
5955 result += 2;
5956 if (buf != NULL)
5957 {
5958 if (quote)
5959 *buf++ = '"';
5960 if (eap->forceit)
5961 *buf++ = '!';
5962 if (quote)
5963 *buf = '"';
5964 }
5965 break;
5966
5967 case ct_LINE1:
5968 case ct_LINE2:
5969 case ct_COUNT:
5970 {
5971 char num_buf[20];
5972 long num = (type == ct_LINE1) ? eap->line1 :
5973 (type == ct_LINE2) ? eap->line2 :
5974 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5975 size_t num_len;
5976
5977 sprintf(num_buf, "%ld", num);
5978 num_len = STRLEN(num_buf);
5979 result = num_len;
5980
5981 if (quote)
5982 result += 2;
5983
5984 if (buf != NULL)
5985 {
5986 if (quote)
5987 *buf++ = '"';
5988 STRCPY(buf, num_buf);
5989 buf += num_len;
5990 if (quote)
5991 *buf = '"';
5992 }
5993
5994 break;
5995 }
5996
5997 case ct_REGISTER:
5998 result = eap->regname ? 1 : 0;
5999 if (quote)
6000 result += 2;
6001 if (buf != NULL)
6002 {
6003 if (quote)
6004 *buf++ = '\'';
6005 if (eap->regname)
6006 *buf++ = eap->regname;
6007 if (quote)
6008 *buf = '\'';
6009 }
6010 break;
6011
6012 case ct_LT:
6013 result = 1;
6014 if (buf != NULL)
6015 *buf = '<';
6016 break;
6017
6018 default:
6019 /* Not recognized: just copy the '<' and return -1. */
6020 result = (size_t)-1;
6021 if (buf != NULL)
6022 *buf = '<';
6023 break;
6024 }
6025
6026 return result;
6027}
6028
6029 static void
6030do_ucmd(eap)
6031 exarg_T *eap;
6032{
6033 char_u *buf;
6034 char_u *p;
6035 char_u *q;
6036
6037 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006038 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006039 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 size_t len, totlen;
6041
6042 size_t split_len = 0;
6043 char_u *split_buf = NULL;
6044 ucmd_T *cmd;
6045#ifdef FEAT_EVAL
6046 scid_T save_current_SID = current_SID;
6047#endif
6048
6049 if (eap->cmdidx == CMD_USER)
6050 cmd = USER_CMD(eap->useridx);
6051 else
6052 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6053
6054 /*
6055 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006056 * First round: "buf" is NULL, compute length, allocate "buf".
6057 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 */
6059 buf = NULL;
6060 for (;;)
6061 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006062 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006063 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006065
6066 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006068 start = vim_strchr(p, '<');
6069 if (start != NULL)
6070 end = vim_strchr(start + 1, '>');
6071 if (buf != NULL)
6072 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006073 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6074 ;
6075 if (*ksp == K_SPECIAL
6076 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006077 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6078# ifdef FEAT_GUI
6079 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6080# endif
6081 ))
6082 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006083 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006084 * KS_SPECIAL KE_FILLER, like for mappings, but
6085 * do_cmdline() doesn't handle that, so convert it back.
6086 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6087 len = ksp - p;
6088 if (len > 0)
6089 {
6090 mch_memmove(q, p, len);
6091 q += len;
6092 }
6093 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6094 p = ksp + 3;
6095 continue;
6096 }
6097 }
6098
6099 /* break if there no <item> is found */
6100 if (start == NULL || end == NULL)
6101 break;
6102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 /* Include the '>' */
6104 ++end;
6105
6106 /* Take everything up to the '<' */
6107 len = start - p;
6108 if (buf == NULL)
6109 totlen += len;
6110 else
6111 {
6112 mch_memmove(q, p, len);
6113 q += len;
6114 }
6115
6116 len = uc_check_code(start, end - start, q, cmd, eap,
6117 &split_buf, &split_len);
6118 if (len == (size_t)-1)
6119 {
6120 /* no match, continue after '<' */
6121 p = start + 1;
6122 len = 1;
6123 }
6124 else
6125 p = end;
6126 if (buf == NULL)
6127 totlen += len;
6128 else
6129 q += len;
6130 }
6131 if (buf != NULL) /* second time here, finished */
6132 {
6133 STRCPY(q, p);
6134 break;
6135 }
6136
6137 totlen += STRLEN(p); /* Add on the trailing characters */
6138 buf = alloc((unsigned)(totlen + 1));
6139 if (buf == NULL)
6140 {
6141 vim_free(split_buf);
6142 return;
6143 }
6144 }
6145
6146#ifdef FEAT_EVAL
6147 current_SID = cmd->uc_scriptID;
6148#endif
6149 (void)do_cmdline(buf, eap->getline, eap->cookie,
6150 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6151#ifdef FEAT_EVAL
6152 current_SID = save_current_SID;
6153#endif
6154 vim_free(buf);
6155 vim_free(split_buf);
6156}
6157
6158# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6159 static char_u *
6160get_user_command_name(idx)
6161 int idx;
6162{
6163 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6164}
6165
6166/*
6167 * Function given to ExpandGeneric() to obtain the list of user command names.
6168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 char_u *
6170get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006171 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 int idx;
6173{
6174 if (idx < curbuf->b_ucmds.ga_len)
6175 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6176 idx -= curbuf->b_ucmds.ga_len;
6177 if (idx < ucmds.ga_len)
6178 return USER_CMD(idx)->uc_name;
6179 return NULL;
6180}
6181
6182/*
6183 * Function given to ExpandGeneric() to obtain the list of user command
6184 * attributes.
6185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 char_u *
6187get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006188 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 int idx;
6190{
6191 static char *user_cmd_flags[] =
6192 {"bang", "bar", "buffer", "complete", "count",
6193 "nargs", "range", "register"};
6194
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006195 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 return NULL;
6197 return (char_u *)user_cmd_flags[idx];
6198}
6199
6200/*
6201 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 char_u *
6204get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006205 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 int idx;
6207{
6208 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6209
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006210 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 return NULL;
6212 return (char_u *)user_cmd_nargs[idx];
6213}
6214
6215/*
6216 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 char_u *
6219get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006220 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 int idx;
6222{
6223 return (char_u *)command_complete[idx].name;
6224}
6225# endif /* FEAT_CMDL_COMPL */
6226
6227#endif /* FEAT_USR_CMDS */
6228
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006229#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6230/*
6231 * Parse a completion argument "value[vallen]".
6232 * The detected completion goes in "*complp", argument type in "*argt".
6233 * When there is an argument, for function and user defined completion, it's
6234 * copied to allocated memory and stored in "*compl_arg".
6235 * Returns FAIL if something is wrong.
6236 */
6237 int
6238parse_compl_arg(value, vallen, complp, argt, compl_arg)
6239 char_u *value;
6240 int vallen;
6241 int *complp;
6242 long *argt;
6243 char_u **compl_arg;
6244{
6245 char_u *arg = NULL;
6246 size_t arglen = 0;
6247 int i;
6248 int valend = vallen;
6249
6250 /* Look for any argument part - which is the part after any ',' */
6251 for (i = 0; i < vallen; ++i)
6252 {
6253 if (value[i] == ',')
6254 {
6255 arg = &value[i + 1];
6256 arglen = vallen - i - 1;
6257 valend = i;
6258 break;
6259 }
6260 }
6261
6262 for (i = 0; command_complete[i].expand != 0; ++i)
6263 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006264 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006265 && STRNCMP(value, command_complete[i].name, valend) == 0)
6266 {
6267 *complp = command_complete[i].expand;
6268 if (command_complete[i].expand == EXPAND_BUFFERS)
6269 *argt |= BUFNAME;
6270 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6271 || command_complete[i].expand == EXPAND_FILES)
6272 *argt |= XFILE;
6273 break;
6274 }
6275 }
6276
6277 if (command_complete[i].expand == 0)
6278 {
6279 EMSG2(_("E180: Invalid complete value: %s"), value);
6280 return FAIL;
6281 }
6282
6283# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6284 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6285 && arg != NULL)
6286# else
6287 if (arg != NULL)
6288# endif
6289 {
6290 EMSG(_("E468: Completion argument only allowed for custom completion"));
6291 return FAIL;
6292 }
6293
6294# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6295 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6296 && arg == NULL)
6297 {
6298 EMSG(_("E467: Custom completion requires a function argument"));
6299 return FAIL;
6300 }
6301
6302 if (arg != NULL)
6303 *compl_arg = vim_strnsave(arg, (int)arglen);
6304# endif
6305 return OK;
6306}
6307#endif
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 static void
6310ex_colorscheme(eap)
6311 exarg_T *eap;
6312{
Bram Moolenaare6850792010-05-14 15:28:44 +02006313 if (*eap->arg == NUL)
6314 {
6315#ifdef FEAT_EVAL
6316 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6317 char_u *p = NULL;
6318
6319 if (expr != NULL)
6320 {
6321 ++emsg_off;
6322 p = eval_to_string(expr, NULL, FALSE);
6323 --emsg_off;
6324 vim_free(expr);
6325 }
6326 if (p != NULL)
6327 {
6328 MSG(p);
6329 vim_free(p);
6330 }
6331 else
6332 MSG("default");
6333#else
6334 MSG(_("unknown"));
6335#endif
6336 }
6337 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6339}
6340
6341 static void
6342ex_highlight(eap)
6343 exarg_T *eap;
6344{
6345 if (*eap->arg == NUL && eap->cmd[2] == '!')
6346 MSG(_("Greetings, Vim user!"));
6347 do_highlight(eap->arg, eap->forceit, FALSE);
6348}
6349
6350
6351/*
6352 * Call this function if we thought we were going to exit, but we won't
6353 * (because of an error). May need to restore the terminal mode.
6354 */
6355 void
6356not_exiting()
6357{
6358 exiting = FALSE;
6359 settmode(TMODE_RAW);
6360}
6361
6362/*
6363 * ":quit": quit current window, quit Vim if closed the last window.
6364 */
6365 static void
6366ex_quit(eap)
6367 exarg_T *eap;
6368{
6369#ifdef FEAT_CMDWIN
6370 if (cmdwin_type != 0)
6371 {
6372 cmdwin_result = Ctrl_C;
6373 return;
6374 }
6375#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006376 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006377 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006378 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006379 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006380 return;
6381 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006382#ifdef FEAT_AUTOCMD
6383 if (curbuf_locked())
6384 return;
6385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
6387#ifdef FEAT_NETBEANS_INTG
6388 netbeansForcedQuit = eap->forceit;
6389#endif
6390
6391 /*
6392 * If there are more files or windows we won't exit.
6393 */
6394 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6395 exiting = TRUE;
6396 if ((!P_HID(curbuf)
6397 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6398 || check_more(TRUE, eap->forceit) == FAIL
6399 || (only_one_window() && check_changed_any(eap->forceit)))
6400 {
6401 not_exiting();
6402 }
6403 else
6404 {
6405#ifdef FEAT_WINDOWS
6406 if (only_one_window()) /* quit last window */
6407#endif
6408 getout(0);
6409#ifdef FEAT_WINDOWS
6410# ifdef FEAT_GUI
6411 need_mouse_correct = TRUE;
6412# endif
6413 /* close window; may free buffer */
6414 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6415#endif
6416 }
6417}
6418
6419/*
6420 * ":cquit".
6421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 static void
6423ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006424 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 getout(1); /* this does not always pass on the exit code to the Manx
6427 compiler. why? */
6428}
6429
6430/*
6431 * ":qall": try to quit all windows
6432 */
6433 static void
6434ex_quit_all(eap)
6435 exarg_T *eap;
6436{
6437# ifdef FEAT_CMDWIN
6438 if (cmdwin_type != 0)
6439 {
6440 if (eap->forceit)
6441 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6442 else
6443 cmdwin_result = K_XF2;
6444 return;
6445 }
6446# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006447
6448 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006449 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006450 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006451 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006452 return;
6453 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006454#ifdef FEAT_AUTOCMD
6455 if (curbuf_locked())
6456 return;
6457#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 exiting = TRUE;
6460 if (eap->forceit || !check_changed_any(FALSE))
6461 getout(0);
6462 not_exiting();
6463}
6464
Bram Moolenaard9967712006-03-11 21:18:15 +00006465#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466/*
6467 * ":close": close current window, unless it is the last one
6468 */
6469 static void
6470ex_close(eap)
6471 exarg_T *eap;
6472{
6473# ifdef FEAT_CMDWIN
6474 if (cmdwin_type != 0)
6475 cmdwin_result = K_IGNORE;
6476 else
6477# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 if (!text_locked()
6479#ifdef FEAT_AUTOCMD
6480 && !curbuf_locked()
6481#endif
6482 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006483 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
Bram Moolenaard9967712006-03-11 21:18:15 +00006486# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487/*
6488 * ":pclose": Close any preview window.
6489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493{
6494 win_T *win;
6495
6496 for (win = firstwin; win != NULL; win = win->w_next)
6497 if (win->w_p_pvw)
6498 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006499 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006500 break;
6501 }
6502}
Bram Moolenaard9967712006-03-11 21:18:15 +00006503# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504
Bram Moolenaarf740b292006-02-16 22:11:02 +00006505/*
6506 * Close window "win" and take care of handling closing the last window for a
6507 * modified buffer.
6508 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006509 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006510ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006513 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 int need_hide;
6516 buf_T *buf = win->w_buffer;
6517
6518 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006521# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if ((p_confirm || cmdmod.confirm) && p_write)
6523 {
6524 dialog_changed(buf, FALSE);
6525 if (buf_valid(buf) && bufIsChanged(buf))
6526 return;
6527 need_hide = FALSE;
6528 }
6529 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
6532 EMSG(_(e_nowrtmsg));
6533 return;
6534 }
6535 }
6536
Bram Moolenaard9967712006-03-11 21:18:15 +00006537# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006539# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006542 if (tp == NULL)
6543 win_close(win, !need_hide && !P_HID(buf));
6544 else
6545 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546}
6547
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549 * ":tabclose": close current tab page, unless it is the last one.
6550 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 */
6552 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006553ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 exarg_T *eap;
6555{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006556 tabpage_T *tp;
6557
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006558# ifdef FEAT_CMDWIN
6559 if (cmdwin_type != 0)
6560 cmdwin_result = K_IGNORE;
6561 else
6562# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006563 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006564 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006568 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006569 tp = find_tabpage((int)eap->line2);
6570 if (tp == NULL)
6571 {
6572 beep_flush();
6573 return;
6574 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006575 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006576 {
6577 tabpage_close_other(tp, eap->forceit);
6578 return;
6579 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006581 if (!text_locked()
6582#ifdef FEAT_AUTOCMD
6583 && !curbuf_locked()
6584#endif
6585 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006586 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006588}
6589
6590/*
6591 * ":tabonly": close all tab pages except the current one
6592 */
6593 static void
6594ex_tabonly(eap)
6595 exarg_T *eap;
6596{
6597 tabpage_T *tp;
6598 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006599
6600# ifdef FEAT_CMDWIN
6601 if (cmdwin_type != 0)
6602 cmdwin_result = K_IGNORE;
6603 else
6604# endif
6605 if (first_tabpage->tp_next == NULL)
6606 MSG(_("Already only one tab page"));
6607 else
6608 {
6609 /* Repeat this up to a 1000 times, because autocommands may mess
6610 * up the lists. */
6611 for (done = 0; done < 1000; ++done)
6612 {
6613 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6614 if (tp->tp_topframe != topframe)
6615 {
6616 tabpage_close_other(tp, eap->forceit);
6617 /* if we failed to close it quit */
6618 if (valid_tabpage(tp))
6619 done = 1000;
6620 /* start over, "tp" is now invalid */
6621 break;
6622 }
6623 if (first_tabpage->tp_next == NULL)
6624 break;
6625 }
6626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006630 * Close the current tab page.
6631 */
6632 void
6633tabpage_close(forceit)
6634 int forceit;
6635{
6636 /* First close all the windows but the current one. If that worked then
6637 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006638 if (lastwin != firstwin)
6639 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006640 if (lastwin == firstwin)
6641 ex_win_close(forceit, curwin, NULL);
6642# ifdef FEAT_GUI
6643 need_mouse_correct = TRUE;
6644# endif
6645}
6646
6647/*
6648 * Close tab page "tp", which is not the current tab page.
6649 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006650 * Also takes care of the tab pages line disappearing when closing the
6651 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006652 */
6653 void
6654tabpage_close_other(tp, forceit)
6655 tabpage_T *tp;
6656 int forceit;
6657{
6658 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006659 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006660 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006661
6662 /* Limit to 1000 windows, autocommands may add a window while we close
6663 * one. OK, so I'm paranoid... */
6664 while (++done < 1000)
6665 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006666 wp = tp->tp_firstwin;
6667 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006668
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006669 /* Autocommands may delete the tab page under our fingers and we may
6670 * fail to close a window with a modified buffer. */
6671 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006672 break;
6673 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006674
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006675 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006676 if (h != tabline_height())
6677 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006678}
6679
6680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 * ":only".
6682 */
6683 static void
6684ex_only(eap)
6685 exarg_T *eap;
6686{
6687# ifdef FEAT_GUI
6688 need_mouse_correct = TRUE;
6689# endif
6690 close_others(TRUE, eap->forceit);
6691}
6692
6693/*
6694 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006695 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006697 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698ex_all(eap)
6699 exarg_T *eap;
6700{
6701 if (eap->addr_count == 0)
6702 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006703 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704}
6705#endif /* FEAT_WINDOWS */
6706
6707 static void
6708ex_hide(eap)
6709 exarg_T *eap;
6710{
6711 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6712 eap->errmsg = e_invarg;
6713 else
6714 {
6715 /* ":hide" or ":hide | cmd": hide current window */
6716 eap->nextcmd = check_nextcmd(eap->arg);
6717#ifdef FEAT_WINDOWS
6718 if (!eap->skip)
6719 {
6720# ifdef FEAT_GUI
6721 need_mouse_correct = TRUE;
6722# endif
6723 win_close(curwin, FALSE); /* don't free buffer */
6724 }
6725#endif
6726 }
6727}
6728
6729/*
6730 * ":stop" and ":suspend": Suspend Vim.
6731 */
6732 static void
6733ex_stop(eap)
6734 exarg_T *eap;
6735{
6736 /*
6737 * Disallow suspending for "rvim".
6738 */
6739 if (!check_restricted()
6740#ifdef WIN3264
6741 /*
6742 * Check if external commands are allowed now.
6743 */
6744 && can_end_termcap_mode(TRUE)
6745#endif
6746 )
6747 {
6748 if (!eap->forceit)
6749 autowrite_all();
6750 windgoto((int)Rows - 1, 0);
6751 out_char('\n');
6752 out_flush();
6753 stoptermcap();
6754 out_flush(); /* needed for SUN to restore xterm buffer */
6755#ifdef FEAT_TITLE
6756 mch_restore_title(3); /* restore window titles */
6757#endif
6758 ui_suspend(); /* call machine specific function */
6759#ifdef FEAT_TITLE
6760 maketitle();
6761 resettitle(); /* force updating the title */
6762#endif
6763 starttermcap();
6764 scroll_start(); /* scroll screen before redrawing */
6765 redraw_later_clear();
6766 shell_resized(); /* may have resized window */
6767 }
6768}
6769
6770/*
6771 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6772 */
6773 static void
6774ex_exit(eap)
6775 exarg_T *eap;
6776{
6777#ifdef FEAT_CMDWIN
6778 if (cmdwin_type != 0)
6779 {
6780 cmdwin_result = Ctrl_C;
6781 return;
6782 }
6783#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006784 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006785 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006786 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006787 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006788 return;
6789 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006790#ifdef FEAT_AUTOCMD
6791 if (curbuf_locked())
6792 return;
6793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
6795 /*
6796 * if more files or windows we won't exit
6797 */
6798 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6799 exiting = TRUE;
6800 if ( ((eap->cmdidx == CMD_wq
6801 || curbufIsChanged())
6802 && do_write(eap) == FAIL)
6803 || check_more(TRUE, eap->forceit) == FAIL
6804 || (only_one_window() && check_changed_any(eap->forceit)))
6805 {
6806 not_exiting();
6807 }
6808 else
6809 {
6810#ifdef FEAT_WINDOWS
6811 if (only_one_window()) /* quit last window, exit Vim */
6812#endif
6813 getout(0);
6814#ifdef FEAT_WINDOWS
6815# ifdef FEAT_GUI
6816 need_mouse_correct = TRUE;
6817# endif
6818 /* quit current window, may free buffer */
6819 win_close(curwin, !P_HID(curwin->w_buffer));
6820#endif
6821 }
6822}
6823
6824/*
6825 * ":print", ":list", ":number".
6826 */
6827 static void
6828ex_print(eap)
6829 exarg_T *eap;
6830{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006831 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6832 EMSG(_(e_emptybuf));
6833 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006835 for ( ;!got_int; ui_breakcheck())
6836 {
6837 print_line(eap->line1,
6838 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6839 || (eap->flags & EXFLAG_NR)),
6840 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6841 if (++eap->line1 > eap->line2)
6842 break;
6843 out_flush(); /* show one line at a time */
6844 }
6845 setpcmark();
6846 /* put cursor at last line */
6847 curwin->w_cursor.lnum = eap->line2;
6848 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852}
6853
6854#ifdef FEAT_BYTEOFF
6855 static void
6856ex_goto(eap)
6857 exarg_T *eap;
6858{
6859 goto_byte(eap->line2);
6860}
6861#endif
6862
6863/*
6864 * ":shell".
6865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 static void
6867ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006868 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 do_shell(NULL, 0);
6871}
6872
6873#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6874 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006875 || defined(FEAT_GUI_MSWIN) \
6876 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 || defined(PROTO)
6878
6879/*
6880 * Handle a file drop. The code is here because a drop is *nearly* like an
6881 * :args command, but not quite (we have a list of exact filenames, so we
6882 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6883 * code is very similar to :args and hence needs access to a lot of the static
6884 * functions in this file.
6885 *
6886 * The list should be allocated using alloc(), as should each item in the
6887 * list. This function takes over responsibility for freeing the list.
6888 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006889 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6891 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6892 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6893 * file functionality is (currently) not in EMX this is not presently a
6894 * problem.
6895 */
6896 void
6897handle_drop(filec, filev, split)
6898 int filec; /* the number of files dropped */
6899 char_u **filev; /* the list of files dropped */
6900 int split; /* force splitting the window */
6901{
6902 exarg_T ea;
6903 int save_msg_scroll = msg_scroll;
6904
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006905 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006906 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006908#ifdef FEAT_AUTOCMD
6909 if (curbuf_locked())
6910 return;
6911#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006912 /* When the screen is being updated we should not change buffers and
6913 * windows structures, it may cause freed memory to be used. */
6914 if (updating_screen)
6915 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
6917 /* Check whether the current buffer is changed. If so, we will need
6918 * to split the current window or data could be lost.
6919 * We don't need to check if the 'hidden' option is set, as in this
6920 * case the buffer won't be lost.
6921 */
6922 if (!P_HID(curbuf) && !split)
6923 {
6924 ++emsg_off;
6925 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6926 --emsg_off;
6927 }
6928 if (split)
6929 {
6930# ifdef FEAT_WINDOWS
6931 if (win_split(0, 0) == FAIL)
6932 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006933 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
6935 /* When splitting the window, create a new alist. Otherwise the
6936 * existing one is overwritten. */
6937 alist_unlink(curwin->w_alist);
6938 alist_new();
6939# else
6940 return; /* can't split, always fail */
6941# endif
6942 }
6943
6944 /*
6945 * Set up the new argument list.
6946 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006947 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949 /*
6950 * Move to the first file.
6951 */
6952 /* Fake up a minimal "next" command for do_argfile() */
6953 vim_memset(&ea, 0, sizeof(ea));
6954 ea.cmd = (char_u *)"next";
6955 do_argfile(&ea, 0);
6956
6957 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6958 * mode that is not needed here. */
6959 need_start_insertmode = FALSE;
6960
6961 /* Restore msg_scroll, otherwise a following command may cause scrolling
6962 * unexpectedly. The screen will be redrawn by the caller, thus
6963 * msg_scroll being set by displaying a message is irrelevant. */
6964 msg_scroll = save_msg_scroll;
6965}
6966#endif
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968/*
6969 * Clear an argument list: free all file names and reset it to zero entries.
6970 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006971 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972alist_clear(al)
6973 alist_T *al;
6974{
6975 while (--al->al_ga.ga_len >= 0)
6976 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6977 ga_clear(&al->al_ga);
6978}
6979
6980/*
6981 * Init an argument list.
6982 */
6983 void
6984alist_init(al)
6985 alist_T *al;
6986{
6987 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6988}
6989
6990#if defined(FEAT_WINDOWS) || defined(PROTO)
6991
6992/*
6993 * Remove a reference from an argument list.
6994 * Ignored when the argument list is the global one.
6995 * If the argument list is no longer used by any window, free it.
6996 */
6997 void
6998alist_unlink(al)
6999 alist_T *al;
7000{
7001 if (al != &global_alist && --al->al_refcount <= 0)
7002 {
7003 alist_clear(al);
7004 vim_free(al);
7005 }
7006}
7007
7008# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7009/*
7010 * Create a new argument list and use it for the current window.
7011 */
7012 void
7013alist_new()
7014{
7015 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7016 if (curwin->w_alist == NULL)
7017 {
7018 curwin->w_alist = &global_alist;
7019 ++global_alist.al_refcount;
7020 }
7021 else
7022 {
7023 curwin->w_alist->al_refcount = 1;
7024 alist_init(curwin->w_alist);
7025 }
7026}
7027# endif
7028#endif
7029
7030#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7031/*
7032 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007033 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7034 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 */
7036 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007037alist_expand(fnum_list, fnum_len)
7038 int *fnum_list;
7039 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007042 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 char_u **new_arg_files;
7044 int new_arg_file_count;
7045 char_u *save_p_su = p_su;
7046 int i;
7047
7048 /* Don't use 'suffixes' here. This should work like the shell did the
7049 * expansion. Also, the vimrc file isn't read yet, thus the user
7050 * can't set the options. */
7051 p_su = empty_option;
7052 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7053 if (old_arg_files != NULL)
7054 {
7055 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007056 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7057 old_arg_count = GARGCOUNT;
7058 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 &new_arg_file_count, &new_arg_files,
7060 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7061 && new_arg_file_count > 0)
7062 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007063 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7064 TRUE, fnum_list, fnum_len);
7065 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068 p_su = save_p_su;
7069}
7070#endif
7071
7072/*
7073 * Set the argument list for the current window.
7074 * Takes over the allocated files[] and the allocated fnames in it.
7075 */
7076 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007077alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 alist_T *al;
7079 int count;
7080 char_u **files;
7081 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007082 int *fnum_list;
7083 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 int i;
7086
7087 alist_clear(al);
7088 if (ga_grow(&al->al_ga, count) == OK)
7089 {
7090 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007091 {
7092 if (got_int)
7093 {
7094 /* When adding many buffers this can take a long time. Allow
7095 * interrupting here. */
7096 while (i < count)
7097 vim_free(files[i++]);
7098 break;
7099 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007100
7101 /* May set buffer name of a buffer previously used for the
7102 * argument list, so that it's re-used by alist_add. */
7103 if (fnum_list != NULL && i < fnum_len)
7104 buf_set_name(fnum_list[i], files[i]);
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007107 ui_breakcheck();
7108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 vim_free(files);
7110 }
7111 else
7112 FreeWild(count, files);
7113#ifdef FEAT_WINDOWS
7114 if (al == &global_alist)
7115#endif
7116 arg_had_last = FALSE;
7117}
7118
7119/*
7120 * Add file "fname" to argument list "al".
7121 * "fname" must have been allocated and "al" must have been checked for room.
7122 */
7123 void
7124alist_add(al, fname, set_fnum)
7125 alist_T *al;
7126 char_u *fname;
7127 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7128{
7129 if (fname == NULL) /* don't add NULL file names */
7130 return;
7131#ifdef BACKSLASH_IN_FILENAME
7132 slash_adjust(fname);
7133#endif
7134 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7135 if (set_fnum > 0)
7136 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7137 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7138 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139}
7140
7141#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7142/*
7143 * Adjust slashes in file names. Called after 'shellslash' was set.
7144 */
7145 void
7146alist_slash_adjust()
7147{
7148 int i;
7149# ifdef FEAT_WINDOWS
7150 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007151 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152# endif
7153
7154 for (i = 0; i < GARGCOUNT; ++i)
7155 if (GARGLIST[i].ae_fname != NULL)
7156 slash_adjust(GARGLIST[i].ae_fname);
7157# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007158 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (wp->w_alist != &global_alist)
7160 for (i = 0; i < WARGCOUNT(wp); ++i)
7161 if (WARGLIST(wp)[i].ae_fname != NULL)
7162 slash_adjust(WARGLIST(wp)[i].ae_fname);
7163# endif
7164}
7165#endif
7166
7167/*
7168 * ":preserve".
7169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 static void
7171ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007172 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007174 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 ml_preserve(curbuf, TRUE);
7176}
7177
7178/*
7179 * ":recover".
7180 */
7181 static void
7182ex_recover(eap)
7183 exarg_T *eap;
7184{
7185 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7186 recoverymode = TRUE;
7187 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7188 && (*eap->arg == NUL
7189 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7190 ml_recover();
7191 recoverymode = FALSE;
7192}
7193
7194/*
7195 * Command modifier used in a wrong way.
7196 */
7197 static void
7198ex_wrongmodifier(eap)
7199 exarg_T *eap;
7200{
7201 eap->errmsg = e_invcmd;
7202}
7203
7204#ifdef FEAT_WINDOWS
7205/*
7206 * :sview [+command] file split window with new file, read-only
7207 * :split [[+command] file] split window with current or new file
7208 * :vsplit [[+command] file] split window vertically with current or new file
7209 * :new [[+command] file] split window with no or new file
7210 * :vnew [[+command] file] split vertically window with no or new file
7211 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007212 *
7213 * :tabedit open new Tab page with empty window
7214 * :tabedit [+command] file open new Tab page and edit "file"
7215 * :tabnew [[+command] file] just like :tabedit
7216 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 */
7218 void
7219ex_splitview(eap)
7220 exarg_T *eap;
7221{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007222 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007223# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007225# endif
7226# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007230# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7232 {
7233 ex_ni(eap);
7234 return;
7235 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007238# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 need_mouse_correct = TRUE;
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_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007244 * quickfix windows. But it's OK when doing ":tab split". */
7245 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
7247 if (eap->cmdidx == CMD_split)
7248 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007249# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 if (eap->cmdidx == CMD_vsplit)
7251 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007256# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007257 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7260 FNAME_MESS, TRUE, curbuf->b_ffname);
7261 if (fname == NULL)
7262 goto theend;
7263 eap->arg = fname;
7264 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007265# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007269# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 && eap->cmdidx != CMD_new)
7275 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007276# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007277 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007278# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007279 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007280# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007281 au_has_group((char_u *)"FileExplorer"))
7282 {
7283 /* No browsing supported but we do have the file explorer:
7284 * Edit the directory. */
7285 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7286 eap->arg = (char_u *)".";
7287 }
7288 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007289# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007290 {
7291 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007293 if (fname == NULL)
7294 goto theend;
7295 eap->arg = fname;
7296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 }
7298 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007301 /*
7302 * Either open new tab page or split the window.
7303 */
7304 if (eap->cmdidx == CMD_tabedit
7305 || eap->cmdidx == CMD_tabfind
7306 || eap->cmdidx == CMD_tabnew)
7307 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007308 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7309 : eap->addr_count == 0 ? 0
7310 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007311 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007312 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007313
7314 /* set the alternate buffer for the window we came from */
7315 if (curwin != old_curwin
7316 && win_valid(old_curwin)
7317 && old_curwin->w_buffer != curbuf
7318 && !cmdmod.keepalt)
7319 old_curwin->w_alt_fnum = curbuf->b_fnum;
7320 }
7321 }
7322 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7324 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007325# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 /* Reset 'scrollbind' when editing another file, but keep it when
7327 * doing ":split" without arguments. */
7328 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007329# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007333 {
7334 RESET_BINDING(curwin);
7335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 else
7337 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 do_exedit(eap, old_curwin);
7340 }
7341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007342# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007346# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347theend:
7348 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007351
7352/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007353 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354 */
7355 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007356tabpage_new()
7357{
7358 exarg_T ea;
7359
7360 vim_memset(&ea, 0, sizeof(ea));
7361 ea.cmdidx = CMD_tabnew;
7362 ea.cmd = (char_u *)"tabn";
7363 ea.arg = (char_u *)"";
7364 ex_splitview(&ea);
7365}
7366
7367/*
7368 * :tabnext command
7369 */
7370 static void
7371ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372 exarg_T *eap;
7373{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007374 switch (eap->cmdidx)
7375 {
7376 case CMD_tabfirst:
7377 case CMD_tabrewind:
7378 goto_tabpage(1);
7379 break;
7380 case CMD_tablast:
7381 goto_tabpage(9999);
7382 break;
7383 case CMD_tabprevious:
7384 case CMD_tabNext:
7385 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7386 break;
7387 default: /* CMD_tabnext */
7388 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7389 break;
7390 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007391}
7392
7393/*
7394 * :tabmove command
7395 */
7396 static void
7397ex_tabmove(eap)
7398 exarg_T *eap;
7399{
7400 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401}
7402
7403/*
7404 * :tabs command: List tabs and their contents.
7405 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406 static void
7407ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007408 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409{
7410 tabpage_T *tp;
7411 win_T *wp;
7412 int tabcount = 1;
7413
7414 msg_start();
7415 msg_scroll = TRUE;
7416 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7417 {
7418 msg_putchar('\n');
7419 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7420 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7421 out_flush(); /* output one line at a time */
7422 ui_breakcheck();
7423
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007424 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425 wp = firstwin;
7426 else
7427 wp = tp->tp_firstwin;
7428 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7429 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007430 msg_putchar('\n');
7431 msg_putchar(wp == curwin ? '>' : ' ');
7432 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007433 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7434 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435 if (buf_spname(wp->w_buffer) != NULL)
7436 STRCPY(IObuff, buf_spname(wp->w_buffer));
7437 else
7438 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7439 IObuff, IOSIZE, TRUE);
7440 msg_outtrans(IObuff);
7441 out_flush(); /* output one line at a time */
7442 ui_breakcheck();
7443 }
7444 }
7445}
7446
7447#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449/*
7450 * ":mode": Set screen mode.
7451 * If no argument given, just get the screen size and redraw.
7452 */
7453 static void
7454ex_mode(eap)
7455 exarg_T *eap;
7456{
7457 if (*eap->arg == NUL)
7458 shell_resized();
7459 else
7460 mch_screenmode(eap->arg);
7461}
7462
7463#ifdef FEAT_WINDOWS
7464/*
7465 * ":resize".
7466 * set, increment or decrement current window height
7467 */
7468 static void
7469ex_resize(eap)
7470 exarg_T *eap;
7471{
7472 int n;
7473 win_T *wp = curwin;
7474
7475 if (eap->addr_count > 0)
7476 {
7477 n = eap->line2;
7478 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7479 ;
7480 }
7481
7482#ifdef FEAT_GUI
7483 need_mouse_correct = TRUE;
7484#endif
7485 n = atol((char *)eap->arg);
7486#ifdef FEAT_VERTSPLIT
7487 if (cmdmod.split & WSP_VERT)
7488 {
7489 if (*eap->arg == '-' || *eap->arg == '+')
7490 n += W_WIDTH(curwin);
7491 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7492 n = 9999;
7493 win_setwidth_win((int)n, wp);
7494 }
7495 else
7496#endif
7497 {
7498 if (*eap->arg == '-' || *eap->arg == '+')
7499 n += curwin->w_height;
7500 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7501 n = 9999;
7502 win_setheight_win((int)n, wp);
7503 }
7504}
7505#endif
7506
7507/*
7508 * ":find [+command] <file>" command.
7509 */
7510 static void
7511ex_find(eap)
7512 exarg_T *eap;
7513{
7514#ifdef FEAT_SEARCHPATH
7515 char_u *fname;
7516 int count;
7517
7518 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7519 TRUE, curbuf->b_ffname);
7520 if (eap->addr_count > 0)
7521 {
7522 /* Repeat finding the file "count" times. This matters when it
7523 * appears several times in the path. */
7524 count = eap->line2;
7525 while (fname != NULL && --count > 0)
7526 {
7527 vim_free(fname);
7528 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7529 FALSE, curbuf->b_ffname);
7530 }
7531 }
7532
7533 if (fname != NULL)
7534 {
7535 eap->arg = fname;
7536#endif
7537 do_exedit(eap, NULL);
7538#ifdef FEAT_SEARCHPATH
7539 vim_free(fname);
7540 }
7541#endif
7542}
7543
7544/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 * ":open" simulation: for now just work like ":visual".
7546 */
7547 static void
7548ex_open(eap)
7549 exarg_T *eap;
7550{
7551 regmatch_T regmatch;
7552 char_u *p;
7553
7554 curwin->w_cursor.lnum = eap->line2;
7555 beginline(BL_SOL | BL_FIX);
7556 if (*eap->arg == '/')
7557 {
7558 /* ":open /pattern/": put cursor in column found with pattern */
7559 ++eap->arg;
7560 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7561 *p = NUL;
7562 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7563 if (regmatch.regprog != NULL)
7564 {
7565 regmatch.rm_ic = p_ic;
7566 p = ml_get_curline();
7567 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007568 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007569 else
7570 EMSG(_(e_nomatch));
7571 vim_free(regmatch.regprog);
7572 }
7573 /* Move to the NUL, ignore any other arguments. */
7574 eap->arg += STRLEN(eap->arg);
7575 }
7576 check_cursor();
7577
7578 eap->cmdidx = CMD_visual;
7579 do_exedit(eap, NULL);
7580}
7581
7582/*
7583 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 */
7585 static void
7586ex_edit(eap)
7587 exarg_T *eap;
7588{
7589 do_exedit(eap, NULL);
7590}
7591
7592/*
7593 * ":edit <file>" command and alikes.
7594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 void
7596do_exedit(eap, old_curwin)
7597 exarg_T *eap;
7598 win_T *old_curwin; /* curwin before doing a split or NULL */
7599{
7600 int n;
7601#ifdef FEAT_WINDOWS
7602 int need_hide;
7603#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007604 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605
7606 /*
7607 * ":vi" command ends Ex mode.
7608 */
7609 if (exmode_active && (eap->cmdidx == CMD_visual
7610 || eap->cmdidx == CMD_view))
7611 {
7612 exmode_active = FALSE;
7613 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 {
7615 /* Special case: ":global/pat/visual\NLvi-commands" */
7616 if (global_busy)
7617 {
7618 int rd = RedrawingDisabled;
7619 int nwr = no_wait_return;
7620 int ms = msg_scroll;
7621#ifdef FEAT_GUI
7622 int he = hold_gui_events;
7623#endif
7624
7625 if (eap->nextcmd != NULL)
7626 {
7627 stuffReadbuff(eap->nextcmd);
7628 eap->nextcmd = NULL;
7629 }
7630
7631 if (exmode_was != EXMODE_VIM)
7632 settmode(TMODE_RAW);
7633 RedrawingDisabled = 0;
7634 no_wait_return = 0;
7635 need_wait_return = FALSE;
7636 msg_scroll = 0;
7637#ifdef FEAT_GUI
7638 hold_gui_events = 0;
7639#endif
7640 must_redraw = CLEAR;
7641
7642 main_loop(FALSE, TRUE);
7643
7644 RedrawingDisabled = rd;
7645 no_wait_return = nwr;
7646 msg_scroll = ms;
7647#ifdef FEAT_GUI
7648 hold_gui_events = he;
7649#endif
7650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
7654
7655 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007656 || eap->cmdidx == CMD_tabnew
7657 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658#ifdef FEAT_VERTSPLIT
7659 || eap->cmdidx == CMD_vnew
7660#endif
7661 ) && *eap->arg == NUL)
7662 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007663 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 setpcmark();
7665 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007666 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7667 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669 else if ((eap->cmdidx != CMD_split
7670#ifdef FEAT_VERTSPLIT
7671 && eap->cmdidx != CMD_vsplit
7672#endif
7673 )
7674 || *eap->arg != NUL
7675#ifdef FEAT_BROWSE
7676 || cmdmod.browse
7677#endif
7678 )
7679 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007680#ifdef FEAT_AUTOCMD
7681 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7682 * can bring us here, others are stopped earlier. */
7683 if (*eap->arg != NUL && curbuf_locked())
7684 return;
7685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 n = readonlymode;
7687 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7688 readonlymode = TRUE;
7689 else if (eap->cmdidx == CMD_enew)
7690 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7691 empty buffer */
7692 setpcmark();
7693 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7694 NULL, eap,
7695 /* ":edit" goes to first line if Vi compatible */
7696 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7697 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7698 ? ECMD_ONE : eap->do_ecmd_lnum,
7699 (P_HID(curbuf) ? ECMD_HIDE : 0)
7700 + (eap->forceit ? ECMD_FORCEIT : 0)
7701#ifdef FEAT_LISTCMDS
7702 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7703#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007704 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
7706 /* Editing the file failed. If the window was split, close it. */
7707#ifdef FEAT_WINDOWS
7708 if (old_curwin != NULL)
7709 {
7710 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7711 if (!need_hide || P_HID(curbuf))
7712 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007713# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7714 cleanup_T cs;
7715
7716 /* Reset the error/interrupt/exception state here so that
7717 * aborting() returns FALSE when closing a window. */
7718 enter_cleanup(&cs);
7719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720# ifdef FEAT_GUI
7721 need_mouse_correct = TRUE;
7722# endif
7723 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007724
7725# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7726 /* Restore the error/interrupt/exception state if not
7727 * discarded by a new aborting error, interrupt, or
7728 * uncaught exception. */
7729 leave_cleanup(&cs);
7730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732 }
7733#endif
7734 }
7735 else if (readonlymode && curbuf->b_nwindows == 1)
7736 {
7737 /* When editing an already visited buffer, 'readonly' won't be set
7738 * but the previous value is kept. With ":view" and ":sview" we
7739 * want the file to be readonly, except when another window is
7740 * editing the same buffer. */
7741 curbuf->b_p_ro = TRUE;
7742 }
7743 readonlymode = n;
7744 }
7745 else
7746 {
7747 if (eap->do_ecmd_cmd != NULL)
7748 do_cmdline_cmd(eap->do_ecmd_cmd);
7749#ifdef FEAT_TITLE
7750 n = curwin->w_arg_idx_invalid;
7751#endif
7752 check_arg_idx(curwin);
7753#ifdef FEAT_TITLE
7754 if (n != curwin->w_arg_idx_invalid)
7755 maketitle();
7756#endif
7757 }
7758
7759#ifdef FEAT_WINDOWS
7760 /*
7761 * if ":split file" worked, set alternate file name in old window to new
7762 * file
7763 */
7764 if (old_curwin != NULL
7765 && *eap->arg != NUL
7766 && curwin != old_curwin
7767 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007768 && old_curwin->w_buffer != curbuf
7769 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 old_curwin->w_alt_fnum = curbuf->b_fnum;
7771#endif
7772
7773 ex_no_reprint = TRUE;
7774}
7775
7776#ifndef FEAT_GUI
7777/*
7778 * ":gui" and ":gvim" when there is no GUI.
7779 */
7780 static void
7781ex_nogui(eap)
7782 exarg_T *eap;
7783{
7784 eap->errmsg = e_nogvim;
7785}
7786#endif
7787
7788#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7789 static void
7790ex_tearoff(eap)
7791 exarg_T *eap;
7792{
7793 gui_make_tearoff(eap->arg);
7794}
7795#endif
7796
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007797#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 static void
7799ex_popup(eap)
7800 exarg_T *eap;
7801{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007802 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804#endif
7805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 static void
7807ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007808 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7811 MSG(_("No swap file"));
7812 else
7813 msg(curbuf->b_ml.ml_mfp->mf_fname);
7814}
7815
7816/*
7817 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7818 * offset.
7819 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 static void
7822ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007823 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825#ifdef FEAT_SCROLLBIND
7826 win_T *wp;
7827 long topline;
7828 long y;
7829 linenr_T old_linenr = curwin->w_cursor.lnum;
7830
7831 setpcmark();
7832
7833 /*
7834 * determine max topline
7835 */
7836 if (curwin->w_p_scb)
7837 {
7838 topline = curwin->w_topline;
7839 for (wp = firstwin; wp; wp = wp->w_next)
7840 {
7841 if (wp->w_p_scb && wp->w_buffer)
7842 {
7843 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7844 if (topline > y)
7845 topline = y;
7846 }
7847 }
7848 if (topline < 1)
7849 topline = 1;
7850 }
7851 else
7852 {
7853 topline = 1;
7854 }
7855
7856
7857 /*
7858 * set all scrollbind windows to the same topline
7859 */
7860 wp = curwin;
7861 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7862 {
7863 if (curwin->w_p_scb)
7864 {
7865 y = topline - curwin->w_topline;
7866 if (y > 0)
7867 scrollup(y, TRUE);
7868 else
7869 scrolldown(-y, TRUE);
7870 curwin->w_scbind_pos = topline;
7871 redraw_later(VALID);
7872 cursor_correct();
7873#ifdef FEAT_WINDOWS
7874 curwin->w_redr_status = TRUE;
7875#endif
7876 }
7877 }
7878 curwin = wp;
7879 if (curwin->w_p_scb)
7880 {
7881 did_syncbind = TRUE;
7882 checkpcmark();
7883 if (old_linenr != curwin->w_cursor.lnum)
7884 {
7885 char_u ctrl_o[2];
7886
7887 ctrl_o[0] = Ctrl_O;
7888 ctrl_o[1] = 0;
7889 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7890 }
7891 }
7892#endif
7893}
7894
7895
7896 static void
7897ex_read(eap)
7898 exarg_T *eap;
7899{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007900 int i;
7901 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7902 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904 if (eap->usefilter) /* :r!cmd */
7905 do_bang(1, eap, FALSE, FALSE, TRUE);
7906 else
7907 {
7908 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7909 return;
7910
7911#ifdef FEAT_BROWSE
7912 if (cmdmod.browse)
7913 {
7914 char_u *browseFile;
7915
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007916 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 NULL, NULL, NULL, curbuf);
7918 if (browseFile != NULL)
7919 {
7920 i = readfile(browseFile, NULL,
7921 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7922 vim_free(browseFile);
7923 }
7924 else
7925 i = OK;
7926 }
7927 else
7928#endif
7929 if (*eap->arg == NUL)
7930 {
7931 if (check_fname() == FAIL) /* check for no file name */
7932 return;
7933 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7934 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7935 }
7936 else
7937 {
7938 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7939 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7940 i = readfile(eap->arg, NULL,
7941 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7942
7943 }
7944 if (i == FAIL)
7945 {
7946#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7947 if (!aborting())
7948#endif
7949 EMSG2(_(e_notopen), eap->arg);
7950 }
7951 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 {
7953 if (empty && exmode_active)
7954 {
7955 /* Delete the empty line that remains. Historically ex does
7956 * this but vi doesn't. */
7957 if (eap->line2 == 0)
7958 lnum = curbuf->b_ml.ml_line_count;
7959 else
7960 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007961 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007962 {
7963 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007964 if (curwin->w_cursor.lnum > 1
7965 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007966 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007967 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007968 }
7969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973}
7974
Bram Moolenaarea408852005-06-25 22:49:46 +00007975static char_u *prev_dir = NULL;
7976
7977#if defined(EXITFREE) || defined(PROTO)
7978 void
7979free_cd_dir()
7980{
7981 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007982 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007983
7984 vim_free(globaldir);
7985 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007986}
7987#endif
7988
7989
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990/*
7991 * ":cd", ":lcd", ":chdir" and ":lchdir".
7992 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007993 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994ex_cd(eap)
7995 exarg_T *eap;
7996{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 char_u *new_dir;
7998 char_u *tofree;
7999
8000 new_dir = eap->arg;
8001#if !defined(UNIX) && !defined(VMS)
8002 /* for non-UNIX ":cd" means: print current directory */
8003 if (*new_dir == NUL)
8004 ex_pwd(NULL);
8005 else
8006#endif
8007 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008008#ifdef FEAT_AUTOCMD
8009 if (allbuf_locked())
8010 return;
8011#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008012 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8013 && !eap->forceit)
8014 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008015 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008016 return;
8017 }
8018
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 /* ":cd -": Change to previous directory */
8020 if (STRCMP(new_dir, "-") == 0)
8021 {
8022 if (prev_dir == NULL)
8023 {
8024 EMSG(_("E186: No previous directory"));
8025 return;
8026 }
8027 new_dir = prev_dir;
8028 }
8029
8030 /* Save current directory for next ":cd -" */
8031 tofree = prev_dir;
8032 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8033 prev_dir = vim_strsave(NameBuff);
8034 else
8035 prev_dir = NULL;
8036
8037#if defined(UNIX) || defined(VMS)
8038 /* for UNIX ":cd" means: go to home directory */
8039 if (*new_dir == NUL)
8040 {
8041 /* use NameBuff for home directory name */
8042# ifdef VMS
8043 char_u *p;
8044
8045 p = mch_getenv((char_u *)"SYS$LOGIN");
8046 if (p == NULL || *p == NUL) /* empty is the same as not set */
8047 NameBuff[0] = NUL;
8048 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008049 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050# else
8051 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8052# endif
8053 new_dir = NameBuff;
8054 }
8055#endif
8056 if (new_dir == NULL || vim_chdir(new_dir))
8057 EMSG(_(e_failed));
8058 else
8059 {
8060 vim_free(curwin->w_localdir);
8061 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8062 {
8063 /* If still in global directory, need to remember current
8064 * directory as global directory. */
8065 if (globaldir == NULL && prev_dir != NULL)
8066 globaldir = vim_strsave(prev_dir);
8067 /* Remember this local directory for the window. */
8068 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8069 curwin->w_localdir = vim_strsave(NameBuff);
8070 }
8071 else
8072 {
8073 /* We are now in the global directory, no need to remember its
8074 * name. */
8075 vim_free(globaldir);
8076 globaldir = NULL;
8077 curwin->w_localdir = NULL;
8078 }
8079
8080 shorten_fnames(TRUE);
8081
8082 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008083 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 ex_pwd(eap);
8085 }
8086 vim_free(tofree);
8087 }
8088}
8089
8090/*
8091 * ":pwd".
8092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 static void
8094ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008095 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8098 {
8099#ifdef BACKSLASH_IN_FILENAME
8100 slash_adjust(NameBuff);
8101#endif
8102 msg(NameBuff);
8103 }
8104 else
8105 EMSG(_("E187: Unknown"));
8106}
8107
8108/*
8109 * ":=".
8110 */
8111 static void
8112ex_equal(eap)
8113 exarg_T *eap;
8114{
8115 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008116 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117}
8118
8119 static void
8120ex_sleep(eap)
8121 exarg_T *eap;
8122{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008123 int n;
8124 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125
8126 if (cursor_valid())
8127 {
8128 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8129 if (n >= 0)
8130 windgoto((int)n, curwin->w_wcol);
8131 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008132
8133 len = eap->line2;
8134 switch (*eap->arg)
8135 {
8136 case 'm': break;
8137 case NUL: len *= 1000L; break;
8138 default: EMSG2(_(e_invarg2), eap->arg); return;
8139 }
8140 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141}
8142
8143/*
8144 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8145 */
8146 void
8147do_sleep(msec)
8148 long msec;
8149{
8150 long done;
8151
8152 cursor_on();
8153 out_flush();
8154 for (done = 0; !got_int && done < msec; done += 1000L)
8155 {
8156 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8157 ui_breakcheck();
8158 }
8159}
8160
8161 static void
8162do_exmap(eap, isabbrev)
8163 exarg_T *eap;
8164 int isabbrev;
8165{
8166 int mode;
8167 char_u *cmdp;
8168
8169 cmdp = eap->cmd;
8170 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8171
8172 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8173 eap->arg, mode, isabbrev))
8174 {
8175 case 1: EMSG(_(e_invarg));
8176 break;
8177 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8178 break;
8179 }
8180}
8181
8182/*
8183 * ":winsize" command (obsolete).
8184 */
8185 static void
8186ex_winsize(eap)
8187 exarg_T *eap;
8188{
8189 int w, h;
8190 char_u *arg = eap->arg;
8191 char_u *p;
8192
8193 w = getdigits(&arg);
8194 arg = skipwhite(arg);
8195 p = arg;
8196 h = getdigits(&arg);
8197 if (*p != NUL && *arg == NUL)
8198 set_shellsize(w, h, TRUE);
8199 else
8200 EMSG(_("E465: :winsize requires two number arguments"));
8201}
8202
8203#ifdef FEAT_WINDOWS
8204 static void
8205ex_wincmd(eap)
8206 exarg_T *eap;
8207{
8208 int xchar = NUL;
8209 char_u *p;
8210
8211 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8212 {
8213 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8214 if (eap->arg[1] == NUL)
8215 {
8216 EMSG(_(e_invarg));
8217 return;
8218 }
8219 xchar = eap->arg[1];
8220 p = eap->arg + 2;
8221 }
8222 else
8223 p = eap->arg + 1;
8224
8225 eap->nextcmd = check_nextcmd(p);
8226 p = skipwhite(p);
8227 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8228 EMSG(_(e_invarg));
8229 else
8230 {
8231 /* Pass flags on for ":vertical wincmd ]". */
8232 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008233 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8235 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008236 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
8238}
8239#endif
8240
Bram Moolenaar843ee412004-06-30 16:16:41 +00008241#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
8243 * ":winpos".
8244 */
8245 static void
8246ex_winpos(eap)
8247 exarg_T *eap;
8248{
8249 int x, y;
8250 char_u *arg = eap->arg;
8251 char_u *p;
8252
8253 if (*arg == NUL)
8254 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008255# if defined(FEAT_GUI) || defined(MSWIN)
8256# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008258# else
8259 if (mch_get_winpos(&x, &y) != FAIL)
8260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8263 msg(IObuff);
8264 }
8265 else
8266# endif
8267 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8268 }
8269 else
8270 {
8271 x = getdigits(&arg);
8272 arg = skipwhite(arg);
8273 p = arg;
8274 y = getdigits(&arg);
8275 if (*p == NUL || *arg != NUL)
8276 {
8277 EMSG(_("E466: :winpos requires two number arguments"));
8278 return;
8279 }
8280# ifdef FEAT_GUI
8281 if (gui.in_use)
8282 gui_mch_set_winpos(x, y);
8283 else if (gui.starting)
8284 {
8285 /* Remember the coordinates for when the window is opened. */
8286 gui_win_x = x;
8287 gui_win_y = y;
8288 }
8289# ifdef HAVE_TGETENT
8290 else
8291# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008292# else
8293# ifdef MSWIN
8294 mch_set_winpos(x, y);
8295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296# endif
8297# ifdef HAVE_TGETENT
8298 if (*T_CWP)
8299 term_set_winpos(x, y);
8300# endif
8301 }
8302}
8303#endif
8304
8305/*
8306 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8307 */
8308 static void
8309ex_operators(eap)
8310 exarg_T *eap;
8311{
8312 oparg_T oa;
8313
8314 clear_oparg(&oa);
8315 oa.regname = eap->regname;
8316 oa.start.lnum = eap->line1;
8317 oa.end.lnum = eap->line2;
8318 oa.line_count = eap->line2 - eap->line1 + 1;
8319 oa.motion_type = MLINE;
8320#ifdef FEAT_VIRTUALEDIT
8321 virtual_op = FALSE;
8322#endif
8323 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8324 {
8325 setpcmark();
8326 curwin->w_cursor.lnum = eap->line1;
8327 beginline(BL_SOL | BL_FIX);
8328 }
8329
8330 switch (eap->cmdidx)
8331 {
8332 case CMD_delete:
8333 oa.op_type = OP_DELETE;
8334 op_delete(&oa);
8335 break;
8336
8337 case CMD_yank:
8338 oa.op_type = OP_YANK;
8339 (void)op_yank(&oa, FALSE, TRUE);
8340 break;
8341
8342 default: /* CMD_rshift or CMD_lshift */
8343 if ((eap->cmdidx == CMD_rshift)
8344#ifdef FEAT_RIGHTLEFT
8345 ^ curwin->w_p_rl
8346#endif
8347 )
8348 oa.op_type = OP_RSHIFT;
8349 else
8350 oa.op_type = OP_LSHIFT;
8351 op_shift(&oa, FALSE, eap->amount);
8352 break;
8353 }
8354#ifdef FEAT_VIRTUALEDIT
8355 virtual_op = MAYBE;
8356#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008357 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
8359
8360/*
8361 * ":put".
8362 */
8363 static void
8364ex_put(eap)
8365 exarg_T *eap;
8366{
8367 /* ":0put" works like ":1put!". */
8368 if (eap->line2 == 0)
8369 {
8370 eap->line2 = 1;
8371 eap->forceit = TRUE;
8372 }
8373 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008374 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8375 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376}
8377
8378/*
8379 * Handle ":copy" and ":move".
8380 */
8381 static void
8382ex_copymove(eap)
8383 exarg_T *eap;
8384{
8385 long n;
8386
8387 n = get_address(&eap->arg, FALSE, FALSE);
8388 if (eap->arg == NULL) /* error detected */
8389 {
8390 eap->nextcmd = NULL;
8391 return;
8392 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008393 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 /*
8396 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8397 */
8398 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8399 {
8400 EMSG(_(e_invaddr));
8401 return;
8402 }
8403
8404 if (eap->cmdidx == CMD_move)
8405 {
8406 if (do_move(eap->line1, eap->line2, n) == FAIL)
8407 return;
8408 }
8409 else
8410 ex_copy(eap->line1, eap->line2, n);
8411 u_clearline();
8412 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008413 ex_may_print(eap);
8414}
8415
8416/*
8417 * Print the current line if flags were given to the Ex command.
8418 */
8419 static void
8420ex_may_print(eap)
8421 exarg_T *eap;
8422{
8423 if (eap->flags != 0)
8424 {
8425 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8426 (eap->flags & EXFLAG_LIST));
8427 ex_no_reprint = TRUE;
8428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429}
8430
8431/*
8432 * ":smagic" and ":snomagic".
8433 */
8434 static void
8435ex_submagic(eap)
8436 exarg_T *eap;
8437{
8438 int magic_save = p_magic;
8439
8440 p_magic = (eap->cmdidx == CMD_smagic);
8441 do_sub(eap);
8442 p_magic = magic_save;
8443}
8444
8445/*
8446 * ":join".
8447 */
8448 static void
8449ex_join(eap)
8450 exarg_T *eap;
8451{
8452 curwin->w_cursor.lnum = eap->line1;
8453 if (eap->line1 == eap->line2)
8454 {
8455 if (eap->addr_count >= 2) /* :2,2join does nothing */
8456 return;
8457 if (eap->line2 == curbuf->b_ml.ml_line_count)
8458 {
8459 beep_flush();
8460 return;
8461 }
8462 ++eap->line2;
8463 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008464 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008466 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467}
8468
8469/*
8470 * ":[addr]@r" or ":[addr]*r": execute register
8471 */
8472 static void
8473ex_at(eap)
8474 exarg_T *eap;
8475{
8476 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008477 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
8479 curwin->w_cursor.lnum = eap->line2;
8480
8481#ifdef USE_ON_FLY_SCROLL
8482 dont_scroll = TRUE; /* disallow scrolling here */
8483#endif
8484
8485 /* get the register name. No name means to use the previous one */
8486 c = *eap->arg;
8487 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8488 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008489 /* Put the register in the typeahead buffer with the "silent" flag. */
8490 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8491 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 else
8496 {
8497 int save_efr = exec_from_reg;
8498
8499 exec_from_reg = TRUE;
8500
8501 /*
8502 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008503 * Continue until the stuff buffer is empty and all added characters
8504 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008506 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8508
8509 exec_from_reg = save_efr;
8510 }
8511}
8512
8513/*
8514 * ":!".
8515 */
8516 static void
8517ex_bang(eap)
8518 exarg_T *eap;
8519{
8520 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8521}
8522
8523/*
8524 * ":undo".
8525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 static void
8527ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008528 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008530 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008531 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008532 else
8533 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008536#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008537 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008538ex_wundo(eap)
8539 exarg_T *eap;
8540{
8541 char_u hash[UNDO_HASH_SIZE];
8542
8543 u_compute_hash(hash);
8544 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8545}
8546
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008547 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008548ex_rundo(eap)
8549 exarg_T *eap;
8550{
8551 char_u hash[UNDO_HASH_SIZE];
8552
8553 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008554 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008555}
8556#endif
8557
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558/*
8559 * ":redo".
8560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 static void
8562ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008563 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 u_redo(1);
8566}
8567
8568/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008569 * ":earlier" and ":later".
8570 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008571 static void
8572ex_later(eap)
8573 exarg_T *eap;
8574{
8575 long count = 0;
8576 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008577 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008578 char_u *p = eap->arg;
8579
8580 if (*p == NUL)
8581 count = 1;
8582 else if (isdigit(*p))
8583 {
8584 count = getdigits(&p);
8585 switch (*p)
8586 {
8587 case 's': ++p; sec = TRUE; break;
8588 case 'm': ++p; sec = TRUE; count *= 60; break;
8589 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008590 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8591 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008592 }
8593 }
8594
8595 if (*p != NUL)
8596 EMSG2(_(e_invarg2), eap->arg);
8597 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008598 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8599 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008600}
8601
8602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 * ":redir": start/stop redirection.
8604 */
8605 static void
8606ex_redir(eap)
8607 exarg_T *eap;
8608{
8609 char *mode;
8610 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008611 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
8613 if (STRICMP(eap->arg, "END") == 0)
8614 close_redir();
8615 else
8616 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008617 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008619 ++arg;
8620 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008622 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 mode = "a";
8624 }
8625 else
8626 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008627 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629 close_redir();
8630
8631 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008632 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 if (fname == NULL)
8634 return;
8635#ifdef FEAT_BROWSE
8636 if (cmdmod.browse)
8637 {
8638 char_u *browseFile;
8639
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008640 browseFile = do_browse(BROWSE_SAVE,
8641 (char_u *)_("Save Redirection"),
8642 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (browseFile == NULL)
8644 return; /* operation cancelled */
8645 vim_free(fname);
8646 fname = browseFile;
8647 eap->forceit = TRUE; /* since dialog already asked */
8648 }
8649#endif
8650
8651 redir_fd = open_exfile(fname, eap->forceit, mode);
8652 vim_free(fname);
8653 }
8654#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008655 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {
8657 /* redirect to a register a-z (resp. A-Z for appending) */
8658 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008659 ++arg;
8660 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008662 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008663 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008665 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008667 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008668 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008669 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008672 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008673 if (*arg == '>')
8674 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008675 /* Make register empty when not using @A-@Z and the
8676 * command is valid. */
8677 if (*arg == NUL && !isupper(redir_reg))
8678 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008680 }
8681 if (*arg != NUL)
8682 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008684 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008685 }
8686 }
8687 else if (*arg == '=' && arg[1] == '>')
8688 {
8689 int append;
8690
8691 /* redirect to a variable */
8692 close_redir();
8693 arg += 2;
8694
8695 if (*arg == '>')
8696 {
8697 ++arg;
8698 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 }
8700 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008701 append = FALSE;
8702
8703 if (var_redir_start(skipwhite(arg), append) == OK)
8704 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 }
8706#endif
8707
8708 /* TODO: redirect to a buffer */
8709
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008711 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008713
8714 /* Make sure redirection is not off. Can happen for cmdline completion
8715 * that indirectly invokes a command to catch its output. */
8716 if (redir_fd != NULL
8717#ifdef FEAT_EVAL
8718 || redir_reg || redir_vname
8719#endif
8720 )
8721 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722}
8723
8724/*
8725 * ":redraw": force redraw
8726 */
8727 static void
8728ex_redraw(eap)
8729 exarg_T *eap;
8730{
8731 int r = RedrawingDisabled;
8732 int p = p_lz;
8733
8734 RedrawingDisabled = 0;
8735 p_lz = FALSE;
8736 update_topline();
8737 update_screen(eap->forceit ? CLEAR :
8738#ifdef FEAT_VISUAL
8739 VIsual_active ? INVERTED :
8740#endif
8741 0);
8742#ifdef FEAT_TITLE
8743 if (need_maketitle)
8744 maketitle();
8745#endif
8746 RedrawingDisabled = r;
8747 p_lz = p;
8748
8749 /* Reset msg_didout, so that a message that's there is overwritten. */
8750 msg_didout = FALSE;
8751 msg_col = 0;
8752
8753 out_flush();
8754}
8755
8756/*
8757 * ":redrawstatus": force redraw of status line(s)
8758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 static void
8760ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008761 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763#if defined(FEAT_WINDOWS)
8764 int r = RedrawingDisabled;
8765 int p = p_lz;
8766
8767 RedrawingDisabled = 0;
8768 p_lz = FALSE;
8769 if (eap->forceit)
8770 status_redraw_all();
8771 else
8772 status_redraw_curbuf();
8773 update_screen(
8774# ifdef FEAT_VISUAL
8775 VIsual_active ? INVERTED :
8776# endif
8777 0);
8778 RedrawingDisabled = r;
8779 p_lz = p;
8780 out_flush();
8781#endif
8782}
8783
8784 static void
8785close_redir()
8786{
8787 if (redir_fd != NULL)
8788 {
8789 fclose(redir_fd);
8790 redir_fd = NULL;
8791 }
8792#ifdef FEAT_EVAL
8793 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008794 if (redir_vname)
8795 {
8796 var_redir_stop();
8797 redir_vname = 0;
8798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799#endif
8800}
8801
8802#if defined(FEAT_SESSION) && defined(USE_CRNL)
8803# define MKSESSION_NL
8804static int mksession_nl = FALSE; /* use NL only in put_eol() */
8805#endif
8806
8807/*
8808 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8809 */
8810 static void
8811ex_mkrc(eap)
8812 exarg_T *eap;
8813{
8814 FILE *fd;
8815 int failed = FALSE;
8816 char_u *fname;
8817#ifdef FEAT_BROWSE
8818 char_u *browseFile = NULL;
8819#endif
8820#ifdef FEAT_SESSION
8821 int view_session = FALSE;
8822 int using_vdir = FALSE; /* using 'viewdir'? */
8823 char_u *viewFile = NULL;
8824 unsigned *flagp;
8825#endif
8826
8827 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8828 {
8829#ifdef FEAT_SESSION
8830 view_session = TRUE;
8831#else
8832 ex_ni(eap);
8833 return;
8834#endif
8835 }
8836
8837#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008838 /* Use the short file name until ":lcd" is used. We also don't use the
8839 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008840 did_lcd = FALSE;
8841
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8843 if (eap->cmdidx == CMD_mkview
8844 && (*eap->arg == NUL
8845 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8846 {
8847 eap->forceit = TRUE;
8848 fname = get_view_file(*eap->arg);
8849 if (fname == NULL)
8850 return;
8851 viewFile = fname;
8852 using_vdir = TRUE;
8853 }
8854 else
8855#endif
8856 if (*eap->arg != NUL)
8857 fname = eap->arg;
8858 else if (eap->cmdidx == CMD_mkvimrc)
8859 fname = (char_u *)VIMRC_FILE;
8860#ifdef FEAT_SESSION
8861 else if (eap->cmdidx == CMD_mksession)
8862 fname = (char_u *)SESSION_FILE;
8863#endif
8864 else
8865 fname = (char_u *)EXRC_FILE;
8866
8867#ifdef FEAT_BROWSE
8868 if (cmdmod.browse)
8869 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008870 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871# ifdef FEAT_SESSION
8872 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8873 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8874# endif
8875 (char_u *)_("Save Setup"),
8876 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8877 if (browseFile == NULL)
8878 goto theend;
8879 fname = browseFile;
8880 eap->forceit = TRUE; /* since dialog already asked */
8881 }
8882#endif
8883
8884#if defined(FEAT_SESSION) && defined(vim_mkdir)
8885 /* When using 'viewdir' may have to create the directory. */
8886 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008887 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888#endif
8889
8890 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8891 if (fd != NULL)
8892 {
8893#ifdef FEAT_SESSION
8894 if (eap->cmdidx == CMD_mkview)
8895 flagp = &vop_flags;
8896 else
8897 flagp = &ssop_flags;
8898#endif
8899
8900#ifdef MKSESSION_NL
8901 /* "unix" in 'sessionoptions': use NL line separator */
8902 if (view_session && (*flagp & SSOP_UNIX))
8903 mksession_nl = TRUE;
8904#endif
8905
8906 /* Write the version command for :mkvimrc */
8907 if (eap->cmdidx == CMD_mkvimrc)
8908 (void)put_line(fd, "version 6.0");
8909
8910#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008911 if (eap->cmdidx == CMD_mksession)
8912 {
8913 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8914 failed = TRUE;
8915 }
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 if (eap->cmdidx != CMD_mkview)
8918#endif
8919 {
8920 /* Write setting 'compatible' first, because it has side effects.
8921 * For that same reason only do it when needed. */
8922 if (p_cp)
8923 (void)put_line(fd, "if !&cp | set cp | endif");
8924 else
8925 (void)put_line(fd, "if &cp | set nocp | endif");
8926 }
8927
8928#ifdef FEAT_SESSION
8929 if (!view_session
8930 || (eap->cmdidx == CMD_mksession
8931 && (*flagp & SSOP_OPTIONS)))
8932#endif
8933 failed |= (makemap(fd, NULL) == FAIL
8934 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8935
8936#ifdef FEAT_SESSION
8937 if (!failed && view_session)
8938 {
8939 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8940 failed = TRUE;
8941 if (eap->cmdidx == CMD_mksession)
8942 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008943 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
Bram Moolenaard9462e32011-04-11 21:35:11 +02008945 dirnow = alloc(MAXPATHL);
8946 if (dirnow == NULL)
8947 failed = TRUE;
8948 else
8949 {
8950 /*
8951 * Change to session file's dir.
8952 */
8953 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008955 *dirnow = NUL;
8956 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8957 {
8958 if (vim_chdirfile(fname) == OK)
8959 shorten_fnames(TRUE);
8960 }
8961 else if (*dirnow != NUL
8962 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8963 {
8964 if (mch_chdir((char *)globaldir) == 0)
8965 shorten_fnames(TRUE);
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaard9462e32011-04-11 21:35:11 +02008968 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969
Bram Moolenaard9462e32011-04-11 21:35:11 +02008970 /* restore original dir */
8971 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008973 {
8974 if (mch_chdir((char *)dirnow) != 0)
8975 EMSG(_(e_prev_dir));
8976 shorten_fnames(TRUE);
8977 }
8978 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 }
8980 }
8981 else
8982 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008983 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8984 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 }
8986 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8987 == FAIL)
8988 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008989 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8990 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008991 if (eap->cmdidx == CMD_mksession)
8992 {
8993 if (put_line(fd, "unlet SessionLoad") == FAIL)
8994 failed = TRUE;
8995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008998 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8999 failed = TRUE;
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 failed |= fclose(fd);
9002
9003 if (failed)
9004 EMSG(_(e_write));
9005#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9006 else if (eap->cmdidx == CMD_mksession)
9007 {
9008 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009009 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
Bram Moolenaard9462e32011-04-11 21:35:11 +02009011 tbuf = alloc(MAXPATHL);
9012 if (tbuf != NULL)
9013 {
9014 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9015 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9016 vim_free(tbuf);
9017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 }
9019#endif
9020#ifdef MKSESSION_NL
9021 mksession_nl = FALSE;
9022#endif
9023 }
9024
9025#ifdef FEAT_BROWSE
9026theend:
9027 vim_free(browseFile);
9028#endif
9029#ifdef FEAT_SESSION
9030 vim_free(viewFile);
9031#endif
9032}
9033
Bram Moolenaardf177f62005-02-22 08:39:57 +00009034#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9035 || defined(PROTO)
9036 int
9037vim_mkdir_emsg(name, prot)
9038 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009039 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009040{
9041 if (vim_mkdir(name, prot) != 0)
9042 {
9043 EMSG2(_("E739: Cannot create directory: %s"), name);
9044 return FAIL;
9045 }
9046 return OK;
9047}
9048#endif
9049
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050/*
9051 * Open a file for writing for an Ex command, with some checks.
9052 * Return file descriptor, or NULL on failure.
9053 */
9054 FILE *
9055open_exfile(fname, forceit, mode)
9056 char_u *fname;
9057 int forceit;
9058 char *mode; /* "w" for create new file or "a" for append */
9059{
9060 FILE *fd;
9061
9062#ifdef UNIX
9063 /* with Unix it is possible to open a directory */
9064 if (mch_isdir(fname))
9065 {
9066 EMSG2(_(e_isadir2), fname);
9067 return NULL;
9068 }
9069#endif
9070 if (!forceit && *mode != 'a' && vim_fexists(fname))
9071 {
9072 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9073 return NULL;
9074 }
9075
9076 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9077 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9078
9079 return fd;
9080}
9081
9082/*
9083 * ":mark" and ":k".
9084 */
9085 static void
9086ex_mark(eap)
9087 exarg_T *eap;
9088{
9089 pos_T pos;
9090
9091 if (*eap->arg == NUL) /* No argument? */
9092 EMSG(_(e_argreq));
9093 else if (eap->arg[1] != NUL) /* more than one character? */
9094 EMSG(_(e_trailing));
9095 else
9096 {
9097 pos = curwin->w_cursor; /* save curwin->w_cursor */
9098 curwin->w_cursor.lnum = eap->line2;
9099 beginline(BL_WHITE | BL_FIX);
9100 if (setmark(*eap->arg) == FAIL) /* set mark */
9101 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9102 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9103 }
9104}
9105
9106/*
9107 * Update w_topline, w_leftcol and the cursor position.
9108 */
9109 void
9110update_topline_cursor()
9111{
9112 check_cursor(); /* put cursor on valid line */
9113 update_topline();
9114 if (!curwin->w_p_wrap)
9115 validate_cursor();
9116 update_curswant();
9117}
9118
9119#ifdef FEAT_EX_EXTRA
9120/*
9121 * ":normal[!] {commands}": Execute normal mode commands.
9122 */
9123 static void
9124ex_normal(eap)
9125 exarg_T *eap;
9126{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 int save_msg_scroll = msg_scroll;
9128 int save_restart_edit = restart_edit;
9129 int save_msg_didout = msg_didout;
9130 int save_State = State;
9131 tasave_T tabuf;
9132 int save_insertmode = p_im;
9133 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009134 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#ifdef FEAT_MBYTE
9136 char_u *arg = NULL;
9137 int l;
9138 char_u *p;
9139#endif
9140
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009141 if (ex_normal_lock > 0)
9142 {
9143 EMSG(_(e_secure));
9144 return;
9145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 if (ex_normal_busy >= p_mmd)
9147 {
9148 EMSG(_("E192: Recursive use of :normal too deep"));
9149 return;
9150 }
9151 ++ex_normal_busy;
9152
9153 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9154 restart_edit = 0; /* don't go to Insert mode */
9155 p_im = FALSE; /* don't use 'insertmode' */
9156
9157#ifdef FEAT_MBYTE
9158 /*
9159 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9160 * this for the K_SPECIAL leading byte, otherwise special keys will not
9161 * work.
9162 */
9163 if (has_mbyte)
9164 {
9165 int len = 0;
9166
9167 /* Count the number of characters to be escaped. */
9168 for (p = eap->arg; *p != NUL; ++p)
9169 {
9170# ifdef FEAT_GUI
9171 if (*p == CSI) /* leadbyte CSI */
9172 len += 2;
9173# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009174 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9176# ifdef FEAT_GUI
9177 || *p == CSI
9178# endif
9179 )
9180 len += 2;
9181 }
9182 if (len > 0)
9183 {
9184 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9185 if (arg != NULL)
9186 {
9187 len = 0;
9188 for (p = eap->arg; *p != NUL; ++p)
9189 {
9190 arg[len++] = *p;
9191# ifdef FEAT_GUI
9192 if (*p == CSI)
9193 {
9194 arg[len++] = KS_EXTRA;
9195 arg[len++] = (int)KE_CSI;
9196 }
9197# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009198 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
9200 arg[len++] = *++p;
9201 if (*p == K_SPECIAL)
9202 {
9203 arg[len++] = KS_SPECIAL;
9204 arg[len++] = KE_FILLER;
9205 }
9206# ifdef FEAT_GUI
9207 else if (*p == CSI)
9208 {
9209 arg[len++] = KS_EXTRA;
9210 arg[len++] = (int)KE_CSI;
9211 }
9212# endif
9213 }
9214 arg[len] = NUL;
9215 }
9216 }
9217 }
9218 }
9219#endif
9220
9221 /*
9222 * Save the current typeahead. This is required to allow using ":normal"
9223 * from an event handler and makes sure we don't hang when the argument
9224 * ends with half a command.
9225 */
9226 save_typeahead(&tabuf);
9227 if (tabuf.typebuf_valid)
9228 {
9229 /*
9230 * Repeat the :normal command for each line in the range. When no
9231 * range given, execute it just once, without positioning the cursor
9232 * first.
9233 */
9234 do
9235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 if (eap->addr_count != 0)
9237 {
9238 curwin->w_cursor.lnum = eap->line1++;
9239 curwin->w_cursor.col = 0;
9240 }
9241
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009242 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243#ifdef FEAT_MBYTE
9244 arg != NULL ? arg :
9245#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009246 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 }
9248 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9249 }
9250
9251 /* Might not return to the main loop when in an event handler. */
9252 update_topline_cursor();
9253
9254 /* Restore the previous typeahead. */
9255 restore_typeahead(&tabuf);
9256
9257 --ex_normal_busy;
9258 msg_scroll = save_msg_scroll;
9259 restart_edit = save_restart_edit;
9260 p_im = save_insertmode;
9261 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009262 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9264
9265 /* Restore the state (needed when called from a function executed for
9266 * 'indentexpr'). */
9267 State = save_State;
9268#ifdef FEAT_MBYTE
9269 vim_free(arg);
9270#endif
9271}
9272
9273/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009274 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 */
9276 static void
9277ex_startinsert(eap)
9278 exarg_T *eap;
9279{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009280 if (eap->forceit)
9281 {
9282 coladvance((colnr_T)MAXCOL);
9283 curwin->w_curswant = MAXCOL;
9284 curwin->w_set_curswant = FALSE;
9285 }
9286
Bram Moolenaara40c5002005-01-09 21:16:21 +00009287 /* Ignore the command when already in Insert mode. Inserting an
9288 * expression register that invokes a function can do this. */
9289 if (State & INSERT)
9290 return;
9291
Bram Moolenaar64969662005-12-14 21:59:55 +00009292 if (eap->cmdidx == CMD_startinsert)
9293 restart_edit = 'a';
9294 else if (eap->cmdidx == CMD_startreplace)
9295 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009297 restart_edit = 'V';
9298
9299 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009301 if (eap->cmdidx == CMD_startinsert)
9302 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 curwin->w_curswant = 0; /* avoid MAXCOL */
9304 }
9305}
9306
9307/*
9308 * ":stopinsert"
9309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 static void
9311ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009312 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 restart_edit = 0;
9315 stop_insert_mode = TRUE;
9316}
9317#endif
9318
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009319#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9320/*
9321 * Execute normal mode command "cmd".
9322 * "remap" can be REMAP_NONE or REMAP_YES.
9323 */
9324 void
9325exec_normal_cmd(cmd, remap, silent)
9326 char_u *cmd;
9327 int remap;
9328 int silent;
9329{
9330 oparg_T oa;
9331
9332 /*
9333 * Stuff the argument into the typeahead buffer.
9334 * Execute normal_cmd() until there is no typeahead left.
9335 */
9336 clear_oparg(&oa);
9337 finish_op = FALSE;
9338 ins_typebuf(cmd, remap, 0, TRUE, silent);
9339 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9340 && !got_int)
9341 {
9342 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009343 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009344 }
9345}
9346#endif
9347
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348#ifdef FEAT_FIND_ID
9349 static void
9350ex_checkpath(eap)
9351 exarg_T *eap;
9352{
9353 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9354 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9355 (linenr_T)1, (linenr_T)MAXLNUM);
9356}
9357
9358#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9359/*
9360 * ":psearch"
9361 */
9362 static void
9363ex_psearch(eap)
9364 exarg_T *eap;
9365{
9366 g_do_tagpreview = p_pvh;
9367 ex_findpat(eap);
9368 g_do_tagpreview = 0;
9369}
9370#endif
9371
9372 static void
9373ex_findpat(eap)
9374 exarg_T *eap;
9375{
9376 int whole = TRUE;
9377 long n;
9378 char_u *p;
9379 int action;
9380
9381 switch (cmdnames[eap->cmdidx].cmd_name[2])
9382 {
9383 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9384 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9385 action = ACTION_GOTO;
9386 else
9387 action = ACTION_SHOW;
9388 break;
9389 case 'i': /* ":ilist" and ":dlist" */
9390 action = ACTION_SHOW_ALL;
9391 break;
9392 case 'u': /* ":ijump" and ":djump" */
9393 action = ACTION_GOTO;
9394 break;
9395 default: /* ":isplit" and ":dsplit" */
9396 action = ACTION_SPLIT;
9397 break;
9398 }
9399
9400 n = 1;
9401 if (vim_isdigit(*eap->arg)) /* get count */
9402 {
9403 n = getdigits(&eap->arg);
9404 eap->arg = skipwhite(eap->arg);
9405 }
9406 if (*eap->arg == '/') /* Match regexp, not just whole words */
9407 {
9408 whole = FALSE;
9409 ++eap->arg;
9410 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9411 if (*p)
9412 {
9413 *p++ = NUL;
9414 p = skipwhite(p);
9415
9416 /* Check for trailing illegal characters */
9417 if (!ends_excmd(*p))
9418 eap->errmsg = e_trailing;
9419 else
9420 eap->nextcmd = check_nextcmd(p);
9421 }
9422 }
9423 if (!eap->skip)
9424 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9425 whole, !eap->forceit,
9426 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9427 n, action, eap->line1, eap->line2);
9428}
9429#endif
9430
9431#ifdef FEAT_WINDOWS
9432
9433# ifdef FEAT_QUICKFIX
9434/*
9435 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9436 */
9437 static void
9438ex_ptag(eap)
9439 exarg_T *eap;
9440{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009441 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9443}
9444
9445/*
9446 * ":pedit"
9447 */
9448 static void
9449ex_pedit(eap)
9450 exarg_T *eap;
9451{
9452 win_T *curwin_save = curwin;
9453
9454 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009455 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 keep_help_flag = curwin_save->w_buffer->b_help;
9457 do_exedit(eap, NULL);
9458 keep_help_flag = FALSE;
9459 if (curwin != curwin_save && win_valid(curwin_save))
9460 {
9461 /* Return cursor to where we were */
9462 validate_cursor();
9463 redraw_later(VALID);
9464 win_enter(curwin_save, TRUE);
9465 }
9466 g_do_tagpreview = 0;
9467}
9468# endif
9469
9470/*
9471 * ":stag", ":stselect" and ":stjump".
9472 */
9473 static void
9474ex_stag(eap)
9475 exarg_T *eap;
9476{
9477 postponed_split = -1;
9478 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009479 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9481 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009482 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484#endif
9485
9486/*
9487 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9488 */
9489 static void
9490ex_tag(eap)
9491 exarg_T *eap;
9492{
9493 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9494}
9495
9496 static void
9497ex_tag_cmd(eap, name)
9498 exarg_T *eap;
9499 char_u *name;
9500{
9501 int cmd;
9502
9503 switch (name[1])
9504 {
9505 case 'j': cmd = DT_JUMP; /* ":tjump" */
9506 break;
9507 case 's': cmd = DT_SELECT; /* ":tselect" */
9508 break;
9509 case 'p': cmd = DT_PREV; /* ":tprevious" */
9510 break;
9511 case 'N': cmd = DT_PREV; /* ":tNext" */
9512 break;
9513 case 'n': cmd = DT_NEXT; /* ":tnext" */
9514 break;
9515 case 'o': cmd = DT_POP; /* ":pop" */
9516 break;
9517 case 'f': /* ":tfirst" */
9518 case 'r': cmd = DT_FIRST; /* ":trewind" */
9519 break;
9520 case 'l': cmd = DT_LAST; /* ":tlast" */
9521 break;
9522 default: /* ":tag" */
9523#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009524 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 {
9526 do_cstag(eap);
9527 return;
9528 }
9529#endif
9530 cmd = DT_TAG;
9531 break;
9532 }
9533
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009534 if (name[0] == 'l')
9535 {
9536#ifndef FEAT_QUICKFIX
9537 ex_ni(eap);
9538 return;
9539#else
9540 cmd = DT_LTAG;
9541#endif
9542 }
9543
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9545 eap->forceit, TRUE);
9546}
9547
9548/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009549 * Check "str" for starting with a special cmdline variable.
9550 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9551 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9552 */
9553 int
9554find_cmdline_var(src, usedlen)
9555 char_u *src;
9556 int *usedlen;
9557{
9558 int len;
9559 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009560 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009561 "%",
9562#define SPEC_PERC 0
9563 "#",
9564#define SPEC_HASH 1
9565 "<cword>", /* cursor word */
9566#define SPEC_CWORD 2
9567 "<cWORD>", /* cursor WORD */
9568#define SPEC_CCWORD 3
9569 "<cfile>", /* cursor path name */
9570#define SPEC_CFILE 4
9571 "<sfile>", /* ":so" file name */
9572#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009573 "<slnum>", /* ":so" file line number */
9574#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009575#ifdef FEAT_AUTOCMD
9576 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009577# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009578 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009579# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009580 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009581# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009582#endif
9583#ifdef FEAT_CLIENTSERVER
9584 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009585# ifdef FEAT_AUTOCMD
9586# define SPEC_CLIENT 10
9587# else
9588# define SPEC_CLIENT 7
9589# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009590#endif
9591 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009592
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009593 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009594 {
9595 len = (int)STRLEN(spec_str[i]);
9596 if (STRNCMP(src, spec_str[i], len) == 0)
9597 {
9598 *usedlen = len;
9599 return i;
9600 }
9601 }
9602 return -1;
9603}
9604
9605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 * Evaluate cmdline variables.
9607 *
9608 * change '%' to curbuf->b_ffname
9609 * '#' to curwin->w_altfile
9610 * '<cword>' to word under the cursor
9611 * '<cWORD>' to WORD under the cursor
9612 * '<cfile>' to path name under the cursor
9613 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009614 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 * '<afile>' to file name for autocommand
9616 * '<abuf>' to buffer number for autocommand
9617 * '<amatch>' to matching name for autocommand
9618 *
9619 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9620 * "" for error without a message) and NULL is returned.
9621 * Returns an allocated string if a valid match was found.
9622 * Returns NULL if no match was found. "usedlen" then still contains the
9623 * number of characters to skip.
9624 */
9625 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009626eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009628 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 int *usedlen; /* characters after src that are used */
9630 linenr_T *lnump; /* line number for :e command, or NULL */
9631 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009632 int *escaped; /* return value has escaped white space (can
9633 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 int i;
9636 char_u *s;
9637 char_u *result;
9638 char_u *resultbuf = NULL;
9639 int resultlen;
9640 buf_T *buf;
9641 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9642 int spec_idx;
9643#ifdef FEAT_MODIFY_FNAME
9644 int skip_mod = FALSE;
9645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647
9648 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009649 if (escaped != NULL)
9650 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651
9652 /*
9653 * Check if there is something to do.
9654 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009655 spec_idx = find_cmdline_var(src, usedlen);
9656 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
9658 *usedlen = 1;
9659 return NULL;
9660 }
9661
9662 /*
9663 * Skip when preceded with a backslash "\%" and "\#".
9664 * Note: In "\\%" the % is also not recognized!
9665 */
9666 if (src > srcstart && src[-1] == '\\')
9667 {
9668 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009669 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 return NULL;
9671 }
9672
9673 /*
9674 * word or WORD under cursor
9675 */
9676 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9677 {
9678 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9679 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9680 if (resultlen == 0)
9681 {
9682 *errormsg = (char_u *)"";
9683 return NULL;
9684 }
9685 }
9686
9687 /*
9688 * '#': Alternate file name
9689 * '%': Current file name
9690 * File name under the cursor
9691 * File name for autocommand
9692 * and following modifiers
9693 */
9694 else
9695 {
9696 switch (spec_idx)
9697 {
9698 case SPEC_PERC: /* '%': current file */
9699 if (curbuf->b_fname == NULL)
9700 {
9701 result = (char_u *)"";
9702 valid = 0; /* Must have ":p:h" to be valid */
9703 }
9704 else
9705#ifdef RISCOS
9706 /* Always use the full path for RISC OS if possible. */
9707 result = curbuf->b_ffname;
9708 if (result == NULL)
9709 result = curbuf->b_fname;
9710#else
9711 result = curbuf->b_fname;
9712#endif
9713 break;
9714
9715 case SPEC_HASH: /* '#' or "#99": alternate file */
9716 if (src[1] == '#') /* "##": the argument list */
9717 {
9718 result = arg_all();
9719 resultbuf = result;
9720 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009721 if (escaped != NULL)
9722 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723#ifdef FEAT_MODIFY_FNAME
9724 skip_mod = TRUE;
9725#endif
9726 break;
9727 }
9728 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009729 if (*s == '<') /* "#<99" uses v:oldfiles */
9730 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 i = (int)getdigits(&s);
9732 *usedlen = (int)(s - src); /* length of what we expand */
9733
Bram Moolenaard812df62008-11-09 12:46:09 +00009734 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009736 if (*usedlen < 2)
9737 {
9738 /* Should we give an error message for #<text? */
9739 *usedlen = 1;
9740 return NULL;
9741 }
9742#ifdef FEAT_EVAL
9743 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9744 (long)i);
9745 if (result == NULL)
9746 {
9747 *errormsg = (char_u *)"";
9748 return NULL;
9749 }
9750#else
9751 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009756 {
9757 buf = buflist_findnr(i);
9758 if (buf == NULL)
9759 {
9760 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9761 return NULL;
9762 }
9763 if (lnump != NULL)
9764 *lnump = ECMD_LAST;
9765 if (buf->b_fname == NULL)
9766 {
9767 result = (char_u *)"";
9768 valid = 0; /* Must have ":p:h" to be valid */
9769 }
9770 else
9771 result = buf->b_fname;
9772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 break;
9774
9775#ifdef FEAT_SEARCHPATH
9776 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009777 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 if (result == NULL)
9779 {
9780 *errormsg = (char_u *)"";
9781 return NULL;
9782 }
9783 resultbuf = result; /* remember allocated string */
9784 break;
9785#endif
9786
9787#ifdef FEAT_AUTOCMD
9788 case SPEC_AFILE: /* file name for autocommand */
9789 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009790 if (result != NULL && !autocmd_fname_full)
9791 {
9792 /* Still need to turn the fname into a full path. It is
9793 * postponed to avoid a delay when <afile> is not used. */
9794 autocmd_fname_full = TRUE;
9795 result = FullName_save(autocmd_fname, FALSE);
9796 vim_free(autocmd_fname);
9797 autocmd_fname = result;
9798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 if (result == NULL)
9800 {
9801 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9802 return NULL;
9803 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009804 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 break;
9806
9807 case SPEC_ABUF: /* buffer number for autocommand */
9808 if (autocmd_bufnr <= 0)
9809 {
9810 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9811 return NULL;
9812 }
9813 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9814 result = strbuf;
9815 break;
9816
9817 case SPEC_AMATCH: /* match name for autocommand */
9818 result = autocmd_match;
9819 if (result == NULL)
9820 {
9821 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9822 return NULL;
9823 }
9824 break;
9825
9826#endif
9827 case SPEC_SFILE: /* file name for ":so" command */
9828 result = sourcing_name;
9829 if (result == NULL)
9830 {
9831 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9832 return NULL;
9833 }
9834 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009835 case SPEC_SLNUM: /* line in file for ":so" command */
9836 if (sourcing_name == NULL || sourcing_lnum == 0)
9837 {
9838 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9839 return NULL;
9840 }
9841 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9842 result = strbuf;
9843 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844#if defined(FEAT_CLIENTSERVER)
9845 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009846 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9847 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 result = strbuf;
9849 break;
9850#endif
9851 }
9852
9853 resultlen = (int)STRLEN(result); /* length of new string */
9854 if (src[*usedlen] == '<') /* remove the file name extension */
9855 {
9856 ++*usedlen;
9857#ifdef RISCOS
9858 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9859#else
9860 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9861#endif
9862 resultlen = (int)(s - result);
9863 }
9864#ifdef FEAT_MODIFY_FNAME
9865 else if (!skip_mod)
9866 {
9867 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9868 &resultlen);
9869 if (result == NULL)
9870 {
9871 *errormsg = (char_u *)"";
9872 return NULL;
9873 }
9874 }
9875#endif
9876 }
9877
9878 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9879 {
9880 if (valid != VALID_HEAD + VALID_PATH)
9881 /* xgettext:no-c-format */
9882 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9883 else
9884 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9885 result = NULL;
9886 }
9887 else
9888 result = vim_strnsave(result, resultlen);
9889 vim_free(resultbuf);
9890 return result;
9891}
9892
9893/*
9894 * Concatenate all files in the argument list, separated by spaces, and return
9895 * it in one allocated string.
9896 * Spaces and backslashes in the file names are escaped with a backslash.
9897 * Returns NULL when out of memory.
9898 */
9899 static char_u *
9900arg_all()
9901{
9902 int len;
9903 int idx;
9904 char_u *retval = NULL;
9905 char_u *p;
9906
9907 /*
9908 * Do this loop two times:
9909 * first time: compute the total length
9910 * second time: concatenate the names
9911 */
9912 for (;;)
9913 {
9914 len = 0;
9915 for (idx = 0; idx < ARGCOUNT; ++idx)
9916 {
9917 p = alist_name(&ARGLIST[idx]);
9918 if (p != NULL)
9919 {
9920 if (len > 0)
9921 {
9922 /* insert a space in between names */
9923 if (retval != NULL)
9924 retval[len] = ' ';
9925 ++len;
9926 }
9927 for ( ; *p != NUL; ++p)
9928 {
9929 if (*p == ' ' || *p == '\\')
9930 {
9931 /* insert a backslash */
9932 if (retval != NULL)
9933 retval[len] = '\\';
9934 ++len;
9935 }
9936 if (retval != NULL)
9937 retval[len] = *p;
9938 ++len;
9939 }
9940 }
9941 }
9942
9943 /* second time: break here */
9944 if (retval != NULL)
9945 {
9946 retval[len] = NUL;
9947 break;
9948 }
9949
9950 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009951 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (retval == NULL)
9953 break;
9954 }
9955
9956 return retval;
9957}
9958
9959#if defined(FEAT_AUTOCMD) || defined(PROTO)
9960/*
9961 * Expand the <sfile> string in "arg".
9962 *
9963 * Returns an allocated string, or NULL for any error.
9964 */
9965 char_u *
9966expand_sfile(arg)
9967 char_u *arg;
9968{
9969 char_u *errormsg;
9970 int len;
9971 char_u *result;
9972 char_u *newres;
9973 char_u *repl;
9974 int srclen;
9975 char_u *p;
9976
9977 result = vim_strsave(arg);
9978 if (result == NULL)
9979 return NULL;
9980
9981 for (p = result; *p; )
9982 {
9983 if (STRNCMP(p, "<sfile>", 7) != 0)
9984 ++p;
9985 else
9986 {
9987 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009988 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 if (errormsg != NULL)
9990 {
9991 if (*errormsg)
9992 emsg(errormsg);
9993 vim_free(result);
9994 return NULL;
9995 }
9996 if (repl == NULL) /* no match (cannot happen) */
9997 {
9998 p += srclen;
9999 continue;
10000 }
10001 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10002 newres = alloc(len);
10003 if (newres == NULL)
10004 {
10005 vim_free(repl);
10006 vim_free(result);
10007 return NULL;
10008 }
10009 mch_memmove(newres, result, (size_t)(p - result));
10010 STRCPY(newres + (p - result), repl);
10011 len = (int)STRLEN(newres);
10012 STRCAT(newres, p + srclen);
10013 vim_free(repl);
10014 vim_free(result);
10015 result = newres;
10016 p = newres + len; /* continue after the match */
10017 }
10018 }
10019
10020 return result;
10021}
10022#endif
10023
10024#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010025static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10026 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10028static frame_T *ses_skipframe __ARGS((frame_T *fr));
10029static int ses_do_frame __ARGS((frame_T *fr));
10030static int ses_do_win __ARGS((win_T *wp));
10031static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10032static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10033static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10034
10035/*
10036 * Write openfile commands for the current buffers to an .exrc file.
10037 * Return FAIL on error, OK otherwise.
10038 */
10039 static int
10040makeopens(fd, dirnow)
10041 FILE *fd;
10042 char_u *dirnow; /* Current directory name */
10043{
10044 buf_T *buf;
10045 int only_save_windows = TRUE;
10046 int nr;
10047 int cnr = 1;
10048 int restore_size = TRUE;
10049 win_T *wp;
10050 char_u *sname;
10051 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010052 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010053 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010054 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010055 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010056 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
10058 if (ssop_flags & SSOP_BUFFERS)
10059 only_save_windows = FALSE; /* Save ALL buffers */
10060
10061 /*
10062 * Begin by setting the this_session variable, and then other
10063 * sessionable variables.
10064 */
10065#ifdef FEAT_EVAL
10066 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10067 return FAIL;
10068 if (ssop_flags & SSOP_GLOBALS)
10069 if (store_session_globals(fd) == FAIL)
10070 return FAIL;
10071#endif
10072
10073 /*
10074 * Close all windows but one.
10075 */
10076 if (put_line(fd, "silent only") == FAIL)
10077 return FAIL;
10078
10079 /*
10080 * Now a :cd command to the session directory or the current directory
10081 */
10082 if (ssop_flags & SSOP_SESDIR)
10083 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010084 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10085 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 return FAIL;
10087 }
10088 else if (ssop_flags & SSOP_CURDIR)
10089 {
10090 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10091 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010092 || fputs("cd ", fd) < 0
10093 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10094 || put_eol(fd) == FAIL)
10095 {
10096 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 vim_free(sname);
10100 }
10101
10102 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010103 * If there is an empty, unnamed buffer we will wipe it out later.
10104 * Remember the buffer number.
10105 */
10106 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10107 return FAIL;
10108 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10109 return FAIL;
10110 if (put_line(fd, "endif") == FAIL)
10111 return FAIL;
10112
10113 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 * Now save the current files, current buffer first.
10115 */
10116 if (put_line(fd, "set shortmess=aoO") == FAIL)
10117 return FAIL;
10118
10119 /* Now put the other buffers into the buffer list */
10120 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10121 {
10122 if (!(only_save_windows && buf->b_nwindows == 0)
10123 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10124 && buf->b_fname != NULL
10125 && buf->b_p_bl)
10126 {
10127 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10128 : buf->b_wininfo->wi_fpos.lnum) < 0
10129 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10130 return FAIL;
10131 }
10132 }
10133
10134 /* the global argument list */
10135 if (ses_arglist(fd, "args", &global_alist.al_ga,
10136 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10137 return FAIL;
10138
10139 if (ssop_flags & SSOP_RESIZE)
10140 {
10141 /* Note: after the restore we still check it worked!*/
10142 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10143 || put_eol(fd) == FAIL)
10144 return FAIL;
10145 }
10146
10147#ifdef FEAT_GUI
10148 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10149 {
10150 int x, y;
10151
10152 if (gui_mch_get_winpos(&x, &y) == OK)
10153 {
10154 /* Note: after the restore we still check it worked!*/
10155 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10156 return FAIL;
10157 }
10158 }
10159#endif
10160
10161 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010162 * May repeat putting Windows for each tab, when "tabpages" is in
10163 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010164 * Don't use goto_tabpage(), it may change directory and trigger
10165 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010167 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010168 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010169 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010171 int need_tabnew = FALSE;
10172
Bram Moolenaar18144c82006-04-12 21:52:12 +000010173 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010175 tabpage_T *tp = find_tabpage(tabnr);
10176
10177 if (tp == NULL)
10178 break; /* done all tab pages */
10179 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010180 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010181 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010182 tab_topframe = topframe;
10183 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010184 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010185 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010186 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010187 tab_topframe = tp->tp_topframe;
10188 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010189 if (tabnr > 1)
10190 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaar18144c82006-04-12 21:52:12 +000010193 /*
10194 * Before creating the window layout, try loading one file. If this
10195 * is aborted we don't end up with a number of useless windows.
10196 * This may have side effects! (e.g., compressed or network file).
10197 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010198 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010199 {
10200 if (ses_do_win(wp)
10201 && wp->w_buffer->b_ffname != NULL
10202 && !wp->w_buffer->b_help
10203#ifdef FEAT_QUICKFIX
10204 && !bt_nofile(wp->w_buffer)
10205#endif
10206 )
10207 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010208 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010209 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10210 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010211 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010212 if (!wp->w_arg_idx_invalid)
10213 edited_win = wp;
10214 break;
10215 }
10216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010218 /* If no file got edited create an empty tab page. */
10219 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10220 return FAIL;
10221
Bram Moolenaar18144c82006-04-12 21:52:12 +000010222 /*
10223 * Save current window layout.
10224 */
10225 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010227 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010229 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10230 return FAIL;
10231 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10232 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
Bram Moolenaar18144c82006-04-12 21:52:12 +000010234 /*
10235 * Check if window sizes can be restored (no windows omitted).
10236 * Remember the window number of the current window after restoring.
10237 */
10238 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010239 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010240 {
10241 if (ses_do_win(wp))
10242 ++nr;
10243 else
10244 restore_size = FALSE;
10245 if (curwin == wp)
10246 cnr = nr;
10247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248
Bram Moolenaar18144c82006-04-12 21:52:12 +000010249 /* Go to the first window. */
10250 if (put_line(fd, "wincmd t") == FAIL)
10251 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010252
Bram Moolenaar18144c82006-04-12 21:52:12 +000010253 /*
10254 * If more than one window, see if sizes can be restored.
10255 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10256 * resized when moving between windows.
10257 * Do this before restoring the view, so that the topline and the
10258 * cursor can be set. This is done again below.
10259 */
10260 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10261 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010262 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264
Bram Moolenaar18144c82006-04-12 21:52:12 +000010265 /*
10266 * Restore the view of the window (options, file, cursor, etc.).
10267 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010268 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010269 {
10270 if (!ses_do_win(wp))
10271 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010272 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10273 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010274 return FAIL;
10275 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10276 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010277 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010278 }
10279
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010280 /* The argument index in the first tab page is zero, need to set it in
10281 * each window. For further tab pages it's the window where we do
10282 * "tabedit". */
10283 cur_arg_idx = next_arg_idx;
10284
Bram Moolenaar18144c82006-04-12 21:52:12 +000010285 /*
10286 * Restore cursor to the current window if it's not the first one.
10287 */
10288 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10289 || put_eol(fd) == FAIL))
10290 return FAIL;
10291
10292 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010293 * Restore window sizes again after jumping around in windows, because
10294 * the current window has a minimum size while others may not.
10295 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010296 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010297 return FAIL;
10298
Bram Moolenaar18144c82006-04-12 21:52:12 +000010299 /* Don't continue in another tab page when doing only the current one
10300 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010301 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010302 break;
10303 }
10304
10305 if (ssop_flags & SSOP_TABPAGES)
10306 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010307 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10308 || put_eol(fd) == FAIL)
10309 return FAIL;
10310 }
10311
Bram Moolenaar9c102382006-05-03 21:26:49 +000010312 /*
10313 * Wipe out an empty unnamed buffer we started in.
10314 */
10315 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10316 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010317 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010318 return FAIL;
10319 if (put_line(fd, "endif") == FAIL)
10320 return FAIL;
10321 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10322 return FAIL;
10323
10324 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10325 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10326 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328
10329 /*
10330 * Lastly, execute the x.vim file if it exists.
10331 */
10332 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10333 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010334 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 || put_line(fd, "endif") == FAIL)
10336 return FAIL;
10337
10338 return OK;
10339}
10340
10341 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010342ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 FILE *fd;
10344 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010345 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346{
10347 int n = 0;
10348 win_T *wp;
10349
10350 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10351 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010352 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 {
10354 if (!ses_do_win(wp))
10355 continue;
10356 ++n;
10357
10358 /* restore height when not full height */
10359 if (wp->w_height + wp->w_status_height < topframe->fr_height
10360 && (fprintf(fd,
10361 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10362 n, (long)wp->w_height, Rows / 2, Rows) < 0
10363 || put_eol(fd) == FAIL))
10364 return FAIL;
10365
10366 /* restore width when not full width */
10367 if (wp->w_width < Columns && (fprintf(fd,
10368 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10369 n, (long)wp->w_width, Columns / 2, Columns) < 0
10370 || put_eol(fd) == FAIL))
10371 return FAIL;
10372 }
10373 }
10374 else
10375 {
10376 /* Just equalise window sizes */
10377 if (put_line(fd, "wincmd =") == FAIL)
10378 return FAIL;
10379 }
10380 return OK;
10381}
10382
10383/*
10384 * Write commands to "fd" to recursively create windows for frame "fr",
10385 * horizontally and vertically split.
10386 * After the commands the last window in the frame is the current window.
10387 * Returns FAIL when writing the commands to "fd" fails.
10388 */
10389 static int
10390ses_win_rec(fd, fr)
10391 FILE *fd;
10392 frame_T *fr;
10393{
10394 frame_T *frc;
10395 int count = 0;
10396
10397 if (fr->fr_layout != FR_LEAF)
10398 {
10399 /* Find first frame that's not skipped and then create a window for
10400 * each following one (first frame is already there). */
10401 frc = ses_skipframe(fr->fr_child);
10402 if (frc != NULL)
10403 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10404 {
10405 /* Make window as big as possible so that we have lots of room
10406 * to split. */
10407 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10408 || put_line(fd, fr->fr_layout == FR_COL
10409 ? "split" : "vsplit") == FAIL)
10410 return FAIL;
10411 ++count;
10412 }
10413
10414 /* Go back to the first window. */
10415 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10416 ? "%dwincmd k" : "%dwincmd h", count) < 0
10417 || put_eol(fd) == FAIL))
10418 return FAIL;
10419
10420 /* Recursively create frames/windows in each window of this column or
10421 * row. */
10422 frc = ses_skipframe(fr->fr_child);
10423 while (frc != NULL)
10424 {
10425 ses_win_rec(fd, frc);
10426 frc = ses_skipframe(frc->fr_next);
10427 /* Go to next window. */
10428 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10429 return FAIL;
10430 }
10431 }
10432 return OK;
10433}
10434
10435/*
10436 * Skip frames that don't contain windows we want to save in the Session.
10437 * Returns NULL when there none.
10438 */
10439 static frame_T *
10440ses_skipframe(fr)
10441 frame_T *fr;
10442{
10443 frame_T *frc;
10444
10445 for (frc = fr; frc != NULL; frc = frc->fr_next)
10446 if (ses_do_frame(frc))
10447 break;
10448 return frc;
10449}
10450
10451/*
10452 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10453 * the Session.
10454 */
10455 static int
10456ses_do_frame(fr)
10457 frame_T *fr;
10458{
10459 frame_T *frc;
10460
10461 if (fr->fr_layout == FR_LEAF)
10462 return ses_do_win(fr->fr_win);
10463 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10464 if (ses_do_frame(frc))
10465 return TRUE;
10466 return FALSE;
10467}
10468
10469/*
10470 * Return non-zero if window "wp" is to be stored in the Session.
10471 */
10472 static int
10473ses_do_win(wp)
10474 win_T *wp;
10475{
10476 if (wp->w_buffer->b_fname == NULL
10477#ifdef FEAT_QUICKFIX
10478 /* When 'buftype' is "nofile" can't restore the window contents. */
10479 || bt_nofile(wp->w_buffer)
10480#endif
10481 )
10482 return (ssop_flags & SSOP_BLANK);
10483 if (wp->w_buffer->b_help)
10484 return (ssop_flags & SSOP_HELP);
10485 return TRUE;
10486}
10487
10488/*
10489 * Write commands to "fd" to restore the view of a window.
10490 * Caller must make sure 'scrolloff' is zero.
10491 */
10492 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010493put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 FILE *fd;
10495 win_T *wp;
10496 int add_edit; /* add ":edit" command to view */
10497 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010498 int current_arg_idx; /* current argument index of the window, use
10499 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 win_T *save_curwin;
10502 int f;
10503 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010504 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505
10506 /* Always restore cursor position for ":mksession". For ":mkview" only
10507 * when 'viewoptions' contains "cursor". */
10508 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10509
10510 /*
10511 * Local argument list.
10512 */
10513 if (wp->w_alist == &global_alist)
10514 {
10515 if (put_line(fd, "argglobal") == FAIL)
10516 return FAIL;
10517 }
10518 else
10519 {
10520 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10521 flagp == &vop_flags
10522 || !(*flagp & SSOP_CURDIR)
10523 || wp->w_localdir != NULL, flagp) == FAIL)
10524 return FAIL;
10525 }
10526
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010527 /* Only when part of a session: restore the argument index. Some
10528 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010529 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010530 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010532 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 || put_eol(fd) == FAIL)
10534 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010535 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 }
10537
10538 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010539 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 {
10541 /*
10542 * Load the file.
10543 */
10544 if (wp->w_buffer->b_ffname != NULL
10545#ifdef FEAT_QUICKFIX
10546 && !bt_nofile(wp->w_buffer)
10547#endif
10548 )
10549 {
10550 /*
10551 * Editing a file in this buffer: use ":edit file".
10552 * This may have side effects! (e.g., compressed or network file).
10553 */
10554 if (fputs("edit ", fd) < 0
10555 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10556 return FAIL;
10557 }
10558 else
10559 {
10560 /* No file in this buffer, just make it empty. */
10561 if (put_line(fd, "enew") == FAIL)
10562 return FAIL;
10563#ifdef FEAT_QUICKFIX
10564 if (wp->w_buffer->b_ffname != NULL)
10565 {
10566 /* The buffer does have a name, but it's not a file name. */
10567 if (fputs("file ", fd) < 0
10568 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10569 return FAIL;
10570 }
10571#endif
10572 do_cursor = FALSE;
10573 }
10574 }
10575
10576 /*
10577 * Local mappings and abbreviations.
10578 */
10579 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10580 && makemap(fd, wp->w_buffer) == FAIL)
10581 return FAIL;
10582
10583 /*
10584 * Local options. Need to go to the window temporarily.
10585 * Store only local values when using ":mkview" and when ":mksession" is
10586 * used and 'sessionoptions' doesn't include "options".
10587 * Some folding options are always stored when "folds" is included,
10588 * otherwise the folds would not be restored correctly.
10589 */
10590 save_curwin = curwin;
10591 curwin = wp;
10592 curbuf = curwin->w_buffer;
10593 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10594 f = makeset(fd, OPT_LOCAL,
10595 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10596#ifdef FEAT_FOLDING
10597 else if (*flagp & SSOP_FOLDS)
10598 f = makefoldset(fd);
10599#endif
10600 else
10601 f = OK;
10602 curwin = save_curwin;
10603 curbuf = curwin->w_buffer;
10604 if (f == FAIL)
10605 return FAIL;
10606
10607#ifdef FEAT_FOLDING
10608 /*
10609 * Save Folds when 'buftype' is empty and for help files.
10610 */
10611 if ((*flagp & SSOP_FOLDS)
10612 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010613# ifdef FEAT_QUICKFIX
10614 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10615# endif
10616 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 {
10618 if (put_folds(fd, wp) == FAIL)
10619 return FAIL;
10620 }
10621#endif
10622
10623 /*
10624 * Set the cursor after creating folds, since that moves the cursor.
10625 */
10626 if (do_cursor)
10627 {
10628
10629 /* Restore the cursor line in the file and relatively in the
10630 * window. Don't use "G", it changes the jumplist. */
10631 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10632 (long)wp->w_cursor.lnum,
10633 (long)(wp->w_cursor.lnum - wp->w_topline),
10634 (long)wp->w_height / 2, (long)wp->w_height) < 0
10635 || put_eol(fd) == FAIL
10636 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10637 || put_line(fd, "exe s:l") == FAIL
10638 || put_line(fd, "normal! zt") == FAIL
10639 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10640 || put_eol(fd) == FAIL)
10641 return FAIL;
10642 /* Restore the cursor column and left offset when not wrapping. */
10643 if (wp->w_cursor.col == 0)
10644 {
10645 if (put_line(fd, "normal! 0") == FAIL)
10646 return FAIL;
10647 }
10648 else
10649 {
10650 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10651 {
10652 if (fprintf(fd,
10653 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10654 (long)wp->w_cursor.col,
10655 (long)(wp->w_cursor.col - wp->w_leftcol),
10656 (long)wp->w_width / 2, (long)wp->w_width) < 0
10657 || put_eol(fd) == FAIL
10658 || put_line(fd, "if s:c > 0") == FAIL
10659 || fprintf(fd,
10660 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10661 (long)wp->w_cursor.col) < 0
10662 || put_eol(fd) == FAIL
10663 || put_line(fd, "else") == FAIL
10664 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10665 || put_eol(fd) == FAIL
10666 || put_line(fd, "endif") == FAIL)
10667 return FAIL;
10668 }
10669 else
10670 {
10671 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10672 || put_eol(fd) == FAIL)
10673 return FAIL;
10674 }
10675 }
10676 }
10677
10678 /*
10679 * Local directory.
10680 */
10681 if (wp->w_localdir != NULL)
10682 {
10683 if (fputs("lcd ", fd) < 0
10684 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10685 || put_eol(fd) == FAIL)
10686 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010687 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 }
10689
10690 return OK;
10691}
10692
10693/*
10694 * Write an argument list to the session file.
10695 * Returns FAIL if writing fails.
10696 */
10697 static int
10698ses_arglist(fd, cmd, gap, fullname, flagp)
10699 FILE *fd;
10700 char *cmd;
10701 garray_T *gap;
10702 int fullname; /* TRUE: use full path name */
10703 unsigned *flagp;
10704{
10705 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010706 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 char_u *s;
10708
10709 if (gap->ga_len == 0)
10710 return put_line(fd, "silent! argdel *");
10711 if (fputs(cmd, fd) < 0)
10712 return FAIL;
10713 for (i = 0; i < gap->ga_len; ++i)
10714 {
10715 /* NULL file names are skipped (only happens when out of memory). */
10716 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10717 if (s != NULL)
10718 {
10719 if (fullname)
10720 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010721 buf = alloc(MAXPATHL);
10722 if (buf != NULL)
10723 {
10724 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10725 s = buf;
10726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 }
10728 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010729 {
10730 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010732 }
10733 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 }
10735 }
10736 return put_eol(fd);
10737}
10738
10739/*
10740 * Write a buffer name to the session file.
10741 * Also ends the line.
10742 * Returns FAIL if writing fails.
10743 */
10744 static int
10745ses_fname(fd, buf, flagp)
10746 FILE *fd;
10747 buf_T *buf;
10748 unsigned *flagp;
10749{
10750 char_u *name;
10751
10752 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010753 * the session file will be sourced.
10754 * Don't do this for ":mkview", we don't know the current directory.
10755 * Don't do this after ":lcd", we don't keep track of what the current
10756 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (buf->b_sfname != NULL
10758 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010759 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010760#ifdef FEAT_AUTOCHDIR
10761 && !p_acd
10762#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010763 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 name = buf->b_sfname;
10765 else
10766 name = buf->b_ffname;
10767 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10768 return FAIL;
10769 return OK;
10770}
10771
10772/*
10773 * Write a file name to the session file.
10774 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10775 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010776 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 */
10778 static int
10779ses_put_fname(fd, name, flagp)
10780 FILE *fd;
10781 char_u *name;
10782 unsigned *flagp;
10783{
10784 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010785 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787
10788 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010789 if (sname == NULL)
10790 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010792 if (*flagp & SSOP_SLASH)
10793 {
10794 /* change all backslashes to forward slashes */
10795 for (p = sname; *p != NUL; mb_ptr_adv(p))
10796 if (*p == '\\')
10797 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010799
10800 /* escapse special characters */
10801 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010803 if (p == NULL)
10804 return FAIL;
10805
10806 /* write the result */
10807 if (fputs((char *)p, fd) < 0)
10808 retval = FAIL;
10809
10810 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 return retval;
10812}
10813
10814/*
10815 * ":loadview [nr]"
10816 */
10817 static void
10818ex_loadview(eap)
10819 exarg_T *eap;
10820{
10821 char_u *fname;
10822
10823 fname = get_view_file(*eap->arg);
10824 if (fname != NULL)
10825 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010826 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 vim_free(fname);
10828 }
10829}
10830
10831/*
10832 * Get the name of the view file for the current buffer.
10833 */
10834 static char_u *
10835get_view_file(c)
10836 int c;
10837{
10838 int len = 0;
10839 char_u *p, *s;
10840 char_u *retval;
10841 char_u *sname;
10842
10843 if (curbuf->b_ffname == NULL)
10844 {
10845 EMSG(_(e_noname));
10846 return NULL;
10847 }
10848 sname = home_replace_save(NULL, curbuf->b_ffname);
10849 if (sname == NULL)
10850 return NULL;
10851
10852 /*
10853 * We want a file name without separators, because we're not going to make
10854 * a directory.
10855 * "normal" path separator -> "=+"
10856 * "=" -> "=="
10857 * ":" path separator -> "=-"
10858 */
10859 for (p = sname; *p; ++p)
10860 if (*p == '=' || vim_ispathsep(*p))
10861 ++len;
10862 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10863 if (retval != NULL)
10864 {
10865 STRCPY(retval, p_vdir);
10866 add_pathsep(retval);
10867 s = retval + STRLEN(retval);
10868 for (p = sname; *p; ++p)
10869 {
10870 if (*p == '=')
10871 {
10872 *s++ = '=';
10873 *s++ = '=';
10874 }
10875 else if (vim_ispathsep(*p))
10876 {
10877 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010878#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 || defined(VMS)
10880 if (*p == ':')
10881 *s++ = '-';
10882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010884 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 }
10886 else
10887 *s++ = *p;
10888 }
10889 *s++ = '=';
10890 *s++ = c;
10891 STRCPY(s, ".vim");
10892 }
10893
10894 vim_free(sname);
10895 return retval;
10896}
10897
10898#endif /* FEAT_SESSION */
10899
10900/*
10901 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10902 * Return FAIL for a write error.
10903 */
10904 int
10905put_eol(fd)
10906 FILE *fd;
10907{
10908 if (
10909#ifdef USE_CRNL
10910 (
10911# ifdef MKSESSION_NL
10912 !mksession_nl &&
10913# endif
10914 (putc('\r', fd) < 0)) ||
10915#endif
10916 (putc('\n', fd) < 0))
10917 return FAIL;
10918 return OK;
10919}
10920
10921/*
10922 * Write a line to "fd".
10923 * Return FAIL for a write error.
10924 */
10925 int
10926put_line(fd, s)
10927 FILE *fd;
10928 char *s;
10929{
10930 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10931 return FAIL;
10932 return OK;
10933}
10934
10935#ifdef FEAT_VIMINFO
10936/*
10937 * ":rviminfo" and ":wviminfo".
10938 */
10939 static void
10940ex_viminfo(eap)
10941 exarg_T *eap;
10942{
10943 char_u *save_viminfo;
10944
10945 save_viminfo = p_viminfo;
10946 if (*p_viminfo == NUL)
10947 p_viminfo = (char_u *)"'100";
10948 if (eap->cmdidx == CMD_rviminfo)
10949 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010950 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10951 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 EMSG(_("E195: Cannot open viminfo file for reading"));
10953 }
10954 else
10955 write_viminfo(eap->arg, eap->forceit);
10956 p_viminfo = save_viminfo;
10957}
10958#endif
10959
10960#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010961/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010962 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010963 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 void
10966dialog_msg(buff, format, fname)
10967 char_u *buff;
10968 char *format;
10969 char_u *fname;
10970{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 if (fname == NULL)
10972 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010973 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975#endif
10976
10977/*
10978 * ":behave {mswin,xterm}"
10979 */
10980 static void
10981ex_behave(eap)
10982 exarg_T *eap;
10983{
10984 if (STRCMP(eap->arg, "mswin") == 0)
10985 {
10986 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10987 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10988 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10989 set_option_value((char_u *)"keymodel", 0L,
10990 (char_u *)"startsel,stopsel", 0);
10991 }
10992 else if (STRCMP(eap->arg, "xterm") == 0)
10993 {
10994 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10995 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10996 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10997 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10998 }
10999 else
11000 EMSG2(_(e_invarg2), eap->arg);
11001}
11002
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011003#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11004/*
11005 * Function given to ExpandGeneric() to obtain the possible arguments of the
11006 * ":behave {mswin,xterm}" command.
11007 */
11008 char_u *
11009get_behave_arg(xp, idx)
11010 expand_T *xp UNUSED;
11011 int idx;
11012{
11013 if (idx == 0)
11014 return (char_u *)"mswin";
11015 if (idx == 1)
11016 return (char_u *)"xterm";
11017 return NULL;
11018}
11019#endif
11020
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021#ifdef FEAT_AUTOCMD
11022static int filetype_detect = FALSE;
11023static int filetype_plugin = FALSE;
11024static int filetype_indent = FALSE;
11025
11026/*
11027 * ":filetype [plugin] [indent] {on,off,detect}"
11028 * on: Load the filetype.vim file to install autocommands for file types.
11029 * off: Load the ftoff.vim file to remove all autocommands for file types.
11030 * plugin on: load filetype.vim and ftplugin.vim
11031 * plugin off: load ftplugof.vim
11032 * indent on: load filetype.vim and indent.vim
11033 * indent off: load indoff.vim
11034 */
11035 static void
11036ex_filetype(eap)
11037 exarg_T *eap;
11038{
11039 char_u *arg = eap->arg;
11040 int plugin = FALSE;
11041 int indent = FALSE;
11042
11043 if (*eap->arg == NUL)
11044 {
11045 /* Print current status. */
11046 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11047 filetype_detect ? "ON" : "OFF",
11048 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11049 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11050 return;
11051 }
11052
11053 /* Accept "plugin" and "indent" in any order. */
11054 for (;;)
11055 {
11056 if (STRNCMP(arg, "plugin", 6) == 0)
11057 {
11058 plugin = TRUE;
11059 arg = skipwhite(arg + 6);
11060 continue;
11061 }
11062 if (STRNCMP(arg, "indent", 6) == 0)
11063 {
11064 indent = TRUE;
11065 arg = skipwhite(arg + 6);
11066 continue;
11067 }
11068 break;
11069 }
11070 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11071 {
11072 if (*arg == 'o' || !filetype_detect)
11073 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011074 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 filetype_detect = TRUE;
11076 if (plugin)
11077 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011078 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 filetype_plugin = TRUE;
11080 }
11081 if (indent)
11082 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011083 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 filetype_indent = TRUE;
11085 }
11086 }
11087 if (*arg == 'd')
11088 {
11089 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011090 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 }
11092 }
11093 else if (STRCMP(arg, "off") == 0)
11094 {
11095 if (plugin || indent)
11096 {
11097 if (plugin)
11098 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011099 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 filetype_plugin = FALSE;
11101 }
11102 if (indent)
11103 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011104 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 filetype_indent = FALSE;
11106 }
11107 }
11108 else
11109 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011110 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 filetype_detect = FALSE;
11112 }
11113 }
11114 else
11115 EMSG2(_(e_invarg2), arg);
11116}
11117
11118/*
11119 * ":setfiletype {name}"
11120 */
11121 static void
11122ex_setfiletype(eap)
11123 exarg_T *eap;
11124{
11125 if (!did_filetype)
11126 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11127}
11128#endif
11129
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 static void
11131ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011132 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133{
11134#ifdef FEAT_DIGRAPHS
11135 if (*eap->arg != NUL)
11136 putdigraph(eap->arg);
11137 else
11138 listdigraphs();
11139#else
11140 EMSG(_("E196: No digraphs in this version"));
11141#endif
11142}
11143
11144 static void
11145ex_set(eap)
11146 exarg_T *eap;
11147{
11148 int flags = 0;
11149
11150 if (eap->cmdidx == CMD_setlocal)
11151 flags = OPT_LOCAL;
11152 else if (eap->cmdidx == CMD_setglobal)
11153 flags = OPT_GLOBAL;
11154#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11155 if (cmdmod.browse && flags == 0)
11156 ex_options(eap);
11157 else
11158#endif
11159 (void)do_set(eap->arg, flags);
11160}
11161
11162#ifdef FEAT_SEARCH_EXTRA
11163/*
11164 * ":nohlsearch"
11165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 static void
11167ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011168 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
11170 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011171 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172}
11173
11174/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011175 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 * Sets nextcmd to the start of the next command, if any. Also called when
11177 * skipping commands to find the next command.
11178 */
11179 static void
11180ex_match(eap)
11181 exarg_T *eap;
11182{
11183 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011184 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 char_u *end;
11186 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011187 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011188
11189 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011190 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011191 else
11192 {
11193 EMSG(e_invcmd);
11194 return;
11195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196
11197 /* First clear any old pattern. */
11198 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011199 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200
11201 if (ends_excmd(*eap->arg))
11202 end = eap->arg;
11203 else if ((STRNICMP(eap->arg, "none", 4) == 0
11204 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11205 end = eap->arg + 4;
11206 else
11207 {
11208 p = skiptowhite(eap->arg);
11209 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011210 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 p = skipwhite(p);
11212 if (*p == NUL)
11213 {
11214 /* There must be two arguments. */
11215 EMSG2(_(e_invarg2), eap->arg);
11216 return;
11217 }
11218 end = skip_regexp(p + 1, *p, TRUE, NULL);
11219 if (!eap->skip)
11220 {
11221 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11222 {
11223 eap->errmsg = e_trailing;
11224 return;
11225 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011226 if (*end != *p)
11227 {
11228 EMSG2(_(e_invarg2), p);
11229 return;
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231
11232 c = *end;
11233 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011234 match_add(curwin, g, p + 1, 10, id);
11235 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011236 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238 }
11239 eap->nextcmd = find_nextcmd(end);
11240}
11241#endif
11242
11243#ifdef FEAT_CRYPT
11244/*
11245 * ":X": Get crypt key
11246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 static void
11248ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011249 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011251 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011252 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253}
11254#endif
11255
11256#ifdef FEAT_FOLDING
11257 static void
11258ex_fold(eap)
11259 exarg_T *eap;
11260{
11261 if (foldManualAllowed(TRUE))
11262 foldCreate(eap->line1, eap->line2);
11263}
11264
11265 static void
11266ex_foldopen(eap)
11267 exarg_T *eap;
11268{
11269 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11270 eap->forceit, FALSE);
11271}
11272
11273 static void
11274ex_folddo(eap)
11275 exarg_T *eap;
11276{
11277 linenr_T lnum;
11278
11279 /* First set the marks for all lines closed/open. */
11280 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11281 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11282 ml_setmarked(lnum);
11283
11284 /* Execute the command on the marked lines. */
11285 global_exe(eap->arg);
11286 ml_clearmarked(); /* clear rest of the marks */
11287}
11288#endif