blob: 9b5a5b164dd6b010915cb729868ec767ecde8637 [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 Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
287#ifndef FEAT_SNIFF
288# define ex_sniff ex_ni
289#endif
290#ifndef FEAT_KEYMAP
291# define ex_loadkeymap ex_ni
292#endif
293static void ex_swapname __ARGS((exarg_T *eap));
294static void ex_syncbind __ARGS((exarg_T *eap));
295static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_pwd __ARGS((exarg_T *eap));
297static void ex_equal __ARGS((exarg_T *eap));
298static void ex_sleep __ARGS((exarg_T *eap));
299static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
300static void ex_winsize __ARGS((exarg_T *eap));
301#ifdef FEAT_WINDOWS
302static void ex_wincmd __ARGS((exarg_T *eap));
303#else
304# define ex_wincmd ex_ni
305#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_winpos __ARGS((exarg_T *eap));
308#else
309# define ex_winpos ex_ni
310#endif
311static void ex_operators __ARGS((exarg_T *eap));
312static void ex_put __ARGS((exarg_T *eap));
313static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000314static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_submagic __ARGS((exarg_T *eap));
316static void ex_join __ARGS((exarg_T *eap));
317static void ex_at __ARGS((exarg_T *eap));
318static void ex_bang __ARGS((exarg_T *eap));
319static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#ifdef FEAT_PERSISTENT_UNDO
321static void ex_wundo __ARGS((exarg_T *eap));
322static void ex_rundo __ARGS((exarg_T *eap));
323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000325static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redir __ARGS((exarg_T *eap));
327static void ex_redraw __ARGS((exarg_T *eap));
328static void ex_redrawstatus __ARGS((exarg_T *eap));
329static void close_redir __ARGS((void));
330static void ex_mkrc __ARGS((exarg_T *eap));
331static void ex_mark __ARGS((exarg_T *eap));
332#ifdef FEAT_USR_CMDS
333static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000334static 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 +0000335#endif
336#ifdef FEAT_EX_EXTRA
337static void ex_normal __ARGS((exarg_T *eap));
338static void ex_startinsert __ARGS((exarg_T *eap));
339static void ex_stopinsert __ARGS((exarg_T *eap));
340#else
341# define ex_normal ex_ni
342# define ex_align ex_ni
343# define ex_retab ex_ni
344# define ex_startinsert ex_ni
345# define ex_stopinsert ex_ni
346# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000347# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349#ifdef FEAT_FIND_ID
350static void ex_checkpath __ARGS((exarg_T *eap));
351static void ex_findpat __ARGS((exarg_T *eap));
352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357static void ex_psearch __ARGS((exarg_T *eap));
358#else
359# define ex_psearch ex_ni
360#endif
361static void ex_tag __ARGS((exarg_T *eap));
362static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000374# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_continue ex_ni
376# define ex_break ex_ni
377# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000378# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379# define ex_throw ex_ni
380# define ex_try ex_ni
381# define ex_catch ex_ni
382# define ex_finally ex_ni
383# define ex_endtry ex_ni
384# define ex_endfunction ex_ni
385# define ex_let ex_ni
386# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000387# define ex_lockvar ex_ni
388# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389# define ex_function ex_ni
390# define ex_delfunction ex_ni
391# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000392# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394static char_u *arg_all __ARGS((void));
395#ifdef FEAT_SESSION
396static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000397static 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 +0000398static void ex_loadview __ARGS((exarg_T *eap));
399static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000400static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#else
402# define ex_loadview ex_ni
403#endif
404#ifndef FEAT_EVAL
405# define ex_compiler ex_ni
406#endif
407#ifdef FEAT_VIMINFO
408static void ex_viminfo __ARGS((exarg_T *eap));
409#else
410# define ex_viminfo ex_ni
411#endif
412static void ex_behave __ARGS((exarg_T *eap));
413#ifdef FEAT_AUTOCMD
414static void ex_filetype __ARGS((exarg_T *eap));
415static void ex_setfiletype __ARGS((exarg_T *eap));
416#else
417# define ex_filetype ex_ni
418# define ex_setfiletype ex_ni
419#endif
420#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000421# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# define ex_diffpatch ex_ni
423# define ex_diffgetput ex_ni
424# define ex_diffsplit ex_ni
425# define ex_diffthis ex_ni
426# define ex_diffupdate ex_ni
427#endif
428static void ex_digraphs __ARGS((exarg_T *eap));
429static void ex_set __ARGS((exarg_T *eap));
430#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
431# define ex_options ex_ni
432#endif
433#ifdef FEAT_SEARCH_EXTRA
434static void ex_nohlsearch __ARGS((exarg_T *eap));
435static void ex_match __ARGS((exarg_T *eap));
436#else
437# define ex_nohlsearch ex_ni
438# define ex_match ex_ni
439#endif
440#ifdef FEAT_CRYPT
441static void ex_X __ARGS((exarg_T *eap));
442#else
443# define ex_X ex_ni
444#endif
445#ifdef FEAT_FOLDING
446static void ex_fold __ARGS((exarg_T *eap));
447static void ex_foldopen __ARGS((exarg_T *eap));
448static void ex_folddo __ARGS((exarg_T *eap));
449#else
450# define ex_fold ex_ni
451# define ex_foldopen ex_ni
452# define ex_folddo ex_ni
453#endif
454#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
455 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
456# define ex_language ex_ni
457#endif
458#ifndef FEAT_SIGNS
459# define ex_sign ex_ni
460#endif
461#ifndef FEAT_SUN_WORKSHOP
462# define ex_wsverb ex_ni
463#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200467# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
470#ifndef FEAT_EVAL
471# define ex_debug ex_ni
472# define ex_breakadd ex_ni
473# define ex_debuggreedy ex_ni
474# define ex_breakdel ex_ni
475# define ex_breaklist ex_ni
476#endif
477
478#ifndef FEAT_CMDHIST
479# define ex_history ex_ni
480#endif
481#ifndef FEAT_JUMPLIST
482# define ex_jumps ex_ni
483# define ex_changes ex_ni
484#endif
485
Bram Moolenaar05159a02005-02-26 23:04:13 +0000486#ifndef FEAT_PROFILE
487# define ex_profile ex_ni
488#endif
489
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490/*
491 * Declare cmdnames[].
492 */
493#define DO_DECLARE_EXCMD
494#include "ex_cmds.h"
495
496/*
497 * Table used to quickly search for a command, based on its first character.
498 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000499static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 CMD_append,
502 CMD_buffer,
503 CMD_change,
504 CMD_delete,
505 CMD_edit,
506 CMD_file,
507 CMD_global,
508 CMD_help,
509 CMD_insert,
510 CMD_join,
511 CMD_k,
512 CMD_list,
513 CMD_move,
514 CMD_next,
515 CMD_open,
516 CMD_print,
517 CMD_quit,
518 CMD_read,
519 CMD_substitute,
520 CMD_t,
521 CMD_undo,
522 CMD_vglobal,
523 CMD_write,
524 CMD_xit,
525 CMD_yank,
526 CMD_z,
527 CMD_bang
528};
529
530static char_u dollar_command[2] = {'$', 0};
531
532
533#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000534/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535typedef struct
536{
537 char_u *line; /* command line */
538 linenr_T lnum; /* sourcing_lnum of the line */
539} wcmd_T;
540
541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 garray_T *lines_gap; /* growarray with line info */
549 int current_line; /* last read line from growarray */
550 int repeating; /* TRUE when looping a second time */
551 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
552 char_u *(*getline) __ARGS((int, void *, int));
553 void *cookie;
554};
555
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000556static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
557static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000559
560/* Struct to save a few things while debugging. Used in do_cmdline() only. */
561struct dbg_stuff
562{
563 int trylevel;
564 int force_abort;
565 except_T *caught_stack;
566 char_u *vv_exception;
567 char_u *vv_throwpoint;
568 int did_emsg;
569 int got_int;
570 int did_throw;
571 int need_rethrow;
572 int check_cstack;
573 except_T *current_exception;
574};
575
576static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
578
579 static void
580save_dbg_stuff(dsp)
581 struct dbg_stuff *dsp;
582{
583 dsp->trylevel = trylevel; trylevel = 0;
584 dsp->force_abort = force_abort; force_abort = FALSE;
585 dsp->caught_stack = caught_stack; caught_stack = NULL;
586 dsp->vv_exception = v_exception(NULL);
587 dsp->vv_throwpoint = v_throwpoint(NULL);
588
589 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
590 dsp->did_emsg = did_emsg; did_emsg = FALSE;
591 dsp->got_int = got_int; got_int = FALSE;
592 dsp->did_throw = did_throw; did_throw = FALSE;
593 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
594 dsp->check_cstack = check_cstack; check_cstack = FALSE;
595 dsp->current_exception = current_exception; current_exception = NULL;
596}
597
598 static void
599restore_dbg_stuff(dsp)
600 struct dbg_stuff *dsp;
601{
602 suppress_errthrow = FALSE;
603 trylevel = dsp->trylevel;
604 force_abort = dsp->force_abort;
605 caught_stack = dsp->caught_stack;
606 (void)v_exception(dsp->vv_exception);
607 (void)v_throwpoint(dsp->vv_throwpoint);
608 did_emsg = dsp->did_emsg;
609 got_int = dsp->got_int;
610 did_throw = dsp->did_throw;
611 need_rethrow = dsp->need_rethrow;
612 check_cstack = dsp->check_cstack;
613 current_exception = dsp->current_exception;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#endif
616
617
618/*
619 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620 * command is given.
621 */
622 void
623do_exmode(improved)
624 int improved; /* TRUE for "improved Ex" mode */
625{
626 int save_msg_scroll;
627 int prev_msg_row;
628 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 int changedtick;
630
631 if (improved)
632 exmode_active = EXMODE_VIM;
633 else
634 exmode_active = EXMODE_NORMAL;
635 State = NORMAL;
636
637 /* When using ":global /pat/ visual" and then "Q" we return to continue
638 * the :global command. */
639 if (global_busy)
640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 save_msg_scroll = msg_scroll;
643 ++RedrawingDisabled; /* don't redisplay the window */
644 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_GUI
646 /* Ignore scrollbar and mouse events in Ex mode */
647 ++hold_gui_events;
648#endif
649#ifdef FEAT_SNIFF
650 want_sniff_request = 0; /* No K_SNIFF wanted */
651#endif
652
653 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
654 while (exmode_active)
655 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000656#ifdef FEAT_EX_EXTRA
657 /* Check for a ":normal" command and no more characters left. */
658 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
659 {
660 exmode_active = FALSE;
661 break;
662 }
663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 msg_scroll = TRUE;
665 need_wait_return = FALSE;
666 ex_pressedreturn = FALSE;
667 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000668 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 prev_msg_row = msg_row;
670 prev_line = curwin->w_cursor.lnum;
671#ifdef FEAT_SNIFF
672 ProcessSniffRequests();
673#endif
674 if (improved)
675 {
676 cmdline_row = msg_row;
677 do_cmdline(NULL, getexline, NULL, 0);
678 }
679 else
680 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
681 lines_left = Rows - 1;
682
Bram Moolenaardf177f62005-02-22 08:39:57 +0000683 if ((prev_line != curwin->w_cursor.lnum
684 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 if (curbuf->b_ml.ml_flags & ML_EMPTY)
687 EMSG(_(e_emptybuf));
688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 if (ex_pressedreturn)
691 {
692 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000693 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000694 msg_row = prev_msg_row;
695 if (prev_msg_row == Rows - 1)
696 msg_row--;
697 }
698 msg_col = 0;
699 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
700 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000703 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
704 {
705 if (curbuf->b_ml.ml_flags & ML_EMPTY)
706 EMSG(_(e_emptybuf));
707 else
708 EMSG(_("E501: At end-of-file"));
709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711
712#ifdef FEAT_GUI
713 --hold_gui_events;
714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 --RedrawingDisabled;
716 --no_wait_return;
717 update_screen(CLEAR);
718 need_wait_return = FALSE;
719 msg_scroll = save_msg_scroll;
720}
721
722/*
723 * Execute a simple command line. Used for translated commands like "*".
724 */
725 int
726do_cmdline_cmd(cmd)
727 char_u *cmd;
728{
729 return do_cmdline(cmd, NULL, NULL,
730 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
731}
732
733/*
734 * do_cmdline(): execute one Ex command line
735 *
736 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100737 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * 2. Split up in parts separated with '|'.
739 *
740 * This function can be called recursively!
741 *
742 * flags:
743 * DOCMD_VERBOSE - The command will be included in the error message.
744 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * DOCMD_KEYTYPED - Don't reset KeyTyped.
747 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
748 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
749 *
750 * return FAIL if cmdline could not be executed, OK otherwise
751 */
752 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 char_u *(*fgetline) __ARGS((int, void *, int));
756 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 int flags;
758{
759 char_u *next_cmdline; /* next cmd to execute */
760 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100761 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 static int recursive = 0; /* recursive depth */
763 int msg_didout_before_start = 0;
764 int count = 0; /* line number count */
765 int did_inc = FALSE; /* incremented RedrawingDisabled */
766 int retval = OK;
767#ifdef FEAT_EVAL
768 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000769 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 int current_line = 0; /* active line in lines_ga */
771 char_u *fname = NULL; /* function or script name */
772 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
773 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000774 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 int initial_trylevel;
776 struct msglist **saved_msg_list = NULL;
777 struct msglist *private_msg_list;
778
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 char_u *(*cmd_getline) __ARGS((int, void *, int));
781 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787# define cmd_cookie cookie
788#endif
789 static int call_depth = 0; /* recursiveness */
790
791#ifdef FEAT_EVAL
792 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
793 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000794 * This ensures that the do_errthrow() call in do_one_cmd() does not
795 * combine the messages stored by an earlier invocation of do_one_cmd()
796 * with the command name of the later one. This would happen when
797 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 saved_msg_list = msg_list;
799 msg_list = &private_msg_list;
800 private_msg_list = NULL;
801#endif
802
803 /* It's possible to create an endless loop with ":execute", catch that
804 * here. The value of 200 allows nested function calls, ":source", etc. */
805 if (call_depth == 200)
806 {
807 EMSG(_("E169: Command too recursive"));
808#ifdef FEAT_EVAL
809 /* When converting to an exception, we do not include the command name
810 * since this is not an error of the specific command. */
811 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
812 msg_list = saved_msg_list;
813#endif
814 return FAIL;
815 }
816 ++call_depth;
817
818#ifdef FEAT_EVAL
819 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000820 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 cstack.cs_trylevel = 0;
822 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
825
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ++ex_nesting_level;
832
833 /* Get the function or script name and the address where the next breakpoint
834 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
837 fname = func_name(real_cookie);
838 breakpoint = func_breakpoint(real_cookie);
839 dbg_tick = func_dbg_tick(real_cookie);
840 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 fname = sourcing_name;
844 breakpoint = source_breakpoint(real_cookie);
845 dbg_tick = source_dbg_tick(real_cookie);
846 }
847
848 /*
849 * Initialize "force_abort" and "suppress_errthrow" at the top level.
850 */
851 if (!recursive)
852 {
853 force_abort = FALSE;
854 suppress_errthrow = FALSE;
855 }
856
857 /*
858 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000859 * exception handling (used when debugging). Otherwise clear it to avoid
860 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000862 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000863 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000864 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200865 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 initial_trylevel = trylevel;
868
869 /*
870 * "did_throw" will be set to TRUE when an exception is being thrown.
871 */
872 did_throw = FALSE;
873#endif
874 /*
875 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000876 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * If force_abort is set, we cancel everything.
878 */
879 did_emsg = FALSE;
880
881 /*
882 * KeyTyped is only set when calling vgetc(). Reset it here when not
883 * calling vgetc() (sourced command lines).
884 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (!(flags & DOCMD_KEYTYPED)
886 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 KeyTyped = FALSE;
888
889 /*
890 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 * - for multiple commands on one line, separated with '|'
893 * - when repeating until there are no more lines (for ":source")
894 */
895 next_cmdline = cmdline;
896 do
897 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100899 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900#endif
901
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000902 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (next_cmdline == NULL
904#ifdef FEAT_EVAL
905 && !force_abort
906 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
909 )
910 did_emsg = FALSE;
911
912 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100914 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 * 3. If a line is given: Make a copy, so we can mess with it.
916 */
917
918#ifdef FEAT_EVAL
919 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000920 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
922 /* Each '|' separated command is stored separately in lines_ga, to
923 * be able to jump to it. Don't use next_cmdline now. */
924 vim_free(cmdline_copy);
925 cmdline_copy = NULL;
926
927 /* Check if a function has returned or, unless it has an unclosed
928 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000932 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933 func_line_end(real_cookie);
934# endif
935 if (func_has_ended(real_cookie))
936 {
937 retval = FAIL;
938 break;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000942 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944 script_line_end();
945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
950 retval = FAIL;
951 break;
952 }
953
954 /* If breakpoints have been added/deleted need to check for it. */
955 if (breakpoint != NULL && dbg_tick != NULL
956 && *dbg_tick != debug_tick)
957 {
958 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100959 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 fname, sourcing_lnum);
961 *dbg_tick = debug_tick;
962 }
963
964 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
965 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
966
967 /* Did we encounter a breakpoint? */
968 if (breakpoint != NULL && *breakpoint != 0
969 && *breakpoint <= sourcing_lnum)
970 {
971 dbg_breakpoint(fname, sourcing_lnum);
972 /* Find next breakpoint. */
973 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100974 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 fname, sourcing_lnum);
976 *dbg_tick = debug_tick;
977 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000979 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 {
981 if (getline_is_func)
982 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000984 script_line_start();
985 }
986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * below, so that it stores lines in or reads them from
994 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 * while/for loop. */
996 cmd_getline = get_loop_line;
997 cmd_cookie = (void *)&cmd_loop_cookie;
998 cmd_loop_cookie.lines_gap = &lines_ga;
999 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 cmd_loop_cookie.cookie = cookie;
1002 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 else
1005 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 cmd_cookie = cookie;
1008 }
1009#endif
1010
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001011 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (next_cmdline == NULL)
1013 {
1014 /*
1015 * Need to set msg_didout for the first line after an ":if",
1016 * otherwise the ":if" will be overwritten.
1017 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_EVAL
1022 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1023#else
1024 0
1025#endif
1026 )) == NULL)
1027 {
1028 /* Don't call wait_return for aborted command line. The NULL
1029 * returned for the end of a sourced file or executed function
1030 * doesn't do this. */
1031 if (KeyTyped && !(flags & DOCMD_REPEAT))
1032 need_wait_return = FALSE;
1033 retval = FAIL;
1034 break;
1035 }
1036 used_getline = TRUE;
1037
1038 /*
1039 * Keep the first typed line. Clear it when more lines are typed.
1040 */
1041 if (flags & DOCMD_KEEPLINE)
1042 {
1043 vim_free(repeat_cmdline);
1044 if (count == 0)
1045 repeat_cmdline = vim_strsave(next_cmdline);
1046 else
1047 repeat_cmdline = NULL;
1048 }
1049 }
1050
1051 /* 3. Make a copy of the command so we can mess with it. */
1052 else if (cmdline_copy == NULL)
1053 {
1054 next_cmdline = vim_strsave(next_cmdline);
1055 if (next_cmdline == NULL)
1056 {
1057 EMSG(_(e_outofmem));
1058 retval = FAIL;
1059 break;
1060 }
1061 }
1062 cmdline_copy = next_cmdline;
1063
1064#ifdef FEAT_EVAL
1065 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 * Save the current line when inside a ":while" or ":for", and when
1067 * the command looks like a ":while" or ":for", because we may need it
1068 * later. When there is a '|' and another command, it is stored
1069 * separately, because we need to be able to jump back to it from an
1070 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 if (current_line == lines_ga.ga_len
1073 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 retval = FAIL;
1078 break;
1079 }
1080 }
1081 did_endif = FALSE;
1082#endif
1083
1084 if (count++ == 0)
1085 {
1086 /*
1087 * All output from the commands is put below each other, without
1088 * waiting for a return. Don't do this when executing commands
1089 * from a script or when being called recursive (e.g. for ":e
1090 * +command file").
1091 */
1092 if (!(flags & DOCMD_NOWAIT) && !recursive)
1093 {
1094 msg_didout_before_start = msg_didout;
1095 msg_didany = FALSE; /* no output yet */
1096 msg_start();
1097 msg_scroll = TRUE; /* put messages below each other */
1098 ++no_wait_return; /* dont wait for return until finished */
1099 ++RedrawingDisabled;
1100 did_inc = TRUE;
1101 }
1102 }
1103
1104 if (p_verbose >= 15 && sourcing_name != NULL)
1105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001107 verbose_enter_scroll();
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 smsg((char_u *)_("line %ld: %s"),
1110 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001111 if (msg_silent == 0)
1112 msg_puts((char_u *)"\n"); /* don't overwrite this */
1113
1114 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 --no_wait_return;
1116 }
1117
1118 /*
1119 * 2. Execute one '|' separated command.
1120 * do_one_cmd() will return NULL if there is no trailing '|'.
1121 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1122 */
1123 ++recursive;
1124 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1125#ifdef FEAT_EVAL
1126 &cstack,
1127#endif
1128 cmd_getline, cmd_cookie);
1129 --recursive;
1130
1131#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 if (cmd_cookie == (void *)&cmd_loop_cookie)
1133 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136#endif
1137
1138 if (next_cmdline == NULL)
1139 {
1140 vim_free(cmdline_copy);
1141 cmdline_copy = NULL;
1142#ifdef FEAT_CMDHIST
1143 /*
1144 * If the command was typed, remember it for the ':' register.
1145 * Do this AFTER executing the command to make :@: work.
1146 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001147 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 && new_last_cmdline != NULL)
1149 {
1150 vim_free(last_cmdline);
1151 last_cmdline = new_last_cmdline;
1152 new_last_cmdline = NULL;
1153 }
1154#endif
1155 }
1156 else
1157 {
1158 /* need to copy the command after the '|' to cmdline_copy, for the
1159 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001160 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 next_cmdline = cmdline_copy;
1162 }
1163
1164
1165#ifdef FEAT_EVAL
1166 /* reset did_emsg for a function that is not aborted by an error */
1167 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001168 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && !func_has_abort(real_cookie))
1170 did_emsg = FALSE;
1171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 ++current_line;
1175
1176 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 * An ":endwhile", ":endfor" and ":continue" is handled here.
1178 * If we were executing commands, jump back to the ":while" or
1179 * ":for".
1180 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* Jump back to the matching ":while" or ":for". Be careful
1187 * not to use a cs_line[] from an entry that isn't a ":while"
1188 * or ":for": It would make "current_line" invalid and can
1189 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (!did_emsg && !got_int && !did_throw
1191 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 && (cstack.cs_flags[cstack.cs_idx]
1193 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && cstack.cs_line[cstack.cs_idx] >= 0
1195 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1196 {
1197 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* remember we jumped there */
1199 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 line_breakcheck(); /* check if CTRL-C typed */
1201
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 /* Check for the next breakpoint at or after the ":while"
1203 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (breakpoint != NULL)
1205 {
1206 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001207 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 fname,
1209 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1210 *dbg_tick = debug_tick;
1211 }
1212 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1218 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 }
1221
1222 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1229 }
1230 }
1231
1232 /*
1233 * When not inside any ":while" loop, clear remembered lines.
1234 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 if (lines_ga.ga_len > 0)
1238 {
1239 sourcing_lnum =
1240 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1241 free_cmdlines(&lines_ga);
1242 }
1243 current_line = 0;
1244 }
1245
1246 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001247 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1248 * being restored at the ":endtry". Reset them here and set the
1249 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1250 * This includes the case where a missing ":endif", ":endwhile" or
1251 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 cstack.cs_lflags &= ~CSL_HAD_FINA;
1256 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1257 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 did_throw ? (void *)current_exception : NULL);
1259 did_emsg = got_int = did_throw = FALSE;
1260 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1261 }
1262
1263 /* Update global "trylevel" for recursive calls to do_cmdline() from
1264 * within this loop. */
1265 trylevel = initial_trylevel + cstack.cs_trylevel;
1266
1267 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001268 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * files) is aborted because of an error, an interrupt, or an uncaught
1270 * exception, cancel everything. If it is left normally, reset
1271 * force_abort to get the non-EH compatible abortion behavior for
1272 * the rest of the script.
1273 */
1274 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1275 force_abort = FALSE;
1276
1277 /* Convert an interrupt to an exception if appropriate. */
1278 (void)do_intthrow(&cstack);
1279#endif /* FEAT_EVAL */
1280
1281 }
1282 /*
1283 * Continue executing command lines when:
1284 * - no CTRL-C typed, no aborting error, no exception thrown or try
1285 * conditionals need to be checked for executing finally clauses or
1286 * catching an interrupt exception
1287 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001288 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 * looping for ":source" command or function call.
1290 */
1291 while (!((got_int
1292#ifdef FEAT_EVAL
1293 || (did_emsg && force_abort) || did_throw
1294#endif
1295 )
1296#ifdef FEAT_EVAL
1297 && cstack.cs_trylevel == 0
1298#endif
1299 )
1300 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001301 && (getline_equal(fgetline, cookie, getexmodeline)
1302 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 && (next_cmdline != NULL
1304#ifdef FEAT_EVAL
1305 || cstack.cs_idx >= 0
1306#endif
1307 || (flags & DOCMD_REPEAT)));
1308
1309 vim_free(cmdline_copy);
1310#ifdef FEAT_EVAL
1311 free_cmdlines(&lines_ga);
1312 ga_clear(&lines_ga);
1313
1314 if (cstack.cs_idx >= 0)
1315 {
1316 /*
1317 * If a sourced file or executed function ran to its end, report the
1318 * unclosed conditional.
1319 */
1320 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001321 && ((getline_equal(fgetline, cookie, getsourceline)
1322 && !source_finished(fgetline, cookie))
1323 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && !func_has_ended(real_cookie))))
1325 {
1326 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1327 EMSG(_(e_endtry));
1328 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1329 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001330 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1331 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 else
1333 EMSG(_(e_endif));
1334 }
1335
1336 /*
1337 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1338 * ":endtry" in a sourced file or executed function. If the try
1339 * conditional is in its finally clause, ignore anything pending.
1340 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001341 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 */
1343 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001344 {
1345 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1346
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001347 if (idx >= 0)
1348 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001349 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1350 &cstack.cs_looplevel);
1351 }
1352 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 trylevel = initial_trylevel;
1354 }
1355
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001356 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1357 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001359 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 ? (char_u *)"endfunction" : (char_u *)NULL);
1361
1362 if (trylevel == 0)
1363 {
1364 /*
1365 * When an exception is being thrown out of the outermost try
1366 * conditional, discard the uncaught exception, disable the conversion
1367 * of interrupts or errors to exceptions, and ensure that no more
1368 * commands are executed.
1369 */
1370 if (did_throw)
1371 {
1372 void *p = NULL;
1373 char_u *saved_sourcing_name;
1374 int saved_sourcing_lnum;
1375 struct msglist *messages = NULL, *next;
1376
1377 /*
1378 * If the uncaught exception is a user exception, report it as an
1379 * error. If it is an error exception, display the saved error
1380 * message now. For an interrupt exception, do nothing; the
1381 * interrupt message is given elsewhere.
1382 */
1383 switch (current_exception->type)
1384 {
1385 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001386 vim_snprintf((char *)IObuff, IOSIZE,
1387 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 current_exception->value);
1389 p = vim_strsave(IObuff);
1390 break;
1391 case ET_ERROR:
1392 messages = current_exception->messages;
1393 current_exception->messages = NULL;
1394 break;
1395 case ET_INTERRUPT:
1396 break;
1397 default:
1398 p = vim_strsave((char_u *)_(e_internal));
1399 }
1400
1401 saved_sourcing_name = sourcing_name;
1402 saved_sourcing_lnum = sourcing_lnum;
1403 sourcing_name = current_exception->throw_name;
1404 sourcing_lnum = current_exception->throw_lnum;
1405 current_exception->throw_name = NULL;
1406
1407 discard_current_exception(); /* uses IObuff if 'verbose' */
1408 suppress_errthrow = TRUE;
1409 force_abort = TRUE;
1410
1411 if (messages != NULL)
1412 {
1413 do
1414 {
1415 next = messages->next;
1416 emsg(messages->msg);
1417 vim_free(messages->msg);
1418 vim_free(messages);
1419 messages = next;
1420 }
1421 while (messages != NULL);
1422 }
1423 else if (p != NULL)
1424 {
1425 emsg(p);
1426 vim_free(p);
1427 }
1428 vim_free(sourcing_name);
1429 sourcing_name = saved_sourcing_name;
1430 sourcing_lnum = saved_sourcing_lnum;
1431 }
1432
1433 /*
1434 * On an interrupt or an aborting error not converted to an exception,
1435 * disable the conversion of errors to exceptions. (Interrupts are not
1436 * converted any more, here.) This enables also the interrupt message
1437 * when force_abort is set and did_emsg unset in case of an interrupt
1438 * from a finally clause after an error.
1439 */
1440 else if (got_int || (did_emsg && force_abort))
1441 suppress_errthrow = TRUE;
1442 }
1443
1444 /*
1445 * The current cstack will be freed when do_cmdline() returns. An uncaught
1446 * exception will have to be rethrown in the previous cstack. If a function
1447 * has just returned or a script file was just finished and the previous
1448 * cstack belongs to the same function or, respectively, script file, it
1449 * will have to be checked for finally clauses to be executed due to the
1450 * ":return" or ":finish". This is done in do_one_cmd().
1451 */
1452 if (did_throw)
1453 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001454 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001456 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 && ex_nesting_level > func_level(real_cookie) + 1))
1458 {
1459 if (!did_throw)
1460 check_cstack = TRUE;
1461 }
1462 else
1463 {
1464 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 --ex_nesting_level;
1467 /*
1468 * Go to debug mode when returning from a function in which we are
1469 * single-stepping.
1470 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if ((getline_equal(fgetline, cookie, getsourceline)
1472 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001474 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 ? (char_u *)_("End of sourced file")
1476 : (char_u *)_("End of function"));
1477 }
1478
1479 /*
1480 * Restore the exception environment (done after returning from the
1481 * debugger).
1482 */
1483 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001484 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 msg_list = saved_msg_list;
1487#endif /* FEAT_EVAL */
1488
1489 /*
1490 * If there was too much output to fit on the command line, ask the user to
1491 * hit return before redrawing the screen. With the ":global" command we do
1492 * this only once after the command is finished.
1493 */
1494 if (did_inc)
1495 {
1496 --RedrawingDisabled;
1497 --no_wait_return;
1498 msg_scroll = FALSE;
1499
1500 /*
1501 * When just finished an ":if"-":else" which was typed, no need to
1502 * wait for hit-return. Also for an error situation.
1503 */
1504 if (retval == FAIL
1505#ifdef FEAT_EVAL
1506 || (did_endif && KeyTyped && !did_emsg)
1507#endif
1508 )
1509 {
1510 need_wait_return = FALSE;
1511 msg_didany = FALSE; /* don't wait when restarting edit */
1512 }
1513 else if (need_wait_return)
1514 {
1515 /*
1516 * The msg_start() above clears msg_didout. The wait_return we do
1517 * here should not overwrite the command that may be shown before
1518 * doing that.
1519 */
1520 msg_didout |= msg_didout_before_start;
1521 wait_return(FALSE);
1522 }
1523 }
1524
1525#ifndef FEAT_EVAL
1526 /*
1527 * Reset if_level, in case a sourced script file contains more ":if" than
1528 * ":endif" (could be ":if x | foo | endif").
1529 */
1530 if_level = 0;
1531#endif
1532
1533 --call_depth;
1534 return retval;
1535}
1536
1537#ifdef FEAT_EVAL
1538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001539 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 */
1541 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 int c;
1544 void *cookie;
1545 int indent;
1546{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 wcmd_T *wp;
1549 char_u *line;
1550
1551 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1552 {
1553 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 if (cp->getline == NULL)
1558 line = getcmdline(c, 0L, indent);
1559 else
1560 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001561 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 ++cp->current_line;
1563
1564 return line;
1565 }
1566
1567 KeyTyped = FALSE;
1568 ++cp->current_line;
1569 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1570 sourcing_lnum = wp->lnum;
1571 return vim_strsave(wp->line);
1572}
1573
1574/*
1575 * Store a line in "gap" so that a ":while" loop can execute it again.
1576 */
1577 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001578store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 garray_T *gap;
1580 char_u *line;
1581{
1582 if (ga_grow(gap, 1) == FAIL)
1583 return FAIL;
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1585 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1586 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return OK;
1588}
1589
1590/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
1593 static void
1594free_cmdlines(gap)
1595 garray_T *gap;
1596{
1597 while (gap->ga_len > 0)
1598 {
1599 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1600 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602}
1603#endif
1604
1605/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001606 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1607 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610getline_equal(fgetline, cookie, func)
1611 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001612 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 char_u *(*func) __ARGS((int, void *, int));
1614{
1615#ifdef FEAT_EVAL
1616 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001617 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001620 * function that's originally used to obtain the lines. This may be
1621 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001622 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001623 cp = (struct loop_cookie *)cookie;
1624 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 gp = cp->getline;
1627 cp = cp->cookie;
1628 }
1629 return gp == func;
1630#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
1633}
1634
1635#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1636/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * getline function. Otherwise return "cookie".
1639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001642 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001643 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644{
1645# ifdef FEAT_EVAL
1646 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001647 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001650 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001652 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001653 cp = (struct loop_cookie *)cookie;
1654 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656 gp = cp->getline;
1657 cp = cp->cookie;
1658 }
1659 return cp;
1660# else
1661 return cookie;
1662# endif
1663}
1664#endif
1665
1666/*
1667 * Execute one Ex command.
1668 *
1669 * If 'sourcing' is TRUE, the command will be included in the error message.
1670 *
1671 * 1. skip comment lines and leading space
1672 * 2. handle command modifiers
1673 * 3. parse range
1674 * 4. parse command
1675 * 5. parse arguments
1676 * 6. switch on command name
1677 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001678 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 *
1680 * This function may be called recursively!
1681 */
1682#if (_MSC_VER == 1200)
1683/*
Bram Moolenaared203462004-06-16 11:19:22 +00001684 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001686 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
1688 static char_u *
1689do_one_cmd(cmdlinep, sourcing,
1690#ifdef FEAT_EVAL
1691 cstack,
1692#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001693 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 char_u **cmdlinep;
1695 int sourcing;
1696#ifdef FEAT_EVAL
1697 struct condstack *cstack;
1698#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001699 char_u *(*fgetline) __ARGS((int, void *, int));
1700 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 char_u *p;
1703 linenr_T lnum;
1704 long n;
1705 char_u *errormsg = NULL; /* error message */
1706 exarg_T ea; /* Ex command arguments */
1707 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001708 int save_msg_scroll = msg_scroll;
1709 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001711#ifdef HAVE_SANDBOX
1712 int did_sandbox = FALSE;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 cmdmod_T save_cmdmod;
1715 int ni; /* set when Not Implemented */
1716
1717 vim_memset(&ea, 0, sizeof(ea));
1718 ea.line1 = 1;
1719 ea.line2 = 1;
1720#ifdef FEAT_EVAL
1721 ++ex_nesting_level;
1722#endif
1723
1724 /* when not editing the last file :q has to be typed twice */
1725 if (quitmore
1726#ifdef FEAT_EVAL
1727 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001728 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730 )
1731 --quitmore;
1732
1733 /*
1734 * Reset browse, confirm, etc.. They are restored when returning, for
1735 * recursive calls.
1736 */
1737 save_cmdmod = cmdmod;
1738 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1739
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001740 /* "#!anything" is handled like a comment. */
1741 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1742 goto doend;
1743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 /*
1745 * Repeat until no more command modifiers are found.
1746 */
1747 ea.cmd = *cmdlinep;
1748 for (;;)
1749 {
1750/*
1751 * 1. skip comment lines and leading white space and colons
1752 */
1753 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1754 ++ea.cmd;
1755
1756 /* in ex mode, an empty line works like :+ */
1757 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && (getline_equal(fgetline, cookie, getexmodeline)
1759 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001760 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
1762 ea.cmd = (char_u *)"+";
1763 ex_pressedreturn = TRUE;
1764 }
1765
1766 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001767 if (*ea.cmd == '"')
1768 goto doend;
1769 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001770 {
1771 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775/*
1776 * 2. handle command modifiers.
1777 */
1778 p = ea.cmd;
1779 if (VIM_ISDIGIT(*ea.cmd))
1780 p = skipwhite(skipdigits(ea.cmd));
1781 switch (*p)
1782 {
1783 /* When adding an entry, also modify cmd_exists(). */
1784 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1785 break;
1786#ifdef FEAT_WINDOWS
1787 cmdmod.split |= WSP_ABOVE;
1788#endif
1789 continue;
1790
1791 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1792 {
1793#ifdef FEAT_WINDOWS
1794 cmdmod.split |= WSP_BELOW;
1795#endif
1796 continue;
1797 }
1798 if (checkforcmd(&ea.cmd, "browse", 3))
1799 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001800#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 cmdmod.browse = TRUE;
1802#endif
1803 continue;
1804 }
1805 if (!checkforcmd(&ea.cmd, "botright", 2))
1806 break;
1807#ifdef FEAT_WINDOWS
1808 cmdmod.split |= WSP_BOT;
1809#endif
1810 continue;
1811
1812 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1813 break;
1814#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1815 cmdmod.confirm = TRUE;
1816#endif
1817 continue;
1818
1819 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1820 {
1821 cmdmod.keepmarks = TRUE;
1822 continue;
1823 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001824 if (checkforcmd(&ea.cmd, "keepalt", 5))
1825 {
1826 cmdmod.keepalt = TRUE;
1827 continue;
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1830 break;
1831 cmdmod.keepjumps = TRUE;
1832 continue;
1833
1834 /* ":hide" and ":hide | cmd" are not modifiers */
1835 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1836 || *p == NUL || ends_excmd(*p))
1837 break;
1838 ea.cmd = p;
1839 cmdmod.hide = TRUE;
1840 continue;
1841
1842 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1843 {
1844 cmdmod.lockmarks = TRUE;
1845 continue;
1846 }
1847
1848 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1849 break;
1850#ifdef FEAT_WINDOWS
1851 cmdmod.split |= WSP_ABOVE;
1852#endif
1853 continue;
1854
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001855 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1856 break;
1857#ifdef FEAT_AUTOCMD
1858 if (cmdmod.save_ei == NULL)
1859 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001860 /* Set 'eventignore' to "all". Restore the
1861 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001862 cmdmod.save_ei = vim_strsave(p_ei);
1863 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001864 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001865 }
1866#endif
1867 continue;
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1870 break;
1871#ifdef FEAT_WINDOWS
1872 cmdmod.split |= WSP_BELOW;
1873#endif
1874 continue;
1875
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001876 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1877 {
1878#ifdef HAVE_SANDBOX
1879 if (!did_sandbox)
1880 ++sandbox;
1881 did_sandbox = TRUE;
1882#endif
1883 continue;
1884 }
1885 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001887 if (save_msg_silent == -1)
1888 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1891 {
1892 /* ":silent!", but not "silent !cmd" */
1893 ea.cmd = skipwhite(ea.cmd + 1);
1894 ++emsg_silent;
1895 ++did_esilent;
1896 }
1897 continue;
1898
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001899 case 't': if (checkforcmd(&p, "tab", 3))
1900 {
1901#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001902 if (vim_isdigit(*ea.cmd))
1903 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1904 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001905 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001906 ea.cmd = p;
1907#endif
1908 continue;
1909 }
1910 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 break;
1912#ifdef FEAT_WINDOWS
1913 cmdmod.split |= WSP_TOP;
1914#endif
1915 continue;
1916
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001917 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1918 break;
1919 if (save_msg_silent == -1)
1920 save_msg_silent = msg_silent;
1921 msg_silent = 0;
1922 continue;
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1925 {
1926#ifdef FEAT_VERTSPLIT
1927 cmdmod.split |= WSP_VERT;
1928#endif
1929 continue;
1930 }
1931 if (!checkforcmd(&p, "verbose", 4))
1932 break;
1933 if (verbose_save < 0)
1934 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001935 if (vim_isdigit(*ea.cmd))
1936 p_verbose = atoi((char *)ea.cmd);
1937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 p_verbose = 1;
1939 ea.cmd = p;
1940 continue;
1941 }
1942 break;
1943 }
1944
1945#ifdef FEAT_EVAL
1946 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1947 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1948#else
1949 ea.skip = (if_level > 0);
1950#endif
1951
1952#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001953# ifdef FEAT_PROFILE
1954 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001955 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001956 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001957 if (getline_equal(fgetline, cookie, get_func_line))
1958 func_line_exec(getline_cookie(fgetline, cookie));
1959 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001960 script_line_exec();
1961 }
1962#endif
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 /* May go to debug mode. If this happens and the ">quit" debug command is
1965 * used, throw an interrupt exception and skip the next command. */
1966 dbg_check_breakpoint(&ea);
1967 if (!ea.skip && got_int)
1968 {
1969 ea.skip = TRUE;
1970 (void)do_intthrow(cstack);
1971 }
1972#endif
1973
1974/*
1975 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1976 *
1977 * where 'addr' is:
1978 *
1979 * % (entire file)
1980 * $ [+-NUM]
1981 * 'x [+-NUM] (where x denotes a currently defined mark)
1982 * . [+-NUM]
1983 * [+-NUM]..
1984 * NUM
1985 *
1986 * The ea.cmd pointer is updated to point to the first character following the
1987 * range spec. If an initial address is found, but no second, the upper bound
1988 * is equal to the lower.
1989 */
1990
1991 /* repeat for all ',' or ';' separated addresses */
1992 for (;;)
1993 {
1994 ea.line1 = ea.line2;
1995 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1996 ea.cmd = skipwhite(ea.cmd);
1997 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1998 if (ea.cmd == NULL) /* error detected */
1999 goto doend;
2000 if (lnum == MAXLNUM)
2001 {
2002 if (*ea.cmd == '%') /* '%' - all lines */
2003 {
2004 ++ea.cmd;
2005 ea.line1 = 1;
2006 ea.line2 = curbuf->b_ml.ml_line_count;
2007 ++ea.addr_count;
2008 }
2009 /* '*' - visual area */
2010 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2011 {
2012 pos_T *fp;
2013
2014 ++ea.cmd;
2015 if (!ea.skip)
2016 {
2017 fp = getmark('<', FALSE);
2018 if (check_mark(fp) == FAIL)
2019 goto doend;
2020 ea.line1 = fp->lnum;
2021 fp = getmark('>', FALSE);
2022 if (check_mark(fp) == FAIL)
2023 goto doend;
2024 ea.line2 = fp->lnum;
2025 ++ea.addr_count;
2026 }
2027 }
2028 }
2029 else
2030 ea.line2 = lnum;
2031 ea.addr_count++;
2032
2033 if (*ea.cmd == ';')
2034 {
2035 if (!ea.skip)
2036 curwin->w_cursor.lnum = ea.line2;
2037 }
2038 else if (*ea.cmd != ',')
2039 break;
2040 ++ea.cmd;
2041 }
2042
2043 /* One address given: set start and end lines */
2044 if (ea.addr_count == 1)
2045 {
2046 ea.line1 = ea.line2;
2047 /* ... but only implicit: really no address given */
2048 if (lnum == MAXLNUM)
2049 ea.addr_count = 0;
2050 }
2051
2052 /* Don't leave the cursor on an illegal line (caused by ';') */
2053 check_cursor_lnum();
2054
2055/*
2056 * 4. parse command
2057 */
2058
2059 /*
2060 * Skip ':' and any white space
2061 */
2062 ea.cmd = skipwhite(ea.cmd);
2063 while (*ea.cmd == ':')
2064 ea.cmd = skipwhite(ea.cmd + 1);
2065
2066 /*
2067 * If we got a line, but no command, then go to the line.
2068 * If we find a '|' or '\n' we set ea.nextcmd.
2069 */
2070 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2071 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2072 {
2073 /*
2074 * strange vi behaviour:
2075 * ":3" jumps to line 3
2076 * ":3|..." prints line 3
2077 * ":|" prints current line
2078 */
2079 if (ea.skip) /* skip this if inside :if */
2080 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002081 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 ea.cmdidx = CMD_print;
2084 ea.argt = RANGE+COUNT+TRLBAR;
2085 if ((errormsg = invalid_range(&ea)) == NULL)
2086 {
2087 correct_range(&ea);
2088 ex_print(&ea);
2089 }
2090 }
2091 else if (ea.addr_count != 0)
2092 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002093 if (ea.line2 > curbuf->b_ml.ml_line_count)
2094 {
2095 /* With '-' in 'cpoptions' a line number past the file is an
2096 * error, otherwise put it at the end of the file. */
2097 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2098 ea.line2 = -1;
2099 else
2100 ea.line2 = curbuf->b_ml.ml_line_count;
2101 }
2102
2103 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002104 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 else
2106 {
2107 if (ea.line2 == 0)
2108 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 else
2110 curwin->w_cursor.lnum = ea.line2;
2111 beginline(BL_SOL | BL_FIX);
2112 }
2113 }
2114 goto doend;
2115 }
2116
2117 /* Find the command and let "p" point to after it. */
2118 p = find_command(&ea, NULL);
2119
2120#ifdef FEAT_USR_CMDS
2121 if (p == NULL)
2122 {
2123 if (!ea.skip)
2124 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2125 goto doend;
2126 }
2127 /* Check for wrong commands. */
2128 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2129 {
2130 errormsg = uc_fun_cmd();
2131 goto doend;
2132 }
2133#endif
2134 if (ea.cmdidx == CMD_SIZE)
2135 {
2136 if (!ea.skip)
2137 {
2138 STRCPY(IObuff, _("E492: Not an editor command"));
2139 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002140 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 errormsg = IObuff;
2142 }
2143 goto doend;
2144 }
2145
2146 ni = (
2147#ifdef FEAT_USR_CMDS
2148 !USER_CMDIDX(ea.cmdidx) &&
2149#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002150 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002151#ifdef HAVE_EX_SCRIPT_NI
2152 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2153#endif
2154 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156#ifndef FEAT_EVAL
2157 /*
2158 * When the expression evaluation is disabled, recognize the ":if" and
2159 * ":endif" commands and ignore everything in between it.
2160 */
2161 if (ea.cmdidx == CMD_if)
2162 ++if_level;
2163 if (if_level)
2164 {
2165 if (ea.cmdidx == CMD_endif)
2166 --if_level;
2167 goto doend;
2168 }
2169
2170#endif
2171
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002172 /* forced commands */
2173 if (*p == '!' && ea.cmdidx != CMD_substitute
2174 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
2176 ++p;
2177 ea.forceit = TRUE;
2178 }
2179 else
2180 ea.forceit = FALSE;
2181
2182/*
2183 * 5. parse arguments
2184 */
2185#ifdef FEAT_USR_CMDS
2186 if (!USER_CMDIDX(ea.cmdidx))
2187#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002188 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190 if (!ea.skip)
2191 {
2192#ifdef HAVE_SANDBOX
2193 if (sandbox != 0 && !(ea.argt & SBOXOK))
2194 {
2195 /* Command not allowed in sandbox. */
2196 errormsg = (char_u *)_(e_sandbox);
2197 goto doend;
2198 }
2199#endif
2200 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2201 {
2202 /* Command not allowed in non-'modifiable' buffer */
2203 errormsg = (char_u *)_(e_modifiable);
2204 goto doend;
2205 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002206
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002207 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208# ifdef FEAT_USR_CMDS
2209 && !USER_CMDIDX(ea.cmdidx)
2210# endif
2211 )
2212 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002213 /* Command not allowed when editing the command line. */
2214#ifdef FEAT_CMDWIN
2215 if (cmdwin_type != 0)
2216 errormsg = (char_u *)_(e_cmdwin);
2217 else
2218#endif
2219 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 goto doend;
2221 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002222#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002223 /* Disallow editing another buffer when "curbuf_lock" is set.
2224 * Do allow ":edit" (check for argument later).
2225 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002227 && ea.cmdidx != CMD_edit
2228 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229# ifdef FEAT_USR_CMDS
2230 && !USER_CMDIDX(ea.cmdidx)
2231# endif
2232 && curbuf_locked())
2233 goto doend;
2234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2237 {
2238 /* no range allowed */
2239 errormsg = (char_u *)_(e_norange);
2240 goto doend;
2241 }
2242 }
2243
2244 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2245 {
2246 errormsg = (char_u *)_(e_nobang);
2247 goto doend;
2248 }
2249
2250 /*
2251 * Don't complain about the range if it is not used
2252 * (could happen if line_count is accidentally set to 0).
2253 */
2254 if (!ea.skip && !ni)
2255 {
2256 /*
2257 * If the range is backwards, ask for confirmation and, if given, swap
2258 * ea.line1 & ea.line2 so it's forwards again.
2259 * When global command is busy, don't ask, will fail below.
2260 */
2261 if (!global_busy && ea.line1 > ea.line2)
2262 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002263 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002265 if (sourcing || exmode_active)
2266 {
2267 errormsg = (char_u *)_("E493: Backwards range given");
2268 goto doend;
2269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (ask_yesno((char_u *)
2271 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002272 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
2274 lnum = ea.line1;
2275 ea.line1 = ea.line2;
2276 ea.line2 = lnum;
2277 }
2278 if ((errormsg = invalid_range(&ea)) != NULL)
2279 goto doend;
2280 }
2281
2282 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2283 ea.line2 = 1;
2284
2285 correct_range(&ea);
2286
2287#ifdef FEAT_FOLDING
2288 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2289 {
2290 /* Put the first line at the start of a closed fold, put the last line
2291 * at the end of a closed fold. */
2292 (void)hasFolding(ea.line1, &ea.line1, NULL);
2293 (void)hasFolding(ea.line2, NULL, &ea.line2);
2294 }
2295#endif
2296
2297#ifdef FEAT_QUICKFIX
2298 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002299 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * option here, so things like % get expanded.
2301 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002302 p = replace_makeprg(&ea, p, cmdlinep);
2303 if (p == NULL)
2304 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
2306
2307 /*
2308 * Skip to start of argument.
2309 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2310 */
2311 if (ea.cmdidx == CMD_bang)
2312 ea.arg = p;
2313 else
2314 ea.arg = skipwhite(p);
2315
2316 /*
2317 * Check for "++opt=val" argument.
2318 * Must be first, allow ":w ++enc=utf8 !cmd"
2319 */
2320 if (ea.argt & ARGOPT)
2321 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2322 if (getargopt(&ea) == FAIL && !ni)
2323 {
2324 errormsg = (char_u *)_(e_invarg);
2325 goto doend;
2326 }
2327
2328 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2329 {
2330 if (*ea.arg == '>') /* append */
2331 {
2332 if (*++ea.arg != '>') /* typed wrong */
2333 {
2334 errormsg = (char_u *)_("E494: Use w or w>>");
2335 goto doend;
2336 }
2337 ea.arg = skipwhite(ea.arg + 1);
2338 ea.append = TRUE;
2339 }
2340 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2341 {
2342 ++ea.arg;
2343 ea.usefilter = TRUE;
2344 }
2345 }
2346
2347 if (ea.cmdidx == CMD_read)
2348 {
2349 if (ea.forceit)
2350 {
2351 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2352 ea.forceit = FALSE;
2353 }
2354 else if (*ea.arg == '!') /* :r !filter */
2355 {
2356 ++ea.arg;
2357 ea.usefilter = TRUE;
2358 }
2359 }
2360
2361 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2362 {
2363 ea.amount = 1;
2364 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2365 {
2366 ++ea.arg;
2367 ++ea.amount;
2368 }
2369 ea.arg = skipwhite(ea.arg);
2370 }
2371
2372 /*
2373 * Check for "+command" argument, before checking for next command.
2374 * Don't do this for ":read !cmd" and ":write !cmd".
2375 */
2376 if ((ea.argt & EDITCMD) && !ea.usefilter)
2377 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2378
2379 /*
2380 * Check for '|' to separate commands and '"' to start comments.
2381 * Don't do this for ":read !cmd" and ":write !cmd".
2382 */
2383 if ((ea.argt & TRLBAR) && !ea.usefilter)
2384 separate_nextcmd(&ea);
2385
2386 /*
2387 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2389 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002391 else if (ea.cmdidx == CMD_bang
2392 || ea.cmdidx == CMD_global
2393 || ea.cmdidx == CMD_vglobal
2394 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 for (p = ea.arg; *p; ++p)
2397 {
2398 /* Remove one backslash before a newline, so that it's possible to
2399 * pass a newline to the shell and also a newline that is preceded
2400 * with a backslash. This makes it impossible to end a shell
2401 * command in a backslash, but that doesn't appear useful.
2402 * Halving the number of backslashes is incompatible with previous
2403 * versions. */
2404 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002405 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else if (*p == '\n')
2407 {
2408 ea.nextcmd = p + 1;
2409 *p = NUL;
2410 break;
2411 }
2412 }
2413 }
2414
2415 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2416 {
2417 ea.line1 = 1;
2418 ea.line2 = curbuf->b_ml.ml_line_count;
2419 }
2420
2421 /* accept numbered register only when no count allowed (:put) */
2422 if ( (ea.argt & REGSTR)
2423 && *ea.arg != NUL
2424#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /* Do not allow register = for user commands */
2426 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#endif
2428 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2429 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002430#ifndef FEAT_CLIPBOARD
2431 /* check these explicitly for a more specific error message */
2432 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002434 errormsg = (char_u *)_(e_invalidreg);
2435 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002438 if (
2439#ifdef FEAT_USR_CMDS
2440 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2441 && USER_CMDIDX(ea.cmdidx)))
2442#else
2443 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2444#endif
2445 )
2446 {
2447 ea.regname = *ea.arg++;
2448#ifdef FEAT_EVAL
2449 /* for '=' register: accept the rest of the line as an expression */
2450 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2451 {
2452 set_expr_line(vim_strsave(ea.arg));
2453 ea.arg += STRLEN(ea.arg);
2454 }
2455#endif
2456 ea.arg = skipwhite(ea.arg);
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459
2460 /*
2461 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2462 * count, it's a buffer name.
2463 */
2464 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2465 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2466 || vim_iswhite(*p)))
2467 {
2468 n = getdigits(&ea.arg);
2469 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002470 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 errormsg = (char_u *)_(e_zerocount);
2473 goto doend;
2474 }
2475 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2476 {
2477 ea.line2 = n;
2478 if (ea.addr_count == 0)
2479 ea.addr_count = 1;
2480 }
2481 else
2482 {
2483 ea.line1 = ea.line2;
2484 ea.line2 += n - 1;
2485 ++ea.addr_count;
2486 /*
2487 * Be vi compatible: no error message for out of range.
2488 */
2489 if (ea.line2 > curbuf->b_ml.ml_line_count)
2490 ea.line2 = curbuf->b_ml.ml_line_count;
2491 }
2492 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002493
2494 /*
2495 * Check for flags: 'l', 'p' and '#'.
2496 */
2497 if (ea.argt & EXFLAGS)
2498 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002500 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002501 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 errormsg = (char_u *)_(e_trailing);
2504 goto doend;
2505 }
2506
2507 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2508 {
2509 errormsg = (char_u *)_(e_argreq);
2510 goto doend;
2511 }
2512
2513#ifdef FEAT_EVAL
2514 /*
2515 * Skip the command when it's not going to be executed.
2516 * The commands like :if, :endif, etc. always need to be executed.
2517 * Also make an exception for commands that handle a trailing command
2518 * themselves.
2519 */
2520 if (ea.skip)
2521 {
2522 switch (ea.cmdidx)
2523 {
2524 /* commands that need evaluation */
2525 case CMD_while:
2526 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002527 case CMD_for:
2528 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 case CMD_if:
2530 case CMD_elseif:
2531 case CMD_else:
2532 case CMD_endif:
2533 case CMD_try:
2534 case CMD_catch:
2535 case CMD_finally:
2536 case CMD_endtry:
2537 case CMD_function:
2538 break;
2539
2540 /* Commands that handle '|' themselves. Check: A command should
2541 * either have the TRLBAR flag, appear in this list or appear in
2542 * the list at ":help :bar". */
2543 case CMD_aboveleft:
2544 case CMD_and:
2545 case CMD_belowright:
2546 case CMD_botright:
2547 case CMD_browse:
2548 case CMD_call:
2549 case CMD_confirm:
2550 case CMD_delfunction:
2551 case CMD_djump:
2552 case CMD_dlist:
2553 case CMD_dsearch:
2554 case CMD_dsplit:
2555 case CMD_echo:
2556 case CMD_echoerr:
2557 case CMD_echomsg:
2558 case CMD_echon:
2559 case CMD_execute:
2560 case CMD_help:
2561 case CMD_hide:
2562 case CMD_ijump:
2563 case CMD_ilist:
2564 case CMD_isearch:
2565 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002566 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 case CMD_keepjumps:
2568 case CMD_keepmarks:
2569 case CMD_leftabove:
2570 case CMD_let:
2571 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002572 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002574 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 case CMD_perl:
2576 case CMD_psearch:
2577 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002578 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002579 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 case CMD_return:
2581 case CMD_rightbelow:
2582 case CMD_ruby:
2583 case CMD_silent:
2584 case CMD_smagic:
2585 case CMD_snomagic:
2586 case CMD_substitute:
2587 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002588 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 case CMD_tcl:
2590 case CMD_throw:
2591 case CMD_tilde:
2592 case CMD_topleft:
2593 case CMD_unlet:
2594 case CMD_verbose:
2595 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002596 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 break;
2598
2599 default: goto doend;
2600 }
2601 }
2602#endif
2603
2604 if (ea.argt & XFILE)
2605 {
2606 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2607 goto doend;
2608 }
2609
2610#ifdef FEAT_LISTCMDS
2611 /*
2612 * Accept buffer name. Cannot be used at the same time with a buffer
2613 * number. Don't do this for a user command.
2614 */
2615 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2616# ifdef FEAT_USR_CMDS
2617 && !USER_CMDIDX(ea.cmdidx)
2618# endif
2619 )
2620 {
2621 /*
2622 * :bdelete, :bwipeout and :bunload take several arguments, separated
2623 * by spaces: find next space (skipping over escaped characters).
2624 * The others take one argument: ignore trailing spaces.
2625 */
2626 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2627 || ea.cmdidx == CMD_bunload)
2628 p = skiptowhite_esc(ea.arg);
2629 else
2630 {
2631 p = ea.arg + STRLEN(ea.arg);
2632 while (p > ea.arg && vim_iswhite(p[-1]))
2633 --p;
2634 }
2635 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2636 if (ea.line2 < 0) /* failed */
2637 goto doend;
2638 ea.addr_count = 1;
2639 ea.arg = skipwhite(p);
2640 }
2641#endif
2642
2643/*
2644 * 6. switch on command name
2645 *
2646 * The "ea" structure holds the arguments that can be used.
2647 */
2648 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002649 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 ea.cookie = cookie;
2651#ifdef FEAT_EVAL
2652 ea.cstack = cstack;
2653#endif
2654
2655#ifdef FEAT_USR_CMDS
2656 if (USER_CMDIDX(ea.cmdidx))
2657 {
2658 /*
2659 * Execute a user-defined command.
2660 */
2661 do_ucmd(&ea);
2662 }
2663 else
2664#endif
2665 {
2666 /*
2667 * Call the function to execute the command.
2668 */
2669 ea.errmsg = NULL;
2670 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2671 if (ea.errmsg != NULL)
2672 errormsg = (char_u *)_(ea.errmsg);
2673 }
2674
2675#ifdef FEAT_EVAL
2676 /*
2677 * If the command just executed called do_cmdline(), any throw or ":return"
2678 * or ":finish" encountered there must also check the cstack of the still
2679 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2680 * exception, or reanimate a returned function or finished script file and
2681 * return or finish it again.
2682 */
2683 if (need_rethrow)
2684 do_throw(cstack);
2685 else if (check_cstack)
2686 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 && current_func_returned())
2691 do_return(&ea, TRUE, FALSE, NULL);
2692 }
2693 need_rethrow = check_cstack = FALSE;
2694#endif
2695
2696doend:
2697 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2698 curwin->w_cursor.lnum = 1;
2699
2700 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2701 {
2702 if (sourcing)
2703 {
2704 if (errormsg != IObuff)
2705 {
2706 STRCPY(IObuff, errormsg);
2707 errormsg = IObuff;
2708 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002709 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711 emsg(errormsg);
2712 }
2713#ifdef FEAT_EVAL
2714 do_errthrow(cstack,
2715 (ea.cmdidx != CMD_SIZE
2716# ifdef FEAT_USR_CMDS
2717 && !USER_CMDIDX(ea.cmdidx)
2718# endif
2719 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2720#endif
2721
2722 if (verbose_save >= 0)
2723 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002724#ifdef FEAT_AUTOCMD
2725 if (cmdmod.save_ei != NULL)
2726 {
2727 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002728 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2729 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002730 free_string_option(cmdmod.save_ei);
2731 }
2732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 cmdmod = save_cmdmod;
2735
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002736 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 /* messages could be enabled for a serious error, need to check if the
2739 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002740 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002741 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 emsg_silent -= did_esilent;
2743 if (emsg_silent < 0)
2744 emsg_silent = 0;
2745 /* Restore msg_scroll, it's set by file I/O commands, even when no
2746 * message is actually displayed. */
2747 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002748
2749 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2750 * somewhere in the line. Put it back in the first column. */
2751 if (redirecting())
2752 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002755#ifdef HAVE_SANDBOX
2756 if (did_sandbox)
2757 --sandbox;
2758#endif
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2761 ea.nextcmd = NULL;
2762
2763#ifdef FEAT_EVAL
2764 --ex_nesting_level;
2765#endif
2766
2767 return ea.nextcmd;
2768}
2769#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002770 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771#endif
2772
2773/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002774 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 * If there is a match advance "pp" to the argument and return TRUE.
2776 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002777 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002779 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 char *cmd; /* name of command */
2781 int len; /* required length */
2782{
2783 int i;
2784
2785 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002786 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 break;
2788 if (i >= len && !isalpha((*pp)[i]))
2789 {
2790 *pp = skipwhite(*pp + i);
2791 return TRUE;
2792 }
2793 return FALSE;
2794}
2795
2796/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002797 * Append "cmd" to the error message in IObuff.
2798 * Takes care of limiting the length and handling 0xa0, which would be
2799 * invisible otherwise.
2800 */
2801 static void
2802append_command(cmd)
2803 char_u *cmd;
2804{
2805 char_u *s = cmd;
2806 char_u *d;
2807
2808 STRCAT(IObuff, ": ");
2809 d = IObuff + STRLEN(IObuff);
2810 while (*s != NUL && d - IObuff < IOSIZE - 7)
2811 {
2812 if (
2813#ifdef FEAT_MBYTE
2814 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2815#endif
2816 *s == 0xa0)
2817 {
2818 s +=
2819#ifdef FEAT_MBYTE
2820 enc_utf8 ? 2 :
2821#endif
2822 1;
2823 STRCPY(d, "<a0>");
2824 d += 4;
2825 }
2826 else
2827 MB_COPY_CHAR(s, d);
2828 }
2829 *d = NUL;
2830}
2831
2832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002834 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002836 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 * Returns NULL for an ambiguous user command.
2838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 static char_u *
2840find_command(eap, full)
2841 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002842 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 int len;
2845 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002846 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
2848 /*
2849 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002850 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 * - the 'k' command can directly be followed by any character.
2852 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2853 * but :sre[wind] is another command, as are :scrip[tnames],
2854 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002855 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 p = eap->cmd;
2858 if (*p == 'k')
2859 {
2860 eap->cmdidx = CMD_k;
2861 ++p;
2862 }
2863 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002864 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2865 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 || p[1] == 'g'
2867 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2868 || p[1] == 'I'
2869 || (p[1] == 'r' && p[2] != 'e')))
2870 {
2871 eap->cmdidx = CMD_substitute;
2872 ++p;
2873 }
2874 else
2875 {
2876 while (ASCII_ISALPHA(*p))
2877 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002878 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002879 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002880 while (ASCII_ISALNUM(*p))
2881 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 /* check for non-alpha command */
2884 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2885 ++p;
2886 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002887 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2888 {
2889 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2890 * :delete with the 'l' flag. Same for 'p'. */
2891 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002892 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002893 break;
2894 if (i == len - 1)
2895 {
2896 --len;
2897 if (p[-1] == 'l')
2898 eap->flags |= EXFLAG_LIST;
2899 else
2900 eap->flags |= EXFLAG_PRINT;
2901 }
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 if (ASCII_ISLOWER(*eap->cmd))
2905 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2906 else
2907 eap->cmdidx = cmdidxs[26];
2908
2909 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2910 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2911 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2912 (size_t)len) == 0)
2913 {
2914#ifdef FEAT_EVAL
2915 if (full != NULL
2916 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2917 *full = TRUE;
2918#endif
2919 break;
2920 }
2921
2922#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002923 /* Look for a user defined command as a last resort. Let ":Print" be
2924 * overruled by a user defined command. */
2925 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2926 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002928 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 while (ASCII_ISALNUM(*p))
2930 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002931 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
2933#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002934 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 eap->cmdidx = CMD_SIZE;
2936 }
2937
2938 return p;
2939}
2940
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002941#ifdef FEAT_USR_CMDS
2942/*
2943 * Search for a user command that matches "eap->cmd".
2944 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2945 * Return a pointer to just after the command.
2946 * Return NULL if there is no matching command.
2947 */
2948 static char_u *
2949find_ucmd(eap, p, full, xp, compl)
2950 exarg_T *eap;
2951 char_u *p; /* end of the command (possibly including count) */
2952 int *full; /* set to TRUE for a full match */
2953 expand_T *xp; /* used for completion, NULL otherwise */
2954 int *compl; /* completion flags or NULL */
2955{
2956 int len = (int)(p - eap->cmd);
2957 int j, k, matchlen = 0;
2958 ucmd_T *uc;
2959 int found = FALSE;
2960 int possible = FALSE;
2961 char_u *cp, *np; /* Point into typed cmd and test name */
2962 garray_T *gap;
2963 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2964 only full match global is accepted. */
2965
2966 /*
2967 * Look for buffer-local user commands first, then global ones.
2968 */
2969 gap = &curbuf->b_ucmds;
2970 for (;;)
2971 {
2972 for (j = 0; j < gap->ga_len; ++j)
2973 {
2974 uc = USER_CMD_GA(gap, j);
2975 cp = eap->cmd;
2976 np = uc->uc_name;
2977 k = 0;
2978 while (k < len && *np != NUL && *cp++ == *np++)
2979 k++;
2980 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2981 {
2982 /* If finding a second match, the command is ambiguous. But
2983 * not if a buffer-local command wasn't a full match and a
2984 * global command is a full match. */
2985 if (k == len && found && *np != NUL)
2986 {
2987 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002988 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002989 amb_local = TRUE;
2990 }
2991
2992 if (!found || (k == len && *np == NUL))
2993 {
2994 /* If we matched up to a digit, then there could
2995 * be another command including the digit that we
2996 * should use instead.
2997 */
2998 if (k == len)
2999 found = TRUE;
3000 else
3001 possible = TRUE;
3002
3003 if (gap == &ucmds)
3004 eap->cmdidx = CMD_USER;
3005 else
3006 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003007 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003008 eap->useridx = j;
3009
3010# ifdef FEAT_CMDL_COMPL
3011 if (compl != NULL)
3012 *compl = uc->uc_compl;
3013# ifdef FEAT_EVAL
3014 if (xp != NULL)
3015 {
3016 xp->xp_arg = uc->uc_compl_arg;
3017 xp->xp_scriptID = uc->uc_scriptID;
3018 }
3019# endif
3020# endif
3021 /* Do not search for further abbreviations
3022 * if this is an exact match. */
3023 matchlen = k;
3024 if (k == len && *np == NUL)
3025 {
3026 if (full != NULL)
3027 *full = TRUE;
3028 amb_local = FALSE;
3029 break;
3030 }
3031 }
3032 }
3033 }
3034
3035 /* Stop if we found a full match or searched all. */
3036 if (j < gap->ga_len || gap == &ucmds)
3037 break;
3038 gap = &ucmds;
3039 }
3040
3041 /* Only found ambiguous matches. */
3042 if (amb_local)
3043 {
3044 if (xp != NULL)
3045 xp->xp_context = EXPAND_UNSUCCESSFUL;
3046 return NULL;
3047 }
3048
3049 /* The match we found may be followed immediately by a number. Move "p"
3050 * back to point to it. */
3051 if (found || possible)
3052 return p + (matchlen - len);
3053 return p;
3054}
3055#endif
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003058static struct cmdmod
3059{
3060 char *name;
3061 int minlen;
3062 int has_count; /* :123verbose :3tab */
3063} cmdmods[] = {
3064 {"aboveleft", 3, FALSE},
3065 {"belowright", 3, FALSE},
3066 {"botright", 2, FALSE},
3067 {"browse", 3, FALSE},
3068 {"confirm", 4, FALSE},
3069 {"hide", 3, FALSE},
3070 {"keepalt", 5, FALSE},
3071 {"keepjumps", 5, FALSE},
3072 {"keepmarks", 3, FALSE},
3073 {"leftabove", 5, FALSE},
3074 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003075 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003076 {"rightbelow", 6, FALSE},
3077 {"sandbox", 3, FALSE},
3078 {"silent", 3, FALSE},
3079 {"tab", 3, TRUE},
3080 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003081 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003082 {"verbose", 4, TRUE},
3083 {"vertical", 4, FALSE},
3084};
3085
3086/*
3087 * Return length of a command modifier (including optional count).
3088 * Return zero when it's not a modifier.
3089 */
3090 int
3091modifier_len(cmd)
3092 char_u *cmd;
3093{
3094 int i, j;
3095 char_u *p = cmd;
3096
3097 if (VIM_ISDIGIT(*cmd))
3098 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003099 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003100 {
3101 for (j = 0; p[j] != NUL; ++j)
3102 if (p[j] != cmdmods[i].name[j])
3103 break;
3104 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3105 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003106 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003107 }
3108 return 0;
3109}
3110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111/*
3112 * Return > 0 if an Ex command "name" exists.
3113 * Return 2 if there is an exact match.
3114 * Return 3 if there is an ambiguous match.
3115 */
3116 int
3117cmd_exists(name)
3118 char_u *name;
3119{
3120 exarg_T ea;
3121 int full = FALSE;
3122 int i;
3123 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003124 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003127 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 for (j = 0; name[j] != NUL; ++j)
3130 if (name[j] != cmdmods[i].name[j])
3131 break;
3132 if (name[j] == NUL && j >= cmdmods[i].minlen)
3133 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3134 }
3135
Bram Moolenaara9587612006-05-04 21:47:50 +00003136 /* Check built-in commands and user defined commands.
3137 * For ":2match" and ":3match" we need to skip the number. */
3138 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003140 p = find_command(&ea, &full);
3141 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003143 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3144 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003145 if (*skipwhite(p) != NUL)
3146 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3148}
3149#endif
3150
3151/*
3152 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3153 * we don't need/want deleted. Maybe this could be done better if we didn't
3154 * repeat all this stuff. The only problem is that they may not stay
3155 * perfectly compatible with each other, but then the command line syntax
3156 * probably won't change that much -- webb.
3157 */
3158 char_u *
3159set_one_cmd_context(xp, buff)
3160 expand_T *xp;
3161 char_u *buff; /* buffer for command string */
3162{
3163 char_u *p;
3164 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003165 int len = 0;
3166 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3168 int compl = EXPAND_NOTHING;
3169#endif
3170#ifdef FEAT_CMDL_COMPL
3171 int delim;
3172#endif
3173 int forceit = FALSE;
3174 int usefilter = FALSE; /* filter instead of file name */
3175
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003176 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 xp->xp_pattern = buff;
3178 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181/*
3182 * 2. skip comment lines and leading space, colons or bars
3183 */
3184 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3185 ;
3186 xp->xp_pattern = cmd;
3187
3188 if (*cmd == NUL)
3189 return NULL;
3190 if (*cmd == '"') /* ignore comment lines */
3191 {
3192 xp->xp_context = EXPAND_NOTHING;
3193 return NULL;
3194 }
3195
3196/*
3197 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3198 */
3199 cmd = skip_range(cmd, &xp->xp_context);
3200
3201/*
3202 * 4. parse command
3203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 xp->xp_pattern = cmd;
3205 if (*cmd == NUL)
3206 return NULL;
3207 if (*cmd == '"')
3208 {
3209 xp->xp_context = EXPAND_NOTHING;
3210 return NULL;
3211 }
3212
3213 if (*cmd == '|' || *cmd == '\n')
3214 return cmd + 1; /* There's another command */
3215
3216 /*
3217 * Isolate the command and search for it in the command table.
3218 * Exceptions:
3219 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003220 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3222 */
3223 if (*cmd == 'k' && cmd[1] != 'e')
3224 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 p = cmd + 1;
3227 }
3228 else
3229 {
3230 p = cmd;
3231 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3232 ++p;
3233 /* check for non-alpha command */
3234 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3235 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 xp->xp_context = EXPAND_UNSUCCESSFUL;
3241 return NULL;
3242 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003244 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3245 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3246 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 break;
3248
3249#ifdef FEAT_USR_CMDS
3250 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3252 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253#endif
3254 }
3255
3256 /*
3257 * If the cursor is touching the command, and it ends in an alpha-numeric
3258 * character, complete the command name.
3259 */
3260 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3261 return NULL;
3262
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3266 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003267 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 p = cmd + 1;
3269 }
3270#ifdef FEAT_USR_CMDS
3271 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3272 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003273 ea.cmd = cmd;
3274 p = find_ucmd(&ea, p, NULL, xp,
3275# if defined(FEAT_CMDL_COMPL)
3276 &compl
3277# else
3278 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003281 if (p == NULL)
3282 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284#endif
3285 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003286 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 /* Not still touching the command and it was an illegal one */
3289 xp->xp_context = EXPAND_UNSUCCESSFUL;
3290 return NULL;
3291 }
3292
3293 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3294
3295 if (*p == '!') /* forced commands */
3296 {
3297 forceit = TRUE;
3298 ++p;
3299 }
3300
3301/*
3302 * 5. parse arguments
3303 */
3304#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003307 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308
3309 arg = skipwhite(p);
3310
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 if (*arg == '>') /* append */
3314 {
3315 if (*++arg == '>')
3316 ++arg;
3317 arg = skipwhite(arg);
3318 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 ++arg;
3322 usefilter = TRUE;
3323 }
3324 }
3325
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 usefilter = forceit; /* :r! filter if forced */
3329 if (*arg == '!') /* :r !filter */
3330 {
3331 ++arg;
3332 usefilter = TRUE;
3333 }
3334 }
3335
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003336 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
3338 while (*arg == *cmd) /* allow any number of '>' or '<' */
3339 ++arg;
3340 arg = skipwhite(arg);
3341 }
3342
3343 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003344 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
3346 /* Check if we're in the +command */
3347 p = arg + 1;
3348 arg = skip_cmd_arg(arg, FALSE);
3349
3350 /* Still touching the command after '+'? */
3351 if (*arg == NUL)
3352 return p;
3353
3354 /* Skip space(s) after +command to get to the real argument */
3355 arg = skipwhite(arg);
3356 }
3357
3358 /*
3359 * Check for '|' to separate commands and '"' to start comments.
3360 * Don't do this for ":read !cmd" and ":write !cmd".
3361 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003362 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
3364 p = arg;
3365 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003366 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 p += 2;
3368 while (*p)
3369 {
3370 if (*p == Ctrl_V)
3371 {
3372 if (p[1] != NUL)
3373 ++p;
3374 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003375 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 || *p == '|' || *p == '\n')
3377 {
3378 if (*(p - 1) != '\\')
3379 {
3380 if (*p == '|' || *p == '\n')
3381 return p + 1;
3382 return NULL; /* It's a comment */
3383 }
3384 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003385 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 }
3388
3389 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003390 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 vim_strchr((char_u *)"|\"", *arg) == NULL)
3392 return NULL;
3393
3394 /* Find start of last argument (argument just before cursor): */
3395 p = buff + STRLEN(buff);
3396 while (p != arg && *p != ' ' && *p != TAB)
3397 p--;
3398 if (*p == ' ' || *p == TAB)
3399 p++;
3400 xp->xp_pattern = p;
3401
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003402 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003404 int c;
3405 int in_quote = FALSE;
3406 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 /*
3409 * Allow spaces within back-quotes to count as part of the argument
3410 * being expanded.
3411 */
3412 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003413 p = xp->xp_pattern;
3414 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003416#ifdef FEAT_MBYTE
3417 if (has_mbyte)
3418 c = mb_ptr2char(p);
3419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003421 c = *p;
3422 if (c == '\\' && p[1] != NUL)
3423 ++p;
3424 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
3426 if (!in_quote)
3427 {
3428 xp->xp_pattern = p;
3429 bow = p + 1;
3430 }
3431 in_quote = !in_quote;
3432 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003433 /* An argument can contain just about everything, except
3434 * characters that end the command and white space. */
3435 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003436#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003437 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003438#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003439 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003440 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003441 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003442 while (*p != NUL)
3443 {
3444#ifdef FEAT_MBYTE
3445 if (has_mbyte)
3446 c = mb_ptr2char(p);
3447 else
3448#endif
3449 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003450 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003451 break;
3452#ifdef FEAT_MBYTE
3453 if (has_mbyte)
3454 len = (*mb_ptr2len)(p);
3455 else
3456#endif
3457 len = 1;
3458 mb_ptr_adv(p);
3459 }
3460 if (in_quote)
3461 bow = p;
3462 else
3463 xp->xp_pattern = p;
3464 p -= len;
3465 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003466 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468
3469 /*
3470 * If we are still inside the quotes, and we passed a space, just
3471 * expand from there.
3472 */
3473 if (bow != NULL && in_quote)
3474 xp->xp_pattern = bow;
3475 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003476
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003477 /* For a shell command more chars need to be escaped. */
3478 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003479 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003480#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003481 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003482#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003483 /* When still after the command name expand executables. */
3484 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003485 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 /* Check for environment variable */
3489 if (*xp->xp_pattern == '$'
3490#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3491 || *xp->xp_pattern == '%'
3492#endif
3493 )
3494 {
3495 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3496 if (!vim_isIDc(*p))
3497 break;
3498 if (*p == NUL)
3499 {
3500 xp->xp_context = EXPAND_ENV_VARS;
3501 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003502#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3503 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003504 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003505 compl = EXPAND_ENV_VARS;
3506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 }
3509 }
3510
3511/*
3512 * 6. switch on command name
3513 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003514 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003516 case CMD_find:
3517 case CMD_sfind:
3518 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003519 if (xp->xp_context == EXPAND_FILES)
3520 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 case CMD_cd:
3523 case CMD_chdir:
3524 case CMD_lcd:
3525 case CMD_lchdir:
3526 if (xp->xp_context == EXPAND_FILES)
3527 xp->xp_context = EXPAND_DIRECTORIES;
3528 break;
3529 case CMD_help:
3530 xp->xp_context = EXPAND_HELP;
3531 xp->xp_pattern = arg;
3532 break;
3533
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003534 /* Command modifiers: return the argument.
3535 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003537 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 case CMD_belowright:
3539 case CMD_botright:
3540 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003541 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003543 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 case CMD_folddoclosed:
3545 case CMD_folddoopen:
3546 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003547 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 case CMD_keepjumps:
3549 case CMD_keepmarks:
3550 case CMD_leftabove:
3551 case CMD_lockmarks:
3552 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003553 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003555 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 case CMD_topleft:
3557 case CMD_verbose:
3558 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003559 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return arg;
3561
Bram Moolenaar4f688582007-07-24 12:34:30 +00003562#ifdef FEAT_CMDL_COMPL
3563# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 case CMD_match:
3565 if (*arg == NUL || !ends_excmd(*arg))
3566 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003567 /* also complete "None" */
3568 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 arg = skipwhite(skiptowhite(arg));
3570 if (*arg != NUL)
3571 {
3572 xp->xp_context = EXPAND_NOTHING;
3573 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3574 }
3575 }
3576 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579/*
3580 * All completion for the +cmdline_compl feature goes here.
3581 */
3582
3583# ifdef FEAT_USR_CMDS
3584 case CMD_command:
3585 /* Check for attributes */
3586 while (*arg == '-')
3587 {
3588 arg++; /* Skip "-" */
3589 p = skiptowhite(arg);
3590 if (*p == NUL)
3591 {
3592 /* Cursor is still in the attribute */
3593 p = vim_strchr(arg, '=');
3594 if (p == NULL)
3595 {
3596 /* No "=", so complete attribute names */
3597 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3598 xp->xp_pattern = arg;
3599 return NULL;
3600 }
3601
3602 /* For the -complete and -nargs attributes, we complete
3603 * their arguments as well.
3604 */
3605 if (STRNICMP(arg, "complete", p - arg) == 0)
3606 {
3607 xp->xp_context = EXPAND_USER_COMPLETE;
3608 xp->xp_pattern = p + 1;
3609 return NULL;
3610 }
3611 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3612 {
3613 xp->xp_context = EXPAND_USER_NARGS;
3614 xp->xp_pattern = p + 1;
3615 return NULL;
3616 }
3617 return NULL;
3618 }
3619 arg = skipwhite(p);
3620 }
3621
3622 /* After the attributes comes the new command name */
3623 p = skiptowhite(arg);
3624 if (*p == NUL)
3625 {
3626 xp->xp_context = EXPAND_USER_COMMANDS;
3627 xp->xp_pattern = arg;
3628 break;
3629 }
3630
3631 /* And finally comes a normal command */
3632 return skipwhite(p);
3633
3634 case CMD_delcommand:
3635 xp->xp_context = EXPAND_USER_COMMANDS;
3636 xp->xp_pattern = arg;
3637 break;
3638# endif
3639
3640 case CMD_global:
3641 case CMD_vglobal:
3642 delim = *arg; /* get the delimiter */
3643 if (delim)
3644 ++arg; /* skip delimiter if there is one */
3645
3646 while (arg[0] != NUL && arg[0] != delim)
3647 {
3648 if (arg[0] == '\\' && arg[1] != NUL)
3649 ++arg;
3650 ++arg;
3651 }
3652 if (arg[0] != NUL)
3653 return arg + 1;
3654 break;
3655 case CMD_and:
3656 case CMD_substitute:
3657 delim = *arg;
3658 if (delim)
3659 {
3660 /* skip "from" part */
3661 ++arg;
3662 arg = skip_regexp(arg, delim, p_magic, NULL);
3663 }
3664 /* skip "to" part */
3665 while (arg[0] != NUL && arg[0] != delim)
3666 {
3667 if (arg[0] == '\\' && arg[1] != NUL)
3668 ++arg;
3669 ++arg;
3670 }
3671 if (arg[0] != NUL) /* skip delimiter */
3672 ++arg;
3673 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3674 ++arg;
3675 if (arg[0] != NUL)
3676 return arg;
3677 break;
3678 case CMD_isearch:
3679 case CMD_dsearch:
3680 case CMD_ilist:
3681 case CMD_dlist:
3682 case CMD_ijump:
3683 case CMD_psearch:
3684 case CMD_djump:
3685 case CMD_isplit:
3686 case CMD_dsplit:
3687 arg = skipwhite(skipdigits(arg)); /* skip count */
3688 if (*arg == '/') /* Match regexp, not just whole words */
3689 {
3690 for (++arg; *arg && *arg != '/'; arg++)
3691 if (*arg == '\\' && arg[1] != NUL)
3692 arg++;
3693 if (*arg)
3694 {
3695 arg = skipwhite(arg + 1);
3696
3697 /* Check for trailing illegal characters */
3698 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3699 xp->xp_context = EXPAND_NOTHING;
3700 else
3701 return arg;
3702 }
3703 }
3704 break;
3705#ifdef FEAT_AUTOCMD
3706 case CMD_autocmd:
3707 return set_context_in_autocmd(xp, arg, FALSE);
3708
3709 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003710 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return set_context_in_autocmd(xp, arg, TRUE);
3712#endif
3713 case CMD_set:
3714 set_context_in_set_cmd(xp, arg, 0);
3715 break;
3716 case CMD_setglobal:
3717 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3718 break;
3719 case CMD_setlocal:
3720 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3721 break;
3722 case CMD_tag:
3723 case CMD_stag:
3724 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003725 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 case CMD_tselect:
3727 case CMD_stselect:
3728 case CMD_ptselect:
3729 case CMD_tjump:
3730 case CMD_stjump:
3731 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003732 if (*p_wop != NUL)
3733 xp->xp_context = EXPAND_TAGS_LISTFILES;
3734 else
3735 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 xp->xp_pattern = arg;
3737 break;
3738 case CMD_augroup:
3739 xp->xp_context = EXPAND_AUGROUP;
3740 xp->xp_pattern = arg;
3741 break;
3742#ifdef FEAT_SYN_HL
3743 case CMD_syntax:
3744 set_context_in_syntax_cmd(xp, arg);
3745 break;
3746#endif
3747#ifdef FEAT_EVAL
3748 case CMD_let:
3749 case CMD_if:
3750 case CMD_elseif:
3751 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003752 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 case CMD_echo:
3754 case CMD_echon:
3755 case CMD_execute:
3756 case CMD_echomsg:
3757 case CMD_echoerr:
3758 case CMD_call:
3759 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003760 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 break;
3762
3763 case CMD_unlet:
3764 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3765 arg = xp->xp_pattern + 1;
3766 xp->xp_context = EXPAND_USER_VARS;
3767 xp->xp_pattern = arg;
3768 break;
3769
3770 case CMD_function:
3771 case CMD_delfunction:
3772 xp->xp_context = EXPAND_USER_FUNC;
3773 xp->xp_pattern = arg;
3774 break;
3775
3776 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003777 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 break;
3779#endif
3780 case CMD_highlight:
3781 set_context_in_highlight_cmd(xp, arg);
3782 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003783#ifdef FEAT_CSCOPE
3784 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003785 case CMD_lcscope:
3786 case CMD_scscope:
3787 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003788 break;
3789#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003790#ifdef FEAT_SIGNS
3791 case CMD_sign:
3792 set_context_in_sign_cmd(xp, arg);
3793 break;
3794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795#ifdef FEAT_LISTCMDS
3796 case CMD_bdelete:
3797 case CMD_bwipeout:
3798 case CMD_bunload:
3799 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3800 arg = xp->xp_pattern + 1;
3801 /*FALLTHROUGH*/
3802 case CMD_buffer:
3803 case CMD_sbuffer:
3804 case CMD_checktime:
3805 xp->xp_context = EXPAND_BUFFERS;
3806 xp->xp_pattern = arg;
3807 break;
3808#endif
3809#ifdef FEAT_USR_CMDS
3810 case CMD_USER:
3811 case CMD_USER_BUF:
3812 if (compl != EXPAND_NOTHING)
3813 {
3814 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003815 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
3817# ifdef FEAT_MENU
3818 if (compl == EXPAND_MENUS)
3819 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3820# endif
3821 if (compl == EXPAND_COMMANDS)
3822 return arg;
3823 if (compl == EXPAND_MAPPINGS)
3824 return set_context_in_map_cmd(xp, (char_u *)"map",
3825 arg, forceit, FALSE, FALSE, CMD_map);
3826 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3827 arg = xp->xp_pattern + 1;
3828 xp->xp_pattern = arg;
3829 }
3830 xp->xp_context = compl;
3831 }
3832 break;
3833#endif
3834 case CMD_map: case CMD_noremap:
3835 case CMD_nmap: case CMD_nnoremap:
3836 case CMD_vmap: case CMD_vnoremap:
3837 case CMD_omap: case CMD_onoremap:
3838 case CMD_imap: case CMD_inoremap:
3839 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003840 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003842 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_unmap:
3844 case CMD_nunmap:
3845 case CMD_vunmap:
3846 case CMD_ounmap:
3847 case CMD_iunmap:
3848 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003849 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003851 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 case CMD_abbreviate: case CMD_noreabbrev:
3853 case CMD_cabbrev: case CMD_cnoreabbrev:
3854 case CMD_iabbrev: case CMD_inoreabbrev:
3855 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003856 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 case CMD_unabbreviate:
3858 case CMD_cunabbrev:
3859 case CMD_iunabbrev:
3860 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003861 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#ifdef FEAT_MENU
3863 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3864 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3865 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3866 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3867 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3868 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3869 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3870 case CMD_tmenu: case CMD_tunmenu:
3871 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3872 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3873#endif
3874
3875 case CMD_colorscheme:
3876 xp->xp_context = EXPAND_COLORS;
3877 xp->xp_pattern = arg;
3878 break;
3879
3880 case CMD_compiler:
3881 xp->xp_context = EXPAND_COMPILER;
3882 xp->xp_pattern = arg;
3883 break;
3884
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003885 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003886 xp->xp_context = EXPAND_OWNSYNTAX;
3887 xp->xp_pattern = arg;
3888 break;
3889
3890 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003891 xp->xp_context = EXPAND_FILETYPE;
3892 xp->xp_pattern = arg;
3893 break;
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3896 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3897 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003898 p = skiptowhite(arg);
3899 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
3901 xp->xp_context = EXPAND_LANGUAGE;
3902 xp->xp_pattern = arg;
3903 }
3904 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003905 {
3906 if ( STRNCMP(arg, "messages", p - arg) == 0
3907 || STRNCMP(arg, "ctype", p - arg) == 0
3908 || STRNCMP(arg, "time", p - arg) == 0)
3909 {
3910 xp->xp_context = EXPAND_LOCALES;
3911 xp->xp_pattern = skipwhite(p);
3912 }
3913 else
3914 xp->xp_context = EXPAND_NOTHING;
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003918#if defined(FEAT_PROFILE)
3919 case CMD_profile:
3920 set_context_in_profile_cmd(xp, arg);
3921 break;
3922#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003923 case CMD_behave:
3924 xp->xp_context = EXPAND_BEHAVE;
3925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927#endif /* FEAT_CMDL_COMPL */
3928
3929 default:
3930 break;
3931 }
3932 return NULL;
3933}
3934
3935/*
3936 * skip a range specifier of the form: addr [,addr] [;addr] ..
3937 *
3938 * Backslashed delimiters after / or ? will be skipped, and commands will
3939 * not be expanded between /'s and ?'s or after "'".
3940 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003941 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * Returns the "cmd" pointer advanced to beyond the range.
3943 */
3944 char_u *
3945skip_range(cmd, ctx)
3946 char_u *cmd;
3947 int *ctx; /* pointer to xp_context or NULL */
3948{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003949 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaardf177f62005-02-22 08:39:57 +00003951 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
3953 if (*cmd == '\'')
3954 {
3955 if (*++cmd == NUL && ctx != NULL)
3956 *ctx = EXPAND_NOTHING;
3957 }
3958 else if (*cmd == '/' || *cmd == '?')
3959 {
3960 delim = *cmd++;
3961 while (*cmd != NUL && *cmd != delim)
3962 if (*cmd++ == '\\' && *cmd != NUL)
3963 ++cmd;
3964 if (*cmd == NUL && ctx != NULL)
3965 *ctx = EXPAND_NOTHING;
3966 }
3967 if (*cmd != NUL)
3968 ++cmd;
3969 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003970
3971 /* Skip ":" and white space. */
3972 while (*cmd == ':')
3973 cmd = skipwhite(cmd + 1);
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return cmd;
3976}
3977
3978/*
3979 * get a single EX address
3980 *
3981 * Set ptr to the next character after the part that was interpreted.
3982 * Set ptr to NULL when an error is encountered.
3983 *
3984 * Return MAXLNUM when no Ex address was found.
3985 */
3986 static linenr_T
3987get_address(ptr, skip, to_other_file)
3988 char_u **ptr;
3989 int skip; /* only skip the address, don't use it */
3990 int to_other_file; /* flag: may jump to other file */
3991{
3992 int c;
3993 int i;
3994 long n;
3995 char_u *cmd;
3996 pos_T pos;
3997 pos_T *fp;
3998 linenr_T lnum;
3999
4000 cmd = skipwhite(*ptr);
4001 lnum = MAXLNUM;
4002 do
4003 {
4004 switch (*cmd)
4005 {
4006 case '.': /* '.' - Cursor position */
4007 ++cmd;
4008 lnum = curwin->w_cursor.lnum;
4009 break;
4010
4011 case '$': /* '$' - last line */
4012 ++cmd;
4013 lnum = curbuf->b_ml.ml_line_count;
4014 break;
4015
4016 case '\'': /* ''' - mark */
4017 if (*++cmd == NUL)
4018 {
4019 cmd = NULL;
4020 goto error;
4021 }
4022 if (skip)
4023 ++cmd;
4024 else
4025 {
4026 /* Only accept a mark in another file when it is
4027 * used by itself: ":'M". */
4028 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4029 ++cmd;
4030 if (fp == (pos_T *)-1)
4031 /* Jumped to another file. */
4032 lnum = curwin->w_cursor.lnum;
4033 else
4034 {
4035 if (check_mark(fp) == FAIL)
4036 {
4037 cmd = NULL;
4038 goto error;
4039 }
4040 lnum = fp->lnum;
4041 }
4042 }
4043 break;
4044
4045 case '/':
4046 case '?': /* '/' or '?' - search */
4047 c = *cmd++;
4048 if (skip) /* skip "/pat/" */
4049 {
4050 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4051 if (*cmd == c)
4052 ++cmd;
4053 }
4054 else
4055 {
4056 pos = curwin->w_cursor; /* save curwin->w_cursor */
4057 /*
4058 * When '/' or '?' follows another address, start
4059 * from there.
4060 */
4061 if (lnum != MAXLNUM)
4062 curwin->w_cursor.lnum = lnum;
4063 /*
4064 * Start a forward search at the end of the line.
4065 * Start a backward search at the start of the line.
4066 * This makes sure we never match in the current
4067 * line, and can match anywhere in the
4068 * next/previous line.
4069 */
4070 if (c == '/')
4071 curwin->w_cursor.col = MAXCOL;
4072 else
4073 curwin->w_cursor.col = 0;
4074 searchcmdlen = 0;
4075 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004076 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 curwin->w_cursor = pos;
4079 cmd = NULL;
4080 goto error;
4081 }
4082 lnum = curwin->w_cursor.lnum;
4083 curwin->w_cursor = pos;
4084 /* adjust command string pointer */
4085 cmd += searchcmdlen;
4086 }
4087 break;
4088
4089 case '\\': /* "\?", "\/" or "\&", repeat search */
4090 ++cmd;
4091 if (*cmd == '&')
4092 i = RE_SUBST;
4093 else if (*cmd == '?' || *cmd == '/')
4094 i = RE_SEARCH;
4095 else
4096 {
4097 EMSG(_(e_backslash));
4098 cmd = NULL;
4099 goto error;
4100 }
4101
4102 if (!skip)
4103 {
4104 /*
4105 * When search follows another address, start from
4106 * there.
4107 */
4108 if (lnum != MAXLNUM)
4109 pos.lnum = lnum;
4110 else
4111 pos.lnum = curwin->w_cursor.lnum;
4112
4113 /*
4114 * Start the search just like for the above
4115 * do_search().
4116 */
4117 if (*cmd != '?')
4118 pos.col = MAXCOL;
4119 else
4120 pos.col = 0;
4121 if (searchit(curwin, curbuf, &pos,
4122 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004123 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004124 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 lnum = pos.lnum;
4126 else
4127 {
4128 cmd = NULL;
4129 goto error;
4130 }
4131 }
4132 ++cmd;
4133 break;
4134
4135 default:
4136 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4137 lnum = getdigits(&cmd);
4138 }
4139
4140 for (;;)
4141 {
4142 cmd = skipwhite(cmd);
4143 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4144 break;
4145
4146 if (lnum == MAXLNUM)
4147 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4148 if (VIM_ISDIGIT(*cmd))
4149 i = '+'; /* "number" is same as "+number" */
4150 else
4151 i = *cmd++;
4152 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4153 n = 1;
4154 else
4155 n = getdigits(&cmd);
4156 if (i == '-')
4157 lnum -= n;
4158 else
4159 lnum += n;
4160 }
4161 } while (*cmd == '/' || *cmd == '?');
4162
4163error:
4164 *ptr = cmd;
4165 return lnum;
4166}
4167
4168/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004169 * Get flags from an Ex command argument.
4170 */
4171 static void
4172get_flags(eap)
4173 exarg_T *eap;
4174{
4175 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4176 {
4177 if (*eap->arg == 'l')
4178 eap->flags |= EXFLAG_LIST;
4179 else if (*eap->arg == 'p')
4180 eap->flags |= EXFLAG_PRINT;
4181 else
4182 eap->flags |= EXFLAG_NR;
4183 eap->arg = skipwhite(eap->arg + 1);
4184 }
4185}
4186
4187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 * Function called for command which is Not Implemented. NI!
4189 */
4190 void
4191ex_ni(eap)
4192 exarg_T *eap;
4193{
4194 if (!eap->skip)
4195 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4196}
4197
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004198#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199/*
4200 * Function called for script command which is Not Implemented. NI!
4201 * Skips over ":perl <<EOF" constructs.
4202 */
4203 static void
4204ex_script_ni(eap)
4205 exarg_T *eap;
4206{
4207 if (!eap->skip)
4208 ex_ni(eap);
4209 else
4210 vim_free(script_get(eap, eap->arg));
4211}
4212#endif
4213
4214/*
4215 * Check range in Ex command for validity.
4216 * Return NULL when valid, error message when invalid.
4217 */
4218 static char_u *
4219invalid_range(eap)
4220 exarg_T *eap;
4221{
4222 if ( eap->line1 < 0
4223 || eap->line2 < 0
4224 || eap->line1 > eap->line2
4225 || ((eap->argt & RANGE)
4226 && !(eap->argt & NOTADR)
4227 && eap->line2 > curbuf->b_ml.ml_line_count
4228#ifdef FEAT_DIFF
4229 + (eap->cmdidx == CMD_diffget)
4230#endif
4231 ))
4232 return (char_u *)_(e_invrange);
4233 return NULL;
4234}
4235
4236/*
4237 * Correct the range for zero line number, if required.
4238 */
4239 static void
4240correct_range(eap)
4241 exarg_T *eap;
4242{
4243 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4244 {
4245 if (eap->line1 == 0)
4246 eap->line1 = 1;
4247 if (eap->line2 == 0)
4248 eap->line2 = 1;
4249 }
4250}
4251
Bram Moolenaar748bf032005-02-02 23:04:36 +00004252#ifdef FEAT_QUICKFIX
4253static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4254
4255/*
4256 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4257 * pattern. Otherwise return eap->arg.
4258 */
4259 static char_u *
4260skip_grep_pat(eap)
4261 exarg_T *eap;
4262{
4263 char_u *p = eap->arg;
4264
Bram Moolenaara37420f2006-02-04 22:37:47 +00004265 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4266 || eap->cmdidx == CMD_vimgrepadd
4267 || eap->cmdidx == CMD_lvimgrepadd
4268 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004269 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004271 if (p == NULL)
4272 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004273 }
4274 return p;
4275}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004276
4277/*
4278 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4279 * in the command line, so that things like % get expanded.
4280 */
4281 static char_u *
4282replace_makeprg(eap, p, cmdlinep)
4283 exarg_T *eap;
4284 char_u *p;
4285 char_u **cmdlinep;
4286{
4287 char_u *new_cmdline;
4288 char_u *program;
4289 char_u *pos;
4290 char_u *ptr;
4291 int len;
4292 int i;
4293
4294 /*
4295 * Don't do it when ":vimgrep" is used for ":grep".
4296 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004297 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4298 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4299 || eap->cmdidx == CMD_grepadd
4300 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004301 && !grep_internal(eap->cmdidx))
4302 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004303 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4304 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004305 {
4306 if (*curbuf->b_p_gp == NUL)
4307 program = p_gp;
4308 else
4309 program = curbuf->b_p_gp;
4310 }
4311 else
4312 {
4313 if (*curbuf->b_p_mp == NUL)
4314 program = p_mp;
4315 else
4316 program = curbuf->b_p_mp;
4317 }
4318
4319 p = skipwhite(p);
4320
4321 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4322 {
4323 /* replace $* by given arguments */
4324 i = 1;
4325 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4326 ++i;
4327 len = (int)STRLEN(p);
4328 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4329 if (new_cmdline == NULL)
4330 return NULL; /* out of memory */
4331 ptr = new_cmdline;
4332 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4333 {
4334 i = (int)(pos - program);
4335 STRNCPY(ptr, program, i);
4336 STRCPY(ptr += i, p);
4337 ptr += len;
4338 program = pos + 2;
4339 }
4340 STRCPY(ptr, program);
4341 }
4342 else
4343 {
4344 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4345 if (new_cmdline == NULL)
4346 return NULL; /* out of memory */
4347 STRCPY(new_cmdline, program);
4348 STRCAT(new_cmdline, " ");
4349 STRCAT(new_cmdline, p);
4350 }
4351 msg_make(p);
4352
4353 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4354 vim_free(*cmdlinep);
4355 *cmdlinep = new_cmdline;
4356 p = new_cmdline;
4357 }
4358 return p;
4359}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362/*
4363 * Expand file name in Ex command argument.
4364 * Return FAIL for failure, OK otherwise.
4365 */
4366 int
4367expand_filename(eap, cmdlinep, errormsgp)
4368 exarg_T *eap;
4369 char_u **cmdlinep;
4370 char_u **errormsgp;
4371{
4372 int has_wildcards; /* need to expand wildcards */
4373 char_u *repl;
4374 int srclen;
4375 char_u *p;
4376 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004377 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar748bf032005-02-02 23:04:36 +00004379#ifdef FEAT_QUICKFIX
4380 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4381 p = skip_grep_pat(eap);
4382#else
4383 p = eap->arg;
4384#endif
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 /*
4387 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4388 * the file name contains a wildcard it should not cause expanding.
4389 * (it will be expanded anyway if there is a wildcard before replacing).
4390 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004391 has_wildcards = mch_has_wildcard(p);
4392 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004394#ifdef FEAT_EVAL
4395 /* Skip over `=expr`, wildcards in it are not expanded. */
4396 if (p[0] == '`' && p[1] == '=')
4397 {
4398 p += 2;
4399 (void)skip_expr(&p);
4400 if (*p == '`')
4401 ++p;
4402 continue;
4403 }
4404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 /*
4406 * Quick check if this cannot be the start of a special string.
4407 * Also removes backslash before '%', '#' and '<'.
4408 */
4409 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4410 {
4411 ++p;
4412 continue;
4413 }
4414
4415 /*
4416 * Try to find a match at this position.
4417 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004418 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4419 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (*errormsgp != NULL) /* error detected */
4421 return FAIL;
4422 if (repl == NULL) /* no match found */
4423 {
4424 p += srclen;
4425 continue;
4426 }
4427
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004428 /* Wildcards won't be expanded below, the replacement is taken
4429 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4430 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4431 {
4432 char_u *l = repl;
4433
4434 repl = expand_env_save(repl);
4435 vim_free(l);
4436 }
4437
Bram Moolenaar63b92542007-03-27 14:57:09 +00004438 /* Need to escape white space et al. with a backslash.
4439 * Don't do this for:
4440 * - replacement that already has been escaped: "##"
4441 * - shell commands (may have to use quotes instead).
4442 * - non-unix systems when there is a single argument (spaces don't
4443 * separate arguments then).
4444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004446 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 && eap->cmdidx != CMD_bang
4448 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004449 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004451 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004453 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#ifndef UNIX
4455 && !(eap->argt & NOSPC)
4456#endif
4457 )
4458 {
4459 char_u *l;
4460#ifdef BACKSLASH_IN_FILENAME
4461 /* Don't escape a backslash here, because rem_backslash() doesn't
4462 * remove it later. */
4463 static char_u *nobslash = (char_u *)" \t\"|";
4464# define ESCAPE_CHARS nobslash
4465#else
4466# define ESCAPE_CHARS escape_chars
4467#endif
4468
4469 for (l = repl; *l; ++l)
4470 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4471 {
4472 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4473 if (l != NULL)
4474 {
4475 vim_free(repl);
4476 repl = l;
4477 }
4478 break;
4479 }
4480 }
4481
4482 /* For a shell command a '!' must be escaped. */
4483 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004484 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 char_u *l;
4487
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004488 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (l != NULL)
4490 {
4491 vim_free(repl);
4492 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004493 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if (strstr((char *)p_sh, "sh") != NULL)
4495 {
4496 l = vim_strsave_escaped(repl, (char_u *)"!");
4497 if (l != NULL)
4498 {
4499 vim_free(repl);
4500 repl = l;
4501 }
4502 }
4503 }
4504 }
4505
4506 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4507 vim_free(repl);
4508 if (p == NULL)
4509 return FAIL;
4510 }
4511
4512 /*
4513 * One file argument: Expand wildcards.
4514 * Don't do this with ":r !command" or ":w !command".
4515 */
4516 if ((eap->argt & NOSPC) && !eap->usefilter)
4517 {
4518 /*
4519 * May do this twice:
4520 * 1. Replace environment variables.
4521 * 2. Replace any other wildcards, remove backslashes.
4522 */
4523 for (n = 1; n <= 2; ++n)
4524 {
4525 if (n == 2)
4526 {
4527#ifdef UNIX
4528 /*
4529 * Only for Unix we check for more than one file name.
4530 * For other systems spaces are considered to be part
4531 * of the file name.
4532 * Only check here if there is no wildcard, otherwise
4533 * ExpandOne() will check for errors. This allows
4534 * ":e `ls ve*.c`" on Unix.
4535 */
4536 if (!has_wildcards)
4537 for (p = eap->arg; *p; ++p)
4538 {
4539 /* skip escaped characters */
4540 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4541 ++p;
4542 else if (vim_iswhite(*p))
4543 {
4544 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4545 return FAIL;
4546 }
4547 }
4548#endif
4549
4550 /*
4551 * Halve the number of backslashes (this is Vi compatible).
4552 * For Unix and OS/2, when wildcards are expanded, this is
4553 * done by ExpandOne() below.
4554 */
4555#if defined(UNIX) || defined(OS2)
4556 if (!has_wildcards)
4557#endif
4558 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560
4561 if (has_wildcards)
4562 {
4563 if (n == 1)
4564 {
4565 /*
4566 * First loop: May expand environment variables. This
4567 * can be done much faster with expand_env() than with
4568 * something else (e.g., calling a shell).
4569 * After expanding environment variables, check again
4570 * if there are still wildcards present.
4571 */
4572 if (vim_strchr(eap->arg, '$') != NULL
4573 || vim_strchr(eap->arg, '~') != NULL)
4574 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004575 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004576 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 has_wildcards = mch_has_wildcard(NameBuff);
4578 p = NameBuff;
4579 }
4580 else
4581 p = NULL;
4582 }
4583 else /* n == 2 */
4584 {
4585 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004586 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 ExpandInit(&xpc);
4589 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004590 if (p_wic)
4591 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004593 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (p == NULL)
4595 return FAIL;
4596 }
4597 if (p != NULL)
4598 {
4599 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4600 p, cmdlinep);
4601 if (n == 2) /* p came from ExpandOne() */
4602 vim_free(p);
4603 }
4604 }
4605 }
4606 }
4607 return OK;
4608}
4609
4610/*
4611 * Replace part of the command line, keeping eap->cmd, eap->arg and
4612 * eap->nextcmd correct.
4613 * "src" points to the part that is to be replaced, of length "srclen".
4614 * "repl" is the replacement string.
4615 * Returns a pointer to the character after the replaced string.
4616 * Returns NULL for failure.
4617 */
4618 static char_u *
4619repl_cmdline(eap, src, srclen, repl, cmdlinep)
4620 exarg_T *eap;
4621 char_u *src;
4622 int srclen;
4623 char_u *repl;
4624 char_u **cmdlinep;
4625{
4626 int len;
4627 int i;
4628 char_u *new_cmdline;
4629
4630 /*
4631 * The new command line is build in new_cmdline[].
4632 * First allocate it.
4633 * Careful: a "+cmd" argument may have been NUL terminated.
4634 */
4635 len = (int)STRLEN(repl);
4636 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004637 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4639 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4640 return NULL; /* out of memory! */
4641
4642 /*
4643 * Copy the stuff before the expanded part.
4644 * Copy the expanded stuff.
4645 * Copy what came after the expanded part.
4646 * Copy the next commands, if there are any.
4647 */
4648 i = (int)(src - *cmdlinep); /* length of part before match */
4649 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 mch_memmove(new_cmdline + i, repl, (size_t)len);
4652 i += len; /* remember the end of the string */
4653 STRCPY(new_cmdline + i, src + srclen);
4654 src = new_cmdline + i; /* remember where to continue */
4655
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004656 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 i = (int)STRLEN(new_cmdline) + 1;
4659 STRCPY(new_cmdline + i, eap->nextcmd);
4660 eap->nextcmd = new_cmdline + i;
4661 }
4662 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4663 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4664 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4665 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4666 vim_free(*cmdlinep);
4667 *cmdlinep = new_cmdline;
4668
4669 return src;
4670}
4671
4672/*
4673 * Check for '|' to separate commands and '"' to start comments.
4674 */
4675 void
4676separate_nextcmd(eap)
4677 exarg_T *eap;
4678{
4679 char_u *p;
4680
Bram Moolenaar86b68352004-12-27 21:59:20 +00004681#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004682 p = skip_grep_pat(eap);
4683#else
4684 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004685#endif
4686
4687 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 if (*p == Ctrl_V)
4690 {
4691 if (eap->argt & (USECTRLV | XFILE))
4692 ++p; /* skip CTRL-V and next char */
4693 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004694 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004695 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (*p == NUL) /* stop at NUL after CTRL-V */
4697 break;
4698 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004699
4700#ifdef FEAT_EVAL
4701 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004702 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004703 {
4704 p += 2;
4705 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004706 }
4707#endif
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 /* Check for '"': start of comment or '|': next command */
4710 /* :@" and :*" do not start a comment!
4711 * :redir @" doesn't either. */
4712 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4713 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4714 || p != eap->arg)
4715 && (eap->cmdidx != CMD_redir
4716 || p != eap->arg + 1 || p[-1] != '@'))
4717 || *p == '|' || *p == '\n')
4718 {
4719 /*
4720 * We remove the '\' before the '|', unless USECTRLV is used
4721 * AND 'b' is present in 'cpoptions'.
4722 */
4723 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4724 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4725 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004726 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 --p;
4728 }
4729 else
4730 {
4731 eap->nextcmd = check_nextcmd(p);
4732 *p = NUL;
4733 break;
4734 }
4735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4739 del_trailing_spaces(eap->arg);
4740}
4741
4742/*
4743 * get + command from ex argument
4744 */
4745 static char_u *
4746getargcmd(argp)
4747 char_u **argp;
4748{
4749 char_u *arg = *argp;
4750 char_u *command = NULL;
4751
4752 if (*arg == '+') /* +[command] */
4753 {
4754 ++arg;
4755 if (vim_isspace(*arg))
4756 command = dollar_command;
4757 else
4758 {
4759 command = arg;
4760 arg = skip_cmd_arg(command, TRUE);
4761 if (*arg != NUL)
4762 *arg++ = NUL; /* terminate command with NUL */
4763 }
4764
4765 arg = skipwhite(arg); /* skip over spaces */
4766 *argp = arg;
4767 }
4768 return command;
4769}
4770
4771/*
4772 * Find end of "+command" argument. Skip over "\ " and "\\".
4773 */
4774 static char_u *
4775skip_cmd_arg(p, rembs)
4776 char_u *p;
4777 int rembs; /* TRUE to halve the number of backslashes */
4778{
4779 while (*p && !vim_isspace(*p))
4780 {
4781 if (*p == '\\' && p[1] != NUL)
4782 {
4783 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004784 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
4786 ++p;
4787 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004788 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 return p;
4791}
4792
4793/*
4794 * Get "++opt=arg" argument.
4795 * Return FAIL or OK.
4796 */
4797 static int
4798getargopt(eap)
4799 exarg_T *eap;
4800{
4801 char_u *arg = eap->arg + 2;
4802 int *pp = NULL;
4803#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004804 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 char_u *p;
4806#endif
4807
4808 /* ":edit ++[no]bin[ary] file" */
4809 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4810 {
4811 if (*arg == 'n')
4812 {
4813 arg += 2;
4814 eap->force_bin = FORCE_NOBIN;
4815 }
4816 else
4817 eap->force_bin = FORCE_BIN;
4818 if (!checkforcmd(&arg, "binary", 3))
4819 return FAIL;
4820 eap->arg = skipwhite(arg);
4821 return OK;
4822 }
4823
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004824 /* ":read ++edit file" */
4825 if (STRNCMP(arg, "edit", 4) == 0)
4826 {
4827 eap->read_edit = TRUE;
4828 eap->arg = skipwhite(arg + 4);
4829 return OK;
4830 }
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (STRNCMP(arg, "ff", 2) == 0)
4833 {
4834 arg += 2;
4835 pp = &eap->force_ff;
4836 }
4837 else if (STRNCMP(arg, "fileformat", 10) == 0)
4838 {
4839 arg += 10;
4840 pp = &eap->force_ff;
4841 }
4842#ifdef FEAT_MBYTE
4843 else if (STRNCMP(arg, "enc", 3) == 0)
4844 {
4845 arg += 3;
4846 pp = &eap->force_enc;
4847 }
4848 else if (STRNCMP(arg, "encoding", 8) == 0)
4849 {
4850 arg += 8;
4851 pp = &eap->force_enc;
4852 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004853 else if (STRNCMP(arg, "bad", 3) == 0)
4854 {
4855 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004856 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858#endif
4859
4860 if (pp == NULL || *arg != '=')
4861 return FAIL;
4862
4863 ++arg;
4864 *pp = (int)(arg - eap->cmd);
4865 arg = skip_cmd_arg(arg, FALSE);
4866 eap->arg = skipwhite(arg);
4867 *arg = NUL;
4868
4869#ifdef FEAT_MBYTE
4870 if (pp == &eap->force_ff)
4871 {
4872#endif
4873 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4874 return FAIL;
4875#ifdef FEAT_MBYTE
4876 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004877 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 /* Make 'fileencoding' lower case. */
4880 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4881 *p = TOLOWER_ASC(*p);
4882 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004883 else
4884 {
4885 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4886 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004887 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004888 if (STRICMP(p, "keep") == 0)
4889 eap->bad_char = BAD_KEEP;
4890 else if (STRICMP(p, "drop") == 0)
4891 eap->bad_char = BAD_DROP;
4892 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4893 eap->bad_char = *p;
4894 else
4895 return FAIL;
4896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#endif
4898
4899 return OK;
4900}
4901
4902/*
4903 * ":abbreviate" and friends.
4904 */
4905 static void
4906ex_abbreviate(eap)
4907 exarg_T *eap;
4908{
4909 do_exmap(eap, TRUE); /* almost the same as mapping */
4910}
4911
4912/*
4913 * ":map" and friends.
4914 */
4915 static void
4916ex_map(eap)
4917 exarg_T *eap;
4918{
4919 /*
4920 * If we are sourcing .exrc or .vimrc in current directory we
4921 * print the mappings for security reasons.
4922 */
4923 if (secure)
4924 {
4925 secure = 2;
4926 msg_outtrans(eap->cmd);
4927 msg_putchar('\n');
4928 }
4929 do_exmap(eap, FALSE);
4930}
4931
4932/*
4933 * ":unmap" and friends.
4934 */
4935 static void
4936ex_unmap(eap)
4937 exarg_T *eap;
4938{
4939 do_exmap(eap, FALSE);
4940}
4941
4942/*
4943 * ":mapclear" and friends.
4944 */
4945 static void
4946ex_mapclear(eap)
4947 exarg_T *eap;
4948{
4949 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4950}
4951
4952/*
4953 * ":abclear" and friends.
4954 */
4955 static void
4956ex_abclear(eap)
4957 exarg_T *eap;
4958{
4959 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4960}
4961
4962#ifdef FEAT_AUTOCMD
4963 static void
4964ex_autocmd(eap)
4965 exarg_T *eap;
4966{
4967 /*
4968 * Disallow auto commands from .exrc and .vimrc in current
4969 * directory for security reasons.
4970 */
4971 if (secure)
4972 {
4973 secure = 2;
4974 eap->errmsg = e_curdir;
4975 }
4976 else if (eap->cmdidx == CMD_autocmd)
4977 do_autocmd(eap->arg, eap->forceit);
4978 else
4979 do_augroup(eap->arg, eap->forceit);
4980}
4981
4982/*
4983 * ":doautocmd": Apply the automatic commands to the current buffer.
4984 */
4985 static void
4986ex_doautocmd(eap)
4987 exarg_T *eap;
4988{
4989 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004990 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991}
4992#endif
4993
4994#ifdef FEAT_LISTCMDS
4995/*
4996 * :[N]bunload[!] [N] [bufname] unload buffer
4997 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4998 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4999 */
5000 static void
5001ex_bunload(eap)
5002 exarg_T *eap;
5003{
5004 eap->errmsg = do_bufdel(
5005 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5006 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5007 : DOBUF_UNLOAD, eap->arg,
5008 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5009}
5010
5011/*
5012 * :[N]buffer [N] to buffer N
5013 * :[N]sbuffer [N] to buffer N
5014 */
5015 static void
5016ex_buffer(eap)
5017 exarg_T *eap;
5018{
5019 if (*eap->arg)
5020 eap->errmsg = e_trailing;
5021 else
5022 {
5023 if (eap->addr_count == 0) /* default is current buffer */
5024 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5025 else
5026 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5027 }
5028}
5029
5030/*
5031 * :[N]bmodified [N] to next mod. buffer
5032 * :[N]sbmodified [N] to next mod. buffer
5033 */
5034 static void
5035ex_bmodified(eap)
5036 exarg_T *eap;
5037{
5038 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5039}
5040
5041/*
5042 * :[N]bnext [N] to next buffer
5043 * :[N]sbnext [N] split and to next buffer
5044 */
5045 static void
5046ex_bnext(eap)
5047 exarg_T *eap;
5048{
5049 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5050}
5051
5052/*
5053 * :[N]bNext [N] to previous buffer
5054 * :[N]bprevious [N] to previous buffer
5055 * :[N]sbNext [N] split and to previous buffer
5056 * :[N]sbprevious [N] split and to previous buffer
5057 */
5058 static void
5059ex_bprevious(eap)
5060 exarg_T *eap;
5061{
5062 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5063}
5064
5065/*
5066 * :brewind to first buffer
5067 * :bfirst to first buffer
5068 * :sbrewind split and to first buffer
5069 * :sbfirst split and to first buffer
5070 */
5071 static void
5072ex_brewind(eap)
5073 exarg_T *eap;
5074{
5075 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5076}
5077
5078/*
5079 * :blast to last buffer
5080 * :sblast split and to last buffer
5081 */
5082 static void
5083ex_blast(eap)
5084 exarg_T *eap;
5085{
5086 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5087}
5088#endif
5089
5090 int
5091ends_excmd(c)
5092 int c;
5093{
5094 return (c == NUL || c == '|' || c == '"' || c == '\n');
5095}
5096
5097#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5098 || defined(PROTO)
5099/*
5100 * Return the next command, after the first '|' or '\n'.
5101 * Return NULL if not found.
5102 */
5103 char_u *
5104find_nextcmd(p)
5105 char_u *p;
5106{
5107 while (*p != '|' && *p != '\n')
5108 {
5109 if (*p == NUL)
5110 return NULL;
5111 ++p;
5112 }
5113 return (p + 1);
5114}
5115#endif
5116
5117/*
5118 * Check if *p is a separator between Ex commands.
5119 * Return NULL if it isn't, (p + 1) if it is.
5120 */
5121 char_u *
5122check_nextcmd(p)
5123 char_u *p;
5124{
5125 p = skipwhite(p);
5126 if (*p == '|' || *p == '\n')
5127 return (p + 1);
5128 else
5129 return NULL;
5130}
5131
5132/*
5133 * - if there are more files to edit
5134 * - and this is the last window
5135 * - and forceit not used
5136 * - and not repeated twice on a row
5137 * return FAIL and give error message if 'message' TRUE
5138 * return OK otherwise
5139 */
5140 static int
5141check_more(message, forceit)
5142 int message; /* when FALSE check only, no messages */
5143 int forceit;
5144{
5145 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5146
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005147 if (!forceit && only_one_window()
5148 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 if (message)
5151 {
5152#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5153 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5154 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005155 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005158 vim_strncpy(buff,
5159 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005160 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005162 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 _("%d more files to edit. Quit anyway?"), n);
5164 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5165 return OK;
5166 return FAIL;
5167 }
5168#endif
5169 if (n == 1)
5170 EMSG(_("E173: 1 more file to edit"));
5171 else
5172 EMSGN(_("E173: %ld more files to edit"), n);
5173 quitmore = 2; /* next try to quit is allowed */
5174 }
5175 return FAIL;
5176 }
5177 return OK;
5178}
5179
5180#ifdef FEAT_CMDL_COMPL
5181/*
5182 * Function given to ExpandGeneric() to obtain the list of command names.
5183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 char_u *
5185get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005186 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 int idx;
5188{
5189 if (idx >= (int)CMD_SIZE)
5190# ifdef FEAT_USR_CMDS
5191 return get_user_command_name(idx);
5192# else
5193 return NULL;
5194# endif
5195 return cmdnames[idx].cmd_name;
5196}
5197#endif
5198
5199#if defined(FEAT_USR_CMDS) || defined(PROTO)
5200static 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));
5201static void uc_list __ARGS((char_u *name, size_t name_len));
5202static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5203static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5204static 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));
5205
5206 static int
5207uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5208 char_u *name;
5209 size_t name_len;
5210 char_u *rep;
5211 long argt;
5212 long def;
5213 int flags;
5214 int compl;
5215 char_u *compl_arg;
5216 int force;
5217{
5218 ucmd_T *cmd = NULL;
5219 char_u *p;
5220 int i;
5221 int cmp = 1;
5222 char_u *rep_buf = NULL;
5223 garray_T *gap;
5224
Bram Moolenaar9c102382006-05-03 21:26:49 +00005225 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (rep_buf == NULL)
5227 {
5228 /* Can't replace termcodes - try using the string as is */
5229 rep_buf = vim_strsave(rep);
5230
5231 /* Give up if out of memory */
5232 if (rep_buf == NULL)
5233 return FAIL;
5234 }
5235
5236 /* get address of growarray: global or in curbuf */
5237 if (flags & UC_BUFFER)
5238 {
5239 gap = &curbuf->b_ucmds;
5240 if (gap->ga_itemsize == 0)
5241 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5242 }
5243 else
5244 gap = &ucmds;
5245
5246 /* Search for the command in the already defined commands. */
5247 for (i = 0; i < gap->ga_len; ++i)
5248 {
5249 size_t len;
5250
5251 cmd = USER_CMD_GA(gap, i);
5252 len = STRLEN(cmd->uc_name);
5253 cmp = STRNCMP(name, cmd->uc_name, name_len);
5254 if (cmp == 0)
5255 {
5256 if (name_len < len)
5257 cmp = -1;
5258 else if (name_len > len)
5259 cmp = 1;
5260 }
5261
5262 if (cmp == 0)
5263 {
5264 if (!force)
5265 {
5266 EMSG(_("E174: Command already exists: add ! to replace it"));
5267 goto fail;
5268 }
5269
5270 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005271 cmd->uc_rep = NULL;
5272#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5273 vim_free(cmd->uc_compl_arg);
5274 cmd->uc_compl_arg = NULL;
5275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 break;
5277 }
5278
5279 /* Stop as soon as we pass the name to add */
5280 if (cmp < 0)
5281 break;
5282 }
5283
5284 /* Extend the array unless we're replacing an existing command */
5285 if (cmp != 0)
5286 {
5287 if (ga_grow(gap, 1) != OK)
5288 goto fail;
5289 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5290 goto fail;
5291
5292 cmd = USER_CMD_GA(gap, i);
5293 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5294
5295 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 cmd->uc_name = p;
5298 }
5299
5300 cmd->uc_rep = rep_buf;
5301 cmd->uc_argt = argt;
5302 cmd->uc_def = def;
5303 cmd->uc_compl = compl;
5304#ifdef FEAT_EVAL
5305 cmd->uc_scriptID = current_SID;
5306# ifdef FEAT_CMDL_COMPL
5307 cmd->uc_compl_arg = compl_arg;
5308# endif
5309#endif
5310
5311 return OK;
5312
5313fail:
5314 vim_free(rep_buf);
5315#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5316 vim_free(compl_arg);
5317#endif
5318 return FAIL;
5319}
5320
5321/*
5322 * List of names for completion for ":command" with the EXPAND_ flag.
5323 * Must be alphabetical for completion.
5324 */
5325static struct
5326{
5327 int expand;
5328 char *name;
5329} command_complete[] =
5330{
5331 {EXPAND_AUGROUP, "augroup"},
5332 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005333 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005335 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005336#if defined(FEAT_CSCOPE)
5337 {EXPAND_CSCOPE, "cscope"},
5338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5340 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005341 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342#endif
5343 {EXPAND_DIRECTORIES, "dir"},
5344 {EXPAND_ENV_VARS, "environment"},
5345 {EXPAND_EVENTS, "event"},
5346 {EXPAND_EXPRESSION, "expression"},
5347 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005348 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005349 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {EXPAND_FUNCTIONS, "function"},
5351 {EXPAND_HELP, "help"},
5352 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005353#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5354 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
5355 {EXPAND_LOCALES, "locale"},
5356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {EXPAND_MAPPINGS, "mapping"},
5358 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005359 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005361 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005362#if defined(FEAT_SIGNS)
5363 {EXPAND_SIGN, "sign"},
5364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {EXPAND_TAGS, "tag"},
5366 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5367 {EXPAND_USER_VARS, "var"},
5368 {0, NULL}
5369};
5370
5371 static void
5372uc_list(name, name_len)
5373 char_u *name;
5374 size_t name_len;
5375{
5376 int i, j;
5377 int found = FALSE;
5378 ucmd_T *cmd;
5379 int len;
5380 long a;
5381 garray_T *gap;
5382
5383 gap = &curbuf->b_ucmds;
5384 for (;;)
5385 {
5386 for (i = 0; i < gap->ga_len; ++i)
5387 {
5388 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005389 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 /* Skip commands which don't match the requested prefix */
5392 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5393 continue;
5394
5395 /* Put out the title first time */
5396 if (!found)
5397 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5398 found = TRUE;
5399 msg_putchar('\n');
5400 if (got_int)
5401 break;
5402
5403 /* Special cases */
5404 msg_putchar(a & BANG ? '!' : ' ');
5405 msg_putchar(a & REGSTR ? '"' : ' ');
5406 msg_putchar(gap != &ucmds ? 'b' : ' ');
5407 msg_putchar(' ');
5408
5409 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5410 len = (int)STRLEN(cmd->uc_name) + 4;
5411
5412 do {
5413 msg_putchar(' ');
5414 ++len;
5415 } while (len < 16);
5416
5417 len = 0;
5418
5419 /* Arguments */
5420 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5421 {
5422 case 0: IObuff[len++] = '0'; break;
5423 case (EXTRA): IObuff[len++] = '*'; break;
5424 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5425 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5426 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5427 }
5428
5429 do {
5430 IObuff[len++] = ' ';
5431 } while (len < 5);
5432
5433 /* Range */
5434 if (a & (RANGE|COUNT))
5435 {
5436 if (a & COUNT)
5437 {
5438 /* -count=N */
5439 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5440 len += (int)STRLEN(IObuff + len);
5441 }
5442 else if (a & DFLALL)
5443 IObuff[len++] = '%';
5444 else if (cmd->uc_def >= 0)
5445 {
5446 /* -range=N */
5447 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5448 len += (int)STRLEN(IObuff + len);
5449 }
5450 else
5451 IObuff[len++] = '.';
5452 }
5453
5454 do {
5455 IObuff[len++] = ' ';
5456 } while (len < 11);
5457
5458 /* Completion */
5459 for (j = 0; command_complete[j].expand != 0; ++j)
5460 if (command_complete[j].expand == cmd->uc_compl)
5461 {
5462 STRCPY(IObuff + len, command_complete[j].name);
5463 len += (int)STRLEN(IObuff + len);
5464 break;
5465 }
5466
5467 do {
5468 IObuff[len++] = ' ';
5469 } while (len < 21);
5470
5471 IObuff[len] = '\0';
5472 msg_outtrans(IObuff);
5473
5474 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005475#ifdef FEAT_EVAL
5476 if (p_verbose > 0)
5477 last_set_msg(cmd->uc_scriptID);
5478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 out_flush();
5480 ui_breakcheck();
5481 if (got_int)
5482 break;
5483 }
5484 if (gap == &ucmds || i < gap->ga_len)
5485 break;
5486 gap = &ucmds;
5487 }
5488
5489 if (!found)
5490 MSG(_("No user-defined commands found"));
5491}
5492
5493 static char_u *
5494uc_fun_cmd()
5495{
5496 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5497 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5498 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5499 0xb9, 0x7f, 0};
5500 int i;
5501
5502 for (i = 0; fcmd[i]; ++i)
5503 IObuff[i] = fcmd[i] - 0x40;
5504 IObuff[i] = 0;
5505 return IObuff;
5506}
5507
5508 static int
5509uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5510 char_u *attr;
5511 size_t len;
5512 long *argt;
5513 long *def;
5514 int *flags;
5515 int *compl;
5516 char_u **compl_arg;
5517{
5518 char_u *p;
5519
5520 if (len == 0)
5521 {
5522 EMSG(_("E175: No attribute specified"));
5523 return FAIL;
5524 }
5525
5526 /* First, try the simple attributes (no arguments) */
5527 if (STRNICMP(attr, "bang", len) == 0)
5528 *argt |= BANG;
5529 else if (STRNICMP(attr, "buffer", len) == 0)
5530 *flags |= UC_BUFFER;
5531 else if (STRNICMP(attr, "register", len) == 0)
5532 *argt |= REGSTR;
5533 else if (STRNICMP(attr, "bar", len) == 0)
5534 *argt |= TRLBAR;
5535 else
5536 {
5537 int i;
5538 char_u *val = NULL;
5539 size_t vallen = 0;
5540 size_t attrlen = len;
5541
5542 /* Look for the attribute name - which is the part before any '=' */
5543 for (i = 0; i < (int)len; ++i)
5544 {
5545 if (attr[i] == '=')
5546 {
5547 val = &attr[i + 1];
5548 vallen = len - i - 1;
5549 attrlen = i;
5550 break;
5551 }
5552 }
5553
5554 if (STRNICMP(attr, "nargs", attrlen) == 0)
5555 {
5556 if (vallen == 1)
5557 {
5558 if (*val == '0')
5559 /* Do nothing - this is the default */;
5560 else if (*val == '1')
5561 *argt |= (EXTRA | NOSPC | NEEDARG);
5562 else if (*val == '*')
5563 *argt |= EXTRA;
5564 else if (*val == '?')
5565 *argt |= (EXTRA | NOSPC);
5566 else if (*val == '+')
5567 *argt |= (EXTRA | NEEDARG);
5568 else
5569 goto wrong_nargs;
5570 }
5571 else
5572 {
5573wrong_nargs:
5574 EMSG(_("E176: Invalid number of arguments"));
5575 return FAIL;
5576 }
5577 }
5578 else if (STRNICMP(attr, "range", attrlen) == 0)
5579 {
5580 *argt |= RANGE;
5581 if (vallen == 1 && *val == '%')
5582 *argt |= DFLALL;
5583 else if (val != NULL)
5584 {
5585 p = val;
5586 if (*def >= 0)
5587 {
5588two_count:
5589 EMSG(_("E177: Count cannot be specified twice"));
5590 return FAIL;
5591 }
5592
5593 *def = getdigits(&p);
5594 *argt |= (ZEROR | NOTADR);
5595
5596 if (p != val + vallen || vallen == 0)
5597 {
5598invalid_count:
5599 EMSG(_("E178: Invalid default value for count"));
5600 return FAIL;
5601 }
5602 }
5603 }
5604 else if (STRNICMP(attr, "count", attrlen) == 0)
5605 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005606 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608 if (val != NULL)
5609 {
5610 p = val;
5611 if (*def >= 0)
5612 goto two_count;
5613
5614 *def = getdigits(&p);
5615
5616 if (p != val + vallen)
5617 goto invalid_count;
5618 }
5619
5620 if (*def < 0)
5621 *def = 0;
5622 }
5623 else if (STRNICMP(attr, "complete", attrlen) == 0)
5624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (val == NULL)
5626 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005627 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 return FAIL;
5629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005631 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5632 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635 else
5636 {
5637 char_u ch = attr[len];
5638 attr[len] = '\0';
5639 EMSG2(_("E181: Invalid attribute: %s"), attr);
5640 attr[len] = ch;
5641 return FAIL;
5642 }
5643 }
5644
5645 return OK;
5646}
5647
Bram Moolenaara850a712009-01-28 14:42:59 +00005648/*
5649 * ":command ..."
5650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 static void
5652ex_command(eap)
5653 exarg_T *eap;
5654{
5655 char_u *name;
5656 char_u *end;
5657 char_u *p;
5658 long argt = 0;
5659 long def = -1;
5660 int flags = 0;
5661 int compl = EXPAND_NOTHING;
5662 char_u *compl_arg = NULL;
5663 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005664 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 p = eap->arg;
5667
5668 /* Check for attributes */
5669 while (*p == '-')
5670 {
5671 ++p;
5672 end = skiptowhite(p);
5673 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5674 == FAIL)
5675 return;
5676 p = skipwhite(end);
5677 }
5678
5679 /* Get the name (if any) and skip to the following argument */
5680 name = p;
5681 if (ASCII_ISALPHA(*p))
5682 while (ASCII_ISALNUM(*p))
5683 ++p;
5684 if (!ends_excmd(*p) && !vim_iswhite(*p))
5685 {
5686 EMSG(_("E182: Invalid command name"));
5687 return;
5688 }
5689 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005690 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692 /* If there is nothing after the name, and no attributes were specified,
5693 * we are listing commands
5694 */
5695 p = skipwhite(end);
5696 if (!has_attr && ends_excmd(*p))
5697 {
5698 uc_list(name, end - name);
5699 }
5700 else if (!ASCII_ISUPPER(*name))
5701 {
5702 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5703 return;
5704 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005705 else if ((name_len == 1 && *name == 'X')
5706 || (name_len <= 4
5707 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5708 {
5709 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5710 return;
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 else
5713 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5714 eap->forceit);
5715}
5716
5717/*
5718 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 * Clear all user commands, global and for current buffer.
5720 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005721 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005723 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 uc_clear(&ucmds);
5726 uc_clear(&curbuf->b_ucmds);
5727}
5728
5729/*
5730 * Clear all user commands for "gap".
5731 */
5732 void
5733uc_clear(gap)
5734 garray_T *gap;
5735{
5736 int i;
5737 ucmd_T *cmd;
5738
5739 for (i = 0; i < gap->ga_len; ++i)
5740 {
5741 cmd = USER_CMD_GA(gap, i);
5742 vim_free(cmd->uc_name);
5743 vim_free(cmd->uc_rep);
5744# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5745 vim_free(cmd->uc_compl_arg);
5746# endif
5747 }
5748 ga_clear(gap);
5749}
5750
5751 static void
5752ex_delcommand(eap)
5753 exarg_T *eap;
5754{
5755 int i = 0;
5756 ucmd_T *cmd = NULL;
5757 int cmp = -1;
5758 garray_T *gap;
5759
5760 gap = &curbuf->b_ucmds;
5761 for (;;)
5762 {
5763 for (i = 0; i < gap->ga_len; ++i)
5764 {
5765 cmd = USER_CMD_GA(gap, i);
5766 cmp = STRCMP(eap->arg, cmd->uc_name);
5767 if (cmp <= 0)
5768 break;
5769 }
5770 if (gap == &ucmds || cmp == 0)
5771 break;
5772 gap = &ucmds;
5773 }
5774
5775 if (cmp != 0)
5776 {
5777 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5778 return;
5779 }
5780
5781 vim_free(cmd->uc_name);
5782 vim_free(cmd->uc_rep);
5783# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5784 vim_free(cmd->uc_compl_arg);
5785# endif
5786
5787 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
5789 if (i < gap->ga_len)
5790 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5791}
5792
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005793/*
5794 * split and quote args for <f-args>
5795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 static char_u *
5797uc_split_args(arg, lenp)
5798 char_u *arg;
5799 size_t *lenp;
5800{
5801 char_u *buf;
5802 char_u *p;
5803 char_u *q;
5804 int len;
5805
5806 /* Precalculate length */
5807 p = arg;
5808 len = 2; /* Initial and final quotes */
5809
5810 while (*p)
5811 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005812 if (p[0] == '\\' && p[1] == '\\')
5813 {
5814 len += 2;
5815 p += 2;
5816 }
5817 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
5819 len += 1;
5820 p += 2;
5821 }
5822 else if (*p == '\\' || *p == '"')
5823 {
5824 len += 2;
5825 p += 1;
5826 }
5827 else if (vim_iswhite(*p))
5828 {
5829 p = skipwhite(p);
5830 if (*p == NUL)
5831 break;
5832 len += 3; /* "," */
5833 }
5834 else
5835 {
5836 ++len;
5837 ++p;
5838 }
5839 }
5840
5841 buf = alloc(len + 1);
5842 if (buf == NULL)
5843 {
5844 *lenp = 0;
5845 return buf;
5846 }
5847
5848 p = arg;
5849 q = buf;
5850 *q++ = '"';
5851 while (*p)
5852 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005853 if (p[0] == '\\' && p[1] == '\\')
5854 {
5855 *q++ = '\\';
5856 *q++ = '\\';
5857 p += 2;
5858 }
5859 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
5861 *q++ = p[1];
5862 p += 2;
5863 }
5864 else if (*p == '\\' || *p == '"')
5865 {
5866 *q++ = '\\';
5867 *q++ = *p++;
5868 }
5869 else if (vim_iswhite(*p))
5870 {
5871 p = skipwhite(p);
5872 if (*p == NUL)
5873 break;
5874 *q++ = '"';
5875 *q++ = ',';
5876 *q++ = '"';
5877 }
5878 else
5879 {
5880 *q++ = *p++;
5881 }
5882 }
5883 *q++ = '"';
5884 *q = 0;
5885
5886 *lenp = len;
5887 return buf;
5888}
5889
5890/*
5891 * Check for a <> code in a user command.
5892 * "code" points to the '<'. "len" the length of the <> (inclusive).
5893 * "buf" is where the result is to be added.
5894 * "split_buf" points to a buffer used for splitting, caller should free it.
5895 * "split_len" is the length of what "split_buf" contains.
5896 * Returns the length of the replacement, which has been added to "buf".
5897 * Returns -1 if there was no match, and only the "<" has been copied.
5898 */
5899 static size_t
5900uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5901 char_u *code;
5902 size_t len;
5903 char_u *buf;
5904 ucmd_T *cmd; /* the user command we're expanding */
5905 exarg_T *eap; /* ex arguments */
5906 char_u **split_buf;
5907 size_t *split_len;
5908{
5909 size_t result = 0;
5910 char_u *p = code + 1;
5911 size_t l = len - 2;
5912 int quote = 0;
5913 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5914 ct_LT, ct_NONE } type = ct_NONE;
5915
5916 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5917 {
5918 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5919 p += 2;
5920 l -= 2;
5921 }
5922
Bram Moolenaar371d5402006-03-20 21:47:49 +00005923 ++l;
5924 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005926 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005928 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005930 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005932 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005934 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005936 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005938 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 type = ct_REGISTER;
5940
5941 switch (type)
5942 {
5943 case ct_ARGS:
5944 /* Simple case first */
5945 if (*eap->arg == NUL)
5946 {
5947 if (quote == 1)
5948 {
5949 result = 2;
5950 if (buf != NULL)
5951 STRCPY(buf, "''");
5952 }
5953 else
5954 result = 0;
5955 break;
5956 }
5957
5958 /* When specified there is a single argument don't split it.
5959 * Works for ":Cmd %" when % is "a b c". */
5960 if ((eap->argt & NOSPC) && quote == 2)
5961 quote = 1;
5962
5963 switch (quote)
5964 {
5965 case 0: /* No quoting, no splitting */
5966 result = STRLEN(eap->arg);
5967 if (buf != NULL)
5968 STRCPY(buf, eap->arg);
5969 break;
5970 case 1: /* Quote, but don't split */
5971 result = STRLEN(eap->arg) + 2;
5972 for (p = eap->arg; *p; ++p)
5973 {
5974 if (*p == '\\' || *p == '"')
5975 ++result;
5976 }
5977
5978 if (buf != NULL)
5979 {
5980 *buf++ = '"';
5981 for (p = eap->arg; *p; ++p)
5982 {
5983 if (*p == '\\' || *p == '"')
5984 *buf++ = '\\';
5985 *buf++ = *p;
5986 }
5987 *buf = '"';
5988 }
5989
5990 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005991 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 /* This is hard, so only do it once, and cache the result */
5993 if (*split_buf == NULL)
5994 *split_buf = uc_split_args(eap->arg, split_len);
5995
5996 result = *split_len;
5997 if (buf != NULL && result != 0)
5998 STRCPY(buf, *split_buf);
5999
6000 break;
6001 }
6002 break;
6003
6004 case ct_BANG:
6005 result = eap->forceit ? 1 : 0;
6006 if (quote)
6007 result += 2;
6008 if (buf != NULL)
6009 {
6010 if (quote)
6011 *buf++ = '"';
6012 if (eap->forceit)
6013 *buf++ = '!';
6014 if (quote)
6015 *buf = '"';
6016 }
6017 break;
6018
6019 case ct_LINE1:
6020 case ct_LINE2:
6021 case ct_COUNT:
6022 {
6023 char num_buf[20];
6024 long num = (type == ct_LINE1) ? eap->line1 :
6025 (type == ct_LINE2) ? eap->line2 :
6026 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6027 size_t num_len;
6028
6029 sprintf(num_buf, "%ld", num);
6030 num_len = STRLEN(num_buf);
6031 result = num_len;
6032
6033 if (quote)
6034 result += 2;
6035
6036 if (buf != NULL)
6037 {
6038 if (quote)
6039 *buf++ = '"';
6040 STRCPY(buf, num_buf);
6041 buf += num_len;
6042 if (quote)
6043 *buf = '"';
6044 }
6045
6046 break;
6047 }
6048
6049 case ct_REGISTER:
6050 result = eap->regname ? 1 : 0;
6051 if (quote)
6052 result += 2;
6053 if (buf != NULL)
6054 {
6055 if (quote)
6056 *buf++ = '\'';
6057 if (eap->regname)
6058 *buf++ = eap->regname;
6059 if (quote)
6060 *buf = '\'';
6061 }
6062 break;
6063
6064 case ct_LT:
6065 result = 1;
6066 if (buf != NULL)
6067 *buf = '<';
6068 break;
6069
6070 default:
6071 /* Not recognized: just copy the '<' and return -1. */
6072 result = (size_t)-1;
6073 if (buf != NULL)
6074 *buf = '<';
6075 break;
6076 }
6077
6078 return result;
6079}
6080
6081 static void
6082do_ucmd(eap)
6083 exarg_T *eap;
6084{
6085 char_u *buf;
6086 char_u *p;
6087 char_u *q;
6088
6089 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006090 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006091 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 size_t len, totlen;
6093
6094 size_t split_len = 0;
6095 char_u *split_buf = NULL;
6096 ucmd_T *cmd;
6097#ifdef FEAT_EVAL
6098 scid_T save_current_SID = current_SID;
6099#endif
6100
6101 if (eap->cmdidx == CMD_USER)
6102 cmd = USER_CMD(eap->useridx);
6103 else
6104 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6105
6106 /*
6107 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006108 * First round: "buf" is NULL, compute length, allocate "buf".
6109 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 */
6111 buf = NULL;
6112 for (;;)
6113 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006114 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006115 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006117
6118 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006120 start = vim_strchr(p, '<');
6121 if (start != NULL)
6122 end = vim_strchr(start + 1, '>');
6123 if (buf != NULL)
6124 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006125 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6126 ;
6127 if (*ksp == K_SPECIAL
6128 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006129 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6130# ifdef FEAT_GUI
6131 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6132# endif
6133 ))
6134 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006135 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006136 * KS_SPECIAL KE_FILLER, like for mappings, but
6137 * do_cmdline() doesn't handle that, so convert it back.
6138 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6139 len = ksp - p;
6140 if (len > 0)
6141 {
6142 mch_memmove(q, p, len);
6143 q += len;
6144 }
6145 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6146 p = ksp + 3;
6147 continue;
6148 }
6149 }
6150
6151 /* break if there no <item> is found */
6152 if (start == NULL || end == NULL)
6153 break;
6154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 /* Include the '>' */
6156 ++end;
6157
6158 /* Take everything up to the '<' */
6159 len = start - p;
6160 if (buf == NULL)
6161 totlen += len;
6162 else
6163 {
6164 mch_memmove(q, p, len);
6165 q += len;
6166 }
6167
6168 len = uc_check_code(start, end - start, q, cmd, eap,
6169 &split_buf, &split_len);
6170 if (len == (size_t)-1)
6171 {
6172 /* no match, continue after '<' */
6173 p = start + 1;
6174 len = 1;
6175 }
6176 else
6177 p = end;
6178 if (buf == NULL)
6179 totlen += len;
6180 else
6181 q += len;
6182 }
6183 if (buf != NULL) /* second time here, finished */
6184 {
6185 STRCPY(q, p);
6186 break;
6187 }
6188
6189 totlen += STRLEN(p); /* Add on the trailing characters */
6190 buf = alloc((unsigned)(totlen + 1));
6191 if (buf == NULL)
6192 {
6193 vim_free(split_buf);
6194 return;
6195 }
6196 }
6197
6198#ifdef FEAT_EVAL
6199 current_SID = cmd->uc_scriptID;
6200#endif
6201 (void)do_cmdline(buf, eap->getline, eap->cookie,
6202 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6203#ifdef FEAT_EVAL
6204 current_SID = save_current_SID;
6205#endif
6206 vim_free(buf);
6207 vim_free(split_buf);
6208}
6209
6210# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6211 static char_u *
6212get_user_command_name(idx)
6213 int idx;
6214{
6215 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6216}
6217
6218/*
6219 * Function given to ExpandGeneric() to obtain the list of user command names.
6220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 char_u *
6222get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006223 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 int idx;
6225{
6226 if (idx < curbuf->b_ucmds.ga_len)
6227 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6228 idx -= curbuf->b_ucmds.ga_len;
6229 if (idx < ucmds.ga_len)
6230 return USER_CMD(idx)->uc_name;
6231 return NULL;
6232}
6233
6234/*
6235 * Function given to ExpandGeneric() to obtain the list of user command
6236 * attributes.
6237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 char_u *
6239get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006240 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 int idx;
6242{
6243 static char *user_cmd_flags[] =
6244 {"bang", "bar", "buffer", "complete", "count",
6245 "nargs", "range", "register"};
6246
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006247 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 return NULL;
6249 return (char_u *)user_cmd_flags[idx];
6250}
6251
6252/*
6253 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 char_u *
6256get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006257 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 int idx;
6259{
6260 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6261
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006262 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 return NULL;
6264 return (char_u *)user_cmd_nargs[idx];
6265}
6266
6267/*
6268 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 char_u *
6271get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006272 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 int idx;
6274{
6275 return (char_u *)command_complete[idx].name;
6276}
6277# endif /* FEAT_CMDL_COMPL */
6278
6279#endif /* FEAT_USR_CMDS */
6280
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006281#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6282/*
6283 * Parse a completion argument "value[vallen]".
6284 * The detected completion goes in "*complp", argument type in "*argt".
6285 * When there is an argument, for function and user defined completion, it's
6286 * copied to allocated memory and stored in "*compl_arg".
6287 * Returns FAIL if something is wrong.
6288 */
6289 int
6290parse_compl_arg(value, vallen, complp, argt, compl_arg)
6291 char_u *value;
6292 int vallen;
6293 int *complp;
6294 long *argt;
6295 char_u **compl_arg;
6296{
6297 char_u *arg = NULL;
6298 size_t arglen = 0;
6299 int i;
6300 int valend = vallen;
6301
6302 /* Look for any argument part - which is the part after any ',' */
6303 for (i = 0; i < vallen; ++i)
6304 {
6305 if (value[i] == ',')
6306 {
6307 arg = &value[i + 1];
6308 arglen = vallen - i - 1;
6309 valend = i;
6310 break;
6311 }
6312 }
6313
6314 for (i = 0; command_complete[i].expand != 0; ++i)
6315 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006316 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006317 && STRNCMP(value, command_complete[i].name, valend) == 0)
6318 {
6319 *complp = command_complete[i].expand;
6320 if (command_complete[i].expand == EXPAND_BUFFERS)
6321 *argt |= BUFNAME;
6322 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6323 || command_complete[i].expand == EXPAND_FILES)
6324 *argt |= XFILE;
6325 break;
6326 }
6327 }
6328
6329 if (command_complete[i].expand == 0)
6330 {
6331 EMSG2(_("E180: Invalid complete value: %s"), value);
6332 return FAIL;
6333 }
6334
6335# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6336 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6337 && arg != NULL)
6338# else
6339 if (arg != NULL)
6340# endif
6341 {
6342 EMSG(_("E468: Completion argument only allowed for custom completion"));
6343 return FAIL;
6344 }
6345
6346# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6347 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6348 && arg == NULL)
6349 {
6350 EMSG(_("E467: Custom completion requires a function argument"));
6351 return FAIL;
6352 }
6353
6354 if (arg != NULL)
6355 *compl_arg = vim_strnsave(arg, (int)arglen);
6356# endif
6357 return OK;
6358}
6359#endif
6360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 static void
6362ex_colorscheme(eap)
6363 exarg_T *eap;
6364{
Bram Moolenaare6850792010-05-14 15:28:44 +02006365 if (*eap->arg == NUL)
6366 {
6367#ifdef FEAT_EVAL
6368 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6369 char_u *p = NULL;
6370
6371 if (expr != NULL)
6372 {
6373 ++emsg_off;
6374 p = eval_to_string(expr, NULL, FALSE);
6375 --emsg_off;
6376 vim_free(expr);
6377 }
6378 if (p != NULL)
6379 {
6380 MSG(p);
6381 vim_free(p);
6382 }
6383 else
6384 MSG("default");
6385#else
6386 MSG(_("unknown"));
6387#endif
6388 }
6389 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6391}
6392
6393 static void
6394ex_highlight(eap)
6395 exarg_T *eap;
6396{
6397 if (*eap->arg == NUL && eap->cmd[2] == '!')
6398 MSG(_("Greetings, Vim user!"));
6399 do_highlight(eap->arg, eap->forceit, FALSE);
6400}
6401
6402
6403/*
6404 * Call this function if we thought we were going to exit, but we won't
6405 * (because of an error). May need to restore the terminal mode.
6406 */
6407 void
6408not_exiting()
6409{
6410 exiting = FALSE;
6411 settmode(TMODE_RAW);
6412}
6413
6414/*
6415 * ":quit": quit current window, quit Vim if closed the last window.
6416 */
6417 static void
6418ex_quit(eap)
6419 exarg_T *eap;
6420{
6421#ifdef FEAT_CMDWIN
6422 if (cmdwin_type != 0)
6423 {
6424 cmdwin_result = Ctrl_C;
6425 return;
6426 }
6427#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006428 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006429 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006430 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006431 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006432 return;
6433 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006434#ifdef FEAT_AUTOCMD
6435 if (curbuf_locked())
6436 return;
6437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438
6439#ifdef FEAT_NETBEANS_INTG
6440 netbeansForcedQuit = eap->forceit;
6441#endif
6442
6443 /*
6444 * If there are more files or windows we won't exit.
6445 */
6446 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6447 exiting = TRUE;
6448 if ((!P_HID(curbuf)
6449 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6450 || check_more(TRUE, eap->forceit) == FAIL
6451 || (only_one_window() && check_changed_any(eap->forceit)))
6452 {
6453 not_exiting();
6454 }
6455 else
6456 {
6457#ifdef FEAT_WINDOWS
6458 if (only_one_window()) /* quit last window */
6459#endif
6460 getout(0);
6461#ifdef FEAT_WINDOWS
6462# ifdef FEAT_GUI
6463 need_mouse_correct = TRUE;
6464# endif
6465 /* close window; may free buffer */
6466 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6467#endif
6468 }
6469}
6470
6471/*
6472 * ":cquit".
6473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 static void
6475ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006476 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 getout(1); /* this does not always pass on the exit code to the Manx
6479 compiler. why? */
6480}
6481
6482/*
6483 * ":qall": try to quit all windows
6484 */
6485 static void
6486ex_quit_all(eap)
6487 exarg_T *eap;
6488{
6489# ifdef FEAT_CMDWIN
6490 if (cmdwin_type != 0)
6491 {
6492 if (eap->forceit)
6493 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6494 else
6495 cmdwin_result = K_XF2;
6496 return;
6497 }
6498# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006499
6500 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006501 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006502 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006503 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006504 return;
6505 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006506#ifdef FEAT_AUTOCMD
6507 if (curbuf_locked())
6508 return;
6509#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 exiting = TRUE;
6512 if (eap->forceit || !check_changed_any(FALSE))
6513 getout(0);
6514 not_exiting();
6515}
6516
Bram Moolenaard9967712006-03-11 21:18:15 +00006517#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518/*
6519 * ":close": close current window, unless it is the last one
6520 */
6521 static void
6522ex_close(eap)
6523 exarg_T *eap;
6524{
6525# ifdef FEAT_CMDWIN
6526 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006527 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 else
6529# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006530 if (!text_locked()
6531#ifdef FEAT_AUTOCMD
6532 && !curbuf_locked()
6533#endif
6534 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006535 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536}
6537
Bram Moolenaard9967712006-03-11 21:18:15 +00006538# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006539/*
6540 * ":pclose": Close any preview window.
6541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006543ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006545{
6546 win_T *win;
6547
6548 for (win = firstwin; win != NULL; win = win->w_next)
6549 if (win->w_p_pvw)
6550 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006552 break;
6553 }
6554}
Bram Moolenaard9967712006-03-11 21:18:15 +00006555# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006556
Bram Moolenaarf740b292006-02-16 22:11:02 +00006557/*
6558 * Close window "win" and take care of handling closing the last window for a
6559 * modified buffer.
6560 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006562ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006563 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006565 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 int need_hide;
6568 buf_T *buf = win->w_buffer;
6569
6570 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006573# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if ((p_confirm || cmdmod.confirm) && p_write)
6575 {
6576 dialog_changed(buf, FALSE);
6577 if (buf_valid(buf) && bufIsChanged(buf))
6578 return;
6579 need_hide = FALSE;
6580 }
6581 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 {
6584 EMSG(_(e_nowrtmsg));
6585 return;
6586 }
6587 }
6588
Bram Moolenaard9967712006-03-11 21:18:15 +00006589# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006591# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006592
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006594 if (tp == NULL)
6595 win_close(win, !need_hide && !P_HID(buf));
6596 else
6597 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598}
6599
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006601 * ":tabclose": close current tab page, unless it is the last one.
6602 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 */
6604 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006605ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 exarg_T *eap;
6607{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006608 tabpage_T *tp;
6609
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006610# ifdef FEAT_CMDWIN
6611 if (cmdwin_type != 0)
6612 cmdwin_result = K_IGNORE;
6613 else
6614# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006615 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006616 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006619 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006620 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006621 tp = find_tabpage((int)eap->line2);
6622 if (tp == NULL)
6623 {
6624 beep_flush();
6625 return;
6626 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006627 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006628 {
6629 tabpage_close_other(tp, eap->forceit);
6630 return;
6631 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006632 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006633 if (!text_locked()
6634#ifdef FEAT_AUTOCMD
6635 && !curbuf_locked()
6636#endif
6637 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006638 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006640}
6641
6642/*
6643 * ":tabonly": close all tab pages except the current one
6644 */
6645 static void
6646ex_tabonly(eap)
6647 exarg_T *eap;
6648{
6649 tabpage_T *tp;
6650 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006651
6652# ifdef FEAT_CMDWIN
6653 if (cmdwin_type != 0)
6654 cmdwin_result = K_IGNORE;
6655 else
6656# endif
6657 if (first_tabpage->tp_next == NULL)
6658 MSG(_("Already only one tab page"));
6659 else
6660 {
6661 /* Repeat this up to a 1000 times, because autocommands may mess
6662 * up the lists. */
6663 for (done = 0; done < 1000; ++done)
6664 {
6665 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6666 if (tp->tp_topframe != topframe)
6667 {
6668 tabpage_close_other(tp, eap->forceit);
6669 /* if we failed to close it quit */
6670 if (valid_tabpage(tp))
6671 done = 1000;
6672 /* start over, "tp" is now invalid */
6673 break;
6674 }
6675 if (first_tabpage->tp_next == NULL)
6676 break;
6677 }
6678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006682 * Close the current tab page.
6683 */
6684 void
6685tabpage_close(forceit)
6686 int forceit;
6687{
6688 /* First close all the windows but the current one. If that worked then
6689 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006690 if (lastwin != firstwin)
6691 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006692 if (lastwin == firstwin)
6693 ex_win_close(forceit, curwin, NULL);
6694# ifdef FEAT_GUI
6695 need_mouse_correct = TRUE;
6696# endif
6697}
6698
6699/*
6700 * Close tab page "tp", which is not the current tab page.
6701 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006702 * Also takes care of the tab pages line disappearing when closing the
6703 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006704 */
6705 void
6706tabpage_close_other(tp, forceit)
6707 tabpage_T *tp;
6708 int forceit;
6709{
6710 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006711 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006712 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006713
6714 /* Limit to 1000 windows, autocommands may add a window while we close
6715 * one. OK, so I'm paranoid... */
6716 while (++done < 1000)
6717 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006718 wp = tp->tp_firstwin;
6719 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006720
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006721 /* Autocommands may delete the tab page under our fingers and we may
6722 * fail to close a window with a modified buffer. */
6723 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724 break;
6725 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006726
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006727 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006728 if (h != tabline_height())
6729 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006730}
6731
6732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 * ":only".
6734 */
6735 static void
6736ex_only(eap)
6737 exarg_T *eap;
6738{
6739# ifdef FEAT_GUI
6740 need_mouse_correct = TRUE;
6741# endif
6742 close_others(TRUE, eap->forceit);
6743}
6744
6745/*
6746 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006747 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006749 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750ex_all(eap)
6751 exarg_T *eap;
6752{
6753 if (eap->addr_count == 0)
6754 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006755 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756}
6757#endif /* FEAT_WINDOWS */
6758
6759 static void
6760ex_hide(eap)
6761 exarg_T *eap;
6762{
6763 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6764 eap->errmsg = e_invarg;
6765 else
6766 {
6767 /* ":hide" or ":hide | cmd": hide current window */
6768 eap->nextcmd = check_nextcmd(eap->arg);
6769#ifdef FEAT_WINDOWS
6770 if (!eap->skip)
6771 {
6772# ifdef FEAT_GUI
6773 need_mouse_correct = TRUE;
6774# endif
6775 win_close(curwin, FALSE); /* don't free buffer */
6776 }
6777#endif
6778 }
6779}
6780
6781/*
6782 * ":stop" and ":suspend": Suspend Vim.
6783 */
6784 static void
6785ex_stop(eap)
6786 exarg_T *eap;
6787{
6788 /*
6789 * Disallow suspending for "rvim".
6790 */
6791 if (!check_restricted()
6792#ifdef WIN3264
6793 /*
6794 * Check if external commands are allowed now.
6795 */
6796 && can_end_termcap_mode(TRUE)
6797#endif
6798 )
6799 {
6800 if (!eap->forceit)
6801 autowrite_all();
6802 windgoto((int)Rows - 1, 0);
6803 out_char('\n');
6804 out_flush();
6805 stoptermcap();
6806 out_flush(); /* needed for SUN to restore xterm buffer */
6807#ifdef FEAT_TITLE
6808 mch_restore_title(3); /* restore window titles */
6809#endif
6810 ui_suspend(); /* call machine specific function */
6811#ifdef FEAT_TITLE
6812 maketitle();
6813 resettitle(); /* force updating the title */
6814#endif
6815 starttermcap();
6816 scroll_start(); /* scroll screen before redrawing */
6817 redraw_later_clear();
6818 shell_resized(); /* may have resized window */
6819 }
6820}
6821
6822/*
6823 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6824 */
6825 static void
6826ex_exit(eap)
6827 exarg_T *eap;
6828{
6829#ifdef FEAT_CMDWIN
6830 if (cmdwin_type != 0)
6831 {
6832 cmdwin_result = Ctrl_C;
6833 return;
6834 }
6835#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006836 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006837 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006838 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006839 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006840 return;
6841 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006842#ifdef FEAT_AUTOCMD
6843 if (curbuf_locked())
6844 return;
6845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
6847 /*
6848 * if more files or windows we won't exit
6849 */
6850 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6851 exiting = TRUE;
6852 if ( ((eap->cmdidx == CMD_wq
6853 || curbufIsChanged())
6854 && do_write(eap) == FAIL)
6855 || check_more(TRUE, eap->forceit) == FAIL
6856 || (only_one_window() && check_changed_any(eap->forceit)))
6857 {
6858 not_exiting();
6859 }
6860 else
6861 {
6862#ifdef FEAT_WINDOWS
6863 if (only_one_window()) /* quit last window, exit Vim */
6864#endif
6865 getout(0);
6866#ifdef FEAT_WINDOWS
6867# ifdef FEAT_GUI
6868 need_mouse_correct = TRUE;
6869# endif
6870 /* quit current window, may free buffer */
6871 win_close(curwin, !P_HID(curwin->w_buffer));
6872#endif
6873 }
6874}
6875
6876/*
6877 * ":print", ":list", ":number".
6878 */
6879 static void
6880ex_print(eap)
6881 exarg_T *eap;
6882{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006883 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6884 EMSG(_(e_emptybuf));
6885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006887 for ( ;!got_int; ui_breakcheck())
6888 {
6889 print_line(eap->line1,
6890 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6891 || (eap->flags & EXFLAG_NR)),
6892 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6893 if (++eap->line1 > eap->line2)
6894 break;
6895 out_flush(); /* show one line at a time */
6896 }
6897 setpcmark();
6898 /* put cursor at last line */
6899 curwin->w_cursor.lnum = eap->line2;
6900 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
6906#ifdef FEAT_BYTEOFF
6907 static void
6908ex_goto(eap)
6909 exarg_T *eap;
6910{
6911 goto_byte(eap->line2);
6912}
6913#endif
6914
6915/*
6916 * ":shell".
6917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 static void
6919ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006920 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 do_shell(NULL, 0);
6923}
6924
6925#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6926 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006927 || defined(FEAT_GUI_MSWIN) \
6928 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 || defined(PROTO)
6930
6931/*
6932 * Handle a file drop. The code is here because a drop is *nearly* like an
6933 * :args command, but not quite (we have a list of exact filenames, so we
6934 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6935 * code is very similar to :args and hence needs access to a lot of the static
6936 * functions in this file.
6937 *
6938 * The list should be allocated using alloc(), as should each item in the
6939 * list. This function takes over responsibility for freeing the list.
6940 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006941 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6943 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6944 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6945 * file functionality is (currently) not in EMX this is not presently a
6946 * problem.
6947 */
6948 void
6949handle_drop(filec, filev, split)
6950 int filec; /* the number of files dropped */
6951 char_u **filev; /* the list of files dropped */
6952 int split; /* force splitting the window */
6953{
6954 exarg_T ea;
6955 int save_msg_scroll = msg_scroll;
6956
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006957 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006958 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006960#ifdef FEAT_AUTOCMD
6961 if (curbuf_locked())
6962 return;
6963#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006964 /* When the screen is being updated we should not change buffers and
6965 * windows structures, it may cause freed memory to be used. */
6966 if (updating_screen)
6967 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
6969 /* Check whether the current buffer is changed. If so, we will need
6970 * to split the current window or data could be lost.
6971 * We don't need to check if the 'hidden' option is set, as in this
6972 * case the buffer won't be lost.
6973 */
6974 if (!P_HID(curbuf) && !split)
6975 {
6976 ++emsg_off;
6977 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6978 --emsg_off;
6979 }
6980 if (split)
6981 {
6982# ifdef FEAT_WINDOWS
6983 if (win_split(0, 0) == FAIL)
6984 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006985 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
6987 /* When splitting the window, create a new alist. Otherwise the
6988 * existing one is overwritten. */
6989 alist_unlink(curwin->w_alist);
6990 alist_new();
6991# else
6992 return; /* can't split, always fail */
6993# endif
6994 }
6995
6996 /*
6997 * Set up the new argument list.
6998 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006999 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 /*
7002 * Move to the first file.
7003 */
7004 /* Fake up a minimal "next" command for do_argfile() */
7005 vim_memset(&ea, 0, sizeof(ea));
7006 ea.cmd = (char_u *)"next";
7007 do_argfile(&ea, 0);
7008
7009 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7010 * mode that is not needed here. */
7011 need_start_insertmode = FALSE;
7012
7013 /* Restore msg_scroll, otherwise a following command may cause scrolling
7014 * unexpectedly. The screen will be redrawn by the caller, thus
7015 * msg_scroll being set by displaying a message is irrelevant. */
7016 msg_scroll = save_msg_scroll;
7017}
7018#endif
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020/*
7021 * Clear an argument list: free all file names and reset it to zero entries.
7022 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007023 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024alist_clear(al)
7025 alist_T *al;
7026{
7027 while (--al->al_ga.ga_len >= 0)
7028 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7029 ga_clear(&al->al_ga);
7030}
7031
7032/*
7033 * Init an argument list.
7034 */
7035 void
7036alist_init(al)
7037 alist_T *al;
7038{
7039 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7040}
7041
7042#if defined(FEAT_WINDOWS) || defined(PROTO)
7043
7044/*
7045 * Remove a reference from an argument list.
7046 * Ignored when the argument list is the global one.
7047 * If the argument list is no longer used by any window, free it.
7048 */
7049 void
7050alist_unlink(al)
7051 alist_T *al;
7052{
7053 if (al != &global_alist && --al->al_refcount <= 0)
7054 {
7055 alist_clear(al);
7056 vim_free(al);
7057 }
7058}
7059
7060# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7061/*
7062 * Create a new argument list and use it for the current window.
7063 */
7064 void
7065alist_new()
7066{
7067 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7068 if (curwin->w_alist == NULL)
7069 {
7070 curwin->w_alist = &global_alist;
7071 ++global_alist.al_refcount;
7072 }
7073 else
7074 {
7075 curwin->w_alist->al_refcount = 1;
7076 alist_init(curwin->w_alist);
7077 }
7078}
7079# endif
7080#endif
7081
7082#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7083/*
7084 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007085 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7086 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 */
7088 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007089alist_expand(fnum_list, fnum_len)
7090 int *fnum_list;
7091 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007094 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 char_u **new_arg_files;
7096 int new_arg_file_count;
7097 char_u *save_p_su = p_su;
7098 int i;
7099
7100 /* Don't use 'suffixes' here. This should work like the shell did the
7101 * expansion. Also, the vimrc file isn't read yet, thus the user
7102 * can't set the options. */
7103 p_su = empty_option;
7104 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7105 if (old_arg_files != NULL)
7106 {
7107 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007108 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7109 old_arg_count = GARGCOUNT;
7110 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007112 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 && new_arg_file_count > 0)
7114 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007115 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7116 TRUE, fnum_list, fnum_len);
7117 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120 p_su = save_p_su;
7121}
7122#endif
7123
7124/*
7125 * Set the argument list for the current window.
7126 * Takes over the allocated files[] and the allocated fnames in it.
7127 */
7128 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007129alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 alist_T *al;
7131 int count;
7132 char_u **files;
7133 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007134 int *fnum_list;
7135 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137 int i;
7138
7139 alist_clear(al);
7140 if (ga_grow(&al->al_ga, count) == OK)
7141 {
7142 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007143 {
7144 if (got_int)
7145 {
7146 /* When adding many buffers this can take a long time. Allow
7147 * interrupting here. */
7148 while (i < count)
7149 vim_free(files[i++]);
7150 break;
7151 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007152
7153 /* May set buffer name of a buffer previously used for the
7154 * argument list, so that it's re-used by alist_add. */
7155 if (fnum_list != NULL && i < fnum_len)
7156 buf_set_name(fnum_list[i], files[i]);
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007159 ui_breakcheck();
7160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 vim_free(files);
7162 }
7163 else
7164 FreeWild(count, files);
7165#ifdef FEAT_WINDOWS
7166 if (al == &global_alist)
7167#endif
7168 arg_had_last = FALSE;
7169}
7170
7171/*
7172 * Add file "fname" to argument list "al".
7173 * "fname" must have been allocated and "al" must have been checked for room.
7174 */
7175 void
7176alist_add(al, fname, set_fnum)
7177 alist_T *al;
7178 char_u *fname;
7179 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7180{
7181 if (fname == NULL) /* don't add NULL file names */
7182 return;
7183#ifdef BACKSLASH_IN_FILENAME
7184 slash_adjust(fname);
7185#endif
7186 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7187 if (set_fnum > 0)
7188 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7189 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7190 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191}
7192
7193#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7194/*
7195 * Adjust slashes in file names. Called after 'shellslash' was set.
7196 */
7197 void
7198alist_slash_adjust()
7199{
7200 int i;
7201# ifdef FEAT_WINDOWS
7202 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007203 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204# endif
7205
7206 for (i = 0; i < GARGCOUNT; ++i)
7207 if (GARGLIST[i].ae_fname != NULL)
7208 slash_adjust(GARGLIST[i].ae_fname);
7209# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007210 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 if (wp->w_alist != &global_alist)
7212 for (i = 0; i < WARGCOUNT(wp); ++i)
7213 if (WARGLIST(wp)[i].ae_fname != NULL)
7214 slash_adjust(WARGLIST(wp)[i].ae_fname);
7215# endif
7216}
7217#endif
7218
7219/*
7220 * ":preserve".
7221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 static void
7223ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007224 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007226 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 ml_preserve(curbuf, TRUE);
7228}
7229
7230/*
7231 * ":recover".
7232 */
7233 static void
7234ex_recover(eap)
7235 exarg_T *eap;
7236{
7237 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7238 recoverymode = TRUE;
7239 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7240 && (*eap->arg == NUL
7241 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7242 ml_recover();
7243 recoverymode = FALSE;
7244}
7245
7246/*
7247 * Command modifier used in a wrong way.
7248 */
7249 static void
7250ex_wrongmodifier(eap)
7251 exarg_T *eap;
7252{
7253 eap->errmsg = e_invcmd;
7254}
7255
7256#ifdef FEAT_WINDOWS
7257/*
7258 * :sview [+command] file split window with new file, read-only
7259 * :split [[+command] file] split window with current or new file
7260 * :vsplit [[+command] file] split window vertically with current or new file
7261 * :new [[+command] file] split window with no or new file
7262 * :vnew [[+command] file] split vertically window with no or new file
7263 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007264 *
7265 * :tabedit open new Tab page with empty window
7266 * :tabedit [+command] file open new Tab page and edit "file"
7267 * :tabnew [[+command] file] just like :tabedit
7268 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 */
7270 void
7271ex_splitview(eap)
7272 exarg_T *eap;
7273{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007274 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007275# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007277# endif
7278# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007282# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7284 {
7285 ex_ni(eap);
7286 return;
7287 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007290# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007292# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007294# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007296 * quickfix windows. But it's OK when doing ":tab split". */
7297 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 if (eap->cmdidx == CMD_split)
7300 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007301# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (eap->cmdidx == CMD_vsplit)
7303 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007308# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007309 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 {
7311 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7312 FNAME_MESS, TRUE, curbuf->b_ffname);
7313 if (fname == NULL)
7314 goto theend;
7315 eap->arg = fname;
7316 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007317# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007321# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007323# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 && eap->cmdidx != CMD_new)
7327 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007328# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007329 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007330# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007331 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007332# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007333 au_has_group((char_u *)"FileExplorer"))
7334 {
7335 /* No browsing supported but we do have the file explorer:
7336 * Edit the directory. */
7337 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7338 eap->arg = (char_u *)".";
7339 }
7340 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007341# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007342 {
7343 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007345 if (fname == NULL)
7346 goto theend;
7347 eap->arg = fname;
7348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007353 /*
7354 * Either open new tab page or split the window.
7355 */
7356 if (eap->cmdidx == CMD_tabedit
7357 || eap->cmdidx == CMD_tabfind
7358 || eap->cmdidx == CMD_tabnew)
7359 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007360 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7361 : eap->addr_count == 0 ? 0
7362 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007363 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007364 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007365
7366 /* set the alternate buffer for the window we came from */
7367 if (curwin != old_curwin
7368 && win_valid(old_curwin)
7369 && old_curwin->w_buffer != curbuf
7370 && !cmdmod.keepalt)
7371 old_curwin->w_alt_fnum = curbuf->b_fnum;
7372 }
7373 }
7374 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7376 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007377# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 /* Reset 'scrollbind' when editing another file, but keep it when
7379 * doing ":split" without arguments. */
7380 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007381# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007385 {
7386 RESET_BINDING(curwin);
7387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 else
7389 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 do_exedit(eap, old_curwin);
7392 }
7393
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007398# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399theend:
7400 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007403
7404/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007405 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406 */
7407 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007408tabpage_new()
7409{
7410 exarg_T ea;
7411
7412 vim_memset(&ea, 0, sizeof(ea));
7413 ea.cmdidx = CMD_tabnew;
7414 ea.cmd = (char_u *)"tabn";
7415 ea.arg = (char_u *)"";
7416 ex_splitview(&ea);
7417}
7418
7419/*
7420 * :tabnext command
7421 */
7422 static void
7423ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424 exarg_T *eap;
7425{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007426 switch (eap->cmdidx)
7427 {
7428 case CMD_tabfirst:
7429 case CMD_tabrewind:
7430 goto_tabpage(1);
7431 break;
7432 case CMD_tablast:
7433 goto_tabpage(9999);
7434 break;
7435 case CMD_tabprevious:
7436 case CMD_tabNext:
7437 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7438 break;
7439 default: /* CMD_tabnext */
7440 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7441 break;
7442 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007443}
7444
7445/*
7446 * :tabmove command
7447 */
7448 static void
7449ex_tabmove(eap)
7450 exarg_T *eap;
7451{
7452 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007453}
7454
7455/*
7456 * :tabs command: List tabs and their contents.
7457 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458 static void
7459ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007460 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007461{
7462 tabpage_T *tp;
7463 win_T *wp;
7464 int tabcount = 1;
7465
7466 msg_start();
7467 msg_scroll = TRUE;
7468 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7469 {
7470 msg_putchar('\n');
7471 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7472 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7473 out_flush(); /* output one line at a time */
7474 ui_breakcheck();
7475
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007476 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007477 wp = firstwin;
7478 else
7479 wp = tp->tp_firstwin;
7480 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7481 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007482 msg_putchar('\n');
7483 msg_putchar(wp == curwin ? '>' : ' ');
7484 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007485 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7486 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007487 if (buf_spname(wp->w_buffer) != NULL)
7488 STRCPY(IObuff, buf_spname(wp->w_buffer));
7489 else
7490 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7491 IObuff, IOSIZE, TRUE);
7492 msg_outtrans(IObuff);
7493 out_flush(); /* output one line at a time */
7494 ui_breakcheck();
7495 }
7496 }
7497}
7498
7499#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501/*
7502 * ":mode": Set screen mode.
7503 * If no argument given, just get the screen size and redraw.
7504 */
7505 static void
7506ex_mode(eap)
7507 exarg_T *eap;
7508{
7509 if (*eap->arg == NUL)
7510 shell_resized();
7511 else
7512 mch_screenmode(eap->arg);
7513}
7514
7515#ifdef FEAT_WINDOWS
7516/*
7517 * ":resize".
7518 * set, increment or decrement current window height
7519 */
7520 static void
7521ex_resize(eap)
7522 exarg_T *eap;
7523{
7524 int n;
7525 win_T *wp = curwin;
7526
7527 if (eap->addr_count > 0)
7528 {
7529 n = eap->line2;
7530 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7531 ;
7532 }
7533
7534#ifdef FEAT_GUI
7535 need_mouse_correct = TRUE;
7536#endif
7537 n = atol((char *)eap->arg);
7538#ifdef FEAT_VERTSPLIT
7539 if (cmdmod.split & WSP_VERT)
7540 {
7541 if (*eap->arg == '-' || *eap->arg == '+')
7542 n += W_WIDTH(curwin);
7543 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7544 n = 9999;
7545 win_setwidth_win((int)n, wp);
7546 }
7547 else
7548#endif
7549 {
7550 if (*eap->arg == '-' || *eap->arg == '+')
7551 n += curwin->w_height;
7552 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7553 n = 9999;
7554 win_setheight_win((int)n, wp);
7555 }
7556}
7557#endif
7558
7559/*
7560 * ":find [+command] <file>" command.
7561 */
7562 static void
7563ex_find(eap)
7564 exarg_T *eap;
7565{
7566#ifdef FEAT_SEARCHPATH
7567 char_u *fname;
7568 int count;
7569
7570 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7571 TRUE, curbuf->b_ffname);
7572 if (eap->addr_count > 0)
7573 {
7574 /* Repeat finding the file "count" times. This matters when it
7575 * appears several times in the path. */
7576 count = eap->line2;
7577 while (fname != NULL && --count > 0)
7578 {
7579 vim_free(fname);
7580 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7581 FALSE, curbuf->b_ffname);
7582 }
7583 }
7584
7585 if (fname != NULL)
7586 {
7587 eap->arg = fname;
7588#endif
7589 do_exedit(eap, NULL);
7590#ifdef FEAT_SEARCHPATH
7591 vim_free(fname);
7592 }
7593#endif
7594}
7595
7596/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007597 * ":open" simulation: for now just work like ":visual".
7598 */
7599 static void
7600ex_open(eap)
7601 exarg_T *eap;
7602{
7603 regmatch_T regmatch;
7604 char_u *p;
7605
7606 curwin->w_cursor.lnum = eap->line2;
7607 beginline(BL_SOL | BL_FIX);
7608 if (*eap->arg == '/')
7609 {
7610 /* ":open /pattern/": put cursor in column found with pattern */
7611 ++eap->arg;
7612 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7613 *p = NUL;
7614 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7615 if (regmatch.regprog != NULL)
7616 {
7617 regmatch.rm_ic = p_ic;
7618 p = ml_get_curline();
7619 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007620 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007621 else
7622 EMSG(_(e_nomatch));
7623 vim_free(regmatch.regprog);
7624 }
7625 /* Move to the NUL, ignore any other arguments. */
7626 eap->arg += STRLEN(eap->arg);
7627 }
7628 check_cursor();
7629
7630 eap->cmdidx = CMD_visual;
7631 do_exedit(eap, NULL);
7632}
7633
7634/*
7635 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 */
7637 static void
7638ex_edit(eap)
7639 exarg_T *eap;
7640{
7641 do_exedit(eap, NULL);
7642}
7643
7644/*
7645 * ":edit <file>" command and alikes.
7646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 void
7648do_exedit(eap, old_curwin)
7649 exarg_T *eap;
7650 win_T *old_curwin; /* curwin before doing a split or NULL */
7651{
7652 int n;
7653#ifdef FEAT_WINDOWS
7654 int need_hide;
7655#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007656 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658 /*
7659 * ":vi" command ends Ex mode.
7660 */
7661 if (exmode_active && (eap->cmdidx == CMD_visual
7662 || eap->cmdidx == CMD_view))
7663 {
7664 exmode_active = FALSE;
7665 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007666 {
7667 /* Special case: ":global/pat/visual\NLvi-commands" */
7668 if (global_busy)
7669 {
7670 int rd = RedrawingDisabled;
7671 int nwr = no_wait_return;
7672 int ms = msg_scroll;
7673#ifdef FEAT_GUI
7674 int he = hold_gui_events;
7675#endif
7676
7677 if (eap->nextcmd != NULL)
7678 {
7679 stuffReadbuff(eap->nextcmd);
7680 eap->nextcmd = NULL;
7681 }
7682
7683 if (exmode_was != EXMODE_VIM)
7684 settmode(TMODE_RAW);
7685 RedrawingDisabled = 0;
7686 no_wait_return = 0;
7687 need_wait_return = FALSE;
7688 msg_scroll = 0;
7689#ifdef FEAT_GUI
7690 hold_gui_events = 0;
7691#endif
7692 must_redraw = CLEAR;
7693
7694 main_loop(FALSE, TRUE);
7695
7696 RedrawingDisabled = rd;
7697 no_wait_return = nwr;
7698 msg_scroll = ms;
7699#ifdef FEAT_GUI
7700 hold_gui_events = he;
7701#endif
7702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706
7707 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007708 || eap->cmdidx == CMD_tabnew
7709 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#ifdef FEAT_VERTSPLIT
7711 || eap->cmdidx == CMD_vnew
7712#endif
7713 ) && *eap->arg == NUL)
7714 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007715 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 setpcmark();
7717 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007718 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7719 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 else if ((eap->cmdidx != CMD_split
7722#ifdef FEAT_VERTSPLIT
7723 && eap->cmdidx != CMD_vsplit
7724#endif
7725 )
7726 || *eap->arg != NUL
7727#ifdef FEAT_BROWSE
7728 || cmdmod.browse
7729#endif
7730 )
7731 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007732#ifdef FEAT_AUTOCMD
7733 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7734 * can bring us here, others are stopped earlier. */
7735 if (*eap->arg != NUL && curbuf_locked())
7736 return;
7737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 n = readonlymode;
7739 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7740 readonlymode = TRUE;
7741 else if (eap->cmdidx == CMD_enew)
7742 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7743 empty buffer */
7744 setpcmark();
7745 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7746 NULL, eap,
7747 /* ":edit" goes to first line if Vi compatible */
7748 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7749 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7750 ? ECMD_ONE : eap->do_ecmd_lnum,
7751 (P_HID(curbuf) ? ECMD_HIDE : 0)
7752 + (eap->forceit ? ECMD_FORCEIT : 0)
7753#ifdef FEAT_LISTCMDS
7754 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7755#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007756 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 /* Editing the file failed. If the window was split, close it. */
7759#ifdef FEAT_WINDOWS
7760 if (old_curwin != NULL)
7761 {
7762 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7763 if (!need_hide || P_HID(curbuf))
7764 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007765# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7766 cleanup_T cs;
7767
7768 /* Reset the error/interrupt/exception state here so that
7769 * aborting() returns FALSE when closing a window. */
7770 enter_cleanup(&cs);
7771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772# ifdef FEAT_GUI
7773 need_mouse_correct = TRUE;
7774# endif
7775 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007776
7777# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7778 /* Restore the error/interrupt/exception state if not
7779 * discarded by a new aborting error, interrupt, or
7780 * uncaught exception. */
7781 leave_cleanup(&cs);
7782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784 }
7785#endif
7786 }
7787 else if (readonlymode && curbuf->b_nwindows == 1)
7788 {
7789 /* When editing an already visited buffer, 'readonly' won't be set
7790 * but the previous value is kept. With ":view" and ":sview" we
7791 * want the file to be readonly, except when another window is
7792 * editing the same buffer. */
7793 curbuf->b_p_ro = TRUE;
7794 }
7795 readonlymode = n;
7796 }
7797 else
7798 {
7799 if (eap->do_ecmd_cmd != NULL)
7800 do_cmdline_cmd(eap->do_ecmd_cmd);
7801#ifdef FEAT_TITLE
7802 n = curwin->w_arg_idx_invalid;
7803#endif
7804 check_arg_idx(curwin);
7805#ifdef FEAT_TITLE
7806 if (n != curwin->w_arg_idx_invalid)
7807 maketitle();
7808#endif
7809 }
7810
7811#ifdef FEAT_WINDOWS
7812 /*
7813 * if ":split file" worked, set alternate file name in old window to new
7814 * file
7815 */
7816 if (old_curwin != NULL
7817 && *eap->arg != NUL
7818 && curwin != old_curwin
7819 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007820 && old_curwin->w_buffer != curbuf
7821 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 old_curwin->w_alt_fnum = curbuf->b_fnum;
7823#endif
7824
7825 ex_no_reprint = TRUE;
7826}
7827
7828#ifndef FEAT_GUI
7829/*
7830 * ":gui" and ":gvim" when there is no GUI.
7831 */
7832 static void
7833ex_nogui(eap)
7834 exarg_T *eap;
7835{
7836 eap->errmsg = e_nogvim;
7837}
7838#endif
7839
7840#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7841 static void
7842ex_tearoff(eap)
7843 exarg_T *eap;
7844{
7845 gui_make_tearoff(eap->arg);
7846}
7847#endif
7848
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007849#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 static void
7851ex_popup(eap)
7852 exarg_T *eap;
7853{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007854 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855}
7856#endif
7857
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 static void
7859ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007860 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7863 MSG(_("No swap file"));
7864 else
7865 msg(curbuf->b_ml.ml_mfp->mf_fname);
7866}
7867
7868/*
7869 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7870 * offset.
7871 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 static void
7874ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007875 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
7877#ifdef FEAT_SCROLLBIND
7878 win_T *wp;
7879 long topline;
7880 long y;
7881 linenr_T old_linenr = curwin->w_cursor.lnum;
7882
7883 setpcmark();
7884
7885 /*
7886 * determine max topline
7887 */
7888 if (curwin->w_p_scb)
7889 {
7890 topline = curwin->w_topline;
7891 for (wp = firstwin; wp; wp = wp->w_next)
7892 {
7893 if (wp->w_p_scb && wp->w_buffer)
7894 {
7895 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7896 if (topline > y)
7897 topline = y;
7898 }
7899 }
7900 if (topline < 1)
7901 topline = 1;
7902 }
7903 else
7904 {
7905 topline = 1;
7906 }
7907
7908
7909 /*
7910 * set all scrollbind windows to the same topline
7911 */
7912 wp = curwin;
7913 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7914 {
7915 if (curwin->w_p_scb)
7916 {
7917 y = topline - curwin->w_topline;
7918 if (y > 0)
7919 scrollup(y, TRUE);
7920 else
7921 scrolldown(-y, TRUE);
7922 curwin->w_scbind_pos = topline;
7923 redraw_later(VALID);
7924 cursor_correct();
7925#ifdef FEAT_WINDOWS
7926 curwin->w_redr_status = TRUE;
7927#endif
7928 }
7929 }
7930 curwin = wp;
7931 if (curwin->w_p_scb)
7932 {
7933 did_syncbind = TRUE;
7934 checkpcmark();
7935 if (old_linenr != curwin->w_cursor.lnum)
7936 {
7937 char_u ctrl_o[2];
7938
7939 ctrl_o[0] = Ctrl_O;
7940 ctrl_o[1] = 0;
7941 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7942 }
7943 }
7944#endif
7945}
7946
7947
7948 static void
7949ex_read(eap)
7950 exarg_T *eap;
7951{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 int i;
7953 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7954 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
7956 if (eap->usefilter) /* :r!cmd */
7957 do_bang(1, eap, FALSE, FALSE, TRUE);
7958 else
7959 {
7960 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7961 return;
7962
7963#ifdef FEAT_BROWSE
7964 if (cmdmod.browse)
7965 {
7966 char_u *browseFile;
7967
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007968 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 NULL, NULL, NULL, curbuf);
7970 if (browseFile != NULL)
7971 {
7972 i = readfile(browseFile, NULL,
7973 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7974 vim_free(browseFile);
7975 }
7976 else
7977 i = OK;
7978 }
7979 else
7980#endif
7981 if (*eap->arg == NUL)
7982 {
7983 if (check_fname() == FAIL) /* check for no file name */
7984 return;
7985 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7986 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7987 }
7988 else
7989 {
7990 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7991 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7992 i = readfile(eap->arg, NULL,
7993 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7994
7995 }
7996 if (i == FAIL)
7997 {
7998#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7999 if (!aborting())
8000#endif
8001 EMSG2(_(e_notopen), eap->arg);
8002 }
8003 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008004 {
8005 if (empty && exmode_active)
8006 {
8007 /* Delete the empty line that remains. Historically ex does
8008 * this but vi doesn't. */
8009 if (eap->line2 == 0)
8010 lnum = curbuf->b_ml.ml_line_count;
8011 else
8012 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008013 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008014 {
8015 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008016 if (curwin->w_cursor.lnum > 1
8017 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008018 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008019 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 }
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025}
8026
Bram Moolenaarea408852005-06-25 22:49:46 +00008027static char_u *prev_dir = NULL;
8028
8029#if defined(EXITFREE) || defined(PROTO)
8030 void
8031free_cd_dir()
8032{
8033 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008034 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008035
8036 vim_free(globaldir);
8037 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008038}
8039#endif
8040
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042/*
8043 * ":cd", ":lcd", ":chdir" and ":lchdir".
8044 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008045 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046ex_cd(eap)
8047 exarg_T *eap;
8048{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 char_u *new_dir;
8050 char_u *tofree;
8051
8052 new_dir = eap->arg;
8053#if !defined(UNIX) && !defined(VMS)
8054 /* for non-UNIX ":cd" means: print current directory */
8055 if (*new_dir == NUL)
8056 ex_pwd(NULL);
8057 else
8058#endif
8059 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008060#ifdef FEAT_AUTOCMD
8061 if (allbuf_locked())
8062 return;
8063#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008064 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8065 && !eap->forceit)
8066 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008067 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008068 return;
8069 }
8070
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 /* ":cd -": Change to previous directory */
8072 if (STRCMP(new_dir, "-") == 0)
8073 {
8074 if (prev_dir == NULL)
8075 {
8076 EMSG(_("E186: No previous directory"));
8077 return;
8078 }
8079 new_dir = prev_dir;
8080 }
8081
8082 /* Save current directory for next ":cd -" */
8083 tofree = prev_dir;
8084 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8085 prev_dir = vim_strsave(NameBuff);
8086 else
8087 prev_dir = NULL;
8088
8089#if defined(UNIX) || defined(VMS)
8090 /* for UNIX ":cd" means: go to home directory */
8091 if (*new_dir == NUL)
8092 {
8093 /* use NameBuff for home directory name */
8094# ifdef VMS
8095 char_u *p;
8096
8097 p = mch_getenv((char_u *)"SYS$LOGIN");
8098 if (p == NULL || *p == NUL) /* empty is the same as not set */
8099 NameBuff[0] = NUL;
8100 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008101 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102# else
8103 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8104# endif
8105 new_dir = NameBuff;
8106 }
8107#endif
8108 if (new_dir == NULL || vim_chdir(new_dir))
8109 EMSG(_(e_failed));
8110 else
8111 {
8112 vim_free(curwin->w_localdir);
8113 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8114 {
8115 /* If still in global directory, need to remember current
8116 * directory as global directory. */
8117 if (globaldir == NULL && prev_dir != NULL)
8118 globaldir = vim_strsave(prev_dir);
8119 /* Remember this local directory for the window. */
8120 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8121 curwin->w_localdir = vim_strsave(NameBuff);
8122 }
8123 else
8124 {
8125 /* We are now in the global directory, no need to remember its
8126 * name. */
8127 vim_free(globaldir);
8128 globaldir = NULL;
8129 curwin->w_localdir = NULL;
8130 }
8131
8132 shorten_fnames(TRUE);
8133
8134 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008135 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 ex_pwd(eap);
8137 }
8138 vim_free(tofree);
8139 }
8140}
8141
8142/*
8143 * ":pwd".
8144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 static void
8146ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008147 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8150 {
8151#ifdef BACKSLASH_IN_FILENAME
8152 slash_adjust(NameBuff);
8153#endif
8154 msg(NameBuff);
8155 }
8156 else
8157 EMSG(_("E187: Unknown"));
8158}
8159
8160/*
8161 * ":=".
8162 */
8163 static void
8164ex_equal(eap)
8165 exarg_T *eap;
8166{
8167 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008168 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171 static void
8172ex_sleep(eap)
8173 exarg_T *eap;
8174{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008175 int n;
8176 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
8178 if (cursor_valid())
8179 {
8180 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8181 if (n >= 0)
8182 windgoto((int)n, curwin->w_wcol);
8183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008184
8185 len = eap->line2;
8186 switch (*eap->arg)
8187 {
8188 case 'm': break;
8189 case NUL: len *= 1000L; break;
8190 default: EMSG2(_(e_invarg2), eap->arg); return;
8191 }
8192 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193}
8194
8195/*
8196 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8197 */
8198 void
8199do_sleep(msec)
8200 long msec;
8201{
8202 long done;
8203
8204 cursor_on();
8205 out_flush();
8206 for (done = 0; !got_int && done < msec; done += 1000L)
8207 {
8208 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8209 ui_breakcheck();
8210 }
8211}
8212
8213 static void
8214do_exmap(eap, isabbrev)
8215 exarg_T *eap;
8216 int isabbrev;
8217{
8218 int mode;
8219 char_u *cmdp;
8220
8221 cmdp = eap->cmd;
8222 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8223
8224 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8225 eap->arg, mode, isabbrev))
8226 {
8227 case 1: EMSG(_(e_invarg));
8228 break;
8229 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8230 break;
8231 }
8232}
8233
8234/*
8235 * ":winsize" command (obsolete).
8236 */
8237 static void
8238ex_winsize(eap)
8239 exarg_T *eap;
8240{
8241 int w, h;
8242 char_u *arg = eap->arg;
8243 char_u *p;
8244
8245 w = getdigits(&arg);
8246 arg = skipwhite(arg);
8247 p = arg;
8248 h = getdigits(&arg);
8249 if (*p != NUL && *arg == NUL)
8250 set_shellsize(w, h, TRUE);
8251 else
8252 EMSG(_("E465: :winsize requires two number arguments"));
8253}
8254
8255#ifdef FEAT_WINDOWS
8256 static void
8257ex_wincmd(eap)
8258 exarg_T *eap;
8259{
8260 int xchar = NUL;
8261 char_u *p;
8262
8263 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8264 {
8265 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8266 if (eap->arg[1] == NUL)
8267 {
8268 EMSG(_(e_invarg));
8269 return;
8270 }
8271 xchar = eap->arg[1];
8272 p = eap->arg + 2;
8273 }
8274 else
8275 p = eap->arg + 1;
8276
8277 eap->nextcmd = check_nextcmd(p);
8278 p = skipwhite(p);
8279 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8280 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008281 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
8283 /* Pass flags on for ":vertical wincmd ]". */
8284 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008285 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8287 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008288 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 }
8290}
8291#endif
8292
Bram Moolenaar843ee412004-06-30 16:16:41 +00008293#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294/*
8295 * ":winpos".
8296 */
8297 static void
8298ex_winpos(eap)
8299 exarg_T *eap;
8300{
8301 int x, y;
8302 char_u *arg = eap->arg;
8303 char_u *p;
8304
8305 if (*arg == NUL)
8306 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008307# if defined(FEAT_GUI) || defined(MSWIN)
8308# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008310# else
8311 if (mch_get_winpos(&x, &y) != FAIL)
8312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
8314 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8315 msg(IObuff);
8316 }
8317 else
8318# endif
8319 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8320 }
8321 else
8322 {
8323 x = getdigits(&arg);
8324 arg = skipwhite(arg);
8325 p = arg;
8326 y = getdigits(&arg);
8327 if (*p == NUL || *arg != NUL)
8328 {
8329 EMSG(_("E466: :winpos requires two number arguments"));
8330 return;
8331 }
8332# ifdef FEAT_GUI
8333 if (gui.in_use)
8334 gui_mch_set_winpos(x, y);
8335 else if (gui.starting)
8336 {
8337 /* Remember the coordinates for when the window is opened. */
8338 gui_win_x = x;
8339 gui_win_y = y;
8340 }
8341# ifdef HAVE_TGETENT
8342 else
8343# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008344# else
8345# ifdef MSWIN
8346 mch_set_winpos(x, y);
8347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348# endif
8349# ifdef HAVE_TGETENT
8350 if (*T_CWP)
8351 term_set_winpos(x, y);
8352# endif
8353 }
8354}
8355#endif
8356
8357/*
8358 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8359 */
8360 static void
8361ex_operators(eap)
8362 exarg_T *eap;
8363{
8364 oparg_T oa;
8365
8366 clear_oparg(&oa);
8367 oa.regname = eap->regname;
8368 oa.start.lnum = eap->line1;
8369 oa.end.lnum = eap->line2;
8370 oa.line_count = eap->line2 - eap->line1 + 1;
8371 oa.motion_type = MLINE;
8372#ifdef FEAT_VIRTUALEDIT
8373 virtual_op = FALSE;
8374#endif
8375 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8376 {
8377 setpcmark();
8378 curwin->w_cursor.lnum = eap->line1;
8379 beginline(BL_SOL | BL_FIX);
8380 }
8381
8382 switch (eap->cmdidx)
8383 {
8384 case CMD_delete:
8385 oa.op_type = OP_DELETE;
8386 op_delete(&oa);
8387 break;
8388
8389 case CMD_yank:
8390 oa.op_type = OP_YANK;
8391 (void)op_yank(&oa, FALSE, TRUE);
8392 break;
8393
8394 default: /* CMD_rshift or CMD_lshift */
8395 if ((eap->cmdidx == CMD_rshift)
8396#ifdef FEAT_RIGHTLEFT
8397 ^ curwin->w_p_rl
8398#endif
8399 )
8400 oa.op_type = OP_RSHIFT;
8401 else
8402 oa.op_type = OP_LSHIFT;
8403 op_shift(&oa, FALSE, eap->amount);
8404 break;
8405 }
8406#ifdef FEAT_VIRTUALEDIT
8407 virtual_op = MAYBE;
8408#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008409 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410}
8411
8412/*
8413 * ":put".
8414 */
8415 static void
8416ex_put(eap)
8417 exarg_T *eap;
8418{
8419 /* ":0put" works like ":1put!". */
8420 if (eap->line2 == 0)
8421 {
8422 eap->line2 = 1;
8423 eap->forceit = TRUE;
8424 }
8425 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008426 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8427 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428}
8429
8430/*
8431 * Handle ":copy" and ":move".
8432 */
8433 static void
8434ex_copymove(eap)
8435 exarg_T *eap;
8436{
8437 long n;
8438
8439 n = get_address(&eap->arg, FALSE, FALSE);
8440 if (eap->arg == NULL) /* error detected */
8441 {
8442 eap->nextcmd = NULL;
8443 return;
8444 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008445 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
8447 /*
8448 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8449 */
8450 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8451 {
8452 EMSG(_(e_invaddr));
8453 return;
8454 }
8455
8456 if (eap->cmdidx == CMD_move)
8457 {
8458 if (do_move(eap->line1, eap->line2, n) == FAIL)
8459 return;
8460 }
8461 else
8462 ex_copy(eap->line1, eap->line2, n);
8463 u_clearline();
8464 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008465 ex_may_print(eap);
8466}
8467
8468/*
8469 * Print the current line if flags were given to the Ex command.
8470 */
8471 static void
8472ex_may_print(eap)
8473 exarg_T *eap;
8474{
8475 if (eap->flags != 0)
8476 {
8477 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8478 (eap->flags & EXFLAG_LIST));
8479 ex_no_reprint = TRUE;
8480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
8483/*
8484 * ":smagic" and ":snomagic".
8485 */
8486 static void
8487ex_submagic(eap)
8488 exarg_T *eap;
8489{
8490 int magic_save = p_magic;
8491
8492 p_magic = (eap->cmdidx == CMD_smagic);
8493 do_sub(eap);
8494 p_magic = magic_save;
8495}
8496
8497/*
8498 * ":join".
8499 */
8500 static void
8501ex_join(eap)
8502 exarg_T *eap;
8503{
8504 curwin->w_cursor.lnum = eap->line1;
8505 if (eap->line1 == eap->line2)
8506 {
8507 if (eap->addr_count >= 2) /* :2,2join does nothing */
8508 return;
8509 if (eap->line2 == curbuf->b_ml.ml_line_count)
8510 {
8511 beep_flush();
8512 return;
8513 }
8514 ++eap->line2;
8515 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008516 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008518 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519}
8520
8521/*
8522 * ":[addr]@r" or ":[addr]*r": execute register
8523 */
8524 static void
8525ex_at(eap)
8526 exarg_T *eap;
8527{
8528 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008529 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530
8531 curwin->w_cursor.lnum = eap->line2;
8532
8533#ifdef USE_ON_FLY_SCROLL
8534 dont_scroll = TRUE; /* disallow scrolling here */
8535#endif
8536
8537 /* get the register name. No name means to use the previous one */
8538 c = *eap->arg;
8539 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8540 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008541 /* Put the register in the typeahead buffer with the "silent" flag. */
8542 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8543 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 else
8548 {
8549 int save_efr = exec_from_reg;
8550
8551 exec_from_reg = TRUE;
8552
8553 /*
8554 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008555 * Continue until the stuff buffer is empty and all added characters
8556 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008558 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8560
8561 exec_from_reg = save_efr;
8562 }
8563}
8564
8565/*
8566 * ":!".
8567 */
8568 static void
8569ex_bang(eap)
8570 exarg_T *eap;
8571{
8572 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8573}
8574
8575/*
8576 * ":undo".
8577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 static void
8579ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008580 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008582 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008583 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008584 else
8585 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586}
8587
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008588#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008589 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008590ex_wundo(eap)
8591 exarg_T *eap;
8592{
8593 char_u hash[UNDO_HASH_SIZE];
8594
8595 u_compute_hash(hash);
8596 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8597}
8598
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008599 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008600ex_rundo(eap)
8601 exarg_T *eap;
8602{
8603 char_u hash[UNDO_HASH_SIZE];
8604
8605 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008606 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008607}
8608#endif
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610/*
8611 * ":redo".
8612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 static void
8614ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008615 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 u_redo(1);
8618}
8619
8620/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008621 * ":earlier" and ":later".
8622 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008623 static void
8624ex_later(eap)
8625 exarg_T *eap;
8626{
8627 long count = 0;
8628 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008629 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008630 char_u *p = eap->arg;
8631
8632 if (*p == NUL)
8633 count = 1;
8634 else if (isdigit(*p))
8635 {
8636 count = getdigits(&p);
8637 switch (*p)
8638 {
8639 case 's': ++p; sec = TRUE; break;
8640 case 'm': ++p; sec = TRUE; count *= 60; break;
8641 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008642 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8643 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008644 }
8645 }
8646
8647 if (*p != NUL)
8648 EMSG2(_(e_invarg2), eap->arg);
8649 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008650 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8651 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008652}
8653
8654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 * ":redir": start/stop redirection.
8656 */
8657 static void
8658ex_redir(eap)
8659 exarg_T *eap;
8660{
8661 char *mode;
8662 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008663 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665 if (STRICMP(eap->arg, "END") == 0)
8666 close_redir();
8667 else
8668 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008669 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008671 ++arg;
8672 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008674 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 mode = "a";
8676 }
8677 else
8678 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008679 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681 close_redir();
8682
8683 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008684 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 if (fname == NULL)
8686 return;
8687#ifdef FEAT_BROWSE
8688 if (cmdmod.browse)
8689 {
8690 char_u *browseFile;
8691
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008692 browseFile = do_browse(BROWSE_SAVE,
8693 (char_u *)_("Save Redirection"),
8694 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 if (browseFile == NULL)
8696 return; /* operation cancelled */
8697 vim_free(fname);
8698 fname = browseFile;
8699 eap->forceit = TRUE; /* since dialog already asked */
8700 }
8701#endif
8702
8703 redir_fd = open_exfile(fname, eap->forceit, mode);
8704 vim_free(fname);
8705 }
8706#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008707 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 /* redirect to a register a-z (resp. A-Z for appending) */
8710 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008711 ++arg;
8712 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008714 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008715 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008717 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008719 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008720 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008721 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008724 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008725 if (*arg == '>')
8726 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008727 /* Make register empty when not using @A-@Z and the
8728 * command is valid. */
8729 if (*arg == NUL && !isupper(redir_reg))
8730 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008732 }
8733 if (*arg != NUL)
8734 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008735 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008736 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008737 }
8738 }
8739 else if (*arg == '=' && arg[1] == '>')
8740 {
8741 int append;
8742
8743 /* redirect to a variable */
8744 close_redir();
8745 arg += 2;
8746
8747 if (*arg == '>')
8748 {
8749 ++arg;
8750 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008753 append = FALSE;
8754
8755 if (var_redir_start(skipwhite(arg), append) == OK)
8756 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 }
8758#endif
8759
8760 /* TODO: redirect to a buffer */
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008763 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008765
8766 /* Make sure redirection is not off. Can happen for cmdline completion
8767 * that indirectly invokes a command to catch its output. */
8768 if (redir_fd != NULL
8769#ifdef FEAT_EVAL
8770 || redir_reg || redir_vname
8771#endif
8772 )
8773 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774}
8775
8776/*
8777 * ":redraw": force redraw
8778 */
8779 static void
8780ex_redraw(eap)
8781 exarg_T *eap;
8782{
8783 int r = RedrawingDisabled;
8784 int p = p_lz;
8785
8786 RedrawingDisabled = 0;
8787 p_lz = FALSE;
8788 update_topline();
8789 update_screen(eap->forceit ? CLEAR :
8790#ifdef FEAT_VISUAL
8791 VIsual_active ? INVERTED :
8792#endif
8793 0);
8794#ifdef FEAT_TITLE
8795 if (need_maketitle)
8796 maketitle();
8797#endif
8798 RedrawingDisabled = r;
8799 p_lz = p;
8800
8801 /* Reset msg_didout, so that a message that's there is overwritten. */
8802 msg_didout = FALSE;
8803 msg_col = 0;
8804
8805 out_flush();
8806}
8807
8808/*
8809 * ":redrawstatus": force redraw of status line(s)
8810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 static void
8812ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008813 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
8815#if defined(FEAT_WINDOWS)
8816 int r = RedrawingDisabled;
8817 int p = p_lz;
8818
8819 RedrawingDisabled = 0;
8820 p_lz = FALSE;
8821 if (eap->forceit)
8822 status_redraw_all();
8823 else
8824 status_redraw_curbuf();
8825 update_screen(
8826# ifdef FEAT_VISUAL
8827 VIsual_active ? INVERTED :
8828# endif
8829 0);
8830 RedrawingDisabled = r;
8831 p_lz = p;
8832 out_flush();
8833#endif
8834}
8835
8836 static void
8837close_redir()
8838{
8839 if (redir_fd != NULL)
8840 {
8841 fclose(redir_fd);
8842 redir_fd = NULL;
8843 }
8844#ifdef FEAT_EVAL
8845 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008846 if (redir_vname)
8847 {
8848 var_redir_stop();
8849 redir_vname = 0;
8850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851#endif
8852}
8853
8854#if defined(FEAT_SESSION) && defined(USE_CRNL)
8855# define MKSESSION_NL
8856static int mksession_nl = FALSE; /* use NL only in put_eol() */
8857#endif
8858
8859/*
8860 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8861 */
8862 static void
8863ex_mkrc(eap)
8864 exarg_T *eap;
8865{
8866 FILE *fd;
8867 int failed = FALSE;
8868 char_u *fname;
8869#ifdef FEAT_BROWSE
8870 char_u *browseFile = NULL;
8871#endif
8872#ifdef FEAT_SESSION
8873 int view_session = FALSE;
8874 int using_vdir = FALSE; /* using 'viewdir'? */
8875 char_u *viewFile = NULL;
8876 unsigned *flagp;
8877#endif
8878
8879 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8880 {
8881#ifdef FEAT_SESSION
8882 view_session = TRUE;
8883#else
8884 ex_ni(eap);
8885 return;
8886#endif
8887 }
8888
8889#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008890 /* Use the short file name until ":lcd" is used. We also don't use the
8891 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008892 did_lcd = FALSE;
8893
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8895 if (eap->cmdidx == CMD_mkview
8896 && (*eap->arg == NUL
8897 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8898 {
8899 eap->forceit = TRUE;
8900 fname = get_view_file(*eap->arg);
8901 if (fname == NULL)
8902 return;
8903 viewFile = fname;
8904 using_vdir = TRUE;
8905 }
8906 else
8907#endif
8908 if (*eap->arg != NUL)
8909 fname = eap->arg;
8910 else if (eap->cmdidx == CMD_mkvimrc)
8911 fname = (char_u *)VIMRC_FILE;
8912#ifdef FEAT_SESSION
8913 else if (eap->cmdidx == CMD_mksession)
8914 fname = (char_u *)SESSION_FILE;
8915#endif
8916 else
8917 fname = (char_u *)EXRC_FILE;
8918
8919#ifdef FEAT_BROWSE
8920 if (cmdmod.browse)
8921 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008922 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923# ifdef FEAT_SESSION
8924 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8925 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8926# endif
8927 (char_u *)_("Save Setup"),
8928 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8929 if (browseFile == NULL)
8930 goto theend;
8931 fname = browseFile;
8932 eap->forceit = TRUE; /* since dialog already asked */
8933 }
8934#endif
8935
8936#if defined(FEAT_SESSION) && defined(vim_mkdir)
8937 /* When using 'viewdir' may have to create the directory. */
8938 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008939 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#endif
8941
8942 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8943 if (fd != NULL)
8944 {
8945#ifdef FEAT_SESSION
8946 if (eap->cmdidx == CMD_mkview)
8947 flagp = &vop_flags;
8948 else
8949 flagp = &ssop_flags;
8950#endif
8951
8952#ifdef MKSESSION_NL
8953 /* "unix" in 'sessionoptions': use NL line separator */
8954 if (view_session && (*flagp & SSOP_UNIX))
8955 mksession_nl = TRUE;
8956#endif
8957
8958 /* Write the version command for :mkvimrc */
8959 if (eap->cmdidx == CMD_mkvimrc)
8960 (void)put_line(fd, "version 6.0");
8961
8962#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008963 if (eap->cmdidx == CMD_mksession)
8964 {
8965 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8966 failed = TRUE;
8967 }
8968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (eap->cmdidx != CMD_mkview)
8970#endif
8971 {
8972 /* Write setting 'compatible' first, because it has side effects.
8973 * For that same reason only do it when needed. */
8974 if (p_cp)
8975 (void)put_line(fd, "if !&cp | set cp | endif");
8976 else
8977 (void)put_line(fd, "if &cp | set nocp | endif");
8978 }
8979
8980#ifdef FEAT_SESSION
8981 if (!view_session
8982 || (eap->cmdidx == CMD_mksession
8983 && (*flagp & SSOP_OPTIONS)))
8984#endif
8985 failed |= (makemap(fd, NULL) == FAIL
8986 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8987
8988#ifdef FEAT_SESSION
8989 if (!failed && view_session)
8990 {
8991 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8992 failed = TRUE;
8993 if (eap->cmdidx == CMD_mksession)
8994 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008995 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996
Bram Moolenaard9462e32011-04-11 21:35:11 +02008997 dirnow = alloc(MAXPATHL);
8998 if (dirnow == NULL)
8999 failed = TRUE;
9000 else
9001 {
9002 /*
9003 * Change to session file's dir.
9004 */
9005 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009007 *dirnow = NUL;
9008 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9009 {
9010 if (vim_chdirfile(fname) == OK)
9011 shorten_fnames(TRUE);
9012 }
9013 else if (*dirnow != NUL
9014 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9015 {
9016 if (mch_chdir((char *)globaldir) == 0)
9017 shorten_fnames(TRUE);
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
Bram Moolenaard9462e32011-04-11 21:35:11 +02009020 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
Bram Moolenaard9462e32011-04-11 21:35:11 +02009022 /* restore original dir */
9023 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009025 {
9026 if (mch_chdir((char *)dirnow) != 0)
9027 EMSG(_(e_prev_dir));
9028 shorten_fnames(TRUE);
9029 }
9030 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 }
9033 else
9034 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009035 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9036 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
9038 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9039 == FAIL)
9040 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009041 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9042 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009043 if (eap->cmdidx == CMD_mksession)
9044 {
9045 if (put_line(fd, "unlet SessionLoad") == FAIL)
9046 failed = TRUE;
9047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009050 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9051 failed = TRUE;
9052
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 failed |= fclose(fd);
9054
9055 if (failed)
9056 EMSG(_(e_write));
9057#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9058 else if (eap->cmdidx == CMD_mksession)
9059 {
9060 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009061 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
Bram Moolenaard9462e32011-04-11 21:35:11 +02009063 tbuf = alloc(MAXPATHL);
9064 if (tbuf != NULL)
9065 {
9066 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9067 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9068 vim_free(tbuf);
9069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
9071#endif
9072#ifdef MKSESSION_NL
9073 mksession_nl = FALSE;
9074#endif
9075 }
9076
9077#ifdef FEAT_BROWSE
9078theend:
9079 vim_free(browseFile);
9080#endif
9081#ifdef FEAT_SESSION
9082 vim_free(viewFile);
9083#endif
9084}
9085
Bram Moolenaardf177f62005-02-22 08:39:57 +00009086#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9087 || defined(PROTO)
9088 int
9089vim_mkdir_emsg(name, prot)
9090 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009091 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009092{
9093 if (vim_mkdir(name, prot) != 0)
9094 {
9095 EMSG2(_("E739: Cannot create directory: %s"), name);
9096 return FAIL;
9097 }
9098 return OK;
9099}
9100#endif
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102/*
9103 * Open a file for writing for an Ex command, with some checks.
9104 * Return file descriptor, or NULL on failure.
9105 */
9106 FILE *
9107open_exfile(fname, forceit, mode)
9108 char_u *fname;
9109 int forceit;
9110 char *mode; /* "w" for create new file or "a" for append */
9111{
9112 FILE *fd;
9113
9114#ifdef UNIX
9115 /* with Unix it is possible to open a directory */
9116 if (mch_isdir(fname))
9117 {
9118 EMSG2(_(e_isadir2), fname);
9119 return NULL;
9120 }
9121#endif
9122 if (!forceit && *mode != 'a' && vim_fexists(fname))
9123 {
9124 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9125 return NULL;
9126 }
9127
9128 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9129 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9130
9131 return fd;
9132}
9133
9134/*
9135 * ":mark" and ":k".
9136 */
9137 static void
9138ex_mark(eap)
9139 exarg_T *eap;
9140{
9141 pos_T pos;
9142
9143 if (*eap->arg == NUL) /* No argument? */
9144 EMSG(_(e_argreq));
9145 else if (eap->arg[1] != NUL) /* more than one character? */
9146 EMSG(_(e_trailing));
9147 else
9148 {
9149 pos = curwin->w_cursor; /* save curwin->w_cursor */
9150 curwin->w_cursor.lnum = eap->line2;
9151 beginline(BL_WHITE | BL_FIX);
9152 if (setmark(*eap->arg) == FAIL) /* set mark */
9153 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9154 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9155 }
9156}
9157
9158/*
9159 * Update w_topline, w_leftcol and the cursor position.
9160 */
9161 void
9162update_topline_cursor()
9163{
9164 check_cursor(); /* put cursor on valid line */
9165 update_topline();
9166 if (!curwin->w_p_wrap)
9167 validate_cursor();
9168 update_curswant();
9169}
9170
9171#ifdef FEAT_EX_EXTRA
9172/*
9173 * ":normal[!] {commands}": Execute normal mode commands.
9174 */
9175 static void
9176ex_normal(eap)
9177 exarg_T *eap;
9178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 int save_msg_scroll = msg_scroll;
9180 int save_restart_edit = restart_edit;
9181 int save_msg_didout = msg_didout;
9182 int save_State = State;
9183 tasave_T tabuf;
9184 int save_insertmode = p_im;
9185 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009186 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187#ifdef FEAT_MBYTE
9188 char_u *arg = NULL;
9189 int l;
9190 char_u *p;
9191#endif
9192
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009193 if (ex_normal_lock > 0)
9194 {
9195 EMSG(_(e_secure));
9196 return;
9197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 if (ex_normal_busy >= p_mmd)
9199 {
9200 EMSG(_("E192: Recursive use of :normal too deep"));
9201 return;
9202 }
9203 ++ex_normal_busy;
9204
9205 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9206 restart_edit = 0; /* don't go to Insert mode */
9207 p_im = FALSE; /* don't use 'insertmode' */
9208
9209#ifdef FEAT_MBYTE
9210 /*
9211 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9212 * this for the K_SPECIAL leading byte, otherwise special keys will not
9213 * work.
9214 */
9215 if (has_mbyte)
9216 {
9217 int len = 0;
9218
9219 /* Count the number of characters to be escaped. */
9220 for (p = eap->arg; *p != NUL; ++p)
9221 {
9222# ifdef FEAT_GUI
9223 if (*p == CSI) /* leadbyte CSI */
9224 len += 2;
9225# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009226 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9228# ifdef FEAT_GUI
9229 || *p == CSI
9230# endif
9231 )
9232 len += 2;
9233 }
9234 if (len > 0)
9235 {
9236 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9237 if (arg != NULL)
9238 {
9239 len = 0;
9240 for (p = eap->arg; *p != NUL; ++p)
9241 {
9242 arg[len++] = *p;
9243# ifdef FEAT_GUI
9244 if (*p == CSI)
9245 {
9246 arg[len++] = KS_EXTRA;
9247 arg[len++] = (int)KE_CSI;
9248 }
9249# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009250 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 {
9252 arg[len++] = *++p;
9253 if (*p == K_SPECIAL)
9254 {
9255 arg[len++] = KS_SPECIAL;
9256 arg[len++] = KE_FILLER;
9257 }
9258# ifdef FEAT_GUI
9259 else if (*p == CSI)
9260 {
9261 arg[len++] = KS_EXTRA;
9262 arg[len++] = (int)KE_CSI;
9263 }
9264# endif
9265 }
9266 arg[len] = NUL;
9267 }
9268 }
9269 }
9270 }
9271#endif
9272
9273 /*
9274 * Save the current typeahead. This is required to allow using ":normal"
9275 * from an event handler and makes sure we don't hang when the argument
9276 * ends with half a command.
9277 */
9278 save_typeahead(&tabuf);
9279 if (tabuf.typebuf_valid)
9280 {
9281 /*
9282 * Repeat the :normal command for each line in the range. When no
9283 * range given, execute it just once, without positioning the cursor
9284 * first.
9285 */
9286 do
9287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 if (eap->addr_count != 0)
9289 {
9290 curwin->w_cursor.lnum = eap->line1++;
9291 curwin->w_cursor.col = 0;
9292 }
9293
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009294 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295#ifdef FEAT_MBYTE
9296 arg != NULL ? arg :
9297#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009298 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 }
9300 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9301 }
9302
9303 /* Might not return to the main loop when in an event handler. */
9304 update_topline_cursor();
9305
9306 /* Restore the previous typeahead. */
9307 restore_typeahead(&tabuf);
9308
9309 --ex_normal_busy;
9310 msg_scroll = save_msg_scroll;
9311 restart_edit = save_restart_edit;
9312 p_im = save_insertmode;
9313 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009314 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9316
9317 /* Restore the state (needed when called from a function executed for
9318 * 'indentexpr'). */
9319 State = save_State;
9320#ifdef FEAT_MBYTE
9321 vim_free(arg);
9322#endif
9323}
9324
9325/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009326 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 */
9328 static void
9329ex_startinsert(eap)
9330 exarg_T *eap;
9331{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009332 if (eap->forceit)
9333 {
9334 coladvance((colnr_T)MAXCOL);
9335 curwin->w_curswant = MAXCOL;
9336 curwin->w_set_curswant = FALSE;
9337 }
9338
Bram Moolenaara40c5002005-01-09 21:16:21 +00009339 /* Ignore the command when already in Insert mode. Inserting an
9340 * expression register that invokes a function can do this. */
9341 if (State & INSERT)
9342 return;
9343
Bram Moolenaar64969662005-12-14 21:59:55 +00009344 if (eap->cmdidx == CMD_startinsert)
9345 restart_edit = 'a';
9346 else if (eap->cmdidx == CMD_startreplace)
9347 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009349 restart_edit = 'V';
9350
9351 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009353 if (eap->cmdidx == CMD_startinsert)
9354 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 curwin->w_curswant = 0; /* avoid MAXCOL */
9356 }
9357}
9358
9359/*
9360 * ":stopinsert"
9361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 static void
9363ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009364 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 restart_edit = 0;
9367 stop_insert_mode = TRUE;
9368}
9369#endif
9370
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009371#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9372/*
9373 * Execute normal mode command "cmd".
9374 * "remap" can be REMAP_NONE or REMAP_YES.
9375 */
9376 void
9377exec_normal_cmd(cmd, remap, silent)
9378 char_u *cmd;
9379 int remap;
9380 int silent;
9381{
9382 oparg_T oa;
9383
9384 /*
9385 * Stuff the argument into the typeahead buffer.
9386 * Execute normal_cmd() until there is no typeahead left.
9387 */
9388 clear_oparg(&oa);
9389 finish_op = FALSE;
9390 ins_typebuf(cmd, remap, 0, TRUE, silent);
9391 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9392 && !got_int)
9393 {
9394 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009395 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009396 }
9397}
9398#endif
9399
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#ifdef FEAT_FIND_ID
9401 static void
9402ex_checkpath(eap)
9403 exarg_T *eap;
9404{
9405 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9406 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9407 (linenr_T)1, (linenr_T)MAXLNUM);
9408}
9409
9410#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9411/*
9412 * ":psearch"
9413 */
9414 static void
9415ex_psearch(eap)
9416 exarg_T *eap;
9417{
9418 g_do_tagpreview = p_pvh;
9419 ex_findpat(eap);
9420 g_do_tagpreview = 0;
9421}
9422#endif
9423
9424 static void
9425ex_findpat(eap)
9426 exarg_T *eap;
9427{
9428 int whole = TRUE;
9429 long n;
9430 char_u *p;
9431 int action;
9432
9433 switch (cmdnames[eap->cmdidx].cmd_name[2])
9434 {
9435 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9436 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9437 action = ACTION_GOTO;
9438 else
9439 action = ACTION_SHOW;
9440 break;
9441 case 'i': /* ":ilist" and ":dlist" */
9442 action = ACTION_SHOW_ALL;
9443 break;
9444 case 'u': /* ":ijump" and ":djump" */
9445 action = ACTION_GOTO;
9446 break;
9447 default: /* ":isplit" and ":dsplit" */
9448 action = ACTION_SPLIT;
9449 break;
9450 }
9451
9452 n = 1;
9453 if (vim_isdigit(*eap->arg)) /* get count */
9454 {
9455 n = getdigits(&eap->arg);
9456 eap->arg = skipwhite(eap->arg);
9457 }
9458 if (*eap->arg == '/') /* Match regexp, not just whole words */
9459 {
9460 whole = FALSE;
9461 ++eap->arg;
9462 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9463 if (*p)
9464 {
9465 *p++ = NUL;
9466 p = skipwhite(p);
9467
9468 /* Check for trailing illegal characters */
9469 if (!ends_excmd(*p))
9470 eap->errmsg = e_trailing;
9471 else
9472 eap->nextcmd = check_nextcmd(p);
9473 }
9474 }
9475 if (!eap->skip)
9476 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9477 whole, !eap->forceit,
9478 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9479 n, action, eap->line1, eap->line2);
9480}
9481#endif
9482
9483#ifdef FEAT_WINDOWS
9484
9485# ifdef FEAT_QUICKFIX
9486/*
9487 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9488 */
9489 static void
9490ex_ptag(eap)
9491 exarg_T *eap;
9492{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009493 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9495}
9496
9497/*
9498 * ":pedit"
9499 */
9500 static void
9501ex_pedit(eap)
9502 exarg_T *eap;
9503{
9504 win_T *curwin_save = curwin;
9505
9506 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009507 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 keep_help_flag = curwin_save->w_buffer->b_help;
9509 do_exedit(eap, NULL);
9510 keep_help_flag = FALSE;
9511 if (curwin != curwin_save && win_valid(curwin_save))
9512 {
9513 /* Return cursor to where we were */
9514 validate_cursor();
9515 redraw_later(VALID);
9516 win_enter(curwin_save, TRUE);
9517 }
9518 g_do_tagpreview = 0;
9519}
9520# endif
9521
9522/*
9523 * ":stag", ":stselect" and ":stjump".
9524 */
9525 static void
9526ex_stag(eap)
9527 exarg_T *eap;
9528{
9529 postponed_split = -1;
9530 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009531 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9533 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009534 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535}
9536#endif
9537
9538/*
9539 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9540 */
9541 static void
9542ex_tag(eap)
9543 exarg_T *eap;
9544{
9545 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9546}
9547
9548 static void
9549ex_tag_cmd(eap, name)
9550 exarg_T *eap;
9551 char_u *name;
9552{
9553 int cmd;
9554
9555 switch (name[1])
9556 {
9557 case 'j': cmd = DT_JUMP; /* ":tjump" */
9558 break;
9559 case 's': cmd = DT_SELECT; /* ":tselect" */
9560 break;
9561 case 'p': cmd = DT_PREV; /* ":tprevious" */
9562 break;
9563 case 'N': cmd = DT_PREV; /* ":tNext" */
9564 break;
9565 case 'n': cmd = DT_NEXT; /* ":tnext" */
9566 break;
9567 case 'o': cmd = DT_POP; /* ":pop" */
9568 break;
9569 case 'f': /* ":tfirst" */
9570 case 'r': cmd = DT_FIRST; /* ":trewind" */
9571 break;
9572 case 'l': cmd = DT_LAST; /* ":tlast" */
9573 break;
9574 default: /* ":tag" */
9575#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009576 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 {
9578 do_cstag(eap);
9579 return;
9580 }
9581#endif
9582 cmd = DT_TAG;
9583 break;
9584 }
9585
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009586 if (name[0] == 'l')
9587 {
9588#ifndef FEAT_QUICKFIX
9589 ex_ni(eap);
9590 return;
9591#else
9592 cmd = DT_LTAG;
9593#endif
9594 }
9595
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9597 eap->forceit, TRUE);
9598}
9599
9600/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009601 * Check "str" for starting with a special cmdline variable.
9602 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9603 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9604 */
9605 int
9606find_cmdline_var(src, usedlen)
9607 char_u *src;
9608 int *usedlen;
9609{
9610 int len;
9611 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009612 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009613 "%",
9614#define SPEC_PERC 0
9615 "#",
9616#define SPEC_HASH 1
9617 "<cword>", /* cursor word */
9618#define SPEC_CWORD 2
9619 "<cWORD>", /* cursor WORD */
9620#define SPEC_CCWORD 3
9621 "<cfile>", /* cursor path name */
9622#define SPEC_CFILE 4
9623 "<sfile>", /* ":so" file name */
9624#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009625 "<slnum>", /* ":so" file line number */
9626#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009627#ifdef FEAT_AUTOCMD
9628 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009629# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009630 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009631# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009632 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009633# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009634#endif
9635#ifdef FEAT_CLIENTSERVER
9636 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009637# ifdef FEAT_AUTOCMD
9638# define SPEC_CLIENT 10
9639# else
9640# define SPEC_CLIENT 7
9641# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009642#endif
9643 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009644
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009645 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009646 {
9647 len = (int)STRLEN(spec_str[i]);
9648 if (STRNCMP(src, spec_str[i], len) == 0)
9649 {
9650 *usedlen = len;
9651 return i;
9652 }
9653 }
9654 return -1;
9655}
9656
9657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 * Evaluate cmdline variables.
9659 *
9660 * change '%' to curbuf->b_ffname
9661 * '#' to curwin->w_altfile
9662 * '<cword>' to word under the cursor
9663 * '<cWORD>' to WORD under the cursor
9664 * '<cfile>' to path name under the cursor
9665 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009666 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 * '<afile>' to file name for autocommand
9668 * '<abuf>' to buffer number for autocommand
9669 * '<amatch>' to matching name for autocommand
9670 *
9671 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9672 * "" for error without a message) and NULL is returned.
9673 * Returns an allocated string if a valid match was found.
9674 * Returns NULL if no match was found. "usedlen" then still contains the
9675 * number of characters to skip.
9676 */
9677 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009678eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009680 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 int *usedlen; /* characters after src that are used */
9682 linenr_T *lnump; /* line number for :e command, or NULL */
9683 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009684 int *escaped; /* return value has escaped white space (can
9685 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687 int i;
9688 char_u *s;
9689 char_u *result;
9690 char_u *resultbuf = NULL;
9691 int resultlen;
9692 buf_T *buf;
9693 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9694 int spec_idx;
9695#ifdef FEAT_MODIFY_FNAME
9696 int skip_mod = FALSE;
9697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699
9700 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009701 if (escaped != NULL)
9702 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
9704 /*
9705 * Check if there is something to do.
9706 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009707 spec_idx = find_cmdline_var(src, usedlen);
9708 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 {
9710 *usedlen = 1;
9711 return NULL;
9712 }
9713
9714 /*
9715 * Skip when preceded with a backslash "\%" and "\#".
9716 * Note: In "\\%" the % is also not recognized!
9717 */
9718 if (src > srcstart && src[-1] == '\\')
9719 {
9720 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009721 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 return NULL;
9723 }
9724
9725 /*
9726 * word or WORD under cursor
9727 */
9728 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9729 {
9730 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9731 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9732 if (resultlen == 0)
9733 {
9734 *errormsg = (char_u *)"";
9735 return NULL;
9736 }
9737 }
9738
9739 /*
9740 * '#': Alternate file name
9741 * '%': Current file name
9742 * File name under the cursor
9743 * File name for autocommand
9744 * and following modifiers
9745 */
9746 else
9747 {
9748 switch (spec_idx)
9749 {
9750 case SPEC_PERC: /* '%': current file */
9751 if (curbuf->b_fname == NULL)
9752 {
9753 result = (char_u *)"";
9754 valid = 0; /* Must have ":p:h" to be valid */
9755 }
9756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 break;
9759
9760 case SPEC_HASH: /* '#' or "#99": alternate file */
9761 if (src[1] == '#') /* "##": the argument list */
9762 {
9763 result = arg_all();
9764 resultbuf = result;
9765 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009766 if (escaped != NULL)
9767 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768#ifdef FEAT_MODIFY_FNAME
9769 skip_mod = TRUE;
9770#endif
9771 break;
9772 }
9773 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009774 if (*s == '<') /* "#<99" uses v:oldfiles */
9775 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 i = (int)getdigits(&s);
9777 *usedlen = (int)(s - src); /* length of what we expand */
9778
Bram Moolenaard812df62008-11-09 12:46:09 +00009779 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009781 if (*usedlen < 2)
9782 {
9783 /* Should we give an error message for #<text? */
9784 *usedlen = 1;
9785 return NULL;
9786 }
9787#ifdef FEAT_EVAL
9788 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9789 (long)i);
9790 if (result == NULL)
9791 {
9792 *errormsg = (char_u *)"";
9793 return NULL;
9794 }
9795#else
9796 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 }
9800 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009801 {
9802 buf = buflist_findnr(i);
9803 if (buf == NULL)
9804 {
9805 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9806 return NULL;
9807 }
9808 if (lnump != NULL)
9809 *lnump = ECMD_LAST;
9810 if (buf->b_fname == NULL)
9811 {
9812 result = (char_u *)"";
9813 valid = 0; /* Must have ":p:h" to be valid */
9814 }
9815 else
9816 result = buf->b_fname;
9817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 break;
9819
9820#ifdef FEAT_SEARCHPATH
9821 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009822 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 if (result == NULL)
9824 {
9825 *errormsg = (char_u *)"";
9826 return NULL;
9827 }
9828 resultbuf = result; /* remember allocated string */
9829 break;
9830#endif
9831
9832#ifdef FEAT_AUTOCMD
9833 case SPEC_AFILE: /* file name for autocommand */
9834 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009835 if (result != NULL && !autocmd_fname_full)
9836 {
9837 /* Still need to turn the fname into a full path. It is
9838 * postponed to avoid a delay when <afile> is not used. */
9839 autocmd_fname_full = TRUE;
9840 result = FullName_save(autocmd_fname, FALSE);
9841 vim_free(autocmd_fname);
9842 autocmd_fname = result;
9843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 if (result == NULL)
9845 {
9846 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9847 return NULL;
9848 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009849 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 break;
9851
9852 case SPEC_ABUF: /* buffer number for autocommand */
9853 if (autocmd_bufnr <= 0)
9854 {
9855 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9856 return NULL;
9857 }
9858 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9859 result = strbuf;
9860 break;
9861
9862 case SPEC_AMATCH: /* match name for autocommand */
9863 result = autocmd_match;
9864 if (result == NULL)
9865 {
9866 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9867 return NULL;
9868 }
9869 break;
9870
9871#endif
9872 case SPEC_SFILE: /* file name for ":so" command */
9873 result = sourcing_name;
9874 if (result == NULL)
9875 {
9876 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9877 return NULL;
9878 }
9879 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009880 case SPEC_SLNUM: /* line in file for ":so" command */
9881 if (sourcing_name == NULL || sourcing_lnum == 0)
9882 {
9883 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9884 return NULL;
9885 }
9886 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9887 result = strbuf;
9888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889#if defined(FEAT_CLIENTSERVER)
9890 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009891 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9892 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 result = strbuf;
9894 break;
9895#endif
9896 }
9897
9898 resultlen = (int)STRLEN(result); /* length of new string */
9899 if (src[*usedlen] == '<') /* remove the file name extension */
9900 {
9901 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 resultlen = (int)(s - result);
9904 }
9905#ifdef FEAT_MODIFY_FNAME
9906 else if (!skip_mod)
9907 {
9908 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9909 &resultlen);
9910 if (result == NULL)
9911 {
9912 *errormsg = (char_u *)"";
9913 return NULL;
9914 }
9915 }
9916#endif
9917 }
9918
9919 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9920 {
9921 if (valid != VALID_HEAD + VALID_PATH)
9922 /* xgettext:no-c-format */
9923 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9924 else
9925 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9926 result = NULL;
9927 }
9928 else
9929 result = vim_strnsave(result, resultlen);
9930 vim_free(resultbuf);
9931 return result;
9932}
9933
9934/*
9935 * Concatenate all files in the argument list, separated by spaces, and return
9936 * it in one allocated string.
9937 * Spaces and backslashes in the file names are escaped with a backslash.
9938 * Returns NULL when out of memory.
9939 */
9940 static char_u *
9941arg_all()
9942{
9943 int len;
9944 int idx;
9945 char_u *retval = NULL;
9946 char_u *p;
9947
9948 /*
9949 * Do this loop two times:
9950 * first time: compute the total length
9951 * second time: concatenate the names
9952 */
9953 for (;;)
9954 {
9955 len = 0;
9956 for (idx = 0; idx < ARGCOUNT; ++idx)
9957 {
9958 p = alist_name(&ARGLIST[idx]);
9959 if (p != NULL)
9960 {
9961 if (len > 0)
9962 {
9963 /* insert a space in between names */
9964 if (retval != NULL)
9965 retval[len] = ' ';
9966 ++len;
9967 }
9968 for ( ; *p != NUL; ++p)
9969 {
9970 if (*p == ' ' || *p == '\\')
9971 {
9972 /* insert a backslash */
9973 if (retval != NULL)
9974 retval[len] = '\\';
9975 ++len;
9976 }
9977 if (retval != NULL)
9978 retval[len] = *p;
9979 ++len;
9980 }
9981 }
9982 }
9983
9984 /* second time: break here */
9985 if (retval != NULL)
9986 {
9987 retval[len] = NUL;
9988 break;
9989 }
9990
9991 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009992 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 if (retval == NULL)
9994 break;
9995 }
9996
9997 return retval;
9998}
9999
10000#if defined(FEAT_AUTOCMD) || defined(PROTO)
10001/*
10002 * Expand the <sfile> string in "arg".
10003 *
10004 * Returns an allocated string, or NULL for any error.
10005 */
10006 char_u *
10007expand_sfile(arg)
10008 char_u *arg;
10009{
10010 char_u *errormsg;
10011 int len;
10012 char_u *result;
10013 char_u *newres;
10014 char_u *repl;
10015 int srclen;
10016 char_u *p;
10017
10018 result = vim_strsave(arg);
10019 if (result == NULL)
10020 return NULL;
10021
10022 for (p = result; *p; )
10023 {
10024 if (STRNCMP(p, "<sfile>", 7) != 0)
10025 ++p;
10026 else
10027 {
10028 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010029 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 if (errormsg != NULL)
10031 {
10032 if (*errormsg)
10033 emsg(errormsg);
10034 vim_free(result);
10035 return NULL;
10036 }
10037 if (repl == NULL) /* no match (cannot happen) */
10038 {
10039 p += srclen;
10040 continue;
10041 }
10042 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10043 newres = alloc(len);
10044 if (newres == NULL)
10045 {
10046 vim_free(repl);
10047 vim_free(result);
10048 return NULL;
10049 }
10050 mch_memmove(newres, result, (size_t)(p - result));
10051 STRCPY(newres + (p - result), repl);
10052 len = (int)STRLEN(newres);
10053 STRCAT(newres, p + srclen);
10054 vim_free(repl);
10055 vim_free(result);
10056 result = newres;
10057 p = newres + len; /* continue after the match */
10058 }
10059 }
10060
10061 return result;
10062}
10063#endif
10064
10065#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010066static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10067 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10069static frame_T *ses_skipframe __ARGS((frame_T *fr));
10070static int ses_do_frame __ARGS((frame_T *fr));
10071static int ses_do_win __ARGS((win_T *wp));
10072static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10073static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10074static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10075
10076/*
10077 * Write openfile commands for the current buffers to an .exrc file.
10078 * Return FAIL on error, OK otherwise.
10079 */
10080 static int
10081makeopens(fd, dirnow)
10082 FILE *fd;
10083 char_u *dirnow; /* Current directory name */
10084{
10085 buf_T *buf;
10086 int only_save_windows = TRUE;
10087 int nr;
10088 int cnr = 1;
10089 int restore_size = TRUE;
10090 win_T *wp;
10091 char_u *sname;
10092 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010093 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010094 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010095 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010096 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010097 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
10099 if (ssop_flags & SSOP_BUFFERS)
10100 only_save_windows = FALSE; /* Save ALL buffers */
10101
10102 /*
10103 * Begin by setting the this_session variable, and then other
10104 * sessionable variables.
10105 */
10106#ifdef FEAT_EVAL
10107 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10108 return FAIL;
10109 if (ssop_flags & SSOP_GLOBALS)
10110 if (store_session_globals(fd) == FAIL)
10111 return FAIL;
10112#endif
10113
10114 /*
10115 * Close all windows but one.
10116 */
10117 if (put_line(fd, "silent only") == FAIL)
10118 return FAIL;
10119
10120 /*
10121 * Now a :cd command to the session directory or the current directory
10122 */
10123 if (ssop_flags & SSOP_SESDIR)
10124 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010125 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10126 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 return FAIL;
10128 }
10129 else if (ssop_flags & SSOP_CURDIR)
10130 {
10131 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10132 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010133 || fputs("cd ", fd) < 0
10134 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10135 || put_eol(fd) == FAIL)
10136 {
10137 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 vim_free(sname);
10141 }
10142
10143 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010144 * If there is an empty, unnamed buffer we will wipe it out later.
10145 * Remember the buffer number.
10146 */
10147 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10148 return FAIL;
10149 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10150 return FAIL;
10151 if (put_line(fd, "endif") == FAIL)
10152 return FAIL;
10153
10154 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 * Now save the current files, current buffer first.
10156 */
10157 if (put_line(fd, "set shortmess=aoO") == FAIL)
10158 return FAIL;
10159
10160 /* Now put the other buffers into the buffer list */
10161 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10162 {
10163 if (!(only_save_windows && buf->b_nwindows == 0)
10164 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10165 && buf->b_fname != NULL
10166 && buf->b_p_bl)
10167 {
10168 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10169 : buf->b_wininfo->wi_fpos.lnum) < 0
10170 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10171 return FAIL;
10172 }
10173 }
10174
10175 /* the global argument list */
10176 if (ses_arglist(fd, "args", &global_alist.al_ga,
10177 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10178 return FAIL;
10179
10180 if (ssop_flags & SSOP_RESIZE)
10181 {
10182 /* Note: after the restore we still check it worked!*/
10183 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10184 || put_eol(fd) == FAIL)
10185 return FAIL;
10186 }
10187
10188#ifdef FEAT_GUI
10189 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10190 {
10191 int x, y;
10192
10193 if (gui_mch_get_winpos(&x, &y) == OK)
10194 {
10195 /* Note: after the restore we still check it worked!*/
10196 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10197 return FAIL;
10198 }
10199 }
10200#endif
10201
10202 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010203 * May repeat putting Windows for each tab, when "tabpages" is in
10204 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010205 * Don't use goto_tabpage(), it may change directory and trigger
10206 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010208 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010209 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010210 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010212 int need_tabnew = FALSE;
10213
Bram Moolenaar18144c82006-04-12 21:52:12 +000010214 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010216 tabpage_T *tp = find_tabpage(tabnr);
10217
10218 if (tp == NULL)
10219 break; /* done all tab pages */
10220 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010221 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010222 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010223 tab_topframe = topframe;
10224 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010225 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010226 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010227 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010228 tab_topframe = tp->tp_topframe;
10229 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010230 if (tabnr > 1)
10231 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
Bram Moolenaar18144c82006-04-12 21:52:12 +000010234 /*
10235 * Before creating the window layout, try loading one file. If this
10236 * is aborted we don't end up with a number of useless windows.
10237 * This may have side effects! (e.g., compressed or network file).
10238 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010239 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010240 {
10241 if (ses_do_win(wp)
10242 && wp->w_buffer->b_ffname != NULL
10243 && !wp->w_buffer->b_help
10244#ifdef FEAT_QUICKFIX
10245 && !bt_nofile(wp->w_buffer)
10246#endif
10247 )
10248 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010249 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010250 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10251 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010252 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010253 if (!wp->w_arg_idx_invalid)
10254 edited_win = wp;
10255 break;
10256 }
10257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010259 /* If no file got edited create an empty tab page. */
10260 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10261 return FAIL;
10262
Bram Moolenaar18144c82006-04-12 21:52:12 +000010263 /*
10264 * Save current window layout.
10265 */
10266 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010268 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010270 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10271 return FAIL;
10272 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274
Bram Moolenaar18144c82006-04-12 21:52:12 +000010275 /*
10276 * Check if window sizes can be restored (no windows omitted).
10277 * Remember the window number of the current window after restoring.
10278 */
10279 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010280 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010281 {
10282 if (ses_do_win(wp))
10283 ++nr;
10284 else
10285 restore_size = FALSE;
10286 if (curwin == wp)
10287 cnr = nr;
10288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289
Bram Moolenaar18144c82006-04-12 21:52:12 +000010290 /* Go to the first window. */
10291 if (put_line(fd, "wincmd t") == FAIL)
10292 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010293
Bram Moolenaar18144c82006-04-12 21:52:12 +000010294 /*
10295 * If more than one window, see if sizes can be restored.
10296 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10297 * resized when moving between windows.
10298 * Do this before restoring the view, so that the topline and the
10299 * cursor can be set. This is done again below.
10300 */
10301 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10302 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010303 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305
Bram Moolenaar18144c82006-04-12 21:52:12 +000010306 /*
10307 * Restore the view of the window (options, file, cursor, etc.).
10308 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010309 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010310 {
10311 if (!ses_do_win(wp))
10312 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010313 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10314 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010315 return FAIL;
10316 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10317 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010318 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010319 }
10320
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010321 /* The argument index in the first tab page is zero, need to set it in
10322 * each window. For further tab pages it's the window where we do
10323 * "tabedit". */
10324 cur_arg_idx = next_arg_idx;
10325
Bram Moolenaar18144c82006-04-12 21:52:12 +000010326 /*
10327 * Restore cursor to the current window if it's not the first one.
10328 */
10329 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10330 || put_eol(fd) == FAIL))
10331 return FAIL;
10332
10333 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010334 * Restore window sizes again after jumping around in windows, because
10335 * the current window has a minimum size while others may not.
10336 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010337 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010338 return FAIL;
10339
Bram Moolenaar18144c82006-04-12 21:52:12 +000010340 /* Don't continue in another tab page when doing only the current one
10341 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010342 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010343 break;
10344 }
10345
10346 if (ssop_flags & SSOP_TABPAGES)
10347 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010348 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10349 || put_eol(fd) == FAIL)
10350 return FAIL;
10351 }
10352
Bram Moolenaar9c102382006-05-03 21:26:49 +000010353 /*
10354 * Wipe out an empty unnamed buffer we started in.
10355 */
10356 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10357 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010358 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010359 return FAIL;
10360 if (put_line(fd, "endif") == FAIL)
10361 return FAIL;
10362 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10363 return FAIL;
10364
10365 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10366 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10367 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369
10370 /*
10371 * Lastly, execute the x.vim file if it exists.
10372 */
10373 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10374 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010375 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 || put_line(fd, "endif") == FAIL)
10377 return FAIL;
10378
10379 return OK;
10380}
10381
10382 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010383ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 FILE *fd;
10385 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010386 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
10388 int n = 0;
10389 win_T *wp;
10390
10391 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10392 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010393 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 {
10395 if (!ses_do_win(wp))
10396 continue;
10397 ++n;
10398
10399 /* restore height when not full height */
10400 if (wp->w_height + wp->w_status_height < topframe->fr_height
10401 && (fprintf(fd,
10402 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10403 n, (long)wp->w_height, Rows / 2, Rows) < 0
10404 || put_eol(fd) == FAIL))
10405 return FAIL;
10406
10407 /* restore width when not full width */
10408 if (wp->w_width < Columns && (fprintf(fd,
10409 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10410 n, (long)wp->w_width, Columns / 2, Columns) < 0
10411 || put_eol(fd) == FAIL))
10412 return FAIL;
10413 }
10414 }
10415 else
10416 {
10417 /* Just equalise window sizes */
10418 if (put_line(fd, "wincmd =") == FAIL)
10419 return FAIL;
10420 }
10421 return OK;
10422}
10423
10424/*
10425 * Write commands to "fd" to recursively create windows for frame "fr",
10426 * horizontally and vertically split.
10427 * After the commands the last window in the frame is the current window.
10428 * Returns FAIL when writing the commands to "fd" fails.
10429 */
10430 static int
10431ses_win_rec(fd, fr)
10432 FILE *fd;
10433 frame_T *fr;
10434{
10435 frame_T *frc;
10436 int count = 0;
10437
10438 if (fr->fr_layout != FR_LEAF)
10439 {
10440 /* Find first frame that's not skipped and then create a window for
10441 * each following one (first frame is already there). */
10442 frc = ses_skipframe(fr->fr_child);
10443 if (frc != NULL)
10444 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10445 {
10446 /* Make window as big as possible so that we have lots of room
10447 * to split. */
10448 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10449 || put_line(fd, fr->fr_layout == FR_COL
10450 ? "split" : "vsplit") == FAIL)
10451 return FAIL;
10452 ++count;
10453 }
10454
10455 /* Go back to the first window. */
10456 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10457 ? "%dwincmd k" : "%dwincmd h", count) < 0
10458 || put_eol(fd) == FAIL))
10459 return FAIL;
10460
10461 /* Recursively create frames/windows in each window of this column or
10462 * row. */
10463 frc = ses_skipframe(fr->fr_child);
10464 while (frc != NULL)
10465 {
10466 ses_win_rec(fd, frc);
10467 frc = ses_skipframe(frc->fr_next);
10468 /* Go to next window. */
10469 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10470 return FAIL;
10471 }
10472 }
10473 return OK;
10474}
10475
10476/*
10477 * Skip frames that don't contain windows we want to save in the Session.
10478 * Returns NULL when there none.
10479 */
10480 static frame_T *
10481ses_skipframe(fr)
10482 frame_T *fr;
10483{
10484 frame_T *frc;
10485
10486 for (frc = fr; frc != NULL; frc = frc->fr_next)
10487 if (ses_do_frame(frc))
10488 break;
10489 return frc;
10490}
10491
10492/*
10493 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10494 * the Session.
10495 */
10496 static int
10497ses_do_frame(fr)
10498 frame_T *fr;
10499{
10500 frame_T *frc;
10501
10502 if (fr->fr_layout == FR_LEAF)
10503 return ses_do_win(fr->fr_win);
10504 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10505 if (ses_do_frame(frc))
10506 return TRUE;
10507 return FALSE;
10508}
10509
10510/*
10511 * Return non-zero if window "wp" is to be stored in the Session.
10512 */
10513 static int
10514ses_do_win(wp)
10515 win_T *wp;
10516{
10517 if (wp->w_buffer->b_fname == NULL
10518#ifdef FEAT_QUICKFIX
10519 /* When 'buftype' is "nofile" can't restore the window contents. */
10520 || bt_nofile(wp->w_buffer)
10521#endif
10522 )
10523 return (ssop_flags & SSOP_BLANK);
10524 if (wp->w_buffer->b_help)
10525 return (ssop_flags & SSOP_HELP);
10526 return TRUE;
10527}
10528
10529/*
10530 * Write commands to "fd" to restore the view of a window.
10531 * Caller must make sure 'scrolloff' is zero.
10532 */
10533 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010534put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 FILE *fd;
10536 win_T *wp;
10537 int add_edit; /* add ":edit" command to view */
10538 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010539 int current_arg_idx; /* current argument index of the window, use
10540 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541{
10542 win_T *save_curwin;
10543 int f;
10544 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010545 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546
10547 /* Always restore cursor position for ":mksession". For ":mkview" only
10548 * when 'viewoptions' contains "cursor". */
10549 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10550
10551 /*
10552 * Local argument list.
10553 */
10554 if (wp->w_alist == &global_alist)
10555 {
10556 if (put_line(fd, "argglobal") == FAIL)
10557 return FAIL;
10558 }
10559 else
10560 {
10561 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10562 flagp == &vop_flags
10563 || !(*flagp & SSOP_CURDIR)
10564 || wp->w_localdir != NULL, flagp) == FAIL)
10565 return FAIL;
10566 }
10567
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010568 /* Only when part of a session: restore the argument index. Some
10569 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010570 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010571 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010573 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 || put_eol(fd) == FAIL)
10575 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010576 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 }
10578
10579 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010580 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 {
10582 /*
10583 * Load the file.
10584 */
10585 if (wp->w_buffer->b_ffname != NULL
10586#ifdef FEAT_QUICKFIX
10587 && !bt_nofile(wp->w_buffer)
10588#endif
10589 )
10590 {
10591 /*
10592 * Editing a file in this buffer: use ":edit file".
10593 * This may have side effects! (e.g., compressed or network file).
10594 */
10595 if (fputs("edit ", fd) < 0
10596 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10597 return FAIL;
10598 }
10599 else
10600 {
10601 /* No file in this buffer, just make it empty. */
10602 if (put_line(fd, "enew") == FAIL)
10603 return FAIL;
10604#ifdef FEAT_QUICKFIX
10605 if (wp->w_buffer->b_ffname != NULL)
10606 {
10607 /* The buffer does have a name, but it's not a file name. */
10608 if (fputs("file ", fd) < 0
10609 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10610 return FAIL;
10611 }
10612#endif
10613 do_cursor = FALSE;
10614 }
10615 }
10616
10617 /*
10618 * Local mappings and abbreviations.
10619 */
10620 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10621 && makemap(fd, wp->w_buffer) == FAIL)
10622 return FAIL;
10623
10624 /*
10625 * Local options. Need to go to the window temporarily.
10626 * Store only local values when using ":mkview" and when ":mksession" is
10627 * used and 'sessionoptions' doesn't include "options".
10628 * Some folding options are always stored when "folds" is included,
10629 * otherwise the folds would not be restored correctly.
10630 */
10631 save_curwin = curwin;
10632 curwin = wp;
10633 curbuf = curwin->w_buffer;
10634 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10635 f = makeset(fd, OPT_LOCAL,
10636 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10637#ifdef FEAT_FOLDING
10638 else if (*flagp & SSOP_FOLDS)
10639 f = makefoldset(fd);
10640#endif
10641 else
10642 f = OK;
10643 curwin = save_curwin;
10644 curbuf = curwin->w_buffer;
10645 if (f == FAIL)
10646 return FAIL;
10647
10648#ifdef FEAT_FOLDING
10649 /*
10650 * Save Folds when 'buftype' is empty and for help files.
10651 */
10652 if ((*flagp & SSOP_FOLDS)
10653 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010654# ifdef FEAT_QUICKFIX
10655 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10656# endif
10657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 {
10659 if (put_folds(fd, wp) == FAIL)
10660 return FAIL;
10661 }
10662#endif
10663
10664 /*
10665 * Set the cursor after creating folds, since that moves the cursor.
10666 */
10667 if (do_cursor)
10668 {
10669
10670 /* Restore the cursor line in the file and relatively in the
10671 * window. Don't use "G", it changes the jumplist. */
10672 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10673 (long)wp->w_cursor.lnum,
10674 (long)(wp->w_cursor.lnum - wp->w_topline),
10675 (long)wp->w_height / 2, (long)wp->w_height) < 0
10676 || put_eol(fd) == FAIL
10677 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10678 || put_line(fd, "exe s:l") == FAIL
10679 || put_line(fd, "normal! zt") == FAIL
10680 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10681 || put_eol(fd) == FAIL)
10682 return FAIL;
10683 /* Restore the cursor column and left offset when not wrapping. */
10684 if (wp->w_cursor.col == 0)
10685 {
10686 if (put_line(fd, "normal! 0") == FAIL)
10687 return FAIL;
10688 }
10689 else
10690 {
10691 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10692 {
10693 if (fprintf(fd,
10694 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10695 (long)wp->w_cursor.col,
10696 (long)(wp->w_cursor.col - wp->w_leftcol),
10697 (long)wp->w_width / 2, (long)wp->w_width) < 0
10698 || put_eol(fd) == FAIL
10699 || put_line(fd, "if s:c > 0") == FAIL
10700 || fprintf(fd,
10701 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10702 (long)wp->w_cursor.col) < 0
10703 || put_eol(fd) == FAIL
10704 || put_line(fd, "else") == FAIL
10705 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10706 || put_eol(fd) == FAIL
10707 || put_line(fd, "endif") == FAIL)
10708 return FAIL;
10709 }
10710 else
10711 {
10712 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10713 || put_eol(fd) == FAIL)
10714 return FAIL;
10715 }
10716 }
10717 }
10718
10719 /*
10720 * Local directory.
10721 */
10722 if (wp->w_localdir != NULL)
10723 {
10724 if (fputs("lcd ", fd) < 0
10725 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10726 || put_eol(fd) == FAIL)
10727 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010728 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 }
10730
10731 return OK;
10732}
10733
10734/*
10735 * Write an argument list to the session file.
10736 * Returns FAIL if writing fails.
10737 */
10738 static int
10739ses_arglist(fd, cmd, gap, fullname, flagp)
10740 FILE *fd;
10741 char *cmd;
10742 garray_T *gap;
10743 int fullname; /* TRUE: use full path name */
10744 unsigned *flagp;
10745{
10746 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010747 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 char_u *s;
10749
10750 if (gap->ga_len == 0)
10751 return put_line(fd, "silent! argdel *");
10752 if (fputs(cmd, fd) < 0)
10753 return FAIL;
10754 for (i = 0; i < gap->ga_len; ++i)
10755 {
10756 /* NULL file names are skipped (only happens when out of memory). */
10757 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10758 if (s != NULL)
10759 {
10760 if (fullname)
10761 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010762 buf = alloc(MAXPATHL);
10763 if (buf != NULL)
10764 {
10765 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10766 s = buf;
10767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 }
10769 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010770 {
10771 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010773 }
10774 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 }
10776 }
10777 return put_eol(fd);
10778}
10779
10780/*
10781 * Write a buffer name to the session file.
10782 * Also ends the line.
10783 * Returns FAIL if writing fails.
10784 */
10785 static int
10786ses_fname(fd, buf, flagp)
10787 FILE *fd;
10788 buf_T *buf;
10789 unsigned *flagp;
10790{
10791 char_u *name;
10792
10793 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010794 * the session file will be sourced.
10795 * Don't do this for ":mkview", we don't know the current directory.
10796 * Don't do this after ":lcd", we don't keep track of what the current
10797 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (buf->b_sfname != NULL
10799 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010800 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010801#ifdef FEAT_AUTOCHDIR
10802 && !p_acd
10803#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010804 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 name = buf->b_sfname;
10806 else
10807 name = buf->b_ffname;
10808 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10809 return FAIL;
10810 return OK;
10811}
10812
10813/*
10814 * Write a file name to the session file.
10815 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10816 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010817 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 */
10819 static int
10820ses_put_fname(fd, name, flagp)
10821 FILE *fd;
10822 char_u *name;
10823 unsigned *flagp;
10824{
10825 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010826 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828
10829 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010830 if (sname == NULL)
10831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010833 if (*flagp & SSOP_SLASH)
10834 {
10835 /* change all backslashes to forward slashes */
10836 for (p = sname; *p != NUL; mb_ptr_adv(p))
10837 if (*p == '\\')
10838 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010840
10841 /* escapse special characters */
10842 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010844 if (p == NULL)
10845 return FAIL;
10846
10847 /* write the result */
10848 if (fputs((char *)p, fd) < 0)
10849 retval = FAIL;
10850
10851 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 return retval;
10853}
10854
10855/*
10856 * ":loadview [nr]"
10857 */
10858 static void
10859ex_loadview(eap)
10860 exarg_T *eap;
10861{
10862 char_u *fname;
10863
10864 fname = get_view_file(*eap->arg);
10865 if (fname != NULL)
10866 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010867 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 vim_free(fname);
10869 }
10870}
10871
10872/*
10873 * Get the name of the view file for the current buffer.
10874 */
10875 static char_u *
10876get_view_file(c)
10877 int c;
10878{
10879 int len = 0;
10880 char_u *p, *s;
10881 char_u *retval;
10882 char_u *sname;
10883
10884 if (curbuf->b_ffname == NULL)
10885 {
10886 EMSG(_(e_noname));
10887 return NULL;
10888 }
10889 sname = home_replace_save(NULL, curbuf->b_ffname);
10890 if (sname == NULL)
10891 return NULL;
10892
10893 /*
10894 * We want a file name without separators, because we're not going to make
10895 * a directory.
10896 * "normal" path separator -> "=+"
10897 * "=" -> "=="
10898 * ":" path separator -> "=-"
10899 */
10900 for (p = sname; *p; ++p)
10901 if (*p == '=' || vim_ispathsep(*p))
10902 ++len;
10903 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10904 if (retval != NULL)
10905 {
10906 STRCPY(retval, p_vdir);
10907 add_pathsep(retval);
10908 s = retval + STRLEN(retval);
10909 for (p = sname; *p; ++p)
10910 {
10911 if (*p == '=')
10912 {
10913 *s++ = '=';
10914 *s++ = '=';
10915 }
10916 else if (vim_ispathsep(*p))
10917 {
10918 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010919#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 if (*p == ':')
10921 *s++ = '-';
10922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010924 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 }
10926 else
10927 *s++ = *p;
10928 }
10929 *s++ = '=';
10930 *s++ = c;
10931 STRCPY(s, ".vim");
10932 }
10933
10934 vim_free(sname);
10935 return retval;
10936}
10937
10938#endif /* FEAT_SESSION */
10939
10940/*
10941 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10942 * Return FAIL for a write error.
10943 */
10944 int
10945put_eol(fd)
10946 FILE *fd;
10947{
10948 if (
10949#ifdef USE_CRNL
10950 (
10951# ifdef MKSESSION_NL
10952 !mksession_nl &&
10953# endif
10954 (putc('\r', fd) < 0)) ||
10955#endif
10956 (putc('\n', fd) < 0))
10957 return FAIL;
10958 return OK;
10959}
10960
10961/*
10962 * Write a line to "fd".
10963 * Return FAIL for a write error.
10964 */
10965 int
10966put_line(fd, s)
10967 FILE *fd;
10968 char *s;
10969{
10970 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10971 return FAIL;
10972 return OK;
10973}
10974
10975#ifdef FEAT_VIMINFO
10976/*
10977 * ":rviminfo" and ":wviminfo".
10978 */
10979 static void
10980ex_viminfo(eap)
10981 exarg_T *eap;
10982{
10983 char_u *save_viminfo;
10984
10985 save_viminfo = p_viminfo;
10986 if (*p_viminfo == NUL)
10987 p_viminfo = (char_u *)"'100";
10988 if (eap->cmdidx == CMD_rviminfo)
10989 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010990 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10991 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 EMSG(_("E195: Cannot open viminfo file for reading"));
10993 }
10994 else
10995 write_viminfo(eap->arg, eap->forceit);
10996 p_viminfo = save_viminfo;
10997}
10998#endif
10999
11000#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011001/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011002 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011003 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 void
11006dialog_msg(buff, format, fname)
11007 char_u *buff;
11008 char *format;
11009 char_u *fname;
11010{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 if (fname == NULL)
11012 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011013 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014}
11015#endif
11016
11017/*
11018 * ":behave {mswin,xterm}"
11019 */
11020 static void
11021ex_behave(eap)
11022 exarg_T *eap;
11023{
11024 if (STRCMP(eap->arg, "mswin") == 0)
11025 {
11026 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11027 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11028 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11029 set_option_value((char_u *)"keymodel", 0L,
11030 (char_u *)"startsel,stopsel", 0);
11031 }
11032 else if (STRCMP(eap->arg, "xterm") == 0)
11033 {
11034 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11035 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11036 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11037 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11038 }
11039 else
11040 EMSG2(_(e_invarg2), eap->arg);
11041}
11042
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011043#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11044/*
11045 * Function given to ExpandGeneric() to obtain the possible arguments of the
11046 * ":behave {mswin,xterm}" command.
11047 */
11048 char_u *
11049get_behave_arg(xp, idx)
11050 expand_T *xp UNUSED;
11051 int idx;
11052{
11053 if (idx == 0)
11054 return (char_u *)"mswin";
11055 if (idx == 1)
11056 return (char_u *)"xterm";
11057 return NULL;
11058}
11059#endif
11060
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061#ifdef FEAT_AUTOCMD
11062static int filetype_detect = FALSE;
11063static int filetype_plugin = FALSE;
11064static int filetype_indent = FALSE;
11065
11066/*
11067 * ":filetype [plugin] [indent] {on,off,detect}"
11068 * on: Load the filetype.vim file to install autocommands for file types.
11069 * off: Load the ftoff.vim file to remove all autocommands for file types.
11070 * plugin on: load filetype.vim and ftplugin.vim
11071 * plugin off: load ftplugof.vim
11072 * indent on: load filetype.vim and indent.vim
11073 * indent off: load indoff.vim
11074 */
11075 static void
11076ex_filetype(eap)
11077 exarg_T *eap;
11078{
11079 char_u *arg = eap->arg;
11080 int plugin = FALSE;
11081 int indent = FALSE;
11082
11083 if (*eap->arg == NUL)
11084 {
11085 /* Print current status. */
11086 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11087 filetype_detect ? "ON" : "OFF",
11088 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11089 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11090 return;
11091 }
11092
11093 /* Accept "plugin" and "indent" in any order. */
11094 for (;;)
11095 {
11096 if (STRNCMP(arg, "plugin", 6) == 0)
11097 {
11098 plugin = TRUE;
11099 arg = skipwhite(arg + 6);
11100 continue;
11101 }
11102 if (STRNCMP(arg, "indent", 6) == 0)
11103 {
11104 indent = TRUE;
11105 arg = skipwhite(arg + 6);
11106 continue;
11107 }
11108 break;
11109 }
11110 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11111 {
11112 if (*arg == 'o' || !filetype_detect)
11113 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011114 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 filetype_detect = TRUE;
11116 if (plugin)
11117 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011118 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 filetype_plugin = TRUE;
11120 }
11121 if (indent)
11122 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011123 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 filetype_indent = TRUE;
11125 }
11126 }
11127 if (*arg == 'd')
11128 {
11129 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011130 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 }
11132 }
11133 else if (STRCMP(arg, "off") == 0)
11134 {
11135 if (plugin || indent)
11136 {
11137 if (plugin)
11138 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011139 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 filetype_plugin = FALSE;
11141 }
11142 if (indent)
11143 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011144 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 filetype_indent = FALSE;
11146 }
11147 }
11148 else
11149 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011150 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 filetype_detect = FALSE;
11152 }
11153 }
11154 else
11155 EMSG2(_(e_invarg2), arg);
11156}
11157
11158/*
11159 * ":setfiletype {name}"
11160 */
11161 static void
11162ex_setfiletype(eap)
11163 exarg_T *eap;
11164{
11165 if (!did_filetype)
11166 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11167}
11168#endif
11169
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 static void
11171ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011172 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173{
11174#ifdef FEAT_DIGRAPHS
11175 if (*eap->arg != NUL)
11176 putdigraph(eap->arg);
11177 else
11178 listdigraphs();
11179#else
11180 EMSG(_("E196: No digraphs in this version"));
11181#endif
11182}
11183
11184 static void
11185ex_set(eap)
11186 exarg_T *eap;
11187{
11188 int flags = 0;
11189
11190 if (eap->cmdidx == CMD_setlocal)
11191 flags = OPT_LOCAL;
11192 else if (eap->cmdidx == CMD_setglobal)
11193 flags = OPT_GLOBAL;
11194#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11195 if (cmdmod.browse && flags == 0)
11196 ex_options(eap);
11197 else
11198#endif
11199 (void)do_set(eap->arg, flags);
11200}
11201
11202#ifdef FEAT_SEARCH_EXTRA
11203/*
11204 * ":nohlsearch"
11205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 static void
11207ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011208 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209{
11210 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011211 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212}
11213
11214/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011215 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 * Sets nextcmd to the start of the next command, if any. Also called when
11217 * skipping commands to find the next command.
11218 */
11219 static void
11220ex_match(eap)
11221 exarg_T *eap;
11222{
11223 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011224 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 char_u *end;
11226 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011227 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011228
11229 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011230 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011231 else
11232 {
11233 EMSG(e_invcmd);
11234 return;
11235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236
11237 /* First clear any old pattern. */
11238 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011239 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240
11241 if (ends_excmd(*eap->arg))
11242 end = eap->arg;
11243 else if ((STRNICMP(eap->arg, "none", 4) == 0
11244 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11245 end = eap->arg + 4;
11246 else
11247 {
11248 p = skiptowhite(eap->arg);
11249 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011250 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 p = skipwhite(p);
11252 if (*p == NUL)
11253 {
11254 /* There must be two arguments. */
11255 EMSG2(_(e_invarg2), eap->arg);
11256 return;
11257 }
11258 end = skip_regexp(p + 1, *p, TRUE, NULL);
11259 if (!eap->skip)
11260 {
11261 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11262 {
11263 eap->errmsg = e_trailing;
11264 return;
11265 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011266 if (*end != *p)
11267 {
11268 EMSG2(_(e_invarg2), p);
11269 return;
11270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271
11272 c = *end;
11273 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011274 match_add(curwin, g, p + 1, 10, id);
11275 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011276 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 }
11278 }
11279 eap->nextcmd = find_nextcmd(end);
11280}
11281#endif
11282
11283#ifdef FEAT_CRYPT
11284/*
11285 * ":X": Get crypt key
11286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 static void
11288ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011289 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011291 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011292 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293}
11294#endif
11295
11296#ifdef FEAT_FOLDING
11297 static void
11298ex_fold(eap)
11299 exarg_T *eap;
11300{
11301 if (foldManualAllowed(TRUE))
11302 foldCreate(eap->line1, eap->line2);
11303}
11304
11305 static void
11306ex_foldopen(eap)
11307 exarg_T *eap;
11308{
11309 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11310 eap->forceit, FALSE);
11311}
11312
11313 static void
11314ex_folddo(eap)
11315 exarg_T *eap;
11316{
11317 linenr_T lnum;
11318
11319 /* First set the marks for all lines closed/open. */
11320 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11321 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11322 ml_setmarked(lnum);
11323
11324 /* Execute the command on the marked lines. */
11325 global_exe(eap->arg);
11326 ml_clearmarked(); /* clear rest of the marks */
11327}
11328#endif