blob: ead520a22190c6034577e465857b060f74c6798e [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
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392static char_u *arg_all __ARGS((void));
393#ifdef FEAT_SESSION
394static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000395static 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 +0000396static void ex_loadview __ARGS((exarg_T *eap));
397static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
406static void ex_viminfo __ARGS((exarg_T *eap));
407#else
408# define ex_viminfo ex_ni
409#endif
410static void ex_behave __ARGS((exarg_T *eap));
411#ifdef FEAT_AUTOCMD
412static void ex_filetype __ARGS((exarg_T *eap));
413static void ex_setfiletype __ARGS((exarg_T *eap));
414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
426static void ex_digraphs __ARGS((exarg_T *eap));
427static void ex_set __ARGS((exarg_T *eap));
428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
432static void ex_nohlsearch __ARGS((exarg_T *eap));
433static void ex_match __ARGS((exarg_T *eap));
434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
439static void ex_X __ARGS((exarg_T *eap));
440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
444static void ex_fold __ARGS((exarg_T *eap));
445static void ex_foldopen __ARGS((exarg_T *eap));
446static void ex_folddo __ARGS((exarg_T *eap));
447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
550 char_u *(*getline) __ARGS((int, void *, int));
551 void *cookie;
552};
553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
555static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
574static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
575static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576
577 static void
578save_dbg_stuff(dsp)
579 struct dbg_stuff *dsp;
580{
581 dsp->trylevel = trylevel; trylevel = 0;
582 dsp->force_abort = force_abort; force_abort = FALSE;
583 dsp->caught_stack = caught_stack; caught_stack = NULL;
584 dsp->vv_exception = v_exception(NULL);
585 dsp->vv_throwpoint = v_throwpoint(NULL);
586
587 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
588 dsp->did_emsg = did_emsg; did_emsg = FALSE;
589 dsp->got_int = got_int; got_int = FALSE;
590 dsp->did_throw = did_throw; did_throw = FALSE;
591 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
592 dsp->check_cstack = check_cstack; check_cstack = FALSE;
593 dsp->current_exception = current_exception; current_exception = NULL;
594}
595
596 static void
597restore_dbg_stuff(dsp)
598 struct dbg_stuff *dsp;
599{
600 suppress_errthrow = FALSE;
601 trylevel = dsp->trylevel;
602 force_abort = dsp->force_abort;
603 caught_stack = dsp->caught_stack;
604 (void)v_exception(dsp->vv_exception);
605 (void)v_throwpoint(dsp->vv_throwpoint);
606 did_emsg = dsp->did_emsg;
607 got_int = dsp->got_int;
608 did_throw = dsp->did_throw;
609 need_rethrow = dsp->need_rethrow;
610 check_cstack = dsp->check_cstack;
611 current_exception = dsp->current_exception;
612}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614
615
616/*
617 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
618 * command is given.
619 */
620 void
621do_exmode(improved)
622 int improved; /* TRUE for "improved Ex" mode */
623{
624 int save_msg_scroll;
625 int prev_msg_row;
626 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 int changedtick;
628
629 if (improved)
630 exmode_active = EXMODE_VIM;
631 else
632 exmode_active = EXMODE_NORMAL;
633 State = NORMAL;
634
635 /* When using ":global /pat/ visual" and then "Q" we return to continue
636 * the :global command. */
637 if (global_busy)
638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 save_msg_scroll = msg_scroll;
641 ++RedrawingDisabled; /* don't redisplay the window */
642 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_GUI
644 /* Ignore scrollbar and mouse events in Ex mode */
645 ++hold_gui_events;
646#endif
647#ifdef FEAT_SNIFF
648 want_sniff_request = 0; /* No K_SNIFF wanted */
649#endif
650
651 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
652 while (exmode_active)
653 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000654#ifdef FEAT_EX_EXTRA
655 /* Check for a ":normal" command and no more characters left. */
656 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
657 {
658 exmode_active = FALSE;
659 break;
660 }
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 msg_scroll = TRUE;
663 need_wait_return = FALSE;
664 ex_pressedreturn = FALSE;
665 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000666 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 prev_msg_row = msg_row;
668 prev_line = curwin->w_cursor.lnum;
669#ifdef FEAT_SNIFF
670 ProcessSniffRequests();
671#endif
672 if (improved)
673 {
674 cmdline_row = msg_row;
675 do_cmdline(NULL, getexline, NULL, 0);
676 }
677 else
678 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
679 lines_left = Rows - 1;
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if ((prev_line != curwin->w_cursor.lnum
682 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (curbuf->b_ml.ml_flags & ML_EMPTY)
685 EMSG(_(e_emptybuf));
686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (ex_pressedreturn)
689 {
690 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000691 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 msg_row = prev_msg_row;
693 if (prev_msg_row == Rows - 1)
694 msg_row--;
695 }
696 msg_col = 0;
697 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
698 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
702 {
703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
704 EMSG(_(e_emptybuf));
705 else
706 EMSG(_("E501: At end-of-file"));
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710#ifdef FEAT_GUI
711 --hold_gui_events;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 --RedrawingDisabled;
714 --no_wait_return;
715 update_screen(CLEAR);
716 need_wait_return = FALSE;
717 msg_scroll = save_msg_scroll;
718}
719
720/*
721 * Execute a simple command line. Used for translated commands like "*".
722 */
723 int
724do_cmdline_cmd(cmd)
725 char_u *cmd;
726{
727 return do_cmdline(cmd, NULL, NULL,
728 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
729}
730
731/*
732 * do_cmdline(): execute one Ex command line
733 *
734 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * 2. Split up in parts separated with '|'.
737 *
738 * This function can be called recursively!
739 *
740 * flags:
741 * DOCMD_VERBOSE - The command will be included in the error message.
742 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * DOCMD_KEYTYPED - Don't reset KeyTyped.
745 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
746 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
747 *
748 * return FAIL if cmdline could not be executed, OK otherwise
749 */
750 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 char_u *(*fgetline) __ARGS((int, void *, int));
754 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int flags;
756{
757 char_u *next_cmdline; /* next cmd to execute */
758 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 static int recursive = 0; /* recursive depth */
761 int msg_didout_before_start = 0;
762 int count = 0; /* line number count */
763 int did_inc = FALSE; /* incremented RedrawingDisabled */
764 int retval = OK;
765#ifdef FEAT_EVAL
766 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int current_line = 0; /* active line in lines_ga */
769 char_u *fname = NULL; /* function or script name */
770 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
771 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000772 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int initial_trylevel;
774 struct msglist **saved_msg_list = NULL;
775 struct msglist *private_msg_list;
776
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 char_u *(*cmd_getline) __ARGS((int, void *, int));
779 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# define cmd_cookie cookie
786#endif
787 static int call_depth = 0; /* recursiveness */
788
789#ifdef FEAT_EVAL
790 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
791 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000792 * This ensures that the do_errthrow() call in do_one_cmd() does not
793 * combine the messages stored by an earlier invocation of do_one_cmd()
794 * with the command name of the later one. This would happen when
795 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 saved_msg_list = msg_list;
797 msg_list = &private_msg_list;
798 private_msg_list = NULL;
799#endif
800
801 /* It's possible to create an endless loop with ":execute", catch that
802 * here. The value of 200 allows nested function calls, ":source", etc. */
803 if (call_depth == 200)
804 {
805 EMSG(_("E169: Command too recursive"));
806#ifdef FEAT_EVAL
807 /* When converting to an exception, we do not include the command name
808 * since this is not an error of the specific command. */
809 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
810 msg_list = saved_msg_list;
811#endif
812 return FAIL;
813 }
814 ++call_depth;
815
816#ifdef FEAT_EVAL
817 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 cstack.cs_trylevel = 0;
820 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000821 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
823
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++ex_nesting_level;
830
831 /* Get the function or script name and the address where the next breakpoint
832 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = func_name(real_cookie);
836 breakpoint = func_breakpoint(real_cookie);
837 dbg_tick = func_dbg_tick(real_cookie);
838 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 fname = sourcing_name;
842 breakpoint = source_breakpoint(real_cookie);
843 dbg_tick = source_dbg_tick(real_cookie);
844 }
845
846 /*
847 * Initialize "force_abort" and "suppress_errthrow" at the top level.
848 */
849 if (!recursive)
850 {
851 force_abort = FALSE;
852 suppress_errthrow = FALSE;
853 }
854
855 /*
856 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 * exception handling (used when debugging). Otherwise clear it to avoid
858 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000860 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000861 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200863 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 initial_trylevel = trylevel;
866
867 /*
868 * "did_throw" will be set to TRUE when an exception is being thrown.
869 */
870 did_throw = FALSE;
871#endif
872 /*
873 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * If force_abort is set, we cancel everything.
876 */
877 did_emsg = FALSE;
878
879 /*
880 * KeyTyped is only set when calling vgetc(). Reset it here when not
881 * calling vgetc() (sourced command lines).
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (!(flags & DOCMD_KEYTYPED)
884 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100972 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cmd_cookie = cookie;
1006 }
1007#endif
1008
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001145 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001166 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
1298 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001299 && (getline_equal(fgetline, cookie, getexmodeline)
1300 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 && (next_cmdline != NULL
1302#ifdef FEAT_EVAL
1303 || cstack.cs_idx >= 0
1304#endif
1305 || (flags & DOCMD_REPEAT)));
1306
1307 vim_free(cmdline_copy);
1308#ifdef FEAT_EVAL
1309 free_cmdlines(&lines_ga);
1310 ga_clear(&lines_ga);
1311
1312 if (cstack.cs_idx >= 0)
1313 {
1314 /*
1315 * If a sourced file or executed function ran to its end, report the
1316 * unclosed conditional.
1317 */
1318 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001319 && ((getline_equal(fgetline, cookie, getsourceline)
1320 && !source_finished(fgetline, cookie))
1321 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 && !func_has_ended(real_cookie))))
1323 {
1324 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1325 EMSG(_(e_endtry));
1326 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1327 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001328 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1329 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
1331 EMSG(_(e_endif));
1332 }
1333
1334 /*
1335 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1336 * ":endtry" in a sourced file or executed function. If the try
1337 * conditional is in its finally clause, ignore anything pending.
1338 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001339 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 */
1341 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001342 {
1343 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1344
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001345 if (idx >= 0)
1346 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1348 &cstack.cs_looplevel);
1349 }
1350 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 trylevel = initial_trylevel;
1352 }
1353
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001354 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1355 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001357 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 ? (char_u *)"endfunction" : (char_u *)NULL);
1359
1360 if (trylevel == 0)
1361 {
1362 /*
1363 * When an exception is being thrown out of the outermost try
1364 * conditional, discard the uncaught exception, disable the conversion
1365 * of interrupts or errors to exceptions, and ensure that no more
1366 * commands are executed.
1367 */
1368 if (did_throw)
1369 {
1370 void *p = NULL;
1371 char_u *saved_sourcing_name;
1372 int saved_sourcing_lnum;
1373 struct msglist *messages = NULL, *next;
1374
1375 /*
1376 * If the uncaught exception is a user exception, report it as an
1377 * error. If it is an error exception, display the saved error
1378 * message now. For an interrupt exception, do nothing; the
1379 * interrupt message is given elsewhere.
1380 */
1381 switch (current_exception->type)
1382 {
1383 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001384 vim_snprintf((char *)IObuff, IOSIZE,
1385 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 current_exception->value);
1387 p = vim_strsave(IObuff);
1388 break;
1389 case ET_ERROR:
1390 messages = current_exception->messages;
1391 current_exception->messages = NULL;
1392 break;
1393 case ET_INTERRUPT:
1394 break;
1395 default:
1396 p = vim_strsave((char_u *)_(e_internal));
1397 }
1398
1399 saved_sourcing_name = sourcing_name;
1400 saved_sourcing_lnum = sourcing_lnum;
1401 sourcing_name = current_exception->throw_name;
1402 sourcing_lnum = current_exception->throw_lnum;
1403 current_exception->throw_name = NULL;
1404
1405 discard_current_exception(); /* uses IObuff if 'verbose' */
1406 suppress_errthrow = TRUE;
1407 force_abort = TRUE;
1408
1409 if (messages != NULL)
1410 {
1411 do
1412 {
1413 next = messages->next;
1414 emsg(messages->msg);
1415 vim_free(messages->msg);
1416 vim_free(messages);
1417 messages = next;
1418 }
1419 while (messages != NULL);
1420 }
1421 else if (p != NULL)
1422 {
1423 emsg(p);
1424 vim_free(p);
1425 }
1426 vim_free(sourcing_name);
1427 sourcing_name = saved_sourcing_name;
1428 sourcing_lnum = saved_sourcing_lnum;
1429 }
1430
1431 /*
1432 * On an interrupt or an aborting error not converted to an exception,
1433 * disable the conversion of errors to exceptions. (Interrupts are not
1434 * converted any more, here.) This enables also the interrupt message
1435 * when force_abort is set and did_emsg unset in case of an interrupt
1436 * from a finally clause after an error.
1437 */
1438 else if (got_int || (did_emsg && force_abort))
1439 suppress_errthrow = TRUE;
1440 }
1441
1442 /*
1443 * The current cstack will be freed when do_cmdline() returns. An uncaught
1444 * exception will have to be rethrown in the previous cstack. If a function
1445 * has just returned or a script file was just finished and the previous
1446 * cstack belongs to the same function or, respectively, script file, it
1447 * will have to be checked for finally clauses to be executed due to the
1448 * ":return" or ":finish". This is done in do_one_cmd().
1449 */
1450 if (did_throw)
1451 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001452 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001454 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 && ex_nesting_level > func_level(real_cookie) + 1))
1456 {
1457 if (!did_throw)
1458 check_cstack = TRUE;
1459 }
1460 else
1461 {
1462 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 --ex_nesting_level;
1465 /*
1466 * Go to debug mode when returning from a function in which we are
1467 * single-stepping.
1468 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001469 if ((getline_equal(fgetline, cookie, getsourceline)
1470 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001472 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ? (char_u *)_("End of sourced file")
1474 : (char_u *)_("End of function"));
1475 }
1476
1477 /*
1478 * Restore the exception environment (done after returning from the
1479 * debugger).
1480 */
1481 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001482 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 msg_list = saved_msg_list;
1485#endif /* FEAT_EVAL */
1486
1487 /*
1488 * If there was too much output to fit on the command line, ask the user to
1489 * hit return before redrawing the screen. With the ":global" command we do
1490 * this only once after the command is finished.
1491 */
1492 if (did_inc)
1493 {
1494 --RedrawingDisabled;
1495 --no_wait_return;
1496 msg_scroll = FALSE;
1497
1498 /*
1499 * When just finished an ":if"-":else" which was typed, no need to
1500 * wait for hit-return. Also for an error situation.
1501 */
1502 if (retval == FAIL
1503#ifdef FEAT_EVAL
1504 || (did_endif && KeyTyped && !did_emsg)
1505#endif
1506 )
1507 {
1508 need_wait_return = FALSE;
1509 msg_didany = FALSE; /* don't wait when restarting edit */
1510 }
1511 else if (need_wait_return)
1512 {
1513 /*
1514 * The msg_start() above clears msg_didout. The wait_return we do
1515 * here should not overwrite the command that may be shown before
1516 * doing that.
1517 */
1518 msg_didout |= msg_didout_before_start;
1519 wait_return(FALSE);
1520 }
1521 }
1522
1523#ifndef FEAT_EVAL
1524 /*
1525 * Reset if_level, in case a sourced script file contains more ":if" than
1526 * ":endif" (could be ":if x | foo | endif").
1527 */
1528 if_level = 0;
1529#endif
1530
1531 --call_depth;
1532 return retval;
1533}
1534
1535#ifdef FEAT_EVAL
1536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 */
1539 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int c;
1542 void *cookie;
1543 int indent;
1544{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wcmd_T *wp;
1547 char_u *line;
1548
1549 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1550 {
1551 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (cp->getline == NULL)
1556 line = getcmdline(c, 0L, indent);
1557 else
1558 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001559 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 ++cp->current_line;
1561
1562 return line;
1563 }
1564
1565 KeyTyped = FALSE;
1566 ++cp->current_line;
1567 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1568 sourcing_lnum = wp->lnum;
1569 return vim_strsave(wp->line);
1570}
1571
1572/*
1573 * Store a line in "gap" so that a ":while" loop can execute it again.
1574 */
1575 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 garray_T *gap;
1578 char_u *line;
1579{
1580 if (ga_grow(gap, 1) == FAIL)
1581 return FAIL;
1582 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1584 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return OK;
1586}
1587
1588/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 static void
1592free_cmdlines(gap)
1593 garray_T *gap;
1594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608getline_equal(fgetline, cookie, func)
1609 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001610 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 char_u *(*func) __ARGS((int, void *, int));
1612{
1613#ifdef FEAT_EVAL
1614 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001640 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
1644 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
1664/*
1665 * Execute one Ex command.
1666 *
1667 * If 'sourcing' is TRUE, the command will be included in the error message.
1668 *
1669 * 1. skip comment lines and leading space
1670 * 2. handle command modifiers
1671 * 3. parse range
1672 * 4. parse command
1673 * 5. parse arguments
1674 * 6. switch on command name
1675 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001676 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 *
1678 * This function may be called recursively!
1679 */
1680#if (_MSC_VER == 1200)
1681/*
Bram Moolenaared203462004-06-16 11:19:22 +00001682 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001684 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 static char_u *
1687do_one_cmd(cmdlinep, sourcing,
1688#ifdef FEAT_EVAL
1689 cstack,
1690#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 char_u **cmdlinep;
1693 int sourcing;
1694#ifdef FEAT_EVAL
1695 struct condstack *cstack;
1696#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001697 char_u *(*fgetline) __ARGS((int, void *, int));
1698 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char_u *errormsg = NULL; /* error message */
1704 exarg_T ea; /* Ex command arguments */
1705 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001706 int save_msg_scroll = msg_scroll;
1707 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001709#ifdef HAVE_SANDBOX
1710 int did_sandbox = FALSE;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 cmdmod_T save_cmdmod;
1713 int ni; /* set when Not Implemented */
1714
1715 vim_memset(&ea, 0, sizeof(ea));
1716 ea.line1 = 1;
1717 ea.line2 = 1;
1718#ifdef FEAT_EVAL
1719 ++ex_nesting_level;
1720#endif
1721
1722 /* when not editing the last file :q has to be typed twice */
1723 if (quitmore
1724#ifdef FEAT_EVAL
1725 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001726 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 )
1729 --quitmore;
1730
1731 /*
1732 * Reset browse, confirm, etc.. They are restored when returning, for
1733 * recursive calls.
1734 */
1735 save_cmdmod = cmdmod;
1736 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1737
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001738 /* "#!anything" is handled like a comment. */
1739 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1740 goto doend;
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * Repeat until no more command modifiers are found.
1744 */
1745 ea.cmd = *cmdlinep;
1746 for (;;)
1747 {
1748/*
1749 * 1. skip comment lines and leading white space and colons
1750 */
1751 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1752 ++ea.cmd;
1753
1754 /* in ex mode, an empty line works like :+ */
1755 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && (getline_equal(fgetline, cookie, getexmodeline)
1757 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 ea.cmd = (char_u *)"+";
1761 ex_pressedreturn = TRUE;
1762 }
1763
1764 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001765 if (*ea.cmd == '"')
1766 goto doend;
1767 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001768 {
1769 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773/*
1774 * 2. handle command modifiers.
1775 */
1776 p = ea.cmd;
1777 if (VIM_ISDIGIT(*ea.cmd))
1778 p = skipwhite(skipdigits(ea.cmd));
1779 switch (*p)
1780 {
1781 /* When adding an entry, also modify cmd_exists(). */
1782 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1783 break;
1784#ifdef FEAT_WINDOWS
1785 cmdmod.split |= WSP_ABOVE;
1786#endif
1787 continue;
1788
1789 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1790 {
1791#ifdef FEAT_WINDOWS
1792 cmdmod.split |= WSP_BELOW;
1793#endif
1794 continue;
1795 }
1796 if (checkforcmd(&ea.cmd, "browse", 3))
1797 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001798#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 cmdmod.browse = TRUE;
1800#endif
1801 continue;
1802 }
1803 if (!checkforcmd(&ea.cmd, "botright", 2))
1804 break;
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BOT;
1807#endif
1808 continue;
1809
1810 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1811 break;
1812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1813 cmdmod.confirm = TRUE;
1814#endif
1815 continue;
1816
1817 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1818 {
1819 cmdmod.keepmarks = TRUE;
1820 continue;
1821 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001822 if (checkforcmd(&ea.cmd, "keepalt", 5))
1823 {
1824 cmdmod.keepalt = TRUE;
1825 continue;
1826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1828 break;
1829 cmdmod.keepjumps = TRUE;
1830 continue;
1831
1832 /* ":hide" and ":hide | cmd" are not modifiers */
1833 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1834 || *p == NUL || ends_excmd(*p))
1835 break;
1836 ea.cmd = p;
1837 cmdmod.hide = TRUE;
1838 continue;
1839
1840 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1841 {
1842 cmdmod.lockmarks = TRUE;
1843 continue;
1844 }
1845
1846 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1847 break;
1848#ifdef FEAT_WINDOWS
1849 cmdmod.split |= WSP_ABOVE;
1850#endif
1851 continue;
1852
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001853 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1854 break;
1855#ifdef FEAT_AUTOCMD
1856 if (cmdmod.save_ei == NULL)
1857 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001858 /* Set 'eventignore' to "all". Restore the
1859 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001860 cmdmod.save_ei = vim_strsave(p_ei);
1861 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001862 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001863 }
1864#endif
1865 continue;
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1868 break;
1869#ifdef FEAT_WINDOWS
1870 cmdmod.split |= WSP_BELOW;
1871#endif
1872 continue;
1873
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001874 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1875 {
1876#ifdef HAVE_SANDBOX
1877 if (!did_sandbox)
1878 ++sandbox;
1879 did_sandbox = TRUE;
1880#endif
1881 continue;
1882 }
1883 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001885 if (save_msg_silent == -1)
1886 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1889 {
1890 /* ":silent!", but not "silent !cmd" */
1891 ea.cmd = skipwhite(ea.cmd + 1);
1892 ++emsg_silent;
1893 ++did_esilent;
1894 }
1895 continue;
1896
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001897 case 't': if (checkforcmd(&p, "tab", 3))
1898 {
1899#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001900 if (vim_isdigit(*ea.cmd))
1901 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1902 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001903 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001904 ea.cmd = p;
1905#endif
1906 continue;
1907 }
1908 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 break;
1910#ifdef FEAT_WINDOWS
1911 cmdmod.split |= WSP_TOP;
1912#endif
1913 continue;
1914
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001915 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1916 break;
1917 if (save_msg_silent == -1)
1918 save_msg_silent = msg_silent;
1919 msg_silent = 0;
1920 continue;
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 cmdmod.split |= WSP_VERT;
1926#endif
1927 continue;
1928 }
1929 if (!checkforcmd(&p, "verbose", 4))
1930 break;
1931 if (verbose_save < 0)
1932 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001933 if (vim_isdigit(*ea.cmd))
1934 p_verbose = atoi((char *)ea.cmd);
1935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 p_verbose = 1;
1937 ea.cmd = p;
1938 continue;
1939 }
1940 break;
1941 }
1942
1943#ifdef FEAT_EVAL
1944 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1945 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1946#else
1947 ea.skip = (if_level > 0);
1948#endif
1949
1950#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001951# ifdef FEAT_PROFILE
1952 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001953 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001955 if (getline_equal(fgetline, cookie, get_func_line))
1956 func_line_exec(getline_cookie(fgetline, cookie));
1957 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001958 script_line_exec();
1959 }
1960#endif
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 /* May go to debug mode. If this happens and the ">quit" debug command is
1963 * used, throw an interrupt exception and skip the next command. */
1964 dbg_check_breakpoint(&ea);
1965 if (!ea.skip && got_int)
1966 {
1967 ea.skip = TRUE;
1968 (void)do_intthrow(cstack);
1969 }
1970#endif
1971
1972/*
1973 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1974 *
1975 * where 'addr' is:
1976 *
1977 * % (entire file)
1978 * $ [+-NUM]
1979 * 'x [+-NUM] (where x denotes a currently defined mark)
1980 * . [+-NUM]
1981 * [+-NUM]..
1982 * NUM
1983 *
1984 * The ea.cmd pointer is updated to point to the first character following the
1985 * range spec. If an initial address is found, but no second, the upper bound
1986 * is equal to the lower.
1987 */
1988
1989 /* repeat for all ',' or ';' separated addresses */
1990 for (;;)
1991 {
1992 ea.line1 = ea.line2;
1993 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1994 ea.cmd = skipwhite(ea.cmd);
1995 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1996 if (ea.cmd == NULL) /* error detected */
1997 goto doend;
1998 if (lnum == MAXLNUM)
1999 {
2000 if (*ea.cmd == '%') /* '%' - all lines */
2001 {
2002 ++ea.cmd;
2003 ea.line1 = 1;
2004 ea.line2 = curbuf->b_ml.ml_line_count;
2005 ++ea.addr_count;
2006 }
2007 /* '*' - visual area */
2008 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2009 {
2010 pos_T *fp;
2011
2012 ++ea.cmd;
2013 if (!ea.skip)
2014 {
2015 fp = getmark('<', FALSE);
2016 if (check_mark(fp) == FAIL)
2017 goto doend;
2018 ea.line1 = fp->lnum;
2019 fp = getmark('>', FALSE);
2020 if (check_mark(fp) == FAIL)
2021 goto doend;
2022 ea.line2 = fp->lnum;
2023 ++ea.addr_count;
2024 }
2025 }
2026 }
2027 else
2028 ea.line2 = lnum;
2029 ea.addr_count++;
2030
2031 if (*ea.cmd == ';')
2032 {
2033 if (!ea.skip)
2034 curwin->w_cursor.lnum = ea.line2;
2035 }
2036 else if (*ea.cmd != ',')
2037 break;
2038 ++ea.cmd;
2039 }
2040
2041 /* One address given: set start and end lines */
2042 if (ea.addr_count == 1)
2043 {
2044 ea.line1 = ea.line2;
2045 /* ... but only implicit: really no address given */
2046 if (lnum == MAXLNUM)
2047 ea.addr_count = 0;
2048 }
2049
2050 /* Don't leave the cursor on an illegal line (caused by ';') */
2051 check_cursor_lnum();
2052
2053/*
2054 * 4. parse command
2055 */
2056
2057 /*
2058 * Skip ':' and any white space
2059 */
2060 ea.cmd = skipwhite(ea.cmd);
2061 while (*ea.cmd == ':')
2062 ea.cmd = skipwhite(ea.cmd + 1);
2063
2064 /*
2065 * If we got a line, but no command, then go to the line.
2066 * If we find a '|' or '\n' we set ea.nextcmd.
2067 */
2068 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2069 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2070 {
2071 /*
2072 * strange vi behaviour:
2073 * ":3" jumps to line 3
2074 * ":3|..." prints line 3
2075 * ":|" prints current line
2076 */
2077 if (ea.skip) /* skip this if inside :if */
2078 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002079 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 ea.cmdidx = CMD_print;
2082 ea.argt = RANGE+COUNT+TRLBAR;
2083 if ((errormsg = invalid_range(&ea)) == NULL)
2084 {
2085 correct_range(&ea);
2086 ex_print(&ea);
2087 }
2088 }
2089 else if (ea.addr_count != 0)
2090 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002091 if (ea.line2 > curbuf->b_ml.ml_line_count)
2092 {
2093 /* With '-' in 'cpoptions' a line number past the file is an
2094 * error, otherwise put it at the end of the file. */
2095 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2096 ea.line2 = -1;
2097 else
2098 ea.line2 = curbuf->b_ml.ml_line_count;
2099 }
2100
2101 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002102 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 {
2105 if (ea.line2 == 0)
2106 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 curwin->w_cursor.lnum = ea.line2;
2109 beginline(BL_SOL | BL_FIX);
2110 }
2111 }
2112 goto doend;
2113 }
2114
2115 /* Find the command and let "p" point to after it. */
2116 p = find_command(&ea, NULL);
2117
2118#ifdef FEAT_USR_CMDS
2119 if (p == NULL)
2120 {
2121 if (!ea.skip)
2122 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2123 goto doend;
2124 }
2125 /* Check for wrong commands. */
2126 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2127 {
2128 errormsg = uc_fun_cmd();
2129 goto doend;
2130 }
2131#endif
2132 if (ea.cmdidx == CMD_SIZE)
2133 {
2134 if (!ea.skip)
2135 {
2136 STRCPY(IObuff, _("E492: Not an editor command"));
2137 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002138 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 errormsg = IObuff;
2140 }
2141 goto doend;
2142 }
2143
2144 ni = (
2145#ifdef FEAT_USR_CMDS
2146 !USER_CMDIDX(ea.cmdidx) &&
2147#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002148 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002149#ifdef HAVE_EX_SCRIPT_NI
2150 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2151#endif
2152 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154#ifndef FEAT_EVAL
2155 /*
2156 * When the expression evaluation is disabled, recognize the ":if" and
2157 * ":endif" commands and ignore everything in between it.
2158 */
2159 if (ea.cmdidx == CMD_if)
2160 ++if_level;
2161 if (if_level)
2162 {
2163 if (ea.cmdidx == CMD_endif)
2164 --if_level;
2165 goto doend;
2166 }
2167
2168#endif
2169
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002170 /* forced commands */
2171 if (*p == '!' && ea.cmdidx != CMD_substitute
2172 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174 ++p;
2175 ea.forceit = TRUE;
2176 }
2177 else
2178 ea.forceit = FALSE;
2179
2180/*
2181 * 5. parse arguments
2182 */
2183#ifdef FEAT_USR_CMDS
2184 if (!USER_CMDIDX(ea.cmdidx))
2185#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 if (!ea.skip)
2189 {
2190#ifdef HAVE_SANDBOX
2191 if (sandbox != 0 && !(ea.argt & SBOXOK))
2192 {
2193 /* Command not allowed in sandbox. */
2194 errormsg = (char_u *)_(e_sandbox);
2195 goto doend;
2196 }
2197#endif
2198 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2199 {
2200 /* Command not allowed in non-'modifiable' buffer */
2201 errormsg = (char_u *)_(e_modifiable);
2202 goto doend;
2203 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002204
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002205 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206# ifdef FEAT_USR_CMDS
2207 && !USER_CMDIDX(ea.cmdidx)
2208# endif
2209 )
2210 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002211 /* Command not allowed when editing the command line. */
2212#ifdef FEAT_CMDWIN
2213 if (cmdwin_type != 0)
2214 errormsg = (char_u *)_(e_cmdwin);
2215 else
2216#endif
2217 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 goto doend;
2219 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002221 /* Disallow editing another buffer when "curbuf_lock" is set.
2222 * Do allow ":edit" (check for argument later).
2223 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002225 && ea.cmdidx != CMD_edit
2226 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002227# ifdef FEAT_USR_CMDS
2228 && !USER_CMDIDX(ea.cmdidx)
2229# endif
2230 && curbuf_locked())
2231 goto doend;
2232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2235 {
2236 /* no range allowed */
2237 errormsg = (char_u *)_(e_norange);
2238 goto doend;
2239 }
2240 }
2241
2242 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2243 {
2244 errormsg = (char_u *)_(e_nobang);
2245 goto doend;
2246 }
2247
2248 /*
2249 * Don't complain about the range if it is not used
2250 * (could happen if line_count is accidentally set to 0).
2251 */
2252 if (!ea.skip && !ni)
2253 {
2254 /*
2255 * If the range is backwards, ask for confirmation and, if given, swap
2256 * ea.line1 & ea.line2 so it's forwards again.
2257 * When global command is busy, don't ask, will fail below.
2258 */
2259 if (!global_busy && ea.line1 > ea.line2)
2260 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002261 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002263 if (sourcing || exmode_active)
2264 {
2265 errormsg = (char_u *)_("E493: Backwards range given");
2266 goto doend;
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (ask_yesno((char_u *)
2269 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002270 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 lnum = ea.line1;
2273 ea.line1 = ea.line2;
2274 ea.line2 = lnum;
2275 }
2276 if ((errormsg = invalid_range(&ea)) != NULL)
2277 goto doend;
2278 }
2279
2280 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2281 ea.line2 = 1;
2282
2283 correct_range(&ea);
2284
2285#ifdef FEAT_FOLDING
2286 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2287 {
2288 /* Put the first line at the start of a closed fold, put the last line
2289 * at the end of a closed fold. */
2290 (void)hasFolding(ea.line1, &ea.line1, NULL);
2291 (void)hasFolding(ea.line2, NULL, &ea.line2);
2292 }
2293#endif
2294
2295#ifdef FEAT_QUICKFIX
2296 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002297 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * option here, so things like % get expanded.
2299 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002300 p = replace_makeprg(&ea, p, cmdlinep);
2301 if (p == NULL)
2302 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304
2305 /*
2306 * Skip to start of argument.
2307 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2308 */
2309 if (ea.cmdidx == CMD_bang)
2310 ea.arg = p;
2311 else
2312 ea.arg = skipwhite(p);
2313
2314 /*
2315 * Check for "++opt=val" argument.
2316 * Must be first, allow ":w ++enc=utf8 !cmd"
2317 */
2318 if (ea.argt & ARGOPT)
2319 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2320 if (getargopt(&ea) == FAIL && !ni)
2321 {
2322 errormsg = (char_u *)_(e_invarg);
2323 goto doend;
2324 }
2325
2326 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2327 {
2328 if (*ea.arg == '>') /* append */
2329 {
2330 if (*++ea.arg != '>') /* typed wrong */
2331 {
2332 errormsg = (char_u *)_("E494: Use w or w>>");
2333 goto doend;
2334 }
2335 ea.arg = skipwhite(ea.arg + 1);
2336 ea.append = TRUE;
2337 }
2338 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2339 {
2340 ++ea.arg;
2341 ea.usefilter = TRUE;
2342 }
2343 }
2344
2345 if (ea.cmdidx == CMD_read)
2346 {
2347 if (ea.forceit)
2348 {
2349 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2350 ea.forceit = FALSE;
2351 }
2352 else if (*ea.arg == '!') /* :r !filter */
2353 {
2354 ++ea.arg;
2355 ea.usefilter = TRUE;
2356 }
2357 }
2358
2359 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2360 {
2361 ea.amount = 1;
2362 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2363 {
2364 ++ea.arg;
2365 ++ea.amount;
2366 }
2367 ea.arg = skipwhite(ea.arg);
2368 }
2369
2370 /*
2371 * Check for "+command" argument, before checking for next command.
2372 * Don't do this for ":read !cmd" and ":write !cmd".
2373 */
2374 if ((ea.argt & EDITCMD) && !ea.usefilter)
2375 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2376
2377 /*
2378 * Check for '|' to separate commands and '"' to start comments.
2379 * Don't do this for ":read !cmd" and ":write !cmd".
2380 */
2381 if ((ea.argt & TRLBAR) && !ea.usefilter)
2382 separate_nextcmd(&ea);
2383
2384 /*
2385 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002386 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2387 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389 else if (ea.cmdidx == CMD_bang
2390 || ea.cmdidx == CMD_global
2391 || ea.cmdidx == CMD_vglobal
2392 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 for (p = ea.arg; *p; ++p)
2395 {
2396 /* Remove one backslash before a newline, so that it's possible to
2397 * pass a newline to the shell and also a newline that is preceded
2398 * with a backslash. This makes it impossible to end a shell
2399 * command in a backslash, but that doesn't appear useful.
2400 * Halving the number of backslashes is incompatible with previous
2401 * versions. */
2402 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002403 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 else if (*p == '\n')
2405 {
2406 ea.nextcmd = p + 1;
2407 *p = NUL;
2408 break;
2409 }
2410 }
2411 }
2412
2413 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2414 {
2415 ea.line1 = 1;
2416 ea.line2 = curbuf->b_ml.ml_line_count;
2417 }
2418
2419 /* accept numbered register only when no count allowed (:put) */
2420 if ( (ea.argt & REGSTR)
2421 && *ea.arg != NUL
2422#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 /* Do not allow register = for user commands */
2424 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#endif
2426 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2427 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002428#ifndef FEAT_CLIPBOARD
2429 /* check these explicitly for a more specific error message */
2430 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002432 errormsg = (char_u *)_(e_invalidreg);
2433 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002436 if (
2437#ifdef FEAT_USR_CMDS
2438 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2439 && USER_CMDIDX(ea.cmdidx)))
2440#else
2441 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2442#endif
2443 )
2444 {
2445 ea.regname = *ea.arg++;
2446#ifdef FEAT_EVAL
2447 /* for '=' register: accept the rest of the line as an expression */
2448 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2449 {
2450 set_expr_line(vim_strsave(ea.arg));
2451 ea.arg += STRLEN(ea.arg);
2452 }
2453#endif
2454 ea.arg = skipwhite(ea.arg);
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457
2458 /*
2459 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2460 * count, it's a buffer name.
2461 */
2462 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2463 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2464 || vim_iswhite(*p)))
2465 {
2466 n = getdigits(&ea.arg);
2467 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002468 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 errormsg = (char_u *)_(e_zerocount);
2471 goto doend;
2472 }
2473 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2474 {
2475 ea.line2 = n;
2476 if (ea.addr_count == 0)
2477 ea.addr_count = 1;
2478 }
2479 else
2480 {
2481 ea.line1 = ea.line2;
2482 ea.line2 += n - 1;
2483 ++ea.addr_count;
2484 /*
2485 * Be vi compatible: no error message for out of range.
2486 */
2487 if (ea.line2 > curbuf->b_ml.ml_line_count)
2488 ea.line2 = curbuf->b_ml.ml_line_count;
2489 }
2490 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002491
2492 /*
2493 * Check for flags: 'l', 'p' and '#'.
2494 */
2495 if (ea.argt & EXFLAGS)
2496 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002498 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002499 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 errormsg = (char_u *)_(e_trailing);
2502 goto doend;
2503 }
2504
2505 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2506 {
2507 errormsg = (char_u *)_(e_argreq);
2508 goto doend;
2509 }
2510
2511#ifdef FEAT_EVAL
2512 /*
2513 * Skip the command when it's not going to be executed.
2514 * The commands like :if, :endif, etc. always need to be executed.
2515 * Also make an exception for commands that handle a trailing command
2516 * themselves.
2517 */
2518 if (ea.skip)
2519 {
2520 switch (ea.cmdidx)
2521 {
2522 /* commands that need evaluation */
2523 case CMD_while:
2524 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002525 case CMD_for:
2526 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 case CMD_if:
2528 case CMD_elseif:
2529 case CMD_else:
2530 case CMD_endif:
2531 case CMD_try:
2532 case CMD_catch:
2533 case CMD_finally:
2534 case CMD_endtry:
2535 case CMD_function:
2536 break;
2537
2538 /* Commands that handle '|' themselves. Check: A command should
2539 * either have the TRLBAR flag, appear in this list or appear in
2540 * the list at ":help :bar". */
2541 case CMD_aboveleft:
2542 case CMD_and:
2543 case CMD_belowright:
2544 case CMD_botright:
2545 case CMD_browse:
2546 case CMD_call:
2547 case CMD_confirm:
2548 case CMD_delfunction:
2549 case CMD_djump:
2550 case CMD_dlist:
2551 case CMD_dsearch:
2552 case CMD_dsplit:
2553 case CMD_echo:
2554 case CMD_echoerr:
2555 case CMD_echomsg:
2556 case CMD_echon:
2557 case CMD_execute:
2558 case CMD_help:
2559 case CMD_hide:
2560 case CMD_ijump:
2561 case CMD_ilist:
2562 case CMD_isearch:
2563 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002564 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 case CMD_keepjumps:
2566 case CMD_keepmarks:
2567 case CMD_leftabove:
2568 case CMD_let:
2569 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002570 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 case CMD_perl:
2574 case CMD_psearch:
2575 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002576 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002577 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 case CMD_return:
2579 case CMD_rightbelow:
2580 case CMD_ruby:
2581 case CMD_silent:
2582 case CMD_smagic:
2583 case CMD_snomagic:
2584 case CMD_substitute:
2585 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002586 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 case CMD_tcl:
2588 case CMD_throw:
2589 case CMD_tilde:
2590 case CMD_topleft:
2591 case CMD_unlet:
2592 case CMD_verbose:
2593 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002594 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
2596
2597 default: goto doend;
2598 }
2599 }
2600#endif
2601
2602 if (ea.argt & XFILE)
2603 {
2604 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2605 goto doend;
2606 }
2607
2608#ifdef FEAT_LISTCMDS
2609 /*
2610 * Accept buffer name. Cannot be used at the same time with a buffer
2611 * number. Don't do this for a user command.
2612 */
2613 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2614# ifdef FEAT_USR_CMDS
2615 && !USER_CMDIDX(ea.cmdidx)
2616# endif
2617 )
2618 {
2619 /*
2620 * :bdelete, :bwipeout and :bunload take several arguments, separated
2621 * by spaces: find next space (skipping over escaped characters).
2622 * The others take one argument: ignore trailing spaces.
2623 */
2624 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2625 || ea.cmdidx == CMD_bunload)
2626 p = skiptowhite_esc(ea.arg);
2627 else
2628 {
2629 p = ea.arg + STRLEN(ea.arg);
2630 while (p > ea.arg && vim_iswhite(p[-1]))
2631 --p;
2632 }
2633 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2634 if (ea.line2 < 0) /* failed */
2635 goto doend;
2636 ea.addr_count = 1;
2637 ea.arg = skipwhite(p);
2638 }
2639#endif
2640
2641/*
2642 * 6. switch on command name
2643 *
2644 * The "ea" structure holds the arguments that can be used.
2645 */
2646 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002647 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 ea.cookie = cookie;
2649#ifdef FEAT_EVAL
2650 ea.cstack = cstack;
2651#endif
2652
2653#ifdef FEAT_USR_CMDS
2654 if (USER_CMDIDX(ea.cmdidx))
2655 {
2656 /*
2657 * Execute a user-defined command.
2658 */
2659 do_ucmd(&ea);
2660 }
2661 else
2662#endif
2663 {
2664 /*
2665 * Call the function to execute the command.
2666 */
2667 ea.errmsg = NULL;
2668 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2669 if (ea.errmsg != NULL)
2670 errormsg = (char_u *)_(ea.errmsg);
2671 }
2672
2673#ifdef FEAT_EVAL
2674 /*
2675 * If the command just executed called do_cmdline(), any throw or ":return"
2676 * or ":finish" encountered there must also check the cstack of the still
2677 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2678 * exception, or reanimate a returned function or finished script file and
2679 * return or finish it again.
2680 */
2681 if (need_rethrow)
2682 do_throw(cstack);
2683 else if (check_cstack)
2684 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002685 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 && current_func_returned())
2689 do_return(&ea, TRUE, FALSE, NULL);
2690 }
2691 need_rethrow = check_cstack = FALSE;
2692#endif
2693
2694doend:
2695 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2696 curwin->w_cursor.lnum = 1;
2697
2698 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2699 {
2700 if (sourcing)
2701 {
2702 if (errormsg != IObuff)
2703 {
2704 STRCPY(IObuff, errormsg);
2705 errormsg = IObuff;
2706 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002707 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 emsg(errormsg);
2710 }
2711#ifdef FEAT_EVAL
2712 do_errthrow(cstack,
2713 (ea.cmdidx != CMD_SIZE
2714# ifdef FEAT_USR_CMDS
2715 && !USER_CMDIDX(ea.cmdidx)
2716# endif
2717 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2718#endif
2719
2720 if (verbose_save >= 0)
2721 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002722#ifdef FEAT_AUTOCMD
2723 if (cmdmod.save_ei != NULL)
2724 {
2725 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002726 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2727 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002728 free_string_option(cmdmod.save_ei);
2729 }
2730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 cmdmod = save_cmdmod;
2733
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002734 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
2736 /* messages could be enabled for a serious error, need to check if the
2737 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002738 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002739 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 emsg_silent -= did_esilent;
2741 if (emsg_silent < 0)
2742 emsg_silent = 0;
2743 /* Restore msg_scroll, it's set by file I/O commands, even when no
2744 * message is actually displayed. */
2745 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002746
2747 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2748 * somewhere in the line. Put it back in the first column. */
2749 if (redirecting())
2750 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002753#ifdef HAVE_SANDBOX
2754 if (did_sandbox)
2755 --sandbox;
2756#endif
2757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2759 ea.nextcmd = NULL;
2760
2761#ifdef FEAT_EVAL
2762 --ex_nesting_level;
2763#endif
2764
2765 return ea.nextcmd;
2766}
2767#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002768 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#endif
2770
2771/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002772 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 * If there is a match advance "pp" to the argument and return TRUE.
2774 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002775 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002777 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 char *cmd; /* name of command */
2779 int len; /* required length */
2780{
2781 int i;
2782
2783 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002784 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 break;
2786 if (i >= len && !isalpha((*pp)[i]))
2787 {
2788 *pp = skipwhite(*pp + i);
2789 return TRUE;
2790 }
2791 return FALSE;
2792}
2793
2794/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002795 * Append "cmd" to the error message in IObuff.
2796 * Takes care of limiting the length and handling 0xa0, which would be
2797 * invisible otherwise.
2798 */
2799 static void
2800append_command(cmd)
2801 char_u *cmd;
2802{
2803 char_u *s = cmd;
2804 char_u *d;
2805
2806 STRCAT(IObuff, ": ");
2807 d = IObuff + STRLEN(IObuff);
2808 while (*s != NUL && d - IObuff < IOSIZE - 7)
2809 {
2810 if (
2811#ifdef FEAT_MBYTE
2812 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2813#endif
2814 *s == 0xa0)
2815 {
2816 s +=
2817#ifdef FEAT_MBYTE
2818 enc_utf8 ? 2 :
2819#endif
2820 1;
2821 STRCPY(d, "<a0>");
2822 d += 4;
2823 }
2824 else
2825 MB_COPY_CHAR(s, d);
2826 }
2827 *d = NUL;
2828}
2829
2830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002832 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002834 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 * Returns NULL for an ambiguous user command.
2836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 static char_u *
2838find_command(eap, full)
2839 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002840 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
2842 int len;
2843 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002844 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
2846 /*
2847 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002848 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * - the 'k' command can directly be followed by any character.
2850 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2851 * but :sre[wind] is another command, as are :scrip[tnames],
2852 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002853 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 */
2855 p = eap->cmd;
2856 if (*p == 'k')
2857 {
2858 eap->cmdidx = CMD_k;
2859 ++p;
2860 }
2861 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002862 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2863 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 || p[1] == 'g'
2865 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2866 || p[1] == 'I'
2867 || (p[1] == 'r' && p[2] != 'e')))
2868 {
2869 eap->cmdidx = CMD_substitute;
2870 ++p;
2871 }
2872 else
2873 {
2874 while (ASCII_ISALPHA(*p))
2875 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002876 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002877 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002878 while (ASCII_ISALNUM(*p))
2879 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 /* check for non-alpha command */
2882 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2883 ++p;
2884 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002885 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2886 {
2887 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2888 * :delete with the 'l' flag. Same for 'p'. */
2889 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002890 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002891 break;
2892 if (i == len - 1)
2893 {
2894 --len;
2895 if (p[-1] == 'l')
2896 eap->flags |= EXFLAG_LIST;
2897 else
2898 eap->flags |= EXFLAG_PRINT;
2899 }
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
2902 if (ASCII_ISLOWER(*eap->cmd))
2903 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2904 else
2905 eap->cmdidx = cmdidxs[26];
2906
2907 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2908 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2909 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2910 (size_t)len) == 0)
2911 {
2912#ifdef FEAT_EVAL
2913 if (full != NULL
2914 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2915 *full = TRUE;
2916#endif
2917 break;
2918 }
2919
2920#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002921 /* Look for a user defined command as a last resort. Let ":Print" be
2922 * overruled by a user defined command. */
2923 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2924 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002926 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 while (ASCII_ISALNUM(*p))
2928 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002929 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002932 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 eap->cmdidx = CMD_SIZE;
2934 }
2935
2936 return p;
2937}
2938
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002939#ifdef FEAT_USR_CMDS
2940/*
2941 * Search for a user command that matches "eap->cmd".
2942 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2943 * Return a pointer to just after the command.
2944 * Return NULL if there is no matching command.
2945 */
2946 static char_u *
2947find_ucmd(eap, p, full, xp, compl)
2948 exarg_T *eap;
2949 char_u *p; /* end of the command (possibly including count) */
2950 int *full; /* set to TRUE for a full match */
2951 expand_T *xp; /* used for completion, NULL otherwise */
2952 int *compl; /* completion flags or NULL */
2953{
2954 int len = (int)(p - eap->cmd);
2955 int j, k, matchlen = 0;
2956 ucmd_T *uc;
2957 int found = FALSE;
2958 int possible = FALSE;
2959 char_u *cp, *np; /* Point into typed cmd and test name */
2960 garray_T *gap;
2961 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2962 only full match global is accepted. */
2963
2964 /*
2965 * Look for buffer-local user commands first, then global ones.
2966 */
2967 gap = &curbuf->b_ucmds;
2968 for (;;)
2969 {
2970 for (j = 0; j < gap->ga_len; ++j)
2971 {
2972 uc = USER_CMD_GA(gap, j);
2973 cp = eap->cmd;
2974 np = uc->uc_name;
2975 k = 0;
2976 while (k < len && *np != NUL && *cp++ == *np++)
2977 k++;
2978 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2979 {
2980 /* If finding a second match, the command is ambiguous. But
2981 * not if a buffer-local command wasn't a full match and a
2982 * global command is a full match. */
2983 if (k == len && found && *np != NUL)
2984 {
2985 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002986 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002987 amb_local = TRUE;
2988 }
2989
2990 if (!found || (k == len && *np == NUL))
2991 {
2992 /* If we matched up to a digit, then there could
2993 * be another command including the digit that we
2994 * should use instead.
2995 */
2996 if (k == len)
2997 found = TRUE;
2998 else
2999 possible = TRUE;
3000
3001 if (gap == &ucmds)
3002 eap->cmdidx = CMD_USER;
3003 else
3004 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003005 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003006 eap->useridx = j;
3007
3008# ifdef FEAT_CMDL_COMPL
3009 if (compl != NULL)
3010 *compl = uc->uc_compl;
3011# ifdef FEAT_EVAL
3012 if (xp != NULL)
3013 {
3014 xp->xp_arg = uc->uc_compl_arg;
3015 xp->xp_scriptID = uc->uc_scriptID;
3016 }
3017# endif
3018# endif
3019 /* Do not search for further abbreviations
3020 * if this is an exact match. */
3021 matchlen = k;
3022 if (k == len && *np == NUL)
3023 {
3024 if (full != NULL)
3025 *full = TRUE;
3026 amb_local = FALSE;
3027 break;
3028 }
3029 }
3030 }
3031 }
3032
3033 /* Stop if we found a full match or searched all. */
3034 if (j < gap->ga_len || gap == &ucmds)
3035 break;
3036 gap = &ucmds;
3037 }
3038
3039 /* Only found ambiguous matches. */
3040 if (amb_local)
3041 {
3042 if (xp != NULL)
3043 xp->xp_context = EXPAND_UNSUCCESSFUL;
3044 return NULL;
3045 }
3046
3047 /* The match we found may be followed immediately by a number. Move "p"
3048 * back to point to it. */
3049 if (found || possible)
3050 return p + (matchlen - len);
3051 return p;
3052}
3053#endif
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003056static struct cmdmod
3057{
3058 char *name;
3059 int minlen;
3060 int has_count; /* :123verbose :3tab */
3061} cmdmods[] = {
3062 {"aboveleft", 3, FALSE},
3063 {"belowright", 3, FALSE},
3064 {"botright", 2, FALSE},
3065 {"browse", 3, FALSE},
3066 {"confirm", 4, FALSE},
3067 {"hide", 3, FALSE},
3068 {"keepalt", 5, FALSE},
3069 {"keepjumps", 5, FALSE},
3070 {"keepmarks", 3, FALSE},
3071 {"leftabove", 5, FALSE},
3072 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003073 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003074 {"rightbelow", 6, FALSE},
3075 {"sandbox", 3, FALSE},
3076 {"silent", 3, FALSE},
3077 {"tab", 3, TRUE},
3078 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003079 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003080 {"verbose", 4, TRUE},
3081 {"vertical", 4, FALSE},
3082};
3083
3084/*
3085 * Return length of a command modifier (including optional count).
3086 * Return zero when it's not a modifier.
3087 */
3088 int
3089modifier_len(cmd)
3090 char_u *cmd;
3091{
3092 int i, j;
3093 char_u *p = cmd;
3094
3095 if (VIM_ISDIGIT(*cmd))
3096 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003097 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003098 {
3099 for (j = 0; p[j] != NUL; ++j)
3100 if (p[j] != cmdmods[i].name[j])
3101 break;
3102 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3103 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003104 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003105 }
3106 return 0;
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * Return > 0 if an Ex command "name" exists.
3111 * Return 2 if there is an exact match.
3112 * Return 3 if there is an ambiguous match.
3113 */
3114 int
3115cmd_exists(name)
3116 char_u *name;
3117{
3118 exarg_T ea;
3119 int full = FALSE;
3120 int i;
3121 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003122 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003125 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
3127 for (j = 0; name[j] != NUL; ++j)
3128 if (name[j] != cmdmods[i].name[j])
3129 break;
3130 if (name[j] == NUL && j >= cmdmods[i].minlen)
3131 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3132 }
3133
Bram Moolenaara9587612006-05-04 21:47:50 +00003134 /* Check built-in commands and user defined commands.
3135 * For ":2match" and ":3match" we need to skip the number. */
3136 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003138 p = find_command(&ea, &full);
3139 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003141 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3142 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003143 if (*skipwhite(p) != NUL)
3144 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3146}
3147#endif
3148
3149/*
3150 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3151 * we don't need/want deleted. Maybe this could be done better if we didn't
3152 * repeat all this stuff. The only problem is that they may not stay
3153 * perfectly compatible with each other, but then the command line syntax
3154 * probably won't change that much -- webb.
3155 */
3156 char_u *
3157set_one_cmd_context(xp, buff)
3158 expand_T *xp;
3159 char_u *buff; /* buffer for command string */
3160{
3161 char_u *p;
3162 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003163 int len = 0;
3164 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3166 int compl = EXPAND_NOTHING;
3167#endif
3168#ifdef FEAT_CMDL_COMPL
3169 int delim;
3170#endif
3171 int forceit = FALSE;
3172 int usefilter = FALSE; /* filter instead of file name */
3173
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003174 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 xp->xp_pattern = buff;
3176 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179/*
3180 * 2. skip comment lines and leading space, colons or bars
3181 */
3182 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3183 ;
3184 xp->xp_pattern = cmd;
3185
3186 if (*cmd == NUL)
3187 return NULL;
3188 if (*cmd == '"') /* ignore comment lines */
3189 {
3190 xp->xp_context = EXPAND_NOTHING;
3191 return NULL;
3192 }
3193
3194/*
3195 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3196 */
3197 cmd = skip_range(cmd, &xp->xp_context);
3198
3199/*
3200 * 4. parse command
3201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 xp->xp_pattern = cmd;
3203 if (*cmd == NUL)
3204 return NULL;
3205 if (*cmd == '"')
3206 {
3207 xp->xp_context = EXPAND_NOTHING;
3208 return NULL;
3209 }
3210
3211 if (*cmd == '|' || *cmd == '\n')
3212 return cmd + 1; /* There's another command */
3213
3214 /*
3215 * Isolate the command and search for it in the command table.
3216 * Exceptions:
3217 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003218 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3220 */
3221 if (*cmd == 'k' && cmd[1] != 'e')
3222 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 p = cmd + 1;
3225 }
3226 else
3227 {
3228 p = cmd;
3229 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3230 ++p;
3231 /* check for non-alpha command */
3232 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3233 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 xp->xp_context = EXPAND_UNSUCCESSFUL;
3239 return NULL;
3240 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003242 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3243 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3244 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 break;
3246
3247#ifdef FEAT_USR_CMDS
3248 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3250 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251#endif
3252 }
3253
3254 /*
3255 * If the cursor is touching the command, and it ends in an alpha-numeric
3256 * character, complete the command name.
3257 */
3258 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3259 return NULL;
3260
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 {
3263 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3264 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 p = cmd + 1;
3267 }
3268#ifdef FEAT_USR_CMDS
3269 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3270 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 ea.cmd = cmd;
3272 p = find_ucmd(&ea, p, NULL, xp,
3273# if defined(FEAT_CMDL_COMPL)
3274 &compl
3275# else
3276 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003279 if (p == NULL)
3280 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282#endif
3283 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 /* Not still touching the command and it was an illegal one */
3287 xp->xp_context = EXPAND_UNSUCCESSFUL;
3288 return NULL;
3289 }
3290
3291 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3292
3293 if (*p == '!') /* forced commands */
3294 {
3295 forceit = TRUE;
3296 ++p;
3297 }
3298
3299/*
3300 * 5. parse arguments
3301 */
3302#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003305 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 arg = skipwhite(p);
3308
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 if (*arg == '>') /* append */
3312 {
3313 if (*++arg == '>')
3314 ++arg;
3315 arg = skipwhite(arg);
3316 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
3319 ++arg;
3320 usefilter = TRUE;
3321 }
3322 }
3323
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
3326 usefilter = forceit; /* :r! filter if forced */
3327 if (*arg == '!') /* :r !filter */
3328 {
3329 ++arg;
3330 usefilter = TRUE;
3331 }
3332 }
3333
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 while (*arg == *cmd) /* allow any number of '>' or '<' */
3337 ++arg;
3338 arg = skipwhite(arg);
3339 }
3340
3341 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 /* Check if we're in the +command */
3345 p = arg + 1;
3346 arg = skip_cmd_arg(arg, FALSE);
3347
3348 /* Still touching the command after '+'? */
3349 if (*arg == NUL)
3350 return p;
3351
3352 /* Skip space(s) after +command to get to the real argument */
3353 arg = skipwhite(arg);
3354 }
3355
3356 /*
3357 * Check for '|' to separate commands and '"' to start comments.
3358 * Don't do this for ":read !cmd" and ":write !cmd".
3359 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003360 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 p = arg;
3363 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003364 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 p += 2;
3366 while (*p)
3367 {
3368 if (*p == Ctrl_V)
3369 {
3370 if (p[1] != NUL)
3371 ++p;
3372 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003373 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 || *p == '|' || *p == '\n')
3375 {
3376 if (*(p - 1) != '\\')
3377 {
3378 if (*p == '|' || *p == '\n')
3379 return p + 1;
3380 return NULL; /* It's a comment */
3381 }
3382 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003383 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 }
3386
3387 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003388 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 vim_strchr((char_u *)"|\"", *arg) == NULL)
3390 return NULL;
3391
3392 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003393 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003395 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003396 while (*p && p < buff + len)
3397 {
3398 if (*p == ' ' || *p == TAB)
3399 {
3400 /* argument starts after a space */
3401 xp->xp_pattern = ++p;
3402 }
3403 else
3404 {
3405 if (*p == '\\' && *(p + 1) != NUL)
3406 ++p; /* skip over escaped character */
3407 mb_ptr_adv(p);
3408 }
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003411 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003413 int c;
3414 int in_quote = FALSE;
3415 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /*
3418 * Allow spaces within back-quotes to count as part of the argument
3419 * being expanded.
3420 */
3421 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003422 p = xp->xp_pattern;
3423 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003425#ifdef FEAT_MBYTE
3426 if (has_mbyte)
3427 c = mb_ptr2char(p);
3428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003430 c = *p;
3431 if (c == '\\' && p[1] != NUL)
3432 ++p;
3433 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
3435 if (!in_quote)
3436 {
3437 xp->xp_pattern = p;
3438 bow = p + 1;
3439 }
3440 in_quote = !in_quote;
3441 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003442 /* An argument can contain just about everything, except
3443 * characters that end the command and white space. */
3444 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003445#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003446 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003447#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003448 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003449 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003450 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003451 while (*p != NUL)
3452 {
3453#ifdef FEAT_MBYTE
3454 if (has_mbyte)
3455 c = mb_ptr2char(p);
3456 else
3457#endif
3458 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003459 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003460 break;
3461#ifdef FEAT_MBYTE
3462 if (has_mbyte)
3463 len = (*mb_ptr2len)(p);
3464 else
3465#endif
3466 len = 1;
3467 mb_ptr_adv(p);
3468 }
3469 if (in_quote)
3470 bow = p;
3471 else
3472 xp->xp_pattern = p;
3473 p -= len;
3474 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003475 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477
3478 /*
3479 * If we are still inside the quotes, and we passed a space, just
3480 * expand from there.
3481 */
3482 if (bow != NULL && in_quote)
3483 xp->xp_pattern = bow;
3484 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003485
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003486 /* For a shell command more chars need to be escaped. */
3487 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003488 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003489#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003490 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003491#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003492 /* When still after the command name expand executables. */
3493 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003494 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 /* Check for environment variable */
3498 if (*xp->xp_pattern == '$'
3499#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3500 || *xp->xp_pattern == '%'
3501#endif
3502 )
3503 {
3504 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3505 if (!vim_isIDc(*p))
3506 break;
3507 if (*p == NUL)
3508 {
3509 xp->xp_context = EXPAND_ENV_VARS;
3510 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003511#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3512 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003513 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003514 compl = EXPAND_ENV_VARS;
3515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003518#if defined(FEAT_CMDL_COMPL)
3519 /* Check for user names */
3520 if (*xp->xp_pattern == '~')
3521 {
3522 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3523 ;
3524 /* Complete ~user only if it partially matches a user name.
3525 * A full match ~user<Tab> will be replaced by user's home
3526 * directory i.e. something like ~user<Tab> -> /home/user/ */
3527 if (*p == NUL && p > xp->xp_pattern + 1
3528 && match_user(xp->xp_pattern + 1) == 1)
3529 {
3530 xp->xp_context = EXPAND_USER;
3531 ++xp->xp_pattern;
3532 }
3533 }
3534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536
3537/*
3538 * 6. switch on command name
3539 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003542 case CMD_find:
3543 case CMD_sfind:
3544 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003545 if (xp->xp_context == EXPAND_FILES)
3546 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 case CMD_cd:
3549 case CMD_chdir:
3550 case CMD_lcd:
3551 case CMD_lchdir:
3552 if (xp->xp_context == EXPAND_FILES)
3553 xp->xp_context = EXPAND_DIRECTORIES;
3554 break;
3555 case CMD_help:
3556 xp->xp_context = EXPAND_HELP;
3557 xp->xp_pattern = arg;
3558 break;
3559
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003560 /* Command modifiers: return the argument.
3561 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003563 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 case CMD_belowright:
3565 case CMD_botright:
3566 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003567 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003569 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 case CMD_folddoclosed:
3571 case CMD_folddoopen:
3572 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003573 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 case CMD_keepjumps:
3575 case CMD_keepmarks:
3576 case CMD_leftabove:
3577 case CMD_lockmarks:
3578 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003579 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003581 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 case CMD_topleft:
3583 case CMD_verbose:
3584 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003585 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 return arg;
3587
Bram Moolenaar4f688582007-07-24 12:34:30 +00003588#ifdef FEAT_CMDL_COMPL
3589# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 case CMD_match:
3591 if (*arg == NUL || !ends_excmd(*arg))
3592 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003593 /* also complete "None" */
3594 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 arg = skipwhite(skiptowhite(arg));
3596 if (*arg != NUL)
3597 {
3598 xp->xp_context = EXPAND_NOTHING;
3599 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3600 }
3601 }
3602 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605/*
3606 * All completion for the +cmdline_compl feature goes here.
3607 */
3608
3609# ifdef FEAT_USR_CMDS
3610 case CMD_command:
3611 /* Check for attributes */
3612 while (*arg == '-')
3613 {
3614 arg++; /* Skip "-" */
3615 p = skiptowhite(arg);
3616 if (*p == NUL)
3617 {
3618 /* Cursor is still in the attribute */
3619 p = vim_strchr(arg, '=');
3620 if (p == NULL)
3621 {
3622 /* No "=", so complete attribute names */
3623 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3624 xp->xp_pattern = arg;
3625 return NULL;
3626 }
3627
3628 /* For the -complete and -nargs attributes, we complete
3629 * their arguments as well.
3630 */
3631 if (STRNICMP(arg, "complete", p - arg) == 0)
3632 {
3633 xp->xp_context = EXPAND_USER_COMPLETE;
3634 xp->xp_pattern = p + 1;
3635 return NULL;
3636 }
3637 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3638 {
3639 xp->xp_context = EXPAND_USER_NARGS;
3640 xp->xp_pattern = p + 1;
3641 return NULL;
3642 }
3643 return NULL;
3644 }
3645 arg = skipwhite(p);
3646 }
3647
3648 /* After the attributes comes the new command name */
3649 p = skiptowhite(arg);
3650 if (*p == NUL)
3651 {
3652 xp->xp_context = EXPAND_USER_COMMANDS;
3653 xp->xp_pattern = arg;
3654 break;
3655 }
3656
3657 /* And finally comes a normal command */
3658 return skipwhite(p);
3659
3660 case CMD_delcommand:
3661 xp->xp_context = EXPAND_USER_COMMANDS;
3662 xp->xp_pattern = arg;
3663 break;
3664# endif
3665
3666 case CMD_global:
3667 case CMD_vglobal:
3668 delim = *arg; /* get the delimiter */
3669 if (delim)
3670 ++arg; /* skip delimiter if there is one */
3671
3672 while (arg[0] != NUL && arg[0] != delim)
3673 {
3674 if (arg[0] == '\\' && arg[1] != NUL)
3675 ++arg;
3676 ++arg;
3677 }
3678 if (arg[0] != NUL)
3679 return arg + 1;
3680 break;
3681 case CMD_and:
3682 case CMD_substitute:
3683 delim = *arg;
3684 if (delim)
3685 {
3686 /* skip "from" part */
3687 ++arg;
3688 arg = skip_regexp(arg, delim, p_magic, NULL);
3689 }
3690 /* skip "to" part */
3691 while (arg[0] != NUL && arg[0] != delim)
3692 {
3693 if (arg[0] == '\\' && arg[1] != NUL)
3694 ++arg;
3695 ++arg;
3696 }
3697 if (arg[0] != NUL) /* skip delimiter */
3698 ++arg;
3699 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3700 ++arg;
3701 if (arg[0] != NUL)
3702 return arg;
3703 break;
3704 case CMD_isearch:
3705 case CMD_dsearch:
3706 case CMD_ilist:
3707 case CMD_dlist:
3708 case CMD_ijump:
3709 case CMD_psearch:
3710 case CMD_djump:
3711 case CMD_isplit:
3712 case CMD_dsplit:
3713 arg = skipwhite(skipdigits(arg)); /* skip count */
3714 if (*arg == '/') /* Match regexp, not just whole words */
3715 {
3716 for (++arg; *arg && *arg != '/'; arg++)
3717 if (*arg == '\\' && arg[1] != NUL)
3718 arg++;
3719 if (*arg)
3720 {
3721 arg = skipwhite(arg + 1);
3722
3723 /* Check for trailing illegal characters */
3724 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3725 xp->xp_context = EXPAND_NOTHING;
3726 else
3727 return arg;
3728 }
3729 }
3730 break;
3731#ifdef FEAT_AUTOCMD
3732 case CMD_autocmd:
3733 return set_context_in_autocmd(xp, arg, FALSE);
3734
3735 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003736 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 return set_context_in_autocmd(xp, arg, TRUE);
3738#endif
3739 case CMD_set:
3740 set_context_in_set_cmd(xp, arg, 0);
3741 break;
3742 case CMD_setglobal:
3743 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3744 break;
3745 case CMD_setlocal:
3746 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3747 break;
3748 case CMD_tag:
3749 case CMD_stag:
3750 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003751 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 case CMD_tselect:
3753 case CMD_stselect:
3754 case CMD_ptselect:
3755 case CMD_tjump:
3756 case CMD_stjump:
3757 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003758 if (*p_wop != NUL)
3759 xp->xp_context = EXPAND_TAGS_LISTFILES;
3760 else
3761 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 xp->xp_pattern = arg;
3763 break;
3764 case CMD_augroup:
3765 xp->xp_context = EXPAND_AUGROUP;
3766 xp->xp_pattern = arg;
3767 break;
3768#ifdef FEAT_SYN_HL
3769 case CMD_syntax:
3770 set_context_in_syntax_cmd(xp, arg);
3771 break;
3772#endif
3773#ifdef FEAT_EVAL
3774 case CMD_let:
3775 case CMD_if:
3776 case CMD_elseif:
3777 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003778 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 case CMD_echo:
3780 case CMD_echon:
3781 case CMD_execute:
3782 case CMD_echomsg:
3783 case CMD_echoerr:
3784 case CMD_call:
3785 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003786 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 break;
3788
3789 case CMD_unlet:
3790 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3791 arg = xp->xp_pattern + 1;
3792 xp->xp_context = EXPAND_USER_VARS;
3793 xp->xp_pattern = arg;
3794 break;
3795
3796 case CMD_function:
3797 case CMD_delfunction:
3798 xp->xp_context = EXPAND_USER_FUNC;
3799 xp->xp_pattern = arg;
3800 break;
3801
3802 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003803 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 break;
3805#endif
3806 case CMD_highlight:
3807 set_context_in_highlight_cmd(xp, arg);
3808 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003809#ifdef FEAT_CSCOPE
3810 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003811 case CMD_lcscope:
3812 case CMD_scscope:
3813 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003814 break;
3815#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003816#ifdef FEAT_SIGNS
3817 case CMD_sign:
3818 set_context_in_sign_cmd(xp, arg);
3819 break;
3820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821#ifdef FEAT_LISTCMDS
3822 case CMD_bdelete:
3823 case CMD_bwipeout:
3824 case CMD_bunload:
3825 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3826 arg = xp->xp_pattern + 1;
3827 /*FALLTHROUGH*/
3828 case CMD_buffer:
3829 case CMD_sbuffer:
3830 case CMD_checktime:
3831 xp->xp_context = EXPAND_BUFFERS;
3832 xp->xp_pattern = arg;
3833 break;
3834#endif
3835#ifdef FEAT_USR_CMDS
3836 case CMD_USER:
3837 case CMD_USER_BUF:
3838 if (compl != EXPAND_NOTHING)
3839 {
3840 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003841 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843# ifdef FEAT_MENU
3844 if (compl == EXPAND_MENUS)
3845 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3846# endif
3847 if (compl == EXPAND_COMMANDS)
3848 return arg;
3849 if (compl == EXPAND_MAPPINGS)
3850 return set_context_in_map_cmd(xp, (char_u *)"map",
3851 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003852 /* Find start of last argument. */
3853 p = arg;
3854 while (*p)
3855 {
3856 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003857 /* argument starts after a space */
3858 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003859 else if (*p == '\\' && *(p + 1) != NUL)
3860 ++p; /* skip over escaped character */
3861 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 xp->xp_pattern = arg;
3864 }
3865 xp->xp_context = compl;
3866 }
3867 break;
3868#endif
3869 case CMD_map: case CMD_noremap:
3870 case CMD_nmap: case CMD_nnoremap:
3871 case CMD_vmap: case CMD_vnoremap:
3872 case CMD_omap: case CMD_onoremap:
3873 case CMD_imap: case CMD_inoremap:
3874 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003875 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003877 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_unmap:
3879 case CMD_nunmap:
3880 case CMD_vunmap:
3881 case CMD_ounmap:
3882 case CMD_iunmap:
3883 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003884 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003886 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_abbreviate: case CMD_noreabbrev:
3888 case CMD_cabbrev: case CMD_cnoreabbrev:
3889 case CMD_iabbrev: case CMD_inoreabbrev:
3890 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003891 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_unabbreviate:
3893 case CMD_cunabbrev:
3894 case CMD_iunabbrev:
3895 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003896 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#ifdef FEAT_MENU
3898 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3899 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3900 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3901 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3902 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3903 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3904 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3905 case CMD_tmenu: case CMD_tunmenu:
3906 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3907 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3908#endif
3909
3910 case CMD_colorscheme:
3911 xp->xp_context = EXPAND_COLORS;
3912 xp->xp_pattern = arg;
3913 break;
3914
3915 case CMD_compiler:
3916 xp->xp_context = EXPAND_COMPILER;
3917 xp->xp_pattern = arg;
3918 break;
3919
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003920 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003921 xp->xp_context = EXPAND_OWNSYNTAX;
3922 xp->xp_pattern = arg;
3923 break;
3924
3925 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003926 xp->xp_context = EXPAND_FILETYPE;
3927 xp->xp_pattern = arg;
3928 break;
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3931 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3932 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003933 p = skiptowhite(arg);
3934 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 xp->xp_context = EXPAND_LANGUAGE;
3937 xp->xp_pattern = arg;
3938 }
3939 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003940 {
3941 if ( STRNCMP(arg, "messages", p - arg) == 0
3942 || STRNCMP(arg, "ctype", p - arg) == 0
3943 || STRNCMP(arg, "time", p - arg) == 0)
3944 {
3945 xp->xp_context = EXPAND_LOCALES;
3946 xp->xp_pattern = skipwhite(p);
3947 }
3948 else
3949 xp->xp_context = EXPAND_NOTHING;
3950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 break;
3952#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003953#if defined(FEAT_PROFILE)
3954 case CMD_profile:
3955 set_context_in_profile_cmd(xp, arg);
3956 break;
3957#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003958 case CMD_behave:
3959 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003960 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003963#if defined(FEAT_CMDHIST)
3964 case CMD_history:
3965 xp->xp_context = EXPAND_HISTORY;
3966 xp->xp_pattern = arg;
3967 break;
3968#endif
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970#endif /* FEAT_CMDL_COMPL */
3971
3972 default:
3973 break;
3974 }
3975 return NULL;
3976}
3977
3978/*
3979 * skip a range specifier of the form: addr [,addr] [;addr] ..
3980 *
3981 * Backslashed delimiters after / or ? will be skipped, and commands will
3982 * not be expanded between /'s and ?'s or after "'".
3983 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003984 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 * Returns the "cmd" pointer advanced to beyond the range.
3986 */
3987 char_u *
3988skip_range(cmd, ctx)
3989 char_u *cmd;
3990 int *ctx; /* pointer to xp_context or NULL */
3991{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003992 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaardf177f62005-02-22 08:39:57 +00003994 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
3996 if (*cmd == '\'')
3997 {
3998 if (*++cmd == NUL && ctx != NULL)
3999 *ctx = EXPAND_NOTHING;
4000 }
4001 else if (*cmd == '/' || *cmd == '?')
4002 {
4003 delim = *cmd++;
4004 while (*cmd != NUL && *cmd != delim)
4005 if (*cmd++ == '\\' && *cmd != NUL)
4006 ++cmd;
4007 if (*cmd == NUL && ctx != NULL)
4008 *ctx = EXPAND_NOTHING;
4009 }
4010 if (*cmd != NUL)
4011 ++cmd;
4012 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004013
4014 /* Skip ":" and white space. */
4015 while (*cmd == ':')
4016 cmd = skipwhite(cmd + 1);
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 return cmd;
4019}
4020
4021/*
4022 * get a single EX address
4023 *
4024 * Set ptr to the next character after the part that was interpreted.
4025 * Set ptr to NULL when an error is encountered.
4026 *
4027 * Return MAXLNUM when no Ex address was found.
4028 */
4029 static linenr_T
4030get_address(ptr, skip, to_other_file)
4031 char_u **ptr;
4032 int skip; /* only skip the address, don't use it */
4033 int to_other_file; /* flag: may jump to other file */
4034{
4035 int c;
4036 int i;
4037 long n;
4038 char_u *cmd;
4039 pos_T pos;
4040 pos_T *fp;
4041 linenr_T lnum;
4042
4043 cmd = skipwhite(*ptr);
4044 lnum = MAXLNUM;
4045 do
4046 {
4047 switch (*cmd)
4048 {
4049 case '.': /* '.' - Cursor position */
4050 ++cmd;
4051 lnum = curwin->w_cursor.lnum;
4052 break;
4053
4054 case '$': /* '$' - last line */
4055 ++cmd;
4056 lnum = curbuf->b_ml.ml_line_count;
4057 break;
4058
4059 case '\'': /* ''' - mark */
4060 if (*++cmd == NUL)
4061 {
4062 cmd = NULL;
4063 goto error;
4064 }
4065 if (skip)
4066 ++cmd;
4067 else
4068 {
4069 /* Only accept a mark in another file when it is
4070 * used by itself: ":'M". */
4071 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4072 ++cmd;
4073 if (fp == (pos_T *)-1)
4074 /* Jumped to another file. */
4075 lnum = curwin->w_cursor.lnum;
4076 else
4077 {
4078 if (check_mark(fp) == FAIL)
4079 {
4080 cmd = NULL;
4081 goto error;
4082 }
4083 lnum = fp->lnum;
4084 }
4085 }
4086 break;
4087
4088 case '/':
4089 case '?': /* '/' or '?' - search */
4090 c = *cmd++;
4091 if (skip) /* skip "/pat/" */
4092 {
4093 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4094 if (*cmd == c)
4095 ++cmd;
4096 }
4097 else
4098 {
4099 pos = curwin->w_cursor; /* save curwin->w_cursor */
4100 /*
4101 * When '/' or '?' follows another address, start
4102 * from there.
4103 */
4104 if (lnum != MAXLNUM)
4105 curwin->w_cursor.lnum = lnum;
4106 /*
4107 * Start a forward search at the end of the line.
4108 * Start a backward search at the start of the line.
4109 * This makes sure we never match in the current
4110 * line, and can match anywhere in the
4111 * next/previous line.
4112 */
4113 if (c == '/')
4114 curwin->w_cursor.col = MAXCOL;
4115 else
4116 curwin->w_cursor.col = 0;
4117 searchcmdlen = 0;
4118 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004119 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 curwin->w_cursor = pos;
4122 cmd = NULL;
4123 goto error;
4124 }
4125 lnum = curwin->w_cursor.lnum;
4126 curwin->w_cursor = pos;
4127 /* adjust command string pointer */
4128 cmd += searchcmdlen;
4129 }
4130 break;
4131
4132 case '\\': /* "\?", "\/" or "\&", repeat search */
4133 ++cmd;
4134 if (*cmd == '&')
4135 i = RE_SUBST;
4136 else if (*cmd == '?' || *cmd == '/')
4137 i = RE_SEARCH;
4138 else
4139 {
4140 EMSG(_(e_backslash));
4141 cmd = NULL;
4142 goto error;
4143 }
4144
4145 if (!skip)
4146 {
4147 /*
4148 * When search follows another address, start from
4149 * there.
4150 */
4151 if (lnum != MAXLNUM)
4152 pos.lnum = lnum;
4153 else
4154 pos.lnum = curwin->w_cursor.lnum;
4155
4156 /*
4157 * Start the search just like for the above
4158 * do_search().
4159 */
4160 if (*cmd != '?')
4161 pos.col = MAXCOL;
4162 else
4163 pos.col = 0;
4164 if (searchit(curwin, curbuf, &pos,
4165 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004166 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004167 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 lnum = pos.lnum;
4169 else
4170 {
4171 cmd = NULL;
4172 goto error;
4173 }
4174 }
4175 ++cmd;
4176 break;
4177
4178 default:
4179 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4180 lnum = getdigits(&cmd);
4181 }
4182
4183 for (;;)
4184 {
4185 cmd = skipwhite(cmd);
4186 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4187 break;
4188
4189 if (lnum == MAXLNUM)
4190 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4191 if (VIM_ISDIGIT(*cmd))
4192 i = '+'; /* "number" is same as "+number" */
4193 else
4194 i = *cmd++;
4195 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4196 n = 1;
4197 else
4198 n = getdigits(&cmd);
4199 if (i == '-')
4200 lnum -= n;
4201 else
4202 lnum += n;
4203 }
4204 } while (*cmd == '/' || *cmd == '?');
4205
4206error:
4207 *ptr = cmd;
4208 return lnum;
4209}
4210
4211/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004212 * Get flags from an Ex command argument.
4213 */
4214 static void
4215get_flags(eap)
4216 exarg_T *eap;
4217{
4218 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4219 {
4220 if (*eap->arg == 'l')
4221 eap->flags |= EXFLAG_LIST;
4222 else if (*eap->arg == 'p')
4223 eap->flags |= EXFLAG_PRINT;
4224 else
4225 eap->flags |= EXFLAG_NR;
4226 eap->arg = skipwhite(eap->arg + 1);
4227 }
4228}
4229
4230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * Function called for command which is Not Implemented. NI!
4232 */
4233 void
4234ex_ni(eap)
4235 exarg_T *eap;
4236{
4237 if (!eap->skip)
4238 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4239}
4240
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004241#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242/*
4243 * Function called for script command which is Not Implemented. NI!
4244 * Skips over ":perl <<EOF" constructs.
4245 */
4246 static void
4247ex_script_ni(eap)
4248 exarg_T *eap;
4249{
4250 if (!eap->skip)
4251 ex_ni(eap);
4252 else
4253 vim_free(script_get(eap, eap->arg));
4254}
4255#endif
4256
4257/*
4258 * Check range in Ex command for validity.
4259 * Return NULL when valid, error message when invalid.
4260 */
4261 static char_u *
4262invalid_range(eap)
4263 exarg_T *eap;
4264{
4265 if ( eap->line1 < 0
4266 || eap->line2 < 0
4267 || eap->line1 > eap->line2
4268 || ((eap->argt & RANGE)
4269 && !(eap->argt & NOTADR)
4270 && eap->line2 > curbuf->b_ml.ml_line_count
4271#ifdef FEAT_DIFF
4272 + (eap->cmdidx == CMD_diffget)
4273#endif
4274 ))
4275 return (char_u *)_(e_invrange);
4276 return NULL;
4277}
4278
4279/*
4280 * Correct the range for zero line number, if required.
4281 */
4282 static void
4283correct_range(eap)
4284 exarg_T *eap;
4285{
4286 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4287 {
4288 if (eap->line1 == 0)
4289 eap->line1 = 1;
4290 if (eap->line2 == 0)
4291 eap->line2 = 1;
4292 }
4293}
4294
Bram Moolenaar748bf032005-02-02 23:04:36 +00004295#ifdef FEAT_QUICKFIX
4296static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4297
4298/*
4299 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4300 * pattern. Otherwise return eap->arg.
4301 */
4302 static char_u *
4303skip_grep_pat(eap)
4304 exarg_T *eap;
4305{
4306 char_u *p = eap->arg;
4307
Bram Moolenaara37420f2006-02-04 22:37:47 +00004308 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4309 || eap->cmdidx == CMD_vimgrepadd
4310 || eap->cmdidx == CMD_lvimgrepadd
4311 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004312 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004313 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004314 if (p == NULL)
4315 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004316 }
4317 return p;
4318}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004319
4320/*
4321 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4322 * in the command line, so that things like % get expanded.
4323 */
4324 static char_u *
4325replace_makeprg(eap, p, cmdlinep)
4326 exarg_T *eap;
4327 char_u *p;
4328 char_u **cmdlinep;
4329{
4330 char_u *new_cmdline;
4331 char_u *program;
4332 char_u *pos;
4333 char_u *ptr;
4334 int len;
4335 int i;
4336
4337 /*
4338 * Don't do it when ":vimgrep" is used for ":grep".
4339 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004340 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4341 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4342 || eap->cmdidx == CMD_grepadd
4343 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004344 && !grep_internal(eap->cmdidx))
4345 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004346 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4347 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004348 {
4349 if (*curbuf->b_p_gp == NUL)
4350 program = p_gp;
4351 else
4352 program = curbuf->b_p_gp;
4353 }
4354 else
4355 {
4356 if (*curbuf->b_p_mp == NUL)
4357 program = p_mp;
4358 else
4359 program = curbuf->b_p_mp;
4360 }
4361
4362 p = skipwhite(p);
4363
4364 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4365 {
4366 /* replace $* by given arguments */
4367 i = 1;
4368 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4369 ++i;
4370 len = (int)STRLEN(p);
4371 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4372 if (new_cmdline == NULL)
4373 return NULL; /* out of memory */
4374 ptr = new_cmdline;
4375 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4376 {
4377 i = (int)(pos - program);
4378 STRNCPY(ptr, program, i);
4379 STRCPY(ptr += i, p);
4380 ptr += len;
4381 program = pos + 2;
4382 }
4383 STRCPY(ptr, program);
4384 }
4385 else
4386 {
4387 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4388 if (new_cmdline == NULL)
4389 return NULL; /* out of memory */
4390 STRCPY(new_cmdline, program);
4391 STRCAT(new_cmdline, " ");
4392 STRCAT(new_cmdline, p);
4393 }
4394 msg_make(p);
4395
4396 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4397 vim_free(*cmdlinep);
4398 *cmdlinep = new_cmdline;
4399 p = new_cmdline;
4400 }
4401 return p;
4402}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004403#endif
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405/*
4406 * Expand file name in Ex command argument.
4407 * Return FAIL for failure, OK otherwise.
4408 */
4409 int
4410expand_filename(eap, cmdlinep, errormsgp)
4411 exarg_T *eap;
4412 char_u **cmdlinep;
4413 char_u **errormsgp;
4414{
4415 int has_wildcards; /* need to expand wildcards */
4416 char_u *repl;
4417 int srclen;
4418 char_u *p;
4419 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004420 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar748bf032005-02-02 23:04:36 +00004422#ifdef FEAT_QUICKFIX
4423 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4424 p = skip_grep_pat(eap);
4425#else
4426 p = eap->arg;
4427#endif
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 /*
4430 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4431 * the file name contains a wildcard it should not cause expanding.
4432 * (it will be expanded anyway if there is a wildcard before replacing).
4433 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004434 has_wildcards = mch_has_wildcard(p);
4435 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004437#ifdef FEAT_EVAL
4438 /* Skip over `=expr`, wildcards in it are not expanded. */
4439 if (p[0] == '`' && p[1] == '=')
4440 {
4441 p += 2;
4442 (void)skip_expr(&p);
4443 if (*p == '`')
4444 ++p;
4445 continue;
4446 }
4447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /*
4449 * Quick check if this cannot be the start of a special string.
4450 * Also removes backslash before '%', '#' and '<'.
4451 */
4452 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4453 {
4454 ++p;
4455 continue;
4456 }
4457
4458 /*
4459 * Try to find a match at this position.
4460 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004461 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4462 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 if (*errormsgp != NULL) /* error detected */
4464 return FAIL;
4465 if (repl == NULL) /* no match found */
4466 {
4467 p += srclen;
4468 continue;
4469 }
4470
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004471 /* Wildcards won't be expanded below, the replacement is taken
4472 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4473 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4474 {
4475 char_u *l = repl;
4476
4477 repl = expand_env_save(repl);
4478 vim_free(l);
4479 }
4480
Bram Moolenaar63b92542007-03-27 14:57:09 +00004481 /* Need to escape white space et al. with a backslash.
4482 * Don't do this for:
4483 * - replacement that already has been escaped: "##"
4484 * - shell commands (may have to use quotes instead).
4485 * - non-unix systems when there is a single argument (spaces don't
4486 * separate arguments then).
4487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004489 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 && eap->cmdidx != CMD_bang
4491 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004492 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004494 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004496 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497#ifndef UNIX
4498 && !(eap->argt & NOSPC)
4499#endif
4500 )
4501 {
4502 char_u *l;
4503#ifdef BACKSLASH_IN_FILENAME
4504 /* Don't escape a backslash here, because rem_backslash() doesn't
4505 * remove it later. */
4506 static char_u *nobslash = (char_u *)" \t\"|";
4507# define ESCAPE_CHARS nobslash
4508#else
4509# define ESCAPE_CHARS escape_chars
4510#endif
4511
4512 for (l = repl; *l; ++l)
4513 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4514 {
4515 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4516 if (l != NULL)
4517 {
4518 vim_free(repl);
4519 repl = l;
4520 }
4521 break;
4522 }
4523 }
4524
4525 /* For a shell command a '!' must be escaped. */
4526 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004527 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 char_u *l;
4530
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004531 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 if (l != NULL)
4533 {
4534 vim_free(repl);
4535 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004536 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (strstr((char *)p_sh, "sh") != NULL)
4538 {
4539 l = vim_strsave_escaped(repl, (char_u *)"!");
4540 if (l != NULL)
4541 {
4542 vim_free(repl);
4543 repl = l;
4544 }
4545 }
4546 }
4547 }
4548
4549 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4550 vim_free(repl);
4551 if (p == NULL)
4552 return FAIL;
4553 }
4554
4555 /*
4556 * One file argument: Expand wildcards.
4557 * Don't do this with ":r !command" or ":w !command".
4558 */
4559 if ((eap->argt & NOSPC) && !eap->usefilter)
4560 {
4561 /*
4562 * May do this twice:
4563 * 1. Replace environment variables.
4564 * 2. Replace any other wildcards, remove backslashes.
4565 */
4566 for (n = 1; n <= 2; ++n)
4567 {
4568 if (n == 2)
4569 {
4570#ifdef UNIX
4571 /*
4572 * Only for Unix we check for more than one file name.
4573 * For other systems spaces are considered to be part
4574 * of the file name.
4575 * Only check here if there is no wildcard, otherwise
4576 * ExpandOne() will check for errors. This allows
4577 * ":e `ls ve*.c`" on Unix.
4578 */
4579 if (!has_wildcards)
4580 for (p = eap->arg; *p; ++p)
4581 {
4582 /* skip escaped characters */
4583 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4584 ++p;
4585 else if (vim_iswhite(*p))
4586 {
4587 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4588 return FAIL;
4589 }
4590 }
4591#endif
4592
4593 /*
4594 * Halve the number of backslashes (this is Vi compatible).
4595 * For Unix and OS/2, when wildcards are expanded, this is
4596 * done by ExpandOne() below.
4597 */
4598#if defined(UNIX) || defined(OS2)
4599 if (!has_wildcards)
4600#endif
4601 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603
4604 if (has_wildcards)
4605 {
4606 if (n == 1)
4607 {
4608 /*
4609 * First loop: May expand environment variables. This
4610 * can be done much faster with expand_env() than with
4611 * something else (e.g., calling a shell).
4612 * After expanding environment variables, check again
4613 * if there are still wildcards present.
4614 */
4615 if (vim_strchr(eap->arg, '$') != NULL
4616 || vim_strchr(eap->arg, '~') != NULL)
4617 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004618 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004619 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 has_wildcards = mch_has_wildcard(NameBuff);
4621 p = NameBuff;
4622 }
4623 else
4624 p = NULL;
4625 }
4626 else /* n == 2 */
4627 {
4628 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004629 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 ExpandInit(&xpc);
4632 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004633 if (p_wic)
4634 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004636 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (p == NULL)
4638 return FAIL;
4639 }
4640 if (p != NULL)
4641 {
4642 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4643 p, cmdlinep);
4644 if (n == 2) /* p came from ExpandOne() */
4645 vim_free(p);
4646 }
4647 }
4648 }
4649 }
4650 return OK;
4651}
4652
4653/*
4654 * Replace part of the command line, keeping eap->cmd, eap->arg and
4655 * eap->nextcmd correct.
4656 * "src" points to the part that is to be replaced, of length "srclen".
4657 * "repl" is the replacement string.
4658 * Returns a pointer to the character after the replaced string.
4659 * Returns NULL for failure.
4660 */
4661 static char_u *
4662repl_cmdline(eap, src, srclen, repl, cmdlinep)
4663 exarg_T *eap;
4664 char_u *src;
4665 int srclen;
4666 char_u *repl;
4667 char_u **cmdlinep;
4668{
4669 int len;
4670 int i;
4671 char_u *new_cmdline;
4672
4673 /*
4674 * The new command line is build in new_cmdline[].
4675 * First allocate it.
4676 * Careful: a "+cmd" argument may have been NUL terminated.
4677 */
4678 len = (int)STRLEN(repl);
4679 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004680 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4682 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4683 return NULL; /* out of memory! */
4684
4685 /*
4686 * Copy the stuff before the expanded part.
4687 * Copy the expanded stuff.
4688 * Copy what came after the expanded part.
4689 * Copy the next commands, if there are any.
4690 */
4691 i = (int)(src - *cmdlinep); /* length of part before match */
4692 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 mch_memmove(new_cmdline + i, repl, (size_t)len);
4695 i += len; /* remember the end of the string */
4696 STRCPY(new_cmdline + i, src + srclen);
4697 src = new_cmdline + i; /* remember where to continue */
4698
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004699 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 i = (int)STRLEN(new_cmdline) + 1;
4702 STRCPY(new_cmdline + i, eap->nextcmd);
4703 eap->nextcmd = new_cmdline + i;
4704 }
4705 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4706 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4707 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4708 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4709 vim_free(*cmdlinep);
4710 *cmdlinep = new_cmdline;
4711
4712 return src;
4713}
4714
4715/*
4716 * Check for '|' to separate commands and '"' to start comments.
4717 */
4718 void
4719separate_nextcmd(eap)
4720 exarg_T *eap;
4721{
4722 char_u *p;
4723
Bram Moolenaar86b68352004-12-27 21:59:20 +00004724#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004725 p = skip_grep_pat(eap);
4726#else
4727 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004728#endif
4729
4730 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 if (*p == Ctrl_V)
4733 {
4734 if (eap->argt & (USECTRLV | XFILE))
4735 ++p; /* skip CTRL-V and next char */
4736 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004737 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004738 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if (*p == NUL) /* stop at NUL after CTRL-V */
4740 break;
4741 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004742
4743#ifdef FEAT_EVAL
4744 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004745 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004746 {
4747 p += 2;
4748 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004749 }
4750#endif
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 /* Check for '"': start of comment or '|': next command */
4753 /* :@" and :*" do not start a comment!
4754 * :redir @" doesn't either. */
4755 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4756 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4757 || p != eap->arg)
4758 && (eap->cmdidx != CMD_redir
4759 || p != eap->arg + 1 || p[-1] != '@'))
4760 || *p == '|' || *p == '\n')
4761 {
4762 /*
4763 * We remove the '\' before the '|', unless USECTRLV is used
4764 * AND 'b' is present in 'cpoptions'.
4765 */
4766 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4767 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4768 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004769 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 --p;
4771 }
4772 else
4773 {
4774 eap->nextcmd = check_nextcmd(p);
4775 *p = NUL;
4776 break;
4777 }
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4782 del_trailing_spaces(eap->arg);
4783}
4784
4785/*
4786 * get + command from ex argument
4787 */
4788 static char_u *
4789getargcmd(argp)
4790 char_u **argp;
4791{
4792 char_u *arg = *argp;
4793 char_u *command = NULL;
4794
4795 if (*arg == '+') /* +[command] */
4796 {
4797 ++arg;
4798 if (vim_isspace(*arg))
4799 command = dollar_command;
4800 else
4801 {
4802 command = arg;
4803 arg = skip_cmd_arg(command, TRUE);
4804 if (*arg != NUL)
4805 *arg++ = NUL; /* terminate command with NUL */
4806 }
4807
4808 arg = skipwhite(arg); /* skip over spaces */
4809 *argp = arg;
4810 }
4811 return command;
4812}
4813
4814/*
4815 * Find end of "+command" argument. Skip over "\ " and "\\".
4816 */
4817 static char_u *
4818skip_cmd_arg(p, rembs)
4819 char_u *p;
4820 int rembs; /* TRUE to halve the number of backslashes */
4821{
4822 while (*p && !vim_isspace(*p))
4823 {
4824 if (*p == '\\' && p[1] != NUL)
4825 {
4826 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004827 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 else
4829 ++p;
4830 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004831 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833 return p;
4834}
4835
4836/*
4837 * Get "++opt=arg" argument.
4838 * Return FAIL or OK.
4839 */
4840 static int
4841getargopt(eap)
4842 exarg_T *eap;
4843{
4844 char_u *arg = eap->arg + 2;
4845 int *pp = NULL;
4846#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004847 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 char_u *p;
4849#endif
4850
4851 /* ":edit ++[no]bin[ary] file" */
4852 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4853 {
4854 if (*arg == 'n')
4855 {
4856 arg += 2;
4857 eap->force_bin = FORCE_NOBIN;
4858 }
4859 else
4860 eap->force_bin = FORCE_BIN;
4861 if (!checkforcmd(&arg, "binary", 3))
4862 return FAIL;
4863 eap->arg = skipwhite(arg);
4864 return OK;
4865 }
4866
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004867 /* ":read ++edit file" */
4868 if (STRNCMP(arg, "edit", 4) == 0)
4869 {
4870 eap->read_edit = TRUE;
4871 eap->arg = skipwhite(arg + 4);
4872 return OK;
4873 }
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if (STRNCMP(arg, "ff", 2) == 0)
4876 {
4877 arg += 2;
4878 pp = &eap->force_ff;
4879 }
4880 else if (STRNCMP(arg, "fileformat", 10) == 0)
4881 {
4882 arg += 10;
4883 pp = &eap->force_ff;
4884 }
4885#ifdef FEAT_MBYTE
4886 else if (STRNCMP(arg, "enc", 3) == 0)
4887 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004888 if (STRNCMP(arg, "encoding", 8) == 0)
4889 arg += 8;
4890 else
4891 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 pp = &eap->force_enc;
4893 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004894 else if (STRNCMP(arg, "bad", 3) == 0)
4895 {
4896 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004897 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#endif
4900
4901 if (pp == NULL || *arg != '=')
4902 return FAIL;
4903
4904 ++arg;
4905 *pp = (int)(arg - eap->cmd);
4906 arg = skip_cmd_arg(arg, FALSE);
4907 eap->arg = skipwhite(arg);
4908 *arg = NUL;
4909
4910#ifdef FEAT_MBYTE
4911 if (pp == &eap->force_ff)
4912 {
4913#endif
4914 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4915 return FAIL;
4916#ifdef FEAT_MBYTE
4917 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004918 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
4920 /* Make 'fileencoding' lower case. */
4921 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4922 *p = TOLOWER_ASC(*p);
4923 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004924 else
4925 {
4926 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4927 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004928 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004929 if (STRICMP(p, "keep") == 0)
4930 eap->bad_char = BAD_KEEP;
4931 else if (STRICMP(p, "drop") == 0)
4932 eap->bad_char = BAD_DROP;
4933 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4934 eap->bad_char = *p;
4935 else
4936 return FAIL;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#endif
4939
4940 return OK;
4941}
4942
4943/*
4944 * ":abbreviate" and friends.
4945 */
4946 static void
4947ex_abbreviate(eap)
4948 exarg_T *eap;
4949{
4950 do_exmap(eap, TRUE); /* almost the same as mapping */
4951}
4952
4953/*
4954 * ":map" and friends.
4955 */
4956 static void
4957ex_map(eap)
4958 exarg_T *eap;
4959{
4960 /*
4961 * If we are sourcing .exrc or .vimrc in current directory we
4962 * print the mappings for security reasons.
4963 */
4964 if (secure)
4965 {
4966 secure = 2;
4967 msg_outtrans(eap->cmd);
4968 msg_putchar('\n');
4969 }
4970 do_exmap(eap, FALSE);
4971}
4972
4973/*
4974 * ":unmap" and friends.
4975 */
4976 static void
4977ex_unmap(eap)
4978 exarg_T *eap;
4979{
4980 do_exmap(eap, FALSE);
4981}
4982
4983/*
4984 * ":mapclear" and friends.
4985 */
4986 static void
4987ex_mapclear(eap)
4988 exarg_T *eap;
4989{
4990 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4991}
4992
4993/*
4994 * ":abclear" and friends.
4995 */
4996 static void
4997ex_abclear(eap)
4998 exarg_T *eap;
4999{
5000 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5001}
5002
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005003#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 static void
5005ex_autocmd(eap)
5006 exarg_T *eap;
5007{
5008 /*
5009 * Disallow auto commands from .exrc and .vimrc in current
5010 * directory for security reasons.
5011 */
5012 if (secure)
5013 {
5014 secure = 2;
5015 eap->errmsg = e_curdir;
5016 }
5017 else if (eap->cmdidx == CMD_autocmd)
5018 do_autocmd(eap->arg, eap->forceit);
5019 else
5020 do_augroup(eap->arg, eap->forceit);
5021}
5022
5023/*
5024 * ":doautocmd": Apply the automatic commands to the current buffer.
5025 */
5026 static void
5027ex_doautocmd(eap)
5028 exarg_T *eap;
5029{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005030 char_u *arg = eap->arg;
5031 int call_do_modelines = check_nomodeline(&arg);
5032
5033 (void)do_doautocmd(arg, TRUE);
5034 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5035 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036}
5037#endif
5038
5039#ifdef FEAT_LISTCMDS
5040/*
5041 * :[N]bunload[!] [N] [bufname] unload buffer
5042 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5043 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5044 */
5045 static void
5046ex_bunload(eap)
5047 exarg_T *eap;
5048{
5049 eap->errmsg = do_bufdel(
5050 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5051 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5052 : DOBUF_UNLOAD, eap->arg,
5053 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5054}
5055
5056/*
5057 * :[N]buffer [N] to buffer N
5058 * :[N]sbuffer [N] to buffer N
5059 */
5060 static void
5061ex_buffer(eap)
5062 exarg_T *eap;
5063{
5064 if (*eap->arg)
5065 eap->errmsg = e_trailing;
5066 else
5067 {
5068 if (eap->addr_count == 0) /* default is current buffer */
5069 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5070 else
5071 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5072 }
5073}
5074
5075/*
5076 * :[N]bmodified [N] to next mod. buffer
5077 * :[N]sbmodified [N] to next mod. buffer
5078 */
5079 static void
5080ex_bmodified(eap)
5081 exarg_T *eap;
5082{
5083 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5084}
5085
5086/*
5087 * :[N]bnext [N] to next buffer
5088 * :[N]sbnext [N] split and to next buffer
5089 */
5090 static void
5091ex_bnext(eap)
5092 exarg_T *eap;
5093{
5094 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5095}
5096
5097/*
5098 * :[N]bNext [N] to previous buffer
5099 * :[N]bprevious [N] to previous buffer
5100 * :[N]sbNext [N] split and to previous buffer
5101 * :[N]sbprevious [N] split and to previous buffer
5102 */
5103 static void
5104ex_bprevious(eap)
5105 exarg_T *eap;
5106{
5107 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5108}
5109
5110/*
5111 * :brewind to first buffer
5112 * :bfirst to first buffer
5113 * :sbrewind split and to first buffer
5114 * :sbfirst split and to first buffer
5115 */
5116 static void
5117ex_brewind(eap)
5118 exarg_T *eap;
5119{
5120 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5121}
5122
5123/*
5124 * :blast to last buffer
5125 * :sblast split and to last buffer
5126 */
5127 static void
5128ex_blast(eap)
5129 exarg_T *eap;
5130{
5131 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5132}
5133#endif
5134
5135 int
5136ends_excmd(c)
5137 int c;
5138{
5139 return (c == NUL || c == '|' || c == '"' || c == '\n');
5140}
5141
5142#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5143 || defined(PROTO)
5144/*
5145 * Return the next command, after the first '|' or '\n'.
5146 * Return NULL if not found.
5147 */
5148 char_u *
5149find_nextcmd(p)
5150 char_u *p;
5151{
5152 while (*p != '|' && *p != '\n')
5153 {
5154 if (*p == NUL)
5155 return NULL;
5156 ++p;
5157 }
5158 return (p + 1);
5159}
5160#endif
5161
5162/*
5163 * Check if *p is a separator between Ex commands.
5164 * Return NULL if it isn't, (p + 1) if it is.
5165 */
5166 char_u *
5167check_nextcmd(p)
5168 char_u *p;
5169{
5170 p = skipwhite(p);
5171 if (*p == '|' || *p == '\n')
5172 return (p + 1);
5173 else
5174 return NULL;
5175}
5176
5177/*
5178 * - if there are more files to edit
5179 * - and this is the last window
5180 * - and forceit not used
5181 * - and not repeated twice on a row
5182 * return FAIL and give error message if 'message' TRUE
5183 * return OK otherwise
5184 */
5185 static int
5186check_more(message, forceit)
5187 int message; /* when FALSE check only, no messages */
5188 int forceit;
5189{
5190 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5191
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005192 if (!forceit && only_one_window()
5193 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 if (message)
5196 {
5197#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5198 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5199 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005200 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005203 vim_strncpy(buff,
5204 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005205 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005207 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 _("%d more files to edit. Quit anyway?"), n);
5209 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5210 return OK;
5211 return FAIL;
5212 }
5213#endif
5214 if (n == 1)
5215 EMSG(_("E173: 1 more file to edit"));
5216 else
5217 EMSGN(_("E173: %ld more files to edit"), n);
5218 quitmore = 2; /* next try to quit is allowed */
5219 }
5220 return FAIL;
5221 }
5222 return OK;
5223}
5224
5225#ifdef FEAT_CMDL_COMPL
5226/*
5227 * Function given to ExpandGeneric() to obtain the list of command names.
5228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 char_u *
5230get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005231 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 int idx;
5233{
5234 if (idx >= (int)CMD_SIZE)
5235# ifdef FEAT_USR_CMDS
5236 return get_user_command_name(idx);
5237# else
5238 return NULL;
5239# endif
5240 return cmdnames[idx].cmd_name;
5241}
5242#endif
5243
5244#if defined(FEAT_USR_CMDS) || defined(PROTO)
5245static 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));
5246static void uc_list __ARGS((char_u *name, size_t name_len));
5247static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5248static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5249static 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));
5250
5251 static int
5252uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5253 char_u *name;
5254 size_t name_len;
5255 char_u *rep;
5256 long argt;
5257 long def;
5258 int flags;
5259 int compl;
5260 char_u *compl_arg;
5261 int force;
5262{
5263 ucmd_T *cmd = NULL;
5264 char_u *p;
5265 int i;
5266 int cmp = 1;
5267 char_u *rep_buf = NULL;
5268 garray_T *gap;
5269
Bram Moolenaar9c102382006-05-03 21:26:49 +00005270 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if (rep_buf == NULL)
5272 {
5273 /* Can't replace termcodes - try using the string as is */
5274 rep_buf = vim_strsave(rep);
5275
5276 /* Give up if out of memory */
5277 if (rep_buf == NULL)
5278 return FAIL;
5279 }
5280
5281 /* get address of growarray: global or in curbuf */
5282 if (flags & UC_BUFFER)
5283 {
5284 gap = &curbuf->b_ucmds;
5285 if (gap->ga_itemsize == 0)
5286 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5287 }
5288 else
5289 gap = &ucmds;
5290
5291 /* Search for the command in the already defined commands. */
5292 for (i = 0; i < gap->ga_len; ++i)
5293 {
5294 size_t len;
5295
5296 cmd = USER_CMD_GA(gap, i);
5297 len = STRLEN(cmd->uc_name);
5298 cmp = STRNCMP(name, cmd->uc_name, name_len);
5299 if (cmp == 0)
5300 {
5301 if (name_len < len)
5302 cmp = -1;
5303 else if (name_len > len)
5304 cmp = 1;
5305 }
5306
5307 if (cmp == 0)
5308 {
5309 if (!force)
5310 {
5311 EMSG(_("E174: Command already exists: add ! to replace it"));
5312 goto fail;
5313 }
5314
5315 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005316 cmd->uc_rep = NULL;
5317#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5318 vim_free(cmd->uc_compl_arg);
5319 cmd->uc_compl_arg = NULL;
5320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 break;
5322 }
5323
5324 /* Stop as soon as we pass the name to add */
5325 if (cmp < 0)
5326 break;
5327 }
5328
5329 /* Extend the array unless we're replacing an existing command */
5330 if (cmp != 0)
5331 {
5332 if (ga_grow(gap, 1) != OK)
5333 goto fail;
5334 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5335 goto fail;
5336
5337 cmd = USER_CMD_GA(gap, i);
5338 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5339
5340 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 cmd->uc_name = p;
5343 }
5344
5345 cmd->uc_rep = rep_buf;
5346 cmd->uc_argt = argt;
5347 cmd->uc_def = def;
5348 cmd->uc_compl = compl;
5349#ifdef FEAT_EVAL
5350 cmd->uc_scriptID = current_SID;
5351# ifdef FEAT_CMDL_COMPL
5352 cmd->uc_compl_arg = compl_arg;
5353# endif
5354#endif
5355
5356 return OK;
5357
5358fail:
5359 vim_free(rep_buf);
5360#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5361 vim_free(compl_arg);
5362#endif
5363 return FAIL;
5364}
5365
5366/*
5367 * List of names for completion for ":command" with the EXPAND_ flag.
5368 * Must be alphabetical for completion.
5369 */
5370static struct
5371{
5372 int expand;
5373 char *name;
5374} command_complete[] =
5375{
5376 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005377 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005379 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005381 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005382#if defined(FEAT_CSCOPE)
5383 {EXPAND_CSCOPE, "cscope"},
5384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5386 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005387 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388#endif
5389 {EXPAND_DIRECTORIES, "dir"},
5390 {EXPAND_ENV_VARS, "environment"},
5391 {EXPAND_EVENTS, "event"},
5392 {EXPAND_EXPRESSION, "expression"},
5393 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005394 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005395 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {EXPAND_FUNCTIONS, "function"},
5397 {EXPAND_HELP, "help"},
5398 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005399#if defined(FEAT_CMDHIST)
5400 {EXPAND_HISTORY, "history"},
5401#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005402#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005403 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005404 {EXPAND_LOCALES, "locale"},
5405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {EXPAND_MAPPINGS, "mapping"},
5407 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005408 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005410 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005411#if defined(FEAT_SIGNS)
5412 {EXPAND_SIGN, "sign"},
5413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {EXPAND_TAGS, "tag"},
5415 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005416 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {EXPAND_USER_VARS, "var"},
5418 {0, NULL}
5419};
5420
5421 static void
5422uc_list(name, name_len)
5423 char_u *name;
5424 size_t name_len;
5425{
5426 int i, j;
5427 int found = FALSE;
5428 ucmd_T *cmd;
5429 int len;
5430 long a;
5431 garray_T *gap;
5432
5433 gap = &curbuf->b_ucmds;
5434 for (;;)
5435 {
5436 for (i = 0; i < gap->ga_len; ++i)
5437 {
5438 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005439 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 /* Skip commands which don't match the requested prefix */
5442 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5443 continue;
5444
5445 /* Put out the title first time */
5446 if (!found)
5447 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5448 found = TRUE;
5449 msg_putchar('\n');
5450 if (got_int)
5451 break;
5452
5453 /* Special cases */
5454 msg_putchar(a & BANG ? '!' : ' ');
5455 msg_putchar(a & REGSTR ? '"' : ' ');
5456 msg_putchar(gap != &ucmds ? 'b' : ' ');
5457 msg_putchar(' ');
5458
5459 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5460 len = (int)STRLEN(cmd->uc_name) + 4;
5461
5462 do {
5463 msg_putchar(' ');
5464 ++len;
5465 } while (len < 16);
5466
5467 len = 0;
5468
5469 /* Arguments */
5470 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5471 {
5472 case 0: IObuff[len++] = '0'; break;
5473 case (EXTRA): IObuff[len++] = '*'; break;
5474 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5475 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5476 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5477 }
5478
5479 do {
5480 IObuff[len++] = ' ';
5481 } while (len < 5);
5482
5483 /* Range */
5484 if (a & (RANGE|COUNT))
5485 {
5486 if (a & COUNT)
5487 {
5488 /* -count=N */
5489 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5490 len += (int)STRLEN(IObuff + len);
5491 }
5492 else if (a & DFLALL)
5493 IObuff[len++] = '%';
5494 else if (cmd->uc_def >= 0)
5495 {
5496 /* -range=N */
5497 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5498 len += (int)STRLEN(IObuff + len);
5499 }
5500 else
5501 IObuff[len++] = '.';
5502 }
5503
5504 do {
5505 IObuff[len++] = ' ';
5506 } while (len < 11);
5507
5508 /* Completion */
5509 for (j = 0; command_complete[j].expand != 0; ++j)
5510 if (command_complete[j].expand == cmd->uc_compl)
5511 {
5512 STRCPY(IObuff + len, command_complete[j].name);
5513 len += (int)STRLEN(IObuff + len);
5514 break;
5515 }
5516
5517 do {
5518 IObuff[len++] = ' ';
5519 } while (len < 21);
5520
5521 IObuff[len] = '\0';
5522 msg_outtrans(IObuff);
5523
5524 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005525#ifdef FEAT_EVAL
5526 if (p_verbose > 0)
5527 last_set_msg(cmd->uc_scriptID);
5528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 out_flush();
5530 ui_breakcheck();
5531 if (got_int)
5532 break;
5533 }
5534 if (gap == &ucmds || i < gap->ga_len)
5535 break;
5536 gap = &ucmds;
5537 }
5538
5539 if (!found)
5540 MSG(_("No user-defined commands found"));
5541}
5542
5543 static char_u *
5544uc_fun_cmd()
5545{
5546 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5547 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5548 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5549 0xb9, 0x7f, 0};
5550 int i;
5551
5552 for (i = 0; fcmd[i]; ++i)
5553 IObuff[i] = fcmd[i] - 0x40;
5554 IObuff[i] = 0;
5555 return IObuff;
5556}
5557
5558 static int
5559uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5560 char_u *attr;
5561 size_t len;
5562 long *argt;
5563 long *def;
5564 int *flags;
5565 int *compl;
5566 char_u **compl_arg;
5567{
5568 char_u *p;
5569
5570 if (len == 0)
5571 {
5572 EMSG(_("E175: No attribute specified"));
5573 return FAIL;
5574 }
5575
5576 /* First, try the simple attributes (no arguments) */
5577 if (STRNICMP(attr, "bang", len) == 0)
5578 *argt |= BANG;
5579 else if (STRNICMP(attr, "buffer", len) == 0)
5580 *flags |= UC_BUFFER;
5581 else if (STRNICMP(attr, "register", len) == 0)
5582 *argt |= REGSTR;
5583 else if (STRNICMP(attr, "bar", len) == 0)
5584 *argt |= TRLBAR;
5585 else
5586 {
5587 int i;
5588 char_u *val = NULL;
5589 size_t vallen = 0;
5590 size_t attrlen = len;
5591
5592 /* Look for the attribute name - which is the part before any '=' */
5593 for (i = 0; i < (int)len; ++i)
5594 {
5595 if (attr[i] == '=')
5596 {
5597 val = &attr[i + 1];
5598 vallen = len - i - 1;
5599 attrlen = i;
5600 break;
5601 }
5602 }
5603
5604 if (STRNICMP(attr, "nargs", attrlen) == 0)
5605 {
5606 if (vallen == 1)
5607 {
5608 if (*val == '0')
5609 /* Do nothing - this is the default */;
5610 else if (*val == '1')
5611 *argt |= (EXTRA | NOSPC | NEEDARG);
5612 else if (*val == '*')
5613 *argt |= EXTRA;
5614 else if (*val == '?')
5615 *argt |= (EXTRA | NOSPC);
5616 else if (*val == '+')
5617 *argt |= (EXTRA | NEEDARG);
5618 else
5619 goto wrong_nargs;
5620 }
5621 else
5622 {
5623wrong_nargs:
5624 EMSG(_("E176: Invalid number of arguments"));
5625 return FAIL;
5626 }
5627 }
5628 else if (STRNICMP(attr, "range", attrlen) == 0)
5629 {
5630 *argt |= RANGE;
5631 if (vallen == 1 && *val == '%')
5632 *argt |= DFLALL;
5633 else if (val != NULL)
5634 {
5635 p = val;
5636 if (*def >= 0)
5637 {
5638two_count:
5639 EMSG(_("E177: Count cannot be specified twice"));
5640 return FAIL;
5641 }
5642
5643 *def = getdigits(&p);
5644 *argt |= (ZEROR | NOTADR);
5645
5646 if (p != val + vallen || vallen == 0)
5647 {
5648invalid_count:
5649 EMSG(_("E178: Invalid default value for count"));
5650 return FAIL;
5651 }
5652 }
5653 }
5654 else if (STRNICMP(attr, "count", attrlen) == 0)
5655 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005656 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 if (val != NULL)
5659 {
5660 p = val;
5661 if (*def >= 0)
5662 goto two_count;
5663
5664 *def = getdigits(&p);
5665
5666 if (p != val + vallen)
5667 goto invalid_count;
5668 }
5669
5670 if (*def < 0)
5671 *def = 0;
5672 }
5673 else if (STRNICMP(attr, "complete", attrlen) == 0)
5674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (val == NULL)
5676 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005677 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 return FAIL;
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005681 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5682 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else
5686 {
5687 char_u ch = attr[len];
5688 attr[len] = '\0';
5689 EMSG2(_("E181: Invalid attribute: %s"), attr);
5690 attr[len] = ch;
5691 return FAIL;
5692 }
5693 }
5694
5695 return OK;
5696}
5697
Bram Moolenaara850a712009-01-28 14:42:59 +00005698/*
5699 * ":command ..."
5700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 static void
5702ex_command(eap)
5703 exarg_T *eap;
5704{
5705 char_u *name;
5706 char_u *end;
5707 char_u *p;
5708 long argt = 0;
5709 long def = -1;
5710 int flags = 0;
5711 int compl = EXPAND_NOTHING;
5712 char_u *compl_arg = NULL;
5713 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005714 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716 p = eap->arg;
5717
5718 /* Check for attributes */
5719 while (*p == '-')
5720 {
5721 ++p;
5722 end = skiptowhite(p);
5723 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5724 == FAIL)
5725 return;
5726 p = skipwhite(end);
5727 }
5728
5729 /* Get the name (if any) and skip to the following argument */
5730 name = p;
5731 if (ASCII_ISALPHA(*p))
5732 while (ASCII_ISALNUM(*p))
5733 ++p;
5734 if (!ends_excmd(*p) && !vim_iswhite(*p))
5735 {
5736 EMSG(_("E182: Invalid command name"));
5737 return;
5738 }
5739 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005740 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 /* If there is nothing after the name, and no attributes were specified,
5743 * we are listing commands
5744 */
5745 p = skipwhite(end);
5746 if (!has_attr && ends_excmd(*p))
5747 {
5748 uc_list(name, end - name);
5749 }
5750 else if (!ASCII_ISUPPER(*name))
5751 {
5752 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5753 return;
5754 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005755 else if ((name_len == 1 && *name == 'X')
5756 || (name_len <= 4
5757 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5758 {
5759 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5760 return;
5761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 else
5763 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5764 eap->forceit);
5765}
5766
5767/*
5768 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 * Clear all user commands, global and for current buffer.
5770 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005771 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005773 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 uc_clear(&ucmds);
5776 uc_clear(&curbuf->b_ucmds);
5777}
5778
5779/*
5780 * Clear all user commands for "gap".
5781 */
5782 void
5783uc_clear(gap)
5784 garray_T *gap;
5785{
5786 int i;
5787 ucmd_T *cmd;
5788
5789 for (i = 0; i < gap->ga_len; ++i)
5790 {
5791 cmd = USER_CMD_GA(gap, i);
5792 vim_free(cmd->uc_name);
5793 vim_free(cmd->uc_rep);
5794# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5795 vim_free(cmd->uc_compl_arg);
5796# endif
5797 }
5798 ga_clear(gap);
5799}
5800
5801 static void
5802ex_delcommand(eap)
5803 exarg_T *eap;
5804{
5805 int i = 0;
5806 ucmd_T *cmd = NULL;
5807 int cmp = -1;
5808 garray_T *gap;
5809
5810 gap = &curbuf->b_ucmds;
5811 for (;;)
5812 {
5813 for (i = 0; i < gap->ga_len; ++i)
5814 {
5815 cmd = USER_CMD_GA(gap, i);
5816 cmp = STRCMP(eap->arg, cmd->uc_name);
5817 if (cmp <= 0)
5818 break;
5819 }
5820 if (gap == &ucmds || cmp == 0)
5821 break;
5822 gap = &ucmds;
5823 }
5824
5825 if (cmp != 0)
5826 {
5827 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5828 return;
5829 }
5830
5831 vim_free(cmd->uc_name);
5832 vim_free(cmd->uc_rep);
5833# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5834 vim_free(cmd->uc_compl_arg);
5835# endif
5836
5837 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839 if (i < gap->ga_len)
5840 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5841}
5842
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005843/*
5844 * split and quote args for <f-args>
5845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 static char_u *
5847uc_split_args(arg, lenp)
5848 char_u *arg;
5849 size_t *lenp;
5850{
5851 char_u *buf;
5852 char_u *p;
5853 char_u *q;
5854 int len;
5855
5856 /* Precalculate length */
5857 p = arg;
5858 len = 2; /* Initial and final quotes */
5859
5860 while (*p)
5861 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005862 if (p[0] == '\\' && p[1] == '\\')
5863 {
5864 len += 2;
5865 p += 2;
5866 }
5867 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
5869 len += 1;
5870 p += 2;
5871 }
5872 else if (*p == '\\' || *p == '"')
5873 {
5874 len += 2;
5875 p += 1;
5876 }
5877 else if (vim_iswhite(*p))
5878 {
5879 p = skipwhite(p);
5880 if (*p == NUL)
5881 break;
5882 len += 3; /* "," */
5883 }
5884 else
5885 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005886#ifdef FEAT_MBYTE
5887 int charlen = (*mb_ptr2len)(p);
5888 len += charlen;
5889 p += charlen;
5890#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 ++len;
5892 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 }
5895 }
5896
5897 buf = alloc(len + 1);
5898 if (buf == NULL)
5899 {
5900 *lenp = 0;
5901 return buf;
5902 }
5903
5904 p = arg;
5905 q = buf;
5906 *q++ = '"';
5907 while (*p)
5908 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005909 if (p[0] == '\\' && p[1] == '\\')
5910 {
5911 *q++ = '\\';
5912 *q++ = '\\';
5913 p += 2;
5914 }
5915 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
5917 *q++ = p[1];
5918 p += 2;
5919 }
5920 else if (*p == '\\' || *p == '"')
5921 {
5922 *q++ = '\\';
5923 *q++ = *p++;
5924 }
5925 else if (vim_iswhite(*p))
5926 {
5927 p = skipwhite(p);
5928 if (*p == NUL)
5929 break;
5930 *q++ = '"';
5931 *q++ = ',';
5932 *q++ = '"';
5933 }
5934 else
5935 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005936 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 }
5939 *q++ = '"';
5940 *q = 0;
5941
5942 *lenp = len;
5943 return buf;
5944}
5945
5946/*
5947 * Check for a <> code in a user command.
5948 * "code" points to the '<'. "len" the length of the <> (inclusive).
5949 * "buf" is where the result is to be added.
5950 * "split_buf" points to a buffer used for splitting, caller should free it.
5951 * "split_len" is the length of what "split_buf" contains.
5952 * Returns the length of the replacement, which has been added to "buf".
5953 * Returns -1 if there was no match, and only the "<" has been copied.
5954 */
5955 static size_t
5956uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5957 char_u *code;
5958 size_t len;
5959 char_u *buf;
5960 ucmd_T *cmd; /* the user command we're expanding */
5961 exarg_T *eap; /* ex arguments */
5962 char_u **split_buf;
5963 size_t *split_len;
5964{
5965 size_t result = 0;
5966 char_u *p = code + 1;
5967 size_t l = len - 2;
5968 int quote = 0;
5969 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5970 ct_LT, ct_NONE } type = ct_NONE;
5971
5972 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5973 {
5974 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5975 p += 2;
5976 l -= 2;
5977 }
5978
Bram Moolenaar371d5402006-03-20 21:47:49 +00005979 ++l;
5980 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005982 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005984 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005986 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005988 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005990 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005992 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005994 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 type = ct_REGISTER;
5996
5997 switch (type)
5998 {
5999 case ct_ARGS:
6000 /* Simple case first */
6001 if (*eap->arg == NUL)
6002 {
6003 if (quote == 1)
6004 {
6005 result = 2;
6006 if (buf != NULL)
6007 STRCPY(buf, "''");
6008 }
6009 else
6010 result = 0;
6011 break;
6012 }
6013
6014 /* When specified there is a single argument don't split it.
6015 * Works for ":Cmd %" when % is "a b c". */
6016 if ((eap->argt & NOSPC) && quote == 2)
6017 quote = 1;
6018
6019 switch (quote)
6020 {
6021 case 0: /* No quoting, no splitting */
6022 result = STRLEN(eap->arg);
6023 if (buf != NULL)
6024 STRCPY(buf, eap->arg);
6025 break;
6026 case 1: /* Quote, but don't split */
6027 result = STRLEN(eap->arg) + 2;
6028 for (p = eap->arg; *p; ++p)
6029 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006030#ifdef FEAT_MBYTE
6031 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6032 /* DBCS can contain \ in a trail byte, skip the
6033 * double-byte character. */
6034 ++p;
6035 else
6036#endif
6037 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 ++result;
6039 }
6040
6041 if (buf != NULL)
6042 {
6043 *buf++ = '"';
6044 for (p = eap->arg; *p; ++p)
6045 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006046#ifdef FEAT_MBYTE
6047 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6048 /* DBCS can contain \ in a trail byte, copy the
6049 * double-byte character to avoid escaping. */
6050 *buf++ = *p++;
6051 else
6052#endif
6053 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 *buf++ = '\\';
6055 *buf++ = *p;
6056 }
6057 *buf = '"';
6058 }
6059
6060 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006061 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 /* This is hard, so only do it once, and cache the result */
6063 if (*split_buf == NULL)
6064 *split_buf = uc_split_args(eap->arg, split_len);
6065
6066 result = *split_len;
6067 if (buf != NULL && result != 0)
6068 STRCPY(buf, *split_buf);
6069
6070 break;
6071 }
6072 break;
6073
6074 case ct_BANG:
6075 result = eap->forceit ? 1 : 0;
6076 if (quote)
6077 result += 2;
6078 if (buf != NULL)
6079 {
6080 if (quote)
6081 *buf++ = '"';
6082 if (eap->forceit)
6083 *buf++ = '!';
6084 if (quote)
6085 *buf = '"';
6086 }
6087 break;
6088
6089 case ct_LINE1:
6090 case ct_LINE2:
6091 case ct_COUNT:
6092 {
6093 char num_buf[20];
6094 long num = (type == ct_LINE1) ? eap->line1 :
6095 (type == ct_LINE2) ? eap->line2 :
6096 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6097 size_t num_len;
6098
6099 sprintf(num_buf, "%ld", num);
6100 num_len = STRLEN(num_buf);
6101 result = num_len;
6102
6103 if (quote)
6104 result += 2;
6105
6106 if (buf != NULL)
6107 {
6108 if (quote)
6109 *buf++ = '"';
6110 STRCPY(buf, num_buf);
6111 buf += num_len;
6112 if (quote)
6113 *buf = '"';
6114 }
6115
6116 break;
6117 }
6118
6119 case ct_REGISTER:
6120 result = eap->regname ? 1 : 0;
6121 if (quote)
6122 result += 2;
6123 if (buf != NULL)
6124 {
6125 if (quote)
6126 *buf++ = '\'';
6127 if (eap->regname)
6128 *buf++ = eap->regname;
6129 if (quote)
6130 *buf = '\'';
6131 }
6132 break;
6133
6134 case ct_LT:
6135 result = 1;
6136 if (buf != NULL)
6137 *buf = '<';
6138 break;
6139
6140 default:
6141 /* Not recognized: just copy the '<' and return -1. */
6142 result = (size_t)-1;
6143 if (buf != NULL)
6144 *buf = '<';
6145 break;
6146 }
6147
6148 return result;
6149}
6150
6151 static void
6152do_ucmd(eap)
6153 exarg_T *eap;
6154{
6155 char_u *buf;
6156 char_u *p;
6157 char_u *q;
6158
6159 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006160 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006161 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 size_t len, totlen;
6163
6164 size_t split_len = 0;
6165 char_u *split_buf = NULL;
6166 ucmd_T *cmd;
6167#ifdef FEAT_EVAL
6168 scid_T save_current_SID = current_SID;
6169#endif
6170
6171 if (eap->cmdidx == CMD_USER)
6172 cmd = USER_CMD(eap->useridx);
6173 else
6174 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6175
6176 /*
6177 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006178 * First round: "buf" is NULL, compute length, allocate "buf".
6179 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 */
6181 buf = NULL;
6182 for (;;)
6183 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006184 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006185 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006187
6188 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006190 start = vim_strchr(p, '<');
6191 if (start != NULL)
6192 end = vim_strchr(start + 1, '>');
6193 if (buf != NULL)
6194 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006195 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6196 ;
6197 if (*ksp == K_SPECIAL
6198 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006199 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6200# ifdef FEAT_GUI
6201 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6202# endif
6203 ))
6204 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006205 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006206 * KS_SPECIAL KE_FILLER, like for mappings, but
6207 * do_cmdline() doesn't handle that, so convert it back.
6208 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6209 len = ksp - p;
6210 if (len > 0)
6211 {
6212 mch_memmove(q, p, len);
6213 q += len;
6214 }
6215 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6216 p = ksp + 3;
6217 continue;
6218 }
6219 }
6220
6221 /* break if there no <item> is found */
6222 if (start == NULL || end == NULL)
6223 break;
6224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 /* Include the '>' */
6226 ++end;
6227
6228 /* Take everything up to the '<' */
6229 len = start - p;
6230 if (buf == NULL)
6231 totlen += len;
6232 else
6233 {
6234 mch_memmove(q, p, len);
6235 q += len;
6236 }
6237
6238 len = uc_check_code(start, end - start, q, cmd, eap,
6239 &split_buf, &split_len);
6240 if (len == (size_t)-1)
6241 {
6242 /* no match, continue after '<' */
6243 p = start + 1;
6244 len = 1;
6245 }
6246 else
6247 p = end;
6248 if (buf == NULL)
6249 totlen += len;
6250 else
6251 q += len;
6252 }
6253 if (buf != NULL) /* second time here, finished */
6254 {
6255 STRCPY(q, p);
6256 break;
6257 }
6258
6259 totlen += STRLEN(p); /* Add on the trailing characters */
6260 buf = alloc((unsigned)(totlen + 1));
6261 if (buf == NULL)
6262 {
6263 vim_free(split_buf);
6264 return;
6265 }
6266 }
6267
6268#ifdef FEAT_EVAL
6269 current_SID = cmd->uc_scriptID;
6270#endif
6271 (void)do_cmdline(buf, eap->getline, eap->cookie,
6272 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6273#ifdef FEAT_EVAL
6274 current_SID = save_current_SID;
6275#endif
6276 vim_free(buf);
6277 vim_free(split_buf);
6278}
6279
6280# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6281 static char_u *
6282get_user_command_name(idx)
6283 int idx;
6284{
6285 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6286}
6287
6288/*
6289 * Function given to ExpandGeneric() to obtain the list of user command names.
6290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 char_u *
6292get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006293 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 int idx;
6295{
6296 if (idx < curbuf->b_ucmds.ga_len)
6297 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6298 idx -= curbuf->b_ucmds.ga_len;
6299 if (idx < ucmds.ga_len)
6300 return USER_CMD(idx)->uc_name;
6301 return NULL;
6302}
6303
6304/*
6305 * Function given to ExpandGeneric() to obtain the list of user command
6306 * attributes.
6307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 char_u *
6309get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006310 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 int idx;
6312{
6313 static char *user_cmd_flags[] =
6314 {"bang", "bar", "buffer", "complete", "count",
6315 "nargs", "range", "register"};
6316
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006317 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 return NULL;
6319 return (char_u *)user_cmd_flags[idx];
6320}
6321
6322/*
6323 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 char_u *
6326get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006327 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 int idx;
6329{
6330 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6331
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006332 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 return NULL;
6334 return (char_u *)user_cmd_nargs[idx];
6335}
6336
6337/*
6338 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 char_u *
6341get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006342 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 int idx;
6344{
6345 return (char_u *)command_complete[idx].name;
6346}
6347# endif /* FEAT_CMDL_COMPL */
6348
6349#endif /* FEAT_USR_CMDS */
6350
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006351#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6352/*
6353 * Parse a completion argument "value[vallen]".
6354 * The detected completion goes in "*complp", argument type in "*argt".
6355 * When there is an argument, for function and user defined completion, it's
6356 * copied to allocated memory and stored in "*compl_arg".
6357 * Returns FAIL if something is wrong.
6358 */
6359 int
6360parse_compl_arg(value, vallen, complp, argt, compl_arg)
6361 char_u *value;
6362 int vallen;
6363 int *complp;
6364 long *argt;
6365 char_u **compl_arg;
6366{
6367 char_u *arg = NULL;
6368 size_t arglen = 0;
6369 int i;
6370 int valend = vallen;
6371
6372 /* Look for any argument part - which is the part after any ',' */
6373 for (i = 0; i < vallen; ++i)
6374 {
6375 if (value[i] == ',')
6376 {
6377 arg = &value[i + 1];
6378 arglen = vallen - i - 1;
6379 valend = i;
6380 break;
6381 }
6382 }
6383
6384 for (i = 0; command_complete[i].expand != 0; ++i)
6385 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006386 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006387 && STRNCMP(value, command_complete[i].name, valend) == 0)
6388 {
6389 *complp = command_complete[i].expand;
6390 if (command_complete[i].expand == EXPAND_BUFFERS)
6391 *argt |= BUFNAME;
6392 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6393 || command_complete[i].expand == EXPAND_FILES)
6394 *argt |= XFILE;
6395 break;
6396 }
6397 }
6398
6399 if (command_complete[i].expand == 0)
6400 {
6401 EMSG2(_("E180: Invalid complete value: %s"), value);
6402 return FAIL;
6403 }
6404
6405# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6406 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6407 && arg != NULL)
6408# else
6409 if (arg != NULL)
6410# endif
6411 {
6412 EMSG(_("E468: Completion argument only allowed for custom completion"));
6413 return FAIL;
6414 }
6415
6416# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6417 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6418 && arg == NULL)
6419 {
6420 EMSG(_("E467: Custom completion requires a function argument"));
6421 return FAIL;
6422 }
6423
6424 if (arg != NULL)
6425 *compl_arg = vim_strnsave(arg, (int)arglen);
6426# endif
6427 return OK;
6428}
6429#endif
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 static void
6432ex_colorscheme(eap)
6433 exarg_T *eap;
6434{
Bram Moolenaare6850792010-05-14 15:28:44 +02006435 if (*eap->arg == NUL)
6436 {
6437#ifdef FEAT_EVAL
6438 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6439 char_u *p = NULL;
6440
6441 if (expr != NULL)
6442 {
6443 ++emsg_off;
6444 p = eval_to_string(expr, NULL, FALSE);
6445 --emsg_off;
6446 vim_free(expr);
6447 }
6448 if (p != NULL)
6449 {
6450 MSG(p);
6451 vim_free(p);
6452 }
6453 else
6454 MSG("default");
6455#else
6456 MSG(_("unknown"));
6457#endif
6458 }
6459 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6461}
6462
6463 static void
6464ex_highlight(eap)
6465 exarg_T *eap;
6466{
6467 if (*eap->arg == NUL && eap->cmd[2] == '!')
6468 MSG(_("Greetings, Vim user!"));
6469 do_highlight(eap->arg, eap->forceit, FALSE);
6470}
6471
6472
6473/*
6474 * Call this function if we thought we were going to exit, but we won't
6475 * (because of an error). May need to restore the terminal mode.
6476 */
6477 void
6478not_exiting()
6479{
6480 exiting = FALSE;
6481 settmode(TMODE_RAW);
6482}
6483
6484/*
6485 * ":quit": quit current window, quit Vim if closed the last window.
6486 */
6487 static void
6488ex_quit(eap)
6489 exarg_T *eap;
6490{
6491#ifdef FEAT_CMDWIN
6492 if (cmdwin_type != 0)
6493 {
6494 cmdwin_result = Ctrl_C;
6495 return;
6496 }
6497#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006498 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006499 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006500 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006501 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006502 return;
6503 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006504#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006505 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02006506 /* Refuse to quick when locked or when the buffer in the last window is
6507 * being closed (can only happen in autocommands). */
6508 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006509 return;
6510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512#ifdef FEAT_NETBEANS_INTG
6513 netbeansForcedQuit = eap->forceit;
6514#endif
6515
6516 /*
6517 * If there are more files or windows we won't exit.
6518 */
6519 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6520 exiting = TRUE;
6521 if ((!P_HID(curbuf)
6522 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6523 || check_more(TRUE, eap->forceit) == FAIL
6524 || (only_one_window() && check_changed_any(eap->forceit)))
6525 {
6526 not_exiting();
6527 }
6528 else
6529 {
6530#ifdef FEAT_WINDOWS
6531 if (only_one_window()) /* quit last window */
6532#endif
6533 getout(0);
6534#ifdef FEAT_WINDOWS
6535# ifdef FEAT_GUI
6536 need_mouse_correct = TRUE;
6537# endif
6538 /* close window; may free buffer */
6539 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6540#endif
6541 }
6542}
6543
6544/*
6545 * ":cquit".
6546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 static void
6548ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006549 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 getout(1); /* this does not always pass on the exit code to the Manx
6552 compiler. why? */
6553}
6554
6555/*
6556 * ":qall": try to quit all windows
6557 */
6558 static void
6559ex_quit_all(eap)
6560 exarg_T *eap;
6561{
6562# ifdef FEAT_CMDWIN
6563 if (cmdwin_type != 0)
6564 {
6565 if (eap->forceit)
6566 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6567 else
6568 cmdwin_result = K_XF2;
6569 return;
6570 }
6571# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006572
6573 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006574 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006575 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006576 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006577 return;
6578 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006579#ifdef FEAT_AUTOCMD
6580 if (curbuf_locked())
6581 return;
6582#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006583
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 exiting = TRUE;
6585 if (eap->forceit || !check_changed_any(FALSE))
6586 getout(0);
6587 not_exiting();
6588}
6589
Bram Moolenaard9967712006-03-11 21:18:15 +00006590#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591/*
6592 * ":close": close current window, unless it is the last one
6593 */
6594 static void
6595ex_close(eap)
6596 exarg_T *eap;
6597{
6598# ifdef FEAT_CMDWIN
6599 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006600 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 else
6602# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006603 if (!text_locked()
6604#ifdef FEAT_AUTOCMD
6605 && !curbuf_locked()
6606#endif
6607 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006608 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
6610
Bram Moolenaard9967712006-03-11 21:18:15 +00006611# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006612/*
6613 * ":pclose": Close any preview window.
6614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006618{
6619 win_T *win;
6620
6621 for (win = firstwin; win != NULL; win = win->w_next)
6622 if (win->w_p_pvw)
6623 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006624 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006625 break;
6626 }
6627}
Bram Moolenaard9967712006-03-11 21:18:15 +00006628# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629
Bram Moolenaarf740b292006-02-16 22:11:02 +00006630/*
6631 * Close window "win" and take care of handling closing the last window for a
6632 * modified buffer.
6633 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006634 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006635ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006636 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006638 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 int need_hide;
6641 buf_T *buf = win->w_buffer;
6642
6643 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006644 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006646# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if ((p_confirm || cmdmod.confirm) && p_write)
6648 {
6649 dialog_changed(buf, FALSE);
6650 if (buf_valid(buf) && bufIsChanged(buf))
6651 return;
6652 need_hide = FALSE;
6653 }
6654 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
6657 EMSG(_(e_nowrtmsg));
6658 return;
6659 }
6660 }
6661
Bram Moolenaard9967712006-03-11 21:18:15 +00006662# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006664# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006667 if (tp == NULL)
6668 win_close(win, !need_hide && !P_HID(buf));
6669 else
6670 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671}
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006674 * ":tabclose": close current tab page, unless it is the last one.
6675 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 */
6677 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006678ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 exarg_T *eap;
6680{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006681 tabpage_T *tp;
6682
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683# ifdef FEAT_CMDWIN
6684 if (cmdwin_type != 0)
6685 cmdwin_result = K_IGNORE;
6686 else
6687# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006688 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006689 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006692 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006694 tp = find_tabpage((int)eap->line2);
6695 if (tp == NULL)
6696 {
6697 beep_flush();
6698 return;
6699 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006700 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 {
6702 tabpage_close_other(tp, eap->forceit);
6703 return;
6704 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006705 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006706 if (!text_locked()
6707#ifdef FEAT_AUTOCMD
6708 && !curbuf_locked()
6709#endif
6710 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006711 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006713}
6714
6715/*
6716 * ":tabonly": close all tab pages except the current one
6717 */
6718 static void
6719ex_tabonly(eap)
6720 exarg_T *eap;
6721{
6722 tabpage_T *tp;
6723 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006724
6725# ifdef FEAT_CMDWIN
6726 if (cmdwin_type != 0)
6727 cmdwin_result = K_IGNORE;
6728 else
6729# endif
6730 if (first_tabpage->tp_next == NULL)
6731 MSG(_("Already only one tab page"));
6732 else
6733 {
6734 /* Repeat this up to a 1000 times, because autocommands may mess
6735 * up the lists. */
6736 for (done = 0; done < 1000; ++done)
6737 {
6738 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6739 if (tp->tp_topframe != topframe)
6740 {
6741 tabpage_close_other(tp, eap->forceit);
6742 /* if we failed to close it quit */
6743 if (valid_tabpage(tp))
6744 done = 1000;
6745 /* start over, "tp" is now invalid */
6746 break;
6747 }
6748 if (first_tabpage->tp_next == NULL)
6749 break;
6750 }
6751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
6754/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006755 * Close the current tab page.
6756 */
6757 void
6758tabpage_close(forceit)
6759 int forceit;
6760{
6761 /* First close all the windows but the current one. If that worked then
6762 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006763 if (lastwin != firstwin)
6764 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006765 if (lastwin == firstwin)
6766 ex_win_close(forceit, curwin, NULL);
6767# ifdef FEAT_GUI
6768 need_mouse_correct = TRUE;
6769# endif
6770}
6771
6772/*
6773 * Close tab page "tp", which is not the current tab page.
6774 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006775 * Also takes care of the tab pages line disappearing when closing the
6776 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006777 */
6778 void
6779tabpage_close_other(tp, forceit)
6780 tabpage_T *tp;
6781 int forceit;
6782{
6783 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006784 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006785 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006786
6787 /* Limit to 1000 windows, autocommands may add a window while we close
6788 * one. OK, so I'm paranoid... */
6789 while (++done < 1000)
6790 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006791 wp = tp->tp_firstwin;
6792 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006793
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006794 /* Autocommands may delete the tab page under our fingers and we may
6795 * fail to close a window with a modified buffer. */
6796 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006797 break;
6798 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006799
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006800 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006801 if (h != tabline_height())
6802 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006803}
6804
6805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * ":only".
6807 */
6808 static void
6809ex_only(eap)
6810 exarg_T *eap;
6811{
6812# ifdef FEAT_GUI
6813 need_mouse_correct = TRUE;
6814# endif
6815 close_others(TRUE, eap->forceit);
6816}
6817
6818/*
6819 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006820 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006822 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823ex_all(eap)
6824 exarg_T *eap;
6825{
6826 if (eap->addr_count == 0)
6827 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006828 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829}
6830#endif /* FEAT_WINDOWS */
6831
6832 static void
6833ex_hide(eap)
6834 exarg_T *eap;
6835{
6836 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6837 eap->errmsg = e_invarg;
6838 else
6839 {
6840 /* ":hide" or ":hide | cmd": hide current window */
6841 eap->nextcmd = check_nextcmd(eap->arg);
6842#ifdef FEAT_WINDOWS
6843 if (!eap->skip)
6844 {
6845# ifdef FEAT_GUI
6846 need_mouse_correct = TRUE;
6847# endif
6848 win_close(curwin, FALSE); /* don't free buffer */
6849 }
6850#endif
6851 }
6852}
6853
6854/*
6855 * ":stop" and ":suspend": Suspend Vim.
6856 */
6857 static void
6858ex_stop(eap)
6859 exarg_T *eap;
6860{
6861 /*
6862 * Disallow suspending for "rvim".
6863 */
6864 if (!check_restricted()
6865#ifdef WIN3264
6866 /*
6867 * Check if external commands are allowed now.
6868 */
6869 && can_end_termcap_mode(TRUE)
6870#endif
6871 )
6872 {
6873 if (!eap->forceit)
6874 autowrite_all();
6875 windgoto((int)Rows - 1, 0);
6876 out_char('\n');
6877 out_flush();
6878 stoptermcap();
6879 out_flush(); /* needed for SUN to restore xterm buffer */
6880#ifdef FEAT_TITLE
6881 mch_restore_title(3); /* restore window titles */
6882#endif
6883 ui_suspend(); /* call machine specific function */
6884#ifdef FEAT_TITLE
6885 maketitle();
6886 resettitle(); /* force updating the title */
6887#endif
6888 starttermcap();
6889 scroll_start(); /* scroll screen before redrawing */
6890 redraw_later_clear();
6891 shell_resized(); /* may have resized window */
6892 }
6893}
6894
6895/*
6896 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6897 */
6898 static void
6899ex_exit(eap)
6900 exarg_T *eap;
6901{
6902#ifdef FEAT_CMDWIN
6903 if (cmdwin_type != 0)
6904 {
6905 cmdwin_result = Ctrl_C;
6906 return;
6907 }
6908#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006909 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006910 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006911 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006912 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006913 return;
6914 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915#ifdef FEAT_AUTOCMD
6916 if (curbuf_locked())
6917 return;
6918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
6920 /*
6921 * if more files or windows we won't exit
6922 */
6923 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6924 exiting = TRUE;
6925 if ( ((eap->cmdidx == CMD_wq
6926 || curbufIsChanged())
6927 && do_write(eap) == FAIL)
6928 || check_more(TRUE, eap->forceit) == FAIL
6929 || (only_one_window() && check_changed_any(eap->forceit)))
6930 {
6931 not_exiting();
6932 }
6933 else
6934 {
6935#ifdef FEAT_WINDOWS
6936 if (only_one_window()) /* quit last window, exit Vim */
6937#endif
6938 getout(0);
6939#ifdef FEAT_WINDOWS
6940# ifdef FEAT_GUI
6941 need_mouse_correct = TRUE;
6942# endif
6943 /* quit current window, may free buffer */
6944 win_close(curwin, !P_HID(curwin->w_buffer));
6945#endif
6946 }
6947}
6948
6949/*
6950 * ":print", ":list", ":number".
6951 */
6952 static void
6953ex_print(eap)
6954 exarg_T *eap;
6955{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006956 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6957 EMSG(_(e_emptybuf));
6958 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006960 for ( ;!got_int; ui_breakcheck())
6961 {
6962 print_line(eap->line1,
6963 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6964 || (eap->flags & EXFLAG_NR)),
6965 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6966 if (++eap->line1 > eap->line2)
6967 break;
6968 out_flush(); /* show one line at a time */
6969 }
6970 setpcmark();
6971 /* put cursor at last line */
6972 curwin->w_cursor.lnum = eap->line2;
6973 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 }
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977}
6978
6979#ifdef FEAT_BYTEOFF
6980 static void
6981ex_goto(eap)
6982 exarg_T *eap;
6983{
6984 goto_byte(eap->line2);
6985}
6986#endif
6987
6988/*
6989 * ":shell".
6990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 static void
6992ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006993 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 do_shell(NULL, 0);
6996}
6997
6998#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6999 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007000 || defined(FEAT_GUI_MSWIN) \
7001 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 || defined(PROTO)
7003
7004/*
7005 * Handle a file drop. The code is here because a drop is *nearly* like an
7006 * :args command, but not quite (we have a list of exact filenames, so we
7007 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7008 * code is very similar to :args and hence needs access to a lot of the static
7009 * functions in this file.
7010 *
7011 * The list should be allocated using alloc(), as should each item in the
7012 * list. This function takes over responsibility for freeing the list.
7013 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007014 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7016 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7017 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7018 * file functionality is (currently) not in EMX this is not presently a
7019 * problem.
7020 */
7021 void
7022handle_drop(filec, filev, split)
7023 int filec; /* the number of files dropped */
7024 char_u **filev; /* the list of files dropped */
7025 int split; /* force splitting the window */
7026{
7027 exarg_T ea;
7028 int save_msg_scroll = msg_scroll;
7029
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007030 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007031 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007033#ifdef FEAT_AUTOCMD
7034 if (curbuf_locked())
7035 return;
7036#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007037 /* When the screen is being updated we should not change buffers and
7038 * windows structures, it may cause freed memory to be used. */
7039 if (updating_screen)
7040 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
7042 /* Check whether the current buffer is changed. If so, we will need
7043 * to split the current window or data could be lost.
7044 * We don't need to check if the 'hidden' option is set, as in this
7045 * case the buffer won't be lost.
7046 */
7047 if (!P_HID(curbuf) && !split)
7048 {
7049 ++emsg_off;
7050 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7051 --emsg_off;
7052 }
7053 if (split)
7054 {
7055# ifdef FEAT_WINDOWS
7056 if (win_split(0, 0) == FAIL)
7057 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007058 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
7060 /* When splitting the window, create a new alist. Otherwise the
7061 * existing one is overwritten. */
7062 alist_unlink(curwin->w_alist);
7063 alist_new();
7064# else
7065 return; /* can't split, always fail */
7066# endif
7067 }
7068
7069 /*
7070 * Set up the new argument list.
7071 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007072 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
7074 /*
7075 * Move to the first file.
7076 */
7077 /* Fake up a minimal "next" command for do_argfile() */
7078 vim_memset(&ea, 0, sizeof(ea));
7079 ea.cmd = (char_u *)"next";
7080 do_argfile(&ea, 0);
7081
7082 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7083 * mode that is not needed here. */
7084 need_start_insertmode = FALSE;
7085
7086 /* Restore msg_scroll, otherwise a following command may cause scrolling
7087 * unexpectedly. The screen will be redrawn by the caller, thus
7088 * msg_scroll being set by displaying a message is irrelevant. */
7089 msg_scroll = save_msg_scroll;
7090}
7091#endif
7092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093/*
7094 * Clear an argument list: free all file names and reset it to zero entries.
7095 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007096 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097alist_clear(al)
7098 alist_T *al;
7099{
7100 while (--al->al_ga.ga_len >= 0)
7101 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7102 ga_clear(&al->al_ga);
7103}
7104
7105/*
7106 * Init an argument list.
7107 */
7108 void
7109alist_init(al)
7110 alist_T *al;
7111{
7112 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7113}
7114
7115#if defined(FEAT_WINDOWS) || defined(PROTO)
7116
7117/*
7118 * Remove a reference from an argument list.
7119 * Ignored when the argument list is the global one.
7120 * If the argument list is no longer used by any window, free it.
7121 */
7122 void
7123alist_unlink(al)
7124 alist_T *al;
7125{
7126 if (al != &global_alist && --al->al_refcount <= 0)
7127 {
7128 alist_clear(al);
7129 vim_free(al);
7130 }
7131}
7132
7133# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7134/*
7135 * Create a new argument list and use it for the current window.
7136 */
7137 void
7138alist_new()
7139{
7140 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7141 if (curwin->w_alist == NULL)
7142 {
7143 curwin->w_alist = &global_alist;
7144 ++global_alist.al_refcount;
7145 }
7146 else
7147 {
7148 curwin->w_alist->al_refcount = 1;
7149 alist_init(curwin->w_alist);
7150 }
7151}
7152# endif
7153#endif
7154
7155#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7156/*
7157 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007158 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7159 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 */
7161 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007162alist_expand(fnum_list, fnum_len)
7163 int *fnum_list;
7164 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007167 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 char_u **new_arg_files;
7169 int new_arg_file_count;
7170 char_u *save_p_su = p_su;
7171 int i;
7172
7173 /* Don't use 'suffixes' here. This should work like the shell did the
7174 * expansion. Also, the vimrc file isn't read yet, thus the user
7175 * can't set the options. */
7176 p_su = empty_option;
7177 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7178 if (old_arg_files != NULL)
7179 {
7180 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007181 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7182 old_arg_count = GARGCOUNT;
7183 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007185 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 && new_arg_file_count > 0)
7187 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007188 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7189 TRUE, fnum_list, fnum_len);
7190 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193 p_su = save_p_su;
7194}
7195#endif
7196
7197/*
7198 * Set the argument list for the current window.
7199 * Takes over the allocated files[] and the allocated fnames in it.
7200 */
7201 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007202alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 alist_T *al;
7204 int count;
7205 char_u **files;
7206 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007207 int *fnum_list;
7208 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 int i;
7211
7212 alist_clear(al);
7213 if (ga_grow(&al->al_ga, count) == OK)
7214 {
7215 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007216 {
7217 if (got_int)
7218 {
7219 /* When adding many buffers this can take a long time. Allow
7220 * interrupting here. */
7221 while (i < count)
7222 vim_free(files[i++]);
7223 break;
7224 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007225
7226 /* May set buffer name of a buffer previously used for the
7227 * argument list, so that it's re-used by alist_add. */
7228 if (fnum_list != NULL && i < fnum_len)
7229 buf_set_name(fnum_list[i], files[i]);
7230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007232 ui_breakcheck();
7233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 vim_free(files);
7235 }
7236 else
7237 FreeWild(count, files);
7238#ifdef FEAT_WINDOWS
7239 if (al == &global_alist)
7240#endif
7241 arg_had_last = FALSE;
7242}
7243
7244/*
7245 * Add file "fname" to argument list "al".
7246 * "fname" must have been allocated and "al" must have been checked for room.
7247 */
7248 void
7249alist_add(al, fname, set_fnum)
7250 alist_T *al;
7251 char_u *fname;
7252 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7253{
7254 if (fname == NULL) /* don't add NULL file names */
7255 return;
7256#ifdef BACKSLASH_IN_FILENAME
7257 slash_adjust(fname);
7258#endif
7259 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7260 if (set_fnum > 0)
7261 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7262 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7263 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264}
7265
7266#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7267/*
7268 * Adjust slashes in file names. Called after 'shellslash' was set.
7269 */
7270 void
7271alist_slash_adjust()
7272{
7273 int i;
7274# ifdef FEAT_WINDOWS
7275 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007276 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277# endif
7278
7279 for (i = 0; i < GARGCOUNT; ++i)
7280 if (GARGLIST[i].ae_fname != NULL)
7281 slash_adjust(GARGLIST[i].ae_fname);
7282# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007283 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 if (wp->w_alist != &global_alist)
7285 for (i = 0; i < WARGCOUNT(wp); ++i)
7286 if (WARGLIST(wp)[i].ae_fname != NULL)
7287 slash_adjust(WARGLIST(wp)[i].ae_fname);
7288# endif
7289}
7290#endif
7291
7292/*
7293 * ":preserve".
7294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 static void
7296ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007297 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007299 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 ml_preserve(curbuf, TRUE);
7301}
7302
7303/*
7304 * ":recover".
7305 */
7306 static void
7307ex_recover(eap)
7308 exarg_T *eap;
7309{
7310 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7311 recoverymode = TRUE;
7312 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7313 && (*eap->arg == NUL
7314 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7315 ml_recover();
7316 recoverymode = FALSE;
7317}
7318
7319/*
7320 * Command modifier used in a wrong way.
7321 */
7322 static void
7323ex_wrongmodifier(eap)
7324 exarg_T *eap;
7325{
7326 eap->errmsg = e_invcmd;
7327}
7328
7329#ifdef FEAT_WINDOWS
7330/*
7331 * :sview [+command] file split window with new file, read-only
7332 * :split [[+command] file] split window with current or new file
7333 * :vsplit [[+command] file] split window vertically with current or new file
7334 * :new [[+command] file] split window with no or new file
7335 * :vnew [[+command] file] split vertically window with no or new file
7336 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007337 *
7338 * :tabedit open new Tab page with empty window
7339 * :tabedit [+command] file open new Tab page and edit "file"
7340 * :tabnew [[+command] file] just like :tabedit
7341 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 */
7343 void
7344ex_splitview(eap)
7345 exarg_T *eap;
7346{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007347 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007348# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350# endif
7351# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007355# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7357 {
7358 ex_ni(eap);
7359 return;
7360 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007367# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007369 * quickfix windows. But it's OK when doing ":tab split". */
7370 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
7372 if (eap->cmdidx == CMD_split)
7373 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007374# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (eap->cmdidx == CMD_vsplit)
7376 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007381# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007382 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
7384 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7385 FNAME_MESS, TRUE, curbuf->b_ffname);
7386 if (fname == NULL)
7387 goto theend;
7388 eap->arg = fname;
7389 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007396# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 && eap->cmdidx != CMD_new)
7400 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007401# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007402 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007403# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007404 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007405# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007406 au_has_group((char_u *)"FileExplorer"))
7407 {
7408 /* No browsing supported but we do have the file explorer:
7409 * Edit the directory. */
7410 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7411 eap->arg = (char_u *)".";
7412 }
7413 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007414# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007415 {
7416 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007418 if (fname == NULL)
7419 goto theend;
7420 eap->arg = fname;
7421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
7423 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007426 /*
7427 * Either open new tab page or split the window.
7428 */
7429 if (eap->cmdidx == CMD_tabedit
7430 || eap->cmdidx == CMD_tabfind
7431 || eap->cmdidx == CMD_tabnew)
7432 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007433 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7434 : eap->addr_count == 0 ? 0
7435 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007436 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007437 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007438
7439 /* set the alternate buffer for the window we came from */
7440 if (curwin != old_curwin
7441 && win_valid(old_curwin)
7442 && old_curwin->w_buffer != curbuf
7443 && !cmdmod.keepalt)
7444 old_curwin->w_alt_fnum = curbuf->b_fnum;
7445 }
7446 }
7447 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7449 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 /* Reset 'scrollbind' when editing another file, but keep it when
7452 * doing ":split" without arguments. */
7453 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007454# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007458 {
7459 RESET_BINDING(curwin);
7460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 else
7462 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 do_exedit(eap, old_curwin);
7465 }
7466
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007471# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472theend:
7473 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007476
7477/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007478 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479 */
7480 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007481tabpage_new()
7482{
7483 exarg_T ea;
7484
7485 vim_memset(&ea, 0, sizeof(ea));
7486 ea.cmdidx = CMD_tabnew;
7487 ea.cmd = (char_u *)"tabn";
7488 ea.arg = (char_u *)"";
7489 ex_splitview(&ea);
7490}
7491
7492/*
7493 * :tabnext command
7494 */
7495 static void
7496ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007497 exarg_T *eap;
7498{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007499 switch (eap->cmdidx)
7500 {
7501 case CMD_tabfirst:
7502 case CMD_tabrewind:
7503 goto_tabpage(1);
7504 break;
7505 case CMD_tablast:
7506 goto_tabpage(9999);
7507 break;
7508 case CMD_tabprevious:
7509 case CMD_tabNext:
7510 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7511 break;
7512 default: /* CMD_tabnext */
7513 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7514 break;
7515 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007516}
7517
7518/*
7519 * :tabmove command
7520 */
7521 static void
7522ex_tabmove(eap)
7523 exarg_T *eap;
7524{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007525 int tab_number = 9999;
7526
7527 if (eap->arg && *eap->arg != NUL)
7528 {
7529 char_u *p = eap->arg;
7530 int relative = 0; /* argument +N/-N means: move N places to the
7531 * right/left relative to the current position. */
7532
7533 if (*eap->arg == '-')
7534 {
7535 relative = -1;
7536 p = eap->arg + 1;
7537 }
7538 else if (*eap->arg == '+')
7539 {
7540 relative = 1;
7541 p = eap->arg + 1;
7542 }
7543 else
7544 p = eap->arg;
7545
7546 if (p == skipdigits(p))
7547 {
7548 /* No numbers as argument. */
7549 eap->errmsg = e_invarg;
7550 return;
7551 }
7552
7553 tab_number = getdigits(&p);
7554 if (relative != 0)
7555 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7556 }
7557 else if (eap->addr_count != 0)
7558 tab_number = eap->line2;
7559
7560 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007561}
7562
7563/*
7564 * :tabs command: List tabs and their contents.
7565 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007566 static void
7567ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007568 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007569{
7570 tabpage_T *tp;
7571 win_T *wp;
7572 int tabcount = 1;
7573
7574 msg_start();
7575 msg_scroll = TRUE;
7576 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7577 {
7578 msg_putchar('\n');
7579 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7580 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7581 out_flush(); /* output one line at a time */
7582 ui_breakcheck();
7583
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007584 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007585 wp = firstwin;
7586 else
7587 wp = tp->tp_firstwin;
7588 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7589 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007590 msg_putchar('\n');
7591 msg_putchar(wp == curwin ? '>' : ' ');
7592 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007593 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7594 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007595 if (buf_spname(wp->w_buffer) != NULL)
7596 STRCPY(IObuff, buf_spname(wp->w_buffer));
7597 else
7598 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7599 IObuff, IOSIZE, TRUE);
7600 msg_outtrans(IObuff);
7601 out_flush(); /* output one line at a time */
7602 ui_breakcheck();
7603 }
7604 }
7605}
7606
7607#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608
7609/*
7610 * ":mode": Set screen mode.
7611 * If no argument given, just get the screen size and redraw.
7612 */
7613 static void
7614ex_mode(eap)
7615 exarg_T *eap;
7616{
7617 if (*eap->arg == NUL)
7618 shell_resized();
7619 else
7620 mch_screenmode(eap->arg);
7621}
7622
7623#ifdef FEAT_WINDOWS
7624/*
7625 * ":resize".
7626 * set, increment or decrement current window height
7627 */
7628 static void
7629ex_resize(eap)
7630 exarg_T *eap;
7631{
7632 int n;
7633 win_T *wp = curwin;
7634
7635 if (eap->addr_count > 0)
7636 {
7637 n = eap->line2;
7638 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7639 ;
7640 }
7641
7642#ifdef FEAT_GUI
7643 need_mouse_correct = TRUE;
7644#endif
7645 n = atol((char *)eap->arg);
7646#ifdef FEAT_VERTSPLIT
7647 if (cmdmod.split & WSP_VERT)
7648 {
7649 if (*eap->arg == '-' || *eap->arg == '+')
7650 n += W_WIDTH(curwin);
7651 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7652 n = 9999;
7653 win_setwidth_win((int)n, wp);
7654 }
7655 else
7656#endif
7657 {
7658 if (*eap->arg == '-' || *eap->arg == '+')
7659 n += curwin->w_height;
7660 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7661 n = 9999;
7662 win_setheight_win((int)n, wp);
7663 }
7664}
7665#endif
7666
7667/*
7668 * ":find [+command] <file>" command.
7669 */
7670 static void
7671ex_find(eap)
7672 exarg_T *eap;
7673{
7674#ifdef FEAT_SEARCHPATH
7675 char_u *fname;
7676 int count;
7677
7678 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7679 TRUE, curbuf->b_ffname);
7680 if (eap->addr_count > 0)
7681 {
7682 /* Repeat finding the file "count" times. This matters when it
7683 * appears several times in the path. */
7684 count = eap->line2;
7685 while (fname != NULL && --count > 0)
7686 {
7687 vim_free(fname);
7688 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7689 FALSE, curbuf->b_ffname);
7690 }
7691 }
7692
7693 if (fname != NULL)
7694 {
7695 eap->arg = fname;
7696#endif
7697 do_exedit(eap, NULL);
7698#ifdef FEAT_SEARCHPATH
7699 vim_free(fname);
7700 }
7701#endif
7702}
7703
7704/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007705 * ":open" simulation: for now just work like ":visual".
7706 */
7707 static void
7708ex_open(eap)
7709 exarg_T *eap;
7710{
7711 regmatch_T regmatch;
7712 char_u *p;
7713
7714 curwin->w_cursor.lnum = eap->line2;
7715 beginline(BL_SOL | BL_FIX);
7716 if (*eap->arg == '/')
7717 {
7718 /* ":open /pattern/": put cursor in column found with pattern */
7719 ++eap->arg;
7720 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7721 *p = NUL;
7722 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7723 if (regmatch.regprog != NULL)
7724 {
7725 regmatch.rm_ic = p_ic;
7726 p = ml_get_curline();
7727 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007728 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007729 else
7730 EMSG(_(e_nomatch));
7731 vim_free(regmatch.regprog);
7732 }
7733 /* Move to the NUL, ignore any other arguments. */
7734 eap->arg += STRLEN(eap->arg);
7735 }
7736 check_cursor();
7737
7738 eap->cmdidx = CMD_visual;
7739 do_exedit(eap, NULL);
7740}
7741
7742/*
7743 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 */
7745 static void
7746ex_edit(eap)
7747 exarg_T *eap;
7748{
7749 do_exedit(eap, NULL);
7750}
7751
7752/*
7753 * ":edit <file>" command and alikes.
7754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 void
7756do_exedit(eap, old_curwin)
7757 exarg_T *eap;
7758 win_T *old_curwin; /* curwin before doing a split or NULL */
7759{
7760 int n;
7761#ifdef FEAT_WINDOWS
7762 int need_hide;
7763#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007764 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765
7766 /*
7767 * ":vi" command ends Ex mode.
7768 */
7769 if (exmode_active && (eap->cmdidx == CMD_visual
7770 || eap->cmdidx == CMD_view))
7771 {
7772 exmode_active = FALSE;
7773 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007774 {
7775 /* Special case: ":global/pat/visual\NLvi-commands" */
7776 if (global_busy)
7777 {
7778 int rd = RedrawingDisabled;
7779 int nwr = no_wait_return;
7780 int ms = msg_scroll;
7781#ifdef FEAT_GUI
7782 int he = hold_gui_events;
7783#endif
7784
7785 if (eap->nextcmd != NULL)
7786 {
7787 stuffReadbuff(eap->nextcmd);
7788 eap->nextcmd = NULL;
7789 }
7790
7791 if (exmode_was != EXMODE_VIM)
7792 settmode(TMODE_RAW);
7793 RedrawingDisabled = 0;
7794 no_wait_return = 0;
7795 need_wait_return = FALSE;
7796 msg_scroll = 0;
7797#ifdef FEAT_GUI
7798 hold_gui_events = 0;
7799#endif
7800 must_redraw = CLEAR;
7801
7802 main_loop(FALSE, TRUE);
7803
7804 RedrawingDisabled = rd;
7805 no_wait_return = nwr;
7806 msg_scroll = ms;
7807#ifdef FEAT_GUI
7808 hold_gui_events = he;
7809#endif
7810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814
7815 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007816 || eap->cmdidx == CMD_tabnew
7817 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818#ifdef FEAT_VERTSPLIT
7819 || eap->cmdidx == CMD_vnew
7820#endif
7821 ) && *eap->arg == NUL)
7822 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007823 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 setpcmark();
7825 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007826 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7827 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
7829 else if ((eap->cmdidx != CMD_split
7830#ifdef FEAT_VERTSPLIT
7831 && eap->cmdidx != CMD_vsplit
7832#endif
7833 )
7834 || *eap->arg != NUL
7835#ifdef FEAT_BROWSE
7836 || cmdmod.browse
7837#endif
7838 )
7839 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007840#ifdef FEAT_AUTOCMD
7841 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7842 * can bring us here, others are stopped earlier. */
7843 if (*eap->arg != NUL && curbuf_locked())
7844 return;
7845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 n = readonlymode;
7847 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7848 readonlymode = TRUE;
7849 else if (eap->cmdidx == CMD_enew)
7850 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7851 empty buffer */
7852 setpcmark();
7853 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7854 NULL, eap,
7855 /* ":edit" goes to first line if Vi compatible */
7856 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7857 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7858 ? ECMD_ONE : eap->do_ecmd_lnum,
7859 (P_HID(curbuf) ? ECMD_HIDE : 0)
7860 + (eap->forceit ? ECMD_FORCEIT : 0)
7861#ifdef FEAT_LISTCMDS
7862 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7863#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007864 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {
7866 /* Editing the file failed. If the window was split, close it. */
7867#ifdef FEAT_WINDOWS
7868 if (old_curwin != NULL)
7869 {
7870 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7871 if (!need_hide || P_HID(curbuf))
7872 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007873# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7874 cleanup_T cs;
7875
7876 /* Reset the error/interrupt/exception state here so that
7877 * aborting() returns FALSE when closing a window. */
7878 enter_cleanup(&cs);
7879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880# ifdef FEAT_GUI
7881 need_mouse_correct = TRUE;
7882# endif
7883 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007884
7885# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7886 /* Restore the error/interrupt/exception state if not
7887 * discarded by a new aborting error, interrupt, or
7888 * uncaught exception. */
7889 leave_cleanup(&cs);
7890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 }
7893#endif
7894 }
7895 else if (readonlymode && curbuf->b_nwindows == 1)
7896 {
7897 /* When editing an already visited buffer, 'readonly' won't be set
7898 * but the previous value is kept. With ":view" and ":sview" we
7899 * want the file to be readonly, except when another window is
7900 * editing the same buffer. */
7901 curbuf->b_p_ro = TRUE;
7902 }
7903 readonlymode = n;
7904 }
7905 else
7906 {
7907 if (eap->do_ecmd_cmd != NULL)
7908 do_cmdline_cmd(eap->do_ecmd_cmd);
7909#ifdef FEAT_TITLE
7910 n = curwin->w_arg_idx_invalid;
7911#endif
7912 check_arg_idx(curwin);
7913#ifdef FEAT_TITLE
7914 if (n != curwin->w_arg_idx_invalid)
7915 maketitle();
7916#endif
7917 }
7918
7919#ifdef FEAT_WINDOWS
7920 /*
7921 * if ":split file" worked, set alternate file name in old window to new
7922 * file
7923 */
7924 if (old_curwin != NULL
7925 && *eap->arg != NUL
7926 && curwin != old_curwin
7927 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007928 && old_curwin->w_buffer != curbuf
7929 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 old_curwin->w_alt_fnum = curbuf->b_fnum;
7931#endif
7932
7933 ex_no_reprint = TRUE;
7934}
7935
7936#ifndef FEAT_GUI
7937/*
7938 * ":gui" and ":gvim" when there is no GUI.
7939 */
7940 static void
7941ex_nogui(eap)
7942 exarg_T *eap;
7943{
7944 eap->errmsg = e_nogvim;
7945}
7946#endif
7947
7948#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7949 static void
7950ex_tearoff(eap)
7951 exarg_T *eap;
7952{
7953 gui_make_tearoff(eap->arg);
7954}
7955#endif
7956
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007957#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 static void
7959ex_popup(eap)
7960 exarg_T *eap;
7961{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007962 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964#endif
7965
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 static void
7967ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007968 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7971 MSG(_("No swap file"));
7972 else
7973 msg(curbuf->b_ml.ml_mfp->mf_fname);
7974}
7975
7976/*
7977 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7978 * offset.
7979 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 static void
7982ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007983 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985#ifdef FEAT_SCROLLBIND
7986 win_T *wp;
7987 long topline;
7988 long y;
7989 linenr_T old_linenr = curwin->w_cursor.lnum;
7990
7991 setpcmark();
7992
7993 /*
7994 * determine max topline
7995 */
7996 if (curwin->w_p_scb)
7997 {
7998 topline = curwin->w_topline;
7999 for (wp = firstwin; wp; wp = wp->w_next)
8000 {
8001 if (wp->w_p_scb && wp->w_buffer)
8002 {
8003 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8004 if (topline > y)
8005 topline = y;
8006 }
8007 }
8008 if (topline < 1)
8009 topline = 1;
8010 }
8011 else
8012 {
8013 topline = 1;
8014 }
8015
8016
8017 /*
8018 * set all scrollbind windows to the same topline
8019 */
8020 wp = curwin;
8021 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8022 {
8023 if (curwin->w_p_scb)
8024 {
8025 y = topline - curwin->w_topline;
8026 if (y > 0)
8027 scrollup(y, TRUE);
8028 else
8029 scrolldown(-y, TRUE);
8030 curwin->w_scbind_pos = topline;
8031 redraw_later(VALID);
8032 cursor_correct();
8033#ifdef FEAT_WINDOWS
8034 curwin->w_redr_status = TRUE;
8035#endif
8036 }
8037 }
8038 curwin = wp;
8039 if (curwin->w_p_scb)
8040 {
8041 did_syncbind = TRUE;
8042 checkpcmark();
8043 if (old_linenr != curwin->w_cursor.lnum)
8044 {
8045 char_u ctrl_o[2];
8046
8047 ctrl_o[0] = Ctrl_O;
8048 ctrl_o[1] = 0;
8049 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8050 }
8051 }
8052#endif
8053}
8054
8055
8056 static void
8057ex_read(eap)
8058 exarg_T *eap;
8059{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008060 int i;
8061 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8062 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 if (eap->usefilter) /* :r!cmd */
8065 do_bang(1, eap, FALSE, FALSE, TRUE);
8066 else
8067 {
8068 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8069 return;
8070
8071#ifdef FEAT_BROWSE
8072 if (cmdmod.browse)
8073 {
8074 char_u *browseFile;
8075
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008076 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 NULL, NULL, NULL, curbuf);
8078 if (browseFile != NULL)
8079 {
8080 i = readfile(browseFile, NULL,
8081 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8082 vim_free(browseFile);
8083 }
8084 else
8085 i = OK;
8086 }
8087 else
8088#endif
8089 if (*eap->arg == NUL)
8090 {
8091 if (check_fname() == FAIL) /* check for no file name */
8092 return;
8093 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8094 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8095 }
8096 else
8097 {
8098 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8099 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8100 i = readfile(eap->arg, NULL,
8101 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8102
8103 }
8104 if (i == FAIL)
8105 {
8106#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8107 if (!aborting())
8108#endif
8109 EMSG2(_(e_notopen), eap->arg);
8110 }
8111 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008112 {
8113 if (empty && exmode_active)
8114 {
8115 /* Delete the empty line that remains. Historically ex does
8116 * this but vi doesn't. */
8117 if (eap->line2 == 0)
8118 lnum = curbuf->b_ml.ml_line_count;
8119 else
8120 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008121 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008122 {
8123 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008124 if (curwin->w_cursor.lnum > 1
8125 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008126 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008127 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008128 }
8129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 }
8133}
8134
Bram Moolenaarea408852005-06-25 22:49:46 +00008135static char_u *prev_dir = NULL;
8136
8137#if defined(EXITFREE) || defined(PROTO)
8138 void
8139free_cd_dir()
8140{
8141 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008142 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008143
8144 vim_free(globaldir);
8145 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008146}
8147#endif
8148
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150/*
8151 * ":cd", ":lcd", ":chdir" and ":lchdir".
8152 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008153 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154ex_cd(eap)
8155 exarg_T *eap;
8156{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 char_u *new_dir;
8158 char_u *tofree;
8159
8160 new_dir = eap->arg;
8161#if !defined(UNIX) && !defined(VMS)
8162 /* for non-UNIX ":cd" means: print current directory */
8163 if (*new_dir == NUL)
8164 ex_pwd(NULL);
8165 else
8166#endif
8167 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008168#ifdef FEAT_AUTOCMD
8169 if (allbuf_locked())
8170 return;
8171#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008172 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8173 && !eap->forceit)
8174 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008175 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008176 return;
8177 }
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 /* ":cd -": Change to previous directory */
8180 if (STRCMP(new_dir, "-") == 0)
8181 {
8182 if (prev_dir == NULL)
8183 {
8184 EMSG(_("E186: No previous directory"));
8185 return;
8186 }
8187 new_dir = prev_dir;
8188 }
8189
8190 /* Save current directory for next ":cd -" */
8191 tofree = prev_dir;
8192 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8193 prev_dir = vim_strsave(NameBuff);
8194 else
8195 prev_dir = NULL;
8196
8197#if defined(UNIX) || defined(VMS)
8198 /* for UNIX ":cd" means: go to home directory */
8199 if (*new_dir == NUL)
8200 {
8201 /* use NameBuff for home directory name */
8202# ifdef VMS
8203 char_u *p;
8204
8205 p = mch_getenv((char_u *)"SYS$LOGIN");
8206 if (p == NULL || *p == NUL) /* empty is the same as not set */
8207 NameBuff[0] = NUL;
8208 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008209 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210# else
8211 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8212# endif
8213 new_dir = NameBuff;
8214 }
8215#endif
8216 if (new_dir == NULL || vim_chdir(new_dir))
8217 EMSG(_(e_failed));
8218 else
8219 {
8220 vim_free(curwin->w_localdir);
8221 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8222 {
8223 /* If still in global directory, need to remember current
8224 * directory as global directory. */
8225 if (globaldir == NULL && prev_dir != NULL)
8226 globaldir = vim_strsave(prev_dir);
8227 /* Remember this local directory for the window. */
8228 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8229 curwin->w_localdir = vim_strsave(NameBuff);
8230 }
8231 else
8232 {
8233 /* We are now in the global directory, no need to remember its
8234 * name. */
8235 vim_free(globaldir);
8236 globaldir = NULL;
8237 curwin->w_localdir = NULL;
8238 }
8239
8240 shorten_fnames(TRUE);
8241
8242 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008243 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 ex_pwd(eap);
8245 }
8246 vim_free(tofree);
8247 }
8248}
8249
8250/*
8251 * ":pwd".
8252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 static void
8254ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008255 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8258 {
8259#ifdef BACKSLASH_IN_FILENAME
8260 slash_adjust(NameBuff);
8261#endif
8262 msg(NameBuff);
8263 }
8264 else
8265 EMSG(_("E187: Unknown"));
8266}
8267
8268/*
8269 * ":=".
8270 */
8271 static void
8272ex_equal(eap)
8273 exarg_T *eap;
8274{
8275 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008276 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
8279 static void
8280ex_sleep(eap)
8281 exarg_T *eap;
8282{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008283 int n;
8284 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285
8286 if (cursor_valid())
8287 {
8288 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8289 if (n >= 0)
8290 windgoto((int)n, curwin->w_wcol);
8291 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008292
8293 len = eap->line2;
8294 switch (*eap->arg)
8295 {
8296 case 'm': break;
8297 case NUL: len *= 1000L; break;
8298 default: EMSG2(_(e_invarg2), eap->arg); return;
8299 }
8300 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
8302
8303/*
8304 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8305 */
8306 void
8307do_sleep(msec)
8308 long msec;
8309{
8310 long done;
8311
8312 cursor_on();
8313 out_flush();
8314 for (done = 0; !got_int && done < msec; done += 1000L)
8315 {
8316 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8317 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008318#ifdef FEAT_NETBEANS_INTG
8319 /* Process the netbeans messages that may have been received in the
8320 * call to ui_breakcheck() when the GUI is in use. This may occur when
8321 * running a test case. */
8322 netbeans_parse_messages();
8323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325}
8326
8327 static void
8328do_exmap(eap, isabbrev)
8329 exarg_T *eap;
8330 int isabbrev;
8331{
8332 int mode;
8333 char_u *cmdp;
8334
8335 cmdp = eap->cmd;
8336 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8337
8338 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8339 eap->arg, mode, isabbrev))
8340 {
8341 case 1: EMSG(_(e_invarg));
8342 break;
8343 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8344 break;
8345 }
8346}
8347
8348/*
8349 * ":winsize" command (obsolete).
8350 */
8351 static void
8352ex_winsize(eap)
8353 exarg_T *eap;
8354{
8355 int w, h;
8356 char_u *arg = eap->arg;
8357 char_u *p;
8358
8359 w = getdigits(&arg);
8360 arg = skipwhite(arg);
8361 p = arg;
8362 h = getdigits(&arg);
8363 if (*p != NUL && *arg == NUL)
8364 set_shellsize(w, h, TRUE);
8365 else
8366 EMSG(_("E465: :winsize requires two number arguments"));
8367}
8368
8369#ifdef FEAT_WINDOWS
8370 static void
8371ex_wincmd(eap)
8372 exarg_T *eap;
8373{
8374 int xchar = NUL;
8375 char_u *p;
8376
8377 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8378 {
8379 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8380 if (eap->arg[1] == NUL)
8381 {
8382 EMSG(_(e_invarg));
8383 return;
8384 }
8385 xchar = eap->arg[1];
8386 p = eap->arg + 2;
8387 }
8388 else
8389 p = eap->arg + 1;
8390
8391 eap->nextcmd = check_nextcmd(p);
8392 p = skipwhite(p);
8393 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8394 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008395 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 /* Pass flags on for ":vertical wincmd ]". */
8398 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008399 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8401 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008402 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 }
8404}
8405#endif
8406
Bram Moolenaar843ee412004-06-30 16:16:41 +00008407#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408/*
8409 * ":winpos".
8410 */
8411 static void
8412ex_winpos(eap)
8413 exarg_T *eap;
8414{
8415 int x, y;
8416 char_u *arg = eap->arg;
8417 char_u *p;
8418
8419 if (*arg == NUL)
8420 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008421# if defined(FEAT_GUI) || defined(MSWIN)
8422# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008424# else
8425 if (mch_get_winpos(&x, &y) != FAIL)
8426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {
8428 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8429 msg(IObuff);
8430 }
8431 else
8432# endif
8433 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8434 }
8435 else
8436 {
8437 x = getdigits(&arg);
8438 arg = skipwhite(arg);
8439 p = arg;
8440 y = getdigits(&arg);
8441 if (*p == NUL || *arg != NUL)
8442 {
8443 EMSG(_("E466: :winpos requires two number arguments"));
8444 return;
8445 }
8446# ifdef FEAT_GUI
8447 if (gui.in_use)
8448 gui_mch_set_winpos(x, y);
8449 else if (gui.starting)
8450 {
8451 /* Remember the coordinates for when the window is opened. */
8452 gui_win_x = x;
8453 gui_win_y = y;
8454 }
8455# ifdef HAVE_TGETENT
8456 else
8457# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008458# else
8459# ifdef MSWIN
8460 mch_set_winpos(x, y);
8461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462# endif
8463# ifdef HAVE_TGETENT
8464 if (*T_CWP)
8465 term_set_winpos(x, y);
8466# endif
8467 }
8468}
8469#endif
8470
8471/*
8472 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8473 */
8474 static void
8475ex_operators(eap)
8476 exarg_T *eap;
8477{
8478 oparg_T oa;
8479
8480 clear_oparg(&oa);
8481 oa.regname = eap->regname;
8482 oa.start.lnum = eap->line1;
8483 oa.end.lnum = eap->line2;
8484 oa.line_count = eap->line2 - eap->line1 + 1;
8485 oa.motion_type = MLINE;
8486#ifdef FEAT_VIRTUALEDIT
8487 virtual_op = FALSE;
8488#endif
8489 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8490 {
8491 setpcmark();
8492 curwin->w_cursor.lnum = eap->line1;
8493 beginline(BL_SOL | BL_FIX);
8494 }
8495
8496 switch (eap->cmdidx)
8497 {
8498 case CMD_delete:
8499 oa.op_type = OP_DELETE;
8500 op_delete(&oa);
8501 break;
8502
8503 case CMD_yank:
8504 oa.op_type = OP_YANK;
8505 (void)op_yank(&oa, FALSE, TRUE);
8506 break;
8507
8508 default: /* CMD_rshift or CMD_lshift */
8509 if ((eap->cmdidx == CMD_rshift)
8510#ifdef FEAT_RIGHTLEFT
8511 ^ curwin->w_p_rl
8512#endif
8513 )
8514 oa.op_type = OP_RSHIFT;
8515 else
8516 oa.op_type = OP_LSHIFT;
8517 op_shift(&oa, FALSE, eap->amount);
8518 break;
8519 }
8520#ifdef FEAT_VIRTUALEDIT
8521 virtual_op = MAYBE;
8522#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008523 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524}
8525
8526/*
8527 * ":put".
8528 */
8529 static void
8530ex_put(eap)
8531 exarg_T *eap;
8532{
8533 /* ":0put" works like ":1put!". */
8534 if (eap->line2 == 0)
8535 {
8536 eap->line2 = 1;
8537 eap->forceit = TRUE;
8538 }
8539 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008540 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8541 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544/*
8545 * Handle ":copy" and ":move".
8546 */
8547 static void
8548ex_copymove(eap)
8549 exarg_T *eap;
8550{
8551 long n;
8552
8553 n = get_address(&eap->arg, FALSE, FALSE);
8554 if (eap->arg == NULL) /* error detected */
8555 {
8556 eap->nextcmd = NULL;
8557 return;
8558 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008559 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560
8561 /*
8562 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8563 */
8564 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8565 {
8566 EMSG(_(e_invaddr));
8567 return;
8568 }
8569
8570 if (eap->cmdidx == CMD_move)
8571 {
8572 if (do_move(eap->line1, eap->line2, n) == FAIL)
8573 return;
8574 }
8575 else
8576 ex_copy(eap->line1, eap->line2, n);
8577 u_clearline();
8578 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008579 ex_may_print(eap);
8580}
8581
8582/*
8583 * Print the current line if flags were given to the Ex command.
8584 */
8585 static void
8586ex_may_print(eap)
8587 exarg_T *eap;
8588{
8589 if (eap->flags != 0)
8590 {
8591 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8592 (eap->flags & EXFLAG_LIST));
8593 ex_no_reprint = TRUE;
8594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595}
8596
8597/*
8598 * ":smagic" and ":snomagic".
8599 */
8600 static void
8601ex_submagic(eap)
8602 exarg_T *eap;
8603{
8604 int magic_save = p_magic;
8605
8606 p_magic = (eap->cmdidx == CMD_smagic);
8607 do_sub(eap);
8608 p_magic = magic_save;
8609}
8610
8611/*
8612 * ":join".
8613 */
8614 static void
8615ex_join(eap)
8616 exarg_T *eap;
8617{
8618 curwin->w_cursor.lnum = eap->line1;
8619 if (eap->line1 == eap->line2)
8620 {
8621 if (eap->addr_count >= 2) /* :2,2join does nothing */
8622 return;
8623 if (eap->line2 == curbuf->b_ml.ml_line_count)
8624 {
8625 beep_flush();
8626 return;
8627 }
8628 ++eap->line2;
8629 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008630 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008632 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
8636 * ":[addr]@r" or ":[addr]*r": execute register
8637 */
8638 static void
8639ex_at(eap)
8640 exarg_T *eap;
8641{
8642 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008643 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
8645 curwin->w_cursor.lnum = eap->line2;
8646
8647#ifdef USE_ON_FLY_SCROLL
8648 dont_scroll = TRUE; /* disallow scrolling here */
8649#endif
8650
8651 /* get the register name. No name means to use the previous one */
8652 c = *eap->arg;
8653 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8654 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008655 /* Put the register in the typeahead buffer with the "silent" flag. */
8656 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8657 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 else
8662 {
8663 int save_efr = exec_from_reg;
8664
8665 exec_from_reg = TRUE;
8666
8667 /*
8668 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008669 * Continue until the stuff buffer is empty and all added characters
8670 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008672 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8674
8675 exec_from_reg = save_efr;
8676 }
8677}
8678
8679/*
8680 * ":!".
8681 */
8682 static void
8683ex_bang(eap)
8684 exarg_T *eap;
8685{
8686 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8687}
8688
8689/*
8690 * ":undo".
8691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 static void
8693ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008694 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008696 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008697 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008698 else
8699 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700}
8701
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008702#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008703 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008704ex_wundo(eap)
8705 exarg_T *eap;
8706{
8707 char_u hash[UNDO_HASH_SIZE];
8708
8709 u_compute_hash(hash);
8710 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8711}
8712
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008713 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008714ex_rundo(eap)
8715 exarg_T *eap;
8716{
8717 char_u hash[UNDO_HASH_SIZE];
8718
8719 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008720 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008721}
8722#endif
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724/*
8725 * ":redo".
8726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
8728ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008729 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 u_redo(1);
8732}
8733
8734/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008735 * ":earlier" and ":later".
8736 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008737 static void
8738ex_later(eap)
8739 exarg_T *eap;
8740{
8741 long count = 0;
8742 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008743 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008744 char_u *p = eap->arg;
8745
8746 if (*p == NUL)
8747 count = 1;
8748 else if (isdigit(*p))
8749 {
8750 count = getdigits(&p);
8751 switch (*p)
8752 {
8753 case 's': ++p; sec = TRUE; break;
8754 case 'm': ++p; sec = TRUE; count *= 60; break;
8755 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008756 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8757 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008758 }
8759 }
8760
8761 if (*p != NUL)
8762 EMSG2(_(e_invarg2), eap->arg);
8763 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008764 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8765 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008766}
8767
8768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 * ":redir": start/stop redirection.
8770 */
8771 static void
8772ex_redir(eap)
8773 exarg_T *eap;
8774{
8775 char *mode;
8776 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008777 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
8779 if (STRICMP(eap->arg, "END") == 0)
8780 close_redir();
8781 else
8782 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008783 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008785 ++arg;
8786 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008788 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 mode = "a";
8790 }
8791 else
8792 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008793 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795 close_redir();
8796
8797 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008798 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 if (fname == NULL)
8800 return;
8801#ifdef FEAT_BROWSE
8802 if (cmdmod.browse)
8803 {
8804 char_u *browseFile;
8805
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008806 browseFile = do_browse(BROWSE_SAVE,
8807 (char_u *)_("Save Redirection"),
8808 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (browseFile == NULL)
8810 return; /* operation cancelled */
8811 vim_free(fname);
8812 fname = browseFile;
8813 eap->forceit = TRUE; /* since dialog already asked */
8814 }
8815#endif
8816
8817 redir_fd = open_exfile(fname, eap->forceit, mode);
8818 vim_free(fname);
8819 }
8820#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008821 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
8823 /* redirect to a register a-z (resp. A-Z for appending) */
8824 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008825 ++arg;
8826 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008828 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008829 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008831 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008833 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008834 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008835 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008838 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008839 if (*arg == '>')
8840 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008841 /* Make register empty when not using @A-@Z and the
8842 * command is valid. */
8843 if (*arg == NUL && !isupper(redir_reg))
8844 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008846 }
8847 if (*arg != NUL)
8848 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008849 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008850 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008851 }
8852 }
8853 else if (*arg == '=' && arg[1] == '>')
8854 {
8855 int append;
8856
8857 /* redirect to a variable */
8858 close_redir();
8859 arg += 2;
8860
8861 if (*arg == '>')
8862 {
8863 ++arg;
8864 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008867 append = FALSE;
8868
8869 if (var_redir_start(skipwhite(arg), append) == OK)
8870 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872#endif
8873
8874 /* TODO: redirect to a buffer */
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008877 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008879
8880 /* Make sure redirection is not off. Can happen for cmdline completion
8881 * that indirectly invokes a command to catch its output. */
8882 if (redir_fd != NULL
8883#ifdef FEAT_EVAL
8884 || redir_reg || redir_vname
8885#endif
8886 )
8887 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890/*
8891 * ":redraw": force redraw
8892 */
8893 static void
8894ex_redraw(eap)
8895 exarg_T *eap;
8896{
8897 int r = RedrawingDisabled;
8898 int p = p_lz;
8899
8900 RedrawingDisabled = 0;
8901 p_lz = FALSE;
8902 update_topline();
8903 update_screen(eap->forceit ? CLEAR :
8904#ifdef FEAT_VISUAL
8905 VIsual_active ? INVERTED :
8906#endif
8907 0);
8908#ifdef FEAT_TITLE
8909 if (need_maketitle)
8910 maketitle();
8911#endif
8912 RedrawingDisabled = r;
8913 p_lz = p;
8914
8915 /* Reset msg_didout, so that a message that's there is overwritten. */
8916 msg_didout = FALSE;
8917 msg_col = 0;
8918
8919 out_flush();
8920}
8921
8922/*
8923 * ":redrawstatus": force redraw of status line(s)
8924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 static void
8926ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008927 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929#if defined(FEAT_WINDOWS)
8930 int r = RedrawingDisabled;
8931 int p = p_lz;
8932
8933 RedrawingDisabled = 0;
8934 p_lz = FALSE;
8935 if (eap->forceit)
8936 status_redraw_all();
8937 else
8938 status_redraw_curbuf();
8939 update_screen(
8940# ifdef FEAT_VISUAL
8941 VIsual_active ? INVERTED :
8942# endif
8943 0);
8944 RedrawingDisabled = r;
8945 p_lz = p;
8946 out_flush();
8947#endif
8948}
8949
8950 static void
8951close_redir()
8952{
8953 if (redir_fd != NULL)
8954 {
8955 fclose(redir_fd);
8956 redir_fd = NULL;
8957 }
8958#ifdef FEAT_EVAL
8959 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008960 if (redir_vname)
8961 {
8962 var_redir_stop();
8963 redir_vname = 0;
8964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965#endif
8966}
8967
8968#if defined(FEAT_SESSION) && defined(USE_CRNL)
8969# define MKSESSION_NL
8970static int mksession_nl = FALSE; /* use NL only in put_eol() */
8971#endif
8972
8973/*
8974 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8975 */
8976 static void
8977ex_mkrc(eap)
8978 exarg_T *eap;
8979{
8980 FILE *fd;
8981 int failed = FALSE;
8982 char_u *fname;
8983#ifdef FEAT_BROWSE
8984 char_u *browseFile = NULL;
8985#endif
8986#ifdef FEAT_SESSION
8987 int view_session = FALSE;
8988 int using_vdir = FALSE; /* using 'viewdir'? */
8989 char_u *viewFile = NULL;
8990 unsigned *flagp;
8991#endif
8992
8993 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8994 {
8995#ifdef FEAT_SESSION
8996 view_session = TRUE;
8997#else
8998 ex_ni(eap);
8999 return;
9000#endif
9001 }
9002
9003#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009004 /* Use the short file name until ":lcd" is used. We also don't use the
9005 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009006 did_lcd = FALSE;
9007
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9009 if (eap->cmdidx == CMD_mkview
9010 && (*eap->arg == NUL
9011 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9012 {
9013 eap->forceit = TRUE;
9014 fname = get_view_file(*eap->arg);
9015 if (fname == NULL)
9016 return;
9017 viewFile = fname;
9018 using_vdir = TRUE;
9019 }
9020 else
9021#endif
9022 if (*eap->arg != NUL)
9023 fname = eap->arg;
9024 else if (eap->cmdidx == CMD_mkvimrc)
9025 fname = (char_u *)VIMRC_FILE;
9026#ifdef FEAT_SESSION
9027 else if (eap->cmdidx == CMD_mksession)
9028 fname = (char_u *)SESSION_FILE;
9029#endif
9030 else
9031 fname = (char_u *)EXRC_FILE;
9032
9033#ifdef FEAT_BROWSE
9034 if (cmdmod.browse)
9035 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009036 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037# ifdef FEAT_SESSION
9038 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9039 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9040# endif
9041 (char_u *)_("Save Setup"),
9042 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9043 if (browseFile == NULL)
9044 goto theend;
9045 fname = browseFile;
9046 eap->forceit = TRUE; /* since dialog already asked */
9047 }
9048#endif
9049
9050#if defined(FEAT_SESSION) && defined(vim_mkdir)
9051 /* When using 'viewdir' may have to create the directory. */
9052 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009053 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054#endif
9055
9056 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9057 if (fd != NULL)
9058 {
9059#ifdef FEAT_SESSION
9060 if (eap->cmdidx == CMD_mkview)
9061 flagp = &vop_flags;
9062 else
9063 flagp = &ssop_flags;
9064#endif
9065
9066#ifdef MKSESSION_NL
9067 /* "unix" in 'sessionoptions': use NL line separator */
9068 if (view_session && (*flagp & SSOP_UNIX))
9069 mksession_nl = TRUE;
9070#endif
9071
9072 /* Write the version command for :mkvimrc */
9073 if (eap->cmdidx == CMD_mkvimrc)
9074 (void)put_line(fd, "version 6.0");
9075
9076#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009077 if (eap->cmdidx == CMD_mksession)
9078 {
9079 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9080 failed = TRUE;
9081 }
9082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 if (eap->cmdidx != CMD_mkview)
9084#endif
9085 {
9086 /* Write setting 'compatible' first, because it has side effects.
9087 * For that same reason only do it when needed. */
9088 if (p_cp)
9089 (void)put_line(fd, "if !&cp | set cp | endif");
9090 else
9091 (void)put_line(fd, "if &cp | set nocp | endif");
9092 }
9093
9094#ifdef FEAT_SESSION
9095 if (!view_session
9096 || (eap->cmdidx == CMD_mksession
9097 && (*flagp & SSOP_OPTIONS)))
9098#endif
9099 failed |= (makemap(fd, NULL) == FAIL
9100 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9101
9102#ifdef FEAT_SESSION
9103 if (!failed && view_session)
9104 {
9105 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9106 failed = TRUE;
9107 if (eap->cmdidx == CMD_mksession)
9108 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009109 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110
Bram Moolenaard9462e32011-04-11 21:35:11 +02009111 dirnow = alloc(MAXPATHL);
9112 if (dirnow == NULL)
9113 failed = TRUE;
9114 else
9115 {
9116 /*
9117 * Change to session file's dir.
9118 */
9119 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009121 *dirnow = NUL;
9122 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9123 {
9124 if (vim_chdirfile(fname) == OK)
9125 shorten_fnames(TRUE);
9126 }
9127 else if (*dirnow != NUL
9128 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9129 {
9130 if (mch_chdir((char *)globaldir) == 0)
9131 shorten_fnames(TRUE);
9132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
Bram Moolenaard9462e32011-04-11 21:35:11 +02009134 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
Bram Moolenaard9462e32011-04-11 21:35:11 +02009136 /* restore original dir */
9137 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009139 {
9140 if (mch_chdir((char *)dirnow) != 0)
9141 EMSG(_(e_prev_dir));
9142 shorten_fnames(TRUE);
9143 }
9144 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 }
9146 }
9147 else
9148 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009149 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9150 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 }
9152 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9153 == FAIL)
9154 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009155 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9156 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009157 if (eap->cmdidx == CMD_mksession)
9158 {
9159 if (put_line(fd, "unlet SessionLoad") == FAIL)
9160 failed = TRUE;
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009164 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9165 failed = TRUE;
9166
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 failed |= fclose(fd);
9168
9169 if (failed)
9170 EMSG(_(e_write));
9171#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9172 else if (eap->cmdidx == CMD_mksession)
9173 {
9174 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009175 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
Bram Moolenaard9462e32011-04-11 21:35:11 +02009177 tbuf = alloc(MAXPATHL);
9178 if (tbuf != NULL)
9179 {
9180 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9181 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9182 vim_free(tbuf);
9183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
9185#endif
9186#ifdef MKSESSION_NL
9187 mksession_nl = FALSE;
9188#endif
9189 }
9190
9191#ifdef FEAT_BROWSE
9192theend:
9193 vim_free(browseFile);
9194#endif
9195#ifdef FEAT_SESSION
9196 vim_free(viewFile);
9197#endif
9198}
9199
Bram Moolenaardf177f62005-02-22 08:39:57 +00009200#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9201 || defined(PROTO)
9202 int
9203vim_mkdir_emsg(name, prot)
9204 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009205 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009206{
9207 if (vim_mkdir(name, prot) != 0)
9208 {
9209 EMSG2(_("E739: Cannot create directory: %s"), name);
9210 return FAIL;
9211 }
9212 return OK;
9213}
9214#endif
9215
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216/*
9217 * Open a file for writing for an Ex command, with some checks.
9218 * Return file descriptor, or NULL on failure.
9219 */
9220 FILE *
9221open_exfile(fname, forceit, mode)
9222 char_u *fname;
9223 int forceit;
9224 char *mode; /* "w" for create new file or "a" for append */
9225{
9226 FILE *fd;
9227
9228#ifdef UNIX
9229 /* with Unix it is possible to open a directory */
9230 if (mch_isdir(fname))
9231 {
9232 EMSG2(_(e_isadir2), fname);
9233 return NULL;
9234 }
9235#endif
9236 if (!forceit && *mode != 'a' && vim_fexists(fname))
9237 {
9238 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9239 return NULL;
9240 }
9241
9242 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9243 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9244
9245 return fd;
9246}
9247
9248/*
9249 * ":mark" and ":k".
9250 */
9251 static void
9252ex_mark(eap)
9253 exarg_T *eap;
9254{
9255 pos_T pos;
9256
9257 if (*eap->arg == NUL) /* No argument? */
9258 EMSG(_(e_argreq));
9259 else if (eap->arg[1] != NUL) /* more than one character? */
9260 EMSG(_(e_trailing));
9261 else
9262 {
9263 pos = curwin->w_cursor; /* save curwin->w_cursor */
9264 curwin->w_cursor.lnum = eap->line2;
9265 beginline(BL_WHITE | BL_FIX);
9266 if (setmark(*eap->arg) == FAIL) /* set mark */
9267 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9268 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9269 }
9270}
9271
9272/*
9273 * Update w_topline, w_leftcol and the cursor position.
9274 */
9275 void
9276update_topline_cursor()
9277{
9278 check_cursor(); /* put cursor on valid line */
9279 update_topline();
9280 if (!curwin->w_p_wrap)
9281 validate_cursor();
9282 update_curswant();
9283}
9284
9285#ifdef FEAT_EX_EXTRA
9286/*
9287 * ":normal[!] {commands}": Execute normal mode commands.
9288 */
9289 static void
9290ex_normal(eap)
9291 exarg_T *eap;
9292{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 int save_msg_scroll = msg_scroll;
9294 int save_restart_edit = restart_edit;
9295 int save_msg_didout = msg_didout;
9296 int save_State = State;
9297 tasave_T tabuf;
9298 int save_insertmode = p_im;
9299 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009300 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301#ifdef FEAT_MBYTE
9302 char_u *arg = NULL;
9303 int l;
9304 char_u *p;
9305#endif
9306
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009307 if (ex_normal_lock > 0)
9308 {
9309 EMSG(_(e_secure));
9310 return;
9311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 if (ex_normal_busy >= p_mmd)
9313 {
9314 EMSG(_("E192: Recursive use of :normal too deep"));
9315 return;
9316 }
9317 ++ex_normal_busy;
9318
9319 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9320 restart_edit = 0; /* don't go to Insert mode */
9321 p_im = FALSE; /* don't use 'insertmode' */
9322
9323#ifdef FEAT_MBYTE
9324 /*
9325 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9326 * this for the K_SPECIAL leading byte, otherwise special keys will not
9327 * work.
9328 */
9329 if (has_mbyte)
9330 {
9331 int len = 0;
9332
9333 /* Count the number of characters to be escaped. */
9334 for (p = eap->arg; *p != NUL; ++p)
9335 {
9336# ifdef FEAT_GUI
9337 if (*p == CSI) /* leadbyte CSI */
9338 len += 2;
9339# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009340 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9342# ifdef FEAT_GUI
9343 || *p == CSI
9344# endif
9345 )
9346 len += 2;
9347 }
9348 if (len > 0)
9349 {
9350 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9351 if (arg != NULL)
9352 {
9353 len = 0;
9354 for (p = eap->arg; *p != NUL; ++p)
9355 {
9356 arg[len++] = *p;
9357# ifdef FEAT_GUI
9358 if (*p == CSI)
9359 {
9360 arg[len++] = KS_EXTRA;
9361 arg[len++] = (int)KE_CSI;
9362 }
9363# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009364 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 {
9366 arg[len++] = *++p;
9367 if (*p == K_SPECIAL)
9368 {
9369 arg[len++] = KS_SPECIAL;
9370 arg[len++] = KE_FILLER;
9371 }
9372# ifdef FEAT_GUI
9373 else if (*p == CSI)
9374 {
9375 arg[len++] = KS_EXTRA;
9376 arg[len++] = (int)KE_CSI;
9377 }
9378# endif
9379 }
9380 arg[len] = NUL;
9381 }
9382 }
9383 }
9384 }
9385#endif
9386
9387 /*
9388 * Save the current typeahead. This is required to allow using ":normal"
9389 * from an event handler and makes sure we don't hang when the argument
9390 * ends with half a command.
9391 */
9392 save_typeahead(&tabuf);
9393 if (tabuf.typebuf_valid)
9394 {
9395 /*
9396 * Repeat the :normal command for each line in the range. When no
9397 * range given, execute it just once, without positioning the cursor
9398 * first.
9399 */
9400 do
9401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 if (eap->addr_count != 0)
9403 {
9404 curwin->w_cursor.lnum = eap->line1++;
9405 curwin->w_cursor.col = 0;
9406 }
9407
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009408 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409#ifdef FEAT_MBYTE
9410 arg != NULL ? arg :
9411#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009412 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 }
9414 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9415 }
9416
9417 /* Might not return to the main loop when in an event handler. */
9418 update_topline_cursor();
9419
9420 /* Restore the previous typeahead. */
9421 restore_typeahead(&tabuf);
9422
9423 --ex_normal_busy;
9424 msg_scroll = save_msg_scroll;
9425 restart_edit = save_restart_edit;
9426 p_im = save_insertmode;
9427 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009428 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9430
9431 /* Restore the state (needed when called from a function executed for
9432 * 'indentexpr'). */
9433 State = save_State;
9434#ifdef FEAT_MBYTE
9435 vim_free(arg);
9436#endif
9437}
9438
9439/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009440 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 */
9442 static void
9443ex_startinsert(eap)
9444 exarg_T *eap;
9445{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009446 if (eap->forceit)
9447 {
9448 coladvance((colnr_T)MAXCOL);
9449 curwin->w_curswant = MAXCOL;
9450 curwin->w_set_curswant = FALSE;
9451 }
9452
Bram Moolenaara40c5002005-01-09 21:16:21 +00009453 /* Ignore the command when already in Insert mode. Inserting an
9454 * expression register that invokes a function can do this. */
9455 if (State & INSERT)
9456 return;
9457
Bram Moolenaar64969662005-12-14 21:59:55 +00009458 if (eap->cmdidx == CMD_startinsert)
9459 restart_edit = 'a';
9460 else if (eap->cmdidx == CMD_startreplace)
9461 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009463 restart_edit = 'V';
9464
9465 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009467 if (eap->cmdidx == CMD_startinsert)
9468 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 curwin->w_curswant = 0; /* avoid MAXCOL */
9470 }
9471}
9472
9473/*
9474 * ":stopinsert"
9475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 static void
9477ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009478 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
9480 restart_edit = 0;
9481 stop_insert_mode = TRUE;
9482}
9483#endif
9484
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009485#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9486/*
9487 * Execute normal mode command "cmd".
9488 * "remap" can be REMAP_NONE or REMAP_YES.
9489 */
9490 void
9491exec_normal_cmd(cmd, remap, silent)
9492 char_u *cmd;
9493 int remap;
9494 int silent;
9495{
9496 oparg_T oa;
9497
9498 /*
9499 * Stuff the argument into the typeahead buffer.
9500 * Execute normal_cmd() until there is no typeahead left.
9501 */
9502 clear_oparg(&oa);
9503 finish_op = FALSE;
9504 ins_typebuf(cmd, remap, 0, TRUE, silent);
9505 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9506 && !got_int)
9507 {
9508 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009509 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009510 }
9511}
9512#endif
9513
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514#ifdef FEAT_FIND_ID
9515 static void
9516ex_checkpath(eap)
9517 exarg_T *eap;
9518{
9519 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9520 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9521 (linenr_T)1, (linenr_T)MAXLNUM);
9522}
9523
9524#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9525/*
9526 * ":psearch"
9527 */
9528 static void
9529ex_psearch(eap)
9530 exarg_T *eap;
9531{
9532 g_do_tagpreview = p_pvh;
9533 ex_findpat(eap);
9534 g_do_tagpreview = 0;
9535}
9536#endif
9537
9538 static void
9539ex_findpat(eap)
9540 exarg_T *eap;
9541{
9542 int whole = TRUE;
9543 long n;
9544 char_u *p;
9545 int action;
9546
9547 switch (cmdnames[eap->cmdidx].cmd_name[2])
9548 {
9549 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9550 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9551 action = ACTION_GOTO;
9552 else
9553 action = ACTION_SHOW;
9554 break;
9555 case 'i': /* ":ilist" and ":dlist" */
9556 action = ACTION_SHOW_ALL;
9557 break;
9558 case 'u': /* ":ijump" and ":djump" */
9559 action = ACTION_GOTO;
9560 break;
9561 default: /* ":isplit" and ":dsplit" */
9562 action = ACTION_SPLIT;
9563 break;
9564 }
9565
9566 n = 1;
9567 if (vim_isdigit(*eap->arg)) /* get count */
9568 {
9569 n = getdigits(&eap->arg);
9570 eap->arg = skipwhite(eap->arg);
9571 }
9572 if (*eap->arg == '/') /* Match regexp, not just whole words */
9573 {
9574 whole = FALSE;
9575 ++eap->arg;
9576 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9577 if (*p)
9578 {
9579 *p++ = NUL;
9580 p = skipwhite(p);
9581
9582 /* Check for trailing illegal characters */
9583 if (!ends_excmd(*p))
9584 eap->errmsg = e_trailing;
9585 else
9586 eap->nextcmd = check_nextcmd(p);
9587 }
9588 }
9589 if (!eap->skip)
9590 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9591 whole, !eap->forceit,
9592 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9593 n, action, eap->line1, eap->line2);
9594}
9595#endif
9596
9597#ifdef FEAT_WINDOWS
9598
9599# ifdef FEAT_QUICKFIX
9600/*
9601 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9602 */
9603 static void
9604ex_ptag(eap)
9605 exarg_T *eap;
9606{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009607 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9609}
9610
9611/*
9612 * ":pedit"
9613 */
9614 static void
9615ex_pedit(eap)
9616 exarg_T *eap;
9617{
9618 win_T *curwin_save = curwin;
9619
9620 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009621 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 keep_help_flag = curwin_save->w_buffer->b_help;
9623 do_exedit(eap, NULL);
9624 keep_help_flag = FALSE;
9625 if (curwin != curwin_save && win_valid(curwin_save))
9626 {
9627 /* Return cursor to where we were */
9628 validate_cursor();
9629 redraw_later(VALID);
9630 win_enter(curwin_save, TRUE);
9631 }
9632 g_do_tagpreview = 0;
9633}
9634# endif
9635
9636/*
9637 * ":stag", ":stselect" and ":stjump".
9638 */
9639 static void
9640ex_stag(eap)
9641 exarg_T *eap;
9642{
9643 postponed_split = -1;
9644 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009645 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9647 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009648 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650#endif
9651
9652/*
9653 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9654 */
9655 static void
9656ex_tag(eap)
9657 exarg_T *eap;
9658{
9659 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9660}
9661
9662 static void
9663ex_tag_cmd(eap, name)
9664 exarg_T *eap;
9665 char_u *name;
9666{
9667 int cmd;
9668
9669 switch (name[1])
9670 {
9671 case 'j': cmd = DT_JUMP; /* ":tjump" */
9672 break;
9673 case 's': cmd = DT_SELECT; /* ":tselect" */
9674 break;
9675 case 'p': cmd = DT_PREV; /* ":tprevious" */
9676 break;
9677 case 'N': cmd = DT_PREV; /* ":tNext" */
9678 break;
9679 case 'n': cmd = DT_NEXT; /* ":tnext" */
9680 break;
9681 case 'o': cmd = DT_POP; /* ":pop" */
9682 break;
9683 case 'f': /* ":tfirst" */
9684 case 'r': cmd = DT_FIRST; /* ":trewind" */
9685 break;
9686 case 'l': cmd = DT_LAST; /* ":tlast" */
9687 break;
9688 default: /* ":tag" */
9689#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009690 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 {
9692 do_cstag(eap);
9693 return;
9694 }
9695#endif
9696 cmd = DT_TAG;
9697 break;
9698 }
9699
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009700 if (name[0] == 'l')
9701 {
9702#ifndef FEAT_QUICKFIX
9703 ex_ni(eap);
9704 return;
9705#else
9706 cmd = DT_LTAG;
9707#endif
9708 }
9709
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9711 eap->forceit, TRUE);
9712}
9713
9714/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009715 * Check "str" for starting with a special cmdline variable.
9716 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9717 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9718 */
9719 int
9720find_cmdline_var(src, usedlen)
9721 char_u *src;
9722 int *usedlen;
9723{
9724 int len;
9725 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009726 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009727 "%",
9728#define SPEC_PERC 0
9729 "#",
9730#define SPEC_HASH 1
9731 "<cword>", /* cursor word */
9732#define SPEC_CWORD 2
9733 "<cWORD>", /* cursor WORD */
9734#define SPEC_CCWORD 3
9735 "<cfile>", /* cursor path name */
9736#define SPEC_CFILE 4
9737 "<sfile>", /* ":so" file name */
9738#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009739 "<slnum>", /* ":so" file line number */
9740#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009741#ifdef FEAT_AUTOCMD
9742 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009743# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009744 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009745# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009746 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009747# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009748#endif
9749#ifdef FEAT_CLIENTSERVER
9750 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009751# ifdef FEAT_AUTOCMD
9752# define SPEC_CLIENT 10
9753# else
9754# define SPEC_CLIENT 7
9755# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009756#endif
9757 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009758
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009759 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009760 {
9761 len = (int)STRLEN(spec_str[i]);
9762 if (STRNCMP(src, spec_str[i], len) == 0)
9763 {
9764 *usedlen = len;
9765 return i;
9766 }
9767 }
9768 return -1;
9769}
9770
9771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 * Evaluate cmdline variables.
9773 *
9774 * change '%' to curbuf->b_ffname
9775 * '#' to curwin->w_altfile
9776 * '<cword>' to word under the cursor
9777 * '<cWORD>' to WORD under the cursor
9778 * '<cfile>' to path name under the cursor
9779 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009780 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 * '<afile>' to file name for autocommand
9782 * '<abuf>' to buffer number for autocommand
9783 * '<amatch>' to matching name for autocommand
9784 *
9785 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9786 * "" for error without a message) and NULL is returned.
9787 * Returns an allocated string if a valid match was found.
9788 * Returns NULL if no match was found. "usedlen" then still contains the
9789 * number of characters to skip.
9790 */
9791 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009792eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009794 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 int *usedlen; /* characters after src that are used */
9796 linenr_T *lnump; /* line number for :e command, or NULL */
9797 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009798 int *escaped; /* return value has escaped white space (can
9799 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801 int i;
9802 char_u *s;
9803 char_u *result;
9804 char_u *resultbuf = NULL;
9805 int resultlen;
9806 buf_T *buf;
9807 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9808 int spec_idx;
9809#ifdef FEAT_MODIFY_FNAME
9810 int skip_mod = FALSE;
9811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813
9814 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009815 if (escaped != NULL)
9816 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817
9818 /*
9819 * Check if there is something to do.
9820 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009821 spec_idx = find_cmdline_var(src, usedlen);
9822 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
9824 *usedlen = 1;
9825 return NULL;
9826 }
9827
9828 /*
9829 * Skip when preceded with a backslash "\%" and "\#".
9830 * Note: In "\\%" the % is also not recognized!
9831 */
9832 if (src > srcstart && src[-1] == '\\')
9833 {
9834 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009835 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 return NULL;
9837 }
9838
9839 /*
9840 * word or WORD under cursor
9841 */
9842 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9843 {
9844 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9845 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9846 if (resultlen == 0)
9847 {
9848 *errormsg = (char_u *)"";
9849 return NULL;
9850 }
9851 }
9852
9853 /*
9854 * '#': Alternate file name
9855 * '%': Current file name
9856 * File name under the cursor
9857 * File name for autocommand
9858 * and following modifiers
9859 */
9860 else
9861 {
9862 switch (spec_idx)
9863 {
9864 case SPEC_PERC: /* '%': current file */
9865 if (curbuf->b_fname == NULL)
9866 {
9867 result = (char_u *)"";
9868 valid = 0; /* Must have ":p:h" to be valid */
9869 }
9870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 break;
9873
9874 case SPEC_HASH: /* '#' or "#99": alternate file */
9875 if (src[1] == '#') /* "##": the argument list */
9876 {
9877 result = arg_all();
9878 resultbuf = result;
9879 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009880 if (escaped != NULL)
9881 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882#ifdef FEAT_MODIFY_FNAME
9883 skip_mod = TRUE;
9884#endif
9885 break;
9886 }
9887 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009888 if (*s == '<') /* "#<99" uses v:oldfiles */
9889 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 i = (int)getdigits(&s);
9891 *usedlen = (int)(s - src); /* length of what we expand */
9892
Bram Moolenaard812df62008-11-09 12:46:09 +00009893 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009895 if (*usedlen < 2)
9896 {
9897 /* Should we give an error message for #<text? */
9898 *usedlen = 1;
9899 return NULL;
9900 }
9901#ifdef FEAT_EVAL
9902 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9903 (long)i);
9904 if (result == NULL)
9905 {
9906 *errormsg = (char_u *)"";
9907 return NULL;
9908 }
9909#else
9910 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 }
9914 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009915 {
9916 buf = buflist_findnr(i);
9917 if (buf == NULL)
9918 {
9919 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9920 return NULL;
9921 }
9922 if (lnump != NULL)
9923 *lnump = ECMD_LAST;
9924 if (buf->b_fname == NULL)
9925 {
9926 result = (char_u *)"";
9927 valid = 0; /* Must have ":p:h" to be valid */
9928 }
9929 else
9930 result = buf->b_fname;
9931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 break;
9933
9934#ifdef FEAT_SEARCHPATH
9935 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009936 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 if (result == NULL)
9938 {
9939 *errormsg = (char_u *)"";
9940 return NULL;
9941 }
9942 resultbuf = result; /* remember allocated string */
9943 break;
9944#endif
9945
9946#ifdef FEAT_AUTOCMD
9947 case SPEC_AFILE: /* file name for autocommand */
9948 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009949 if (result != NULL && !autocmd_fname_full)
9950 {
9951 /* Still need to turn the fname into a full path. It is
9952 * postponed to avoid a delay when <afile> is not used. */
9953 autocmd_fname_full = TRUE;
9954 result = FullName_save(autocmd_fname, FALSE);
9955 vim_free(autocmd_fname);
9956 autocmd_fname = result;
9957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (result == NULL)
9959 {
9960 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9961 return NULL;
9962 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009963 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 break;
9965
9966 case SPEC_ABUF: /* buffer number for autocommand */
9967 if (autocmd_bufnr <= 0)
9968 {
9969 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9970 return NULL;
9971 }
9972 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9973 result = strbuf;
9974 break;
9975
9976 case SPEC_AMATCH: /* match name for autocommand */
9977 result = autocmd_match;
9978 if (result == NULL)
9979 {
9980 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9981 return NULL;
9982 }
9983 break;
9984
9985#endif
9986 case SPEC_SFILE: /* file name for ":so" command */
9987 result = sourcing_name;
9988 if (result == NULL)
9989 {
9990 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9991 return NULL;
9992 }
9993 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009994 case SPEC_SLNUM: /* line in file for ":so" command */
9995 if (sourcing_name == NULL || sourcing_lnum == 0)
9996 {
9997 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9998 return NULL;
9999 }
10000 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10001 result = strbuf;
10002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003#if defined(FEAT_CLIENTSERVER)
10004 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010005 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10006 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 result = strbuf;
10008 break;
10009#endif
10010 }
10011
10012 resultlen = (int)STRLEN(result); /* length of new string */
10013 if (src[*usedlen] == '<') /* remove the file name extension */
10014 {
10015 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 resultlen = (int)(s - result);
10018 }
10019#ifdef FEAT_MODIFY_FNAME
10020 else if (!skip_mod)
10021 {
10022 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10023 &resultlen);
10024 if (result == NULL)
10025 {
10026 *errormsg = (char_u *)"";
10027 return NULL;
10028 }
10029 }
10030#endif
10031 }
10032
10033 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10034 {
10035 if (valid != VALID_HEAD + VALID_PATH)
10036 /* xgettext:no-c-format */
10037 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10038 else
10039 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10040 result = NULL;
10041 }
10042 else
10043 result = vim_strnsave(result, resultlen);
10044 vim_free(resultbuf);
10045 return result;
10046}
10047
10048/*
10049 * Concatenate all files in the argument list, separated by spaces, and return
10050 * it in one allocated string.
10051 * Spaces and backslashes in the file names are escaped with a backslash.
10052 * Returns NULL when out of memory.
10053 */
10054 static char_u *
10055arg_all()
10056{
10057 int len;
10058 int idx;
10059 char_u *retval = NULL;
10060 char_u *p;
10061
10062 /*
10063 * Do this loop two times:
10064 * first time: compute the total length
10065 * second time: concatenate the names
10066 */
10067 for (;;)
10068 {
10069 len = 0;
10070 for (idx = 0; idx < ARGCOUNT; ++idx)
10071 {
10072 p = alist_name(&ARGLIST[idx]);
10073 if (p != NULL)
10074 {
10075 if (len > 0)
10076 {
10077 /* insert a space in between names */
10078 if (retval != NULL)
10079 retval[len] = ' ';
10080 ++len;
10081 }
10082 for ( ; *p != NUL; ++p)
10083 {
10084 if (*p == ' ' || *p == '\\')
10085 {
10086 /* insert a backslash */
10087 if (retval != NULL)
10088 retval[len] = '\\';
10089 ++len;
10090 }
10091 if (retval != NULL)
10092 retval[len] = *p;
10093 ++len;
10094 }
10095 }
10096 }
10097
10098 /* second time: break here */
10099 if (retval != NULL)
10100 {
10101 retval[len] = NUL;
10102 break;
10103 }
10104
10105 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010106 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 if (retval == NULL)
10108 break;
10109 }
10110
10111 return retval;
10112}
10113
10114#if defined(FEAT_AUTOCMD) || defined(PROTO)
10115/*
10116 * Expand the <sfile> string in "arg".
10117 *
10118 * Returns an allocated string, or NULL for any error.
10119 */
10120 char_u *
10121expand_sfile(arg)
10122 char_u *arg;
10123{
10124 char_u *errormsg;
10125 int len;
10126 char_u *result;
10127 char_u *newres;
10128 char_u *repl;
10129 int srclen;
10130 char_u *p;
10131
10132 result = vim_strsave(arg);
10133 if (result == NULL)
10134 return NULL;
10135
10136 for (p = result; *p; )
10137 {
10138 if (STRNCMP(p, "<sfile>", 7) != 0)
10139 ++p;
10140 else
10141 {
10142 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010143 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (errormsg != NULL)
10145 {
10146 if (*errormsg)
10147 emsg(errormsg);
10148 vim_free(result);
10149 return NULL;
10150 }
10151 if (repl == NULL) /* no match (cannot happen) */
10152 {
10153 p += srclen;
10154 continue;
10155 }
10156 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10157 newres = alloc(len);
10158 if (newres == NULL)
10159 {
10160 vim_free(repl);
10161 vim_free(result);
10162 return NULL;
10163 }
10164 mch_memmove(newres, result, (size_t)(p - result));
10165 STRCPY(newres + (p - result), repl);
10166 len = (int)STRLEN(newres);
10167 STRCAT(newres, p + srclen);
10168 vim_free(repl);
10169 vim_free(result);
10170 result = newres;
10171 p = newres + len; /* continue after the match */
10172 }
10173 }
10174
10175 return result;
10176}
10177#endif
10178
10179#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010180static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10181 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10183static frame_T *ses_skipframe __ARGS((frame_T *fr));
10184static int ses_do_frame __ARGS((frame_T *fr));
10185static int ses_do_win __ARGS((win_T *wp));
10186static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10187static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10188static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10189
10190/*
10191 * Write openfile commands for the current buffers to an .exrc file.
10192 * Return FAIL on error, OK otherwise.
10193 */
10194 static int
10195makeopens(fd, dirnow)
10196 FILE *fd;
10197 char_u *dirnow; /* Current directory name */
10198{
10199 buf_T *buf;
10200 int only_save_windows = TRUE;
10201 int nr;
10202 int cnr = 1;
10203 int restore_size = TRUE;
10204 win_T *wp;
10205 char_u *sname;
10206 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010207 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010208 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010209 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010210 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010211 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212
10213 if (ssop_flags & SSOP_BUFFERS)
10214 only_save_windows = FALSE; /* Save ALL buffers */
10215
10216 /*
10217 * Begin by setting the this_session variable, and then other
10218 * sessionable variables.
10219 */
10220#ifdef FEAT_EVAL
10221 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10222 return FAIL;
10223 if (ssop_flags & SSOP_GLOBALS)
10224 if (store_session_globals(fd) == FAIL)
10225 return FAIL;
10226#endif
10227
10228 /*
10229 * Close all windows but one.
10230 */
10231 if (put_line(fd, "silent only") == FAIL)
10232 return FAIL;
10233
10234 /*
10235 * Now a :cd command to the session directory or the current directory
10236 */
10237 if (ssop_flags & SSOP_SESDIR)
10238 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010239 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10240 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 return FAIL;
10242 }
10243 else if (ssop_flags & SSOP_CURDIR)
10244 {
10245 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10246 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010247 || fputs("cd ", fd) < 0
10248 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10249 || put_eol(fd) == FAIL)
10250 {
10251 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 vim_free(sname);
10255 }
10256
10257 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010258 * If there is an empty, unnamed buffer we will wipe it out later.
10259 * Remember the buffer number.
10260 */
10261 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10262 return FAIL;
10263 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10264 return FAIL;
10265 if (put_line(fd, "endif") == FAIL)
10266 return FAIL;
10267
10268 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 * Now save the current files, current buffer first.
10270 */
10271 if (put_line(fd, "set shortmess=aoO") == FAIL)
10272 return FAIL;
10273
10274 /* Now put the other buffers into the buffer list */
10275 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10276 {
10277 if (!(only_save_windows && buf->b_nwindows == 0)
10278 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10279 && buf->b_fname != NULL
10280 && buf->b_p_bl)
10281 {
10282 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10283 : buf->b_wininfo->wi_fpos.lnum) < 0
10284 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10285 return FAIL;
10286 }
10287 }
10288
10289 /* the global argument list */
10290 if (ses_arglist(fd, "args", &global_alist.al_ga,
10291 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10292 return FAIL;
10293
10294 if (ssop_flags & SSOP_RESIZE)
10295 {
10296 /* Note: after the restore we still check it worked!*/
10297 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10298 || put_eol(fd) == FAIL)
10299 return FAIL;
10300 }
10301
10302#ifdef FEAT_GUI
10303 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10304 {
10305 int x, y;
10306
10307 if (gui_mch_get_winpos(&x, &y) == OK)
10308 {
10309 /* Note: after the restore we still check it worked!*/
10310 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10311 return FAIL;
10312 }
10313 }
10314#endif
10315
10316 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010317 * May repeat putting Windows for each tab, when "tabpages" is in
10318 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010319 * Don't use goto_tabpage(), it may change directory and trigger
10320 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010322 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010323 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010324 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010326 int need_tabnew = FALSE;
10327
Bram Moolenaar18144c82006-04-12 21:52:12 +000010328 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010330 tabpage_T *tp = find_tabpage(tabnr);
10331
10332 if (tp == NULL)
10333 break; /* done all tab pages */
10334 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010335 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010336 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010337 tab_topframe = topframe;
10338 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010339 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010340 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010341 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010342 tab_topframe = tp->tp_topframe;
10343 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010344 if (tabnr > 1)
10345 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347
Bram Moolenaar18144c82006-04-12 21:52:12 +000010348 /*
10349 * Before creating the window layout, try loading one file. If this
10350 * is aborted we don't end up with a number of useless windows.
10351 * This may have side effects! (e.g., compressed or network file).
10352 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010353 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010354 {
10355 if (ses_do_win(wp)
10356 && wp->w_buffer->b_ffname != NULL
10357 && !wp->w_buffer->b_help
10358#ifdef FEAT_QUICKFIX
10359 && !bt_nofile(wp->w_buffer)
10360#endif
10361 )
10362 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010363 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010364 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10365 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010366 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010367 if (!wp->w_arg_idx_invalid)
10368 edited_win = wp;
10369 break;
10370 }
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010373 /* If no file got edited create an empty tab page. */
10374 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10375 return FAIL;
10376
Bram Moolenaar18144c82006-04-12 21:52:12 +000010377 /*
10378 * Save current window layout.
10379 */
10380 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010382 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010384 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10385 return FAIL;
10386 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388
Bram Moolenaar18144c82006-04-12 21:52:12 +000010389 /*
10390 * Check if window sizes can be restored (no windows omitted).
10391 * Remember the window number of the current window after restoring.
10392 */
10393 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010394 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010395 {
10396 if (ses_do_win(wp))
10397 ++nr;
10398 else
10399 restore_size = FALSE;
10400 if (curwin == wp)
10401 cnr = nr;
10402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403
Bram Moolenaar18144c82006-04-12 21:52:12 +000010404 /* Go to the first window. */
10405 if (put_line(fd, "wincmd t") == FAIL)
10406 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010407
Bram Moolenaar18144c82006-04-12 21:52:12 +000010408 /*
10409 * If more than one window, see if sizes can be restored.
10410 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10411 * resized when moving between windows.
10412 * Do this before restoring the view, so that the topline and the
10413 * cursor can be set. This is done again below.
10414 */
10415 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10416 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010417 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419
Bram Moolenaar18144c82006-04-12 21:52:12 +000010420 /*
10421 * Restore the view of the window (options, file, cursor, etc.).
10422 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010423 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010424 {
10425 if (!ses_do_win(wp))
10426 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010427 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10428 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010429 return FAIL;
10430 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10431 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010432 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010433 }
10434
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010435 /* The argument index in the first tab page is zero, need to set it in
10436 * each window. For further tab pages it's the window where we do
10437 * "tabedit". */
10438 cur_arg_idx = next_arg_idx;
10439
Bram Moolenaar18144c82006-04-12 21:52:12 +000010440 /*
10441 * Restore cursor to the current window if it's not the first one.
10442 */
10443 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10444 || put_eol(fd) == FAIL))
10445 return FAIL;
10446
10447 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010448 * Restore window sizes again after jumping around in windows, because
10449 * the current window has a minimum size while others may not.
10450 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010451 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010452 return FAIL;
10453
Bram Moolenaar18144c82006-04-12 21:52:12 +000010454 /* Don't continue in another tab page when doing only the current one
10455 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010456 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010457 break;
10458 }
10459
10460 if (ssop_flags & SSOP_TABPAGES)
10461 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010462 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10463 || put_eol(fd) == FAIL)
10464 return FAIL;
10465 }
10466
Bram Moolenaar9c102382006-05-03 21:26:49 +000010467 /*
10468 * Wipe out an empty unnamed buffer we started in.
10469 */
10470 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10471 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010472 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010473 return FAIL;
10474 if (put_line(fd, "endif") == FAIL)
10475 return FAIL;
10476 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10477 return FAIL;
10478
10479 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10480 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10481 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484 /*
10485 * Lastly, execute the x.vim file if it exists.
10486 */
10487 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10488 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010489 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 || put_line(fd, "endif") == FAIL)
10491 return FAIL;
10492
10493 return OK;
10494}
10495
10496 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010497ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 FILE *fd;
10499 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010500 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 int n = 0;
10503 win_T *wp;
10504
10505 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10506 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010507 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 {
10509 if (!ses_do_win(wp))
10510 continue;
10511 ++n;
10512
10513 /* restore height when not full height */
10514 if (wp->w_height + wp->w_status_height < topframe->fr_height
10515 && (fprintf(fd,
10516 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10517 n, (long)wp->w_height, Rows / 2, Rows) < 0
10518 || put_eol(fd) == FAIL))
10519 return FAIL;
10520
10521 /* restore width when not full width */
10522 if (wp->w_width < Columns && (fprintf(fd,
10523 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10524 n, (long)wp->w_width, Columns / 2, Columns) < 0
10525 || put_eol(fd) == FAIL))
10526 return FAIL;
10527 }
10528 }
10529 else
10530 {
10531 /* Just equalise window sizes */
10532 if (put_line(fd, "wincmd =") == FAIL)
10533 return FAIL;
10534 }
10535 return OK;
10536}
10537
10538/*
10539 * Write commands to "fd" to recursively create windows for frame "fr",
10540 * horizontally and vertically split.
10541 * After the commands the last window in the frame is the current window.
10542 * Returns FAIL when writing the commands to "fd" fails.
10543 */
10544 static int
10545ses_win_rec(fd, fr)
10546 FILE *fd;
10547 frame_T *fr;
10548{
10549 frame_T *frc;
10550 int count = 0;
10551
10552 if (fr->fr_layout != FR_LEAF)
10553 {
10554 /* Find first frame that's not skipped and then create a window for
10555 * each following one (first frame is already there). */
10556 frc = ses_skipframe(fr->fr_child);
10557 if (frc != NULL)
10558 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10559 {
10560 /* Make window as big as possible so that we have lots of room
10561 * to split. */
10562 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10563 || put_line(fd, fr->fr_layout == FR_COL
10564 ? "split" : "vsplit") == FAIL)
10565 return FAIL;
10566 ++count;
10567 }
10568
10569 /* Go back to the first window. */
10570 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10571 ? "%dwincmd k" : "%dwincmd h", count) < 0
10572 || put_eol(fd) == FAIL))
10573 return FAIL;
10574
10575 /* Recursively create frames/windows in each window of this column or
10576 * row. */
10577 frc = ses_skipframe(fr->fr_child);
10578 while (frc != NULL)
10579 {
10580 ses_win_rec(fd, frc);
10581 frc = ses_skipframe(frc->fr_next);
10582 /* Go to next window. */
10583 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10584 return FAIL;
10585 }
10586 }
10587 return OK;
10588}
10589
10590/*
10591 * Skip frames that don't contain windows we want to save in the Session.
10592 * Returns NULL when there none.
10593 */
10594 static frame_T *
10595ses_skipframe(fr)
10596 frame_T *fr;
10597{
10598 frame_T *frc;
10599
10600 for (frc = fr; frc != NULL; frc = frc->fr_next)
10601 if (ses_do_frame(frc))
10602 break;
10603 return frc;
10604}
10605
10606/*
10607 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10608 * the Session.
10609 */
10610 static int
10611ses_do_frame(fr)
10612 frame_T *fr;
10613{
10614 frame_T *frc;
10615
10616 if (fr->fr_layout == FR_LEAF)
10617 return ses_do_win(fr->fr_win);
10618 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10619 if (ses_do_frame(frc))
10620 return TRUE;
10621 return FALSE;
10622}
10623
10624/*
10625 * Return non-zero if window "wp" is to be stored in the Session.
10626 */
10627 static int
10628ses_do_win(wp)
10629 win_T *wp;
10630{
10631 if (wp->w_buffer->b_fname == NULL
10632#ifdef FEAT_QUICKFIX
10633 /* When 'buftype' is "nofile" can't restore the window contents. */
10634 || bt_nofile(wp->w_buffer)
10635#endif
10636 )
10637 return (ssop_flags & SSOP_BLANK);
10638 if (wp->w_buffer->b_help)
10639 return (ssop_flags & SSOP_HELP);
10640 return TRUE;
10641}
10642
10643/*
10644 * Write commands to "fd" to restore the view of a window.
10645 * Caller must make sure 'scrolloff' is zero.
10646 */
10647 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010648put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 FILE *fd;
10650 win_T *wp;
10651 int add_edit; /* add ":edit" command to view */
10652 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010653 int current_arg_idx; /* current argument index of the window, use
10654 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655{
10656 win_T *save_curwin;
10657 int f;
10658 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010659 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660
10661 /* Always restore cursor position for ":mksession". For ":mkview" only
10662 * when 'viewoptions' contains "cursor". */
10663 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10664
10665 /*
10666 * Local argument list.
10667 */
10668 if (wp->w_alist == &global_alist)
10669 {
10670 if (put_line(fd, "argglobal") == FAIL)
10671 return FAIL;
10672 }
10673 else
10674 {
10675 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10676 flagp == &vop_flags
10677 || !(*flagp & SSOP_CURDIR)
10678 || wp->w_localdir != NULL, flagp) == FAIL)
10679 return FAIL;
10680 }
10681
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010682 /* Only when part of a session: restore the argument index. Some
10683 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010684 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010685 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010687 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 || put_eol(fd) == FAIL)
10689 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010690 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 }
10692
10693 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010694 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 {
10696 /*
10697 * Load the file.
10698 */
10699 if (wp->w_buffer->b_ffname != NULL
10700#ifdef FEAT_QUICKFIX
10701 && !bt_nofile(wp->w_buffer)
10702#endif
10703 )
10704 {
10705 /*
10706 * Editing a file in this buffer: use ":edit file".
10707 * This may have side effects! (e.g., compressed or network file).
10708 */
10709 if (fputs("edit ", fd) < 0
10710 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10711 return FAIL;
10712 }
10713 else
10714 {
10715 /* No file in this buffer, just make it empty. */
10716 if (put_line(fd, "enew") == FAIL)
10717 return FAIL;
10718#ifdef FEAT_QUICKFIX
10719 if (wp->w_buffer->b_ffname != NULL)
10720 {
10721 /* The buffer does have a name, but it's not a file name. */
10722 if (fputs("file ", fd) < 0
10723 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10724 return FAIL;
10725 }
10726#endif
10727 do_cursor = FALSE;
10728 }
10729 }
10730
10731 /*
10732 * Local mappings and abbreviations.
10733 */
10734 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10735 && makemap(fd, wp->w_buffer) == FAIL)
10736 return FAIL;
10737
10738 /*
10739 * Local options. Need to go to the window temporarily.
10740 * Store only local values when using ":mkview" and when ":mksession" is
10741 * used and 'sessionoptions' doesn't include "options".
10742 * Some folding options are always stored when "folds" is included,
10743 * otherwise the folds would not be restored correctly.
10744 */
10745 save_curwin = curwin;
10746 curwin = wp;
10747 curbuf = curwin->w_buffer;
10748 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10749 f = makeset(fd, OPT_LOCAL,
10750 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10751#ifdef FEAT_FOLDING
10752 else if (*flagp & SSOP_FOLDS)
10753 f = makefoldset(fd);
10754#endif
10755 else
10756 f = OK;
10757 curwin = save_curwin;
10758 curbuf = curwin->w_buffer;
10759 if (f == FAIL)
10760 return FAIL;
10761
10762#ifdef FEAT_FOLDING
10763 /*
10764 * Save Folds when 'buftype' is empty and for help files.
10765 */
10766 if ((*flagp & SSOP_FOLDS)
10767 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010768# ifdef FEAT_QUICKFIX
10769 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10770# endif
10771 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 {
10773 if (put_folds(fd, wp) == FAIL)
10774 return FAIL;
10775 }
10776#endif
10777
10778 /*
10779 * Set the cursor after creating folds, since that moves the cursor.
10780 */
10781 if (do_cursor)
10782 {
10783
10784 /* Restore the cursor line in the file and relatively in the
10785 * window. Don't use "G", it changes the jumplist. */
10786 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10787 (long)wp->w_cursor.lnum,
10788 (long)(wp->w_cursor.lnum - wp->w_topline),
10789 (long)wp->w_height / 2, (long)wp->w_height) < 0
10790 || put_eol(fd) == FAIL
10791 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10792 || put_line(fd, "exe s:l") == FAIL
10793 || put_line(fd, "normal! zt") == FAIL
10794 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10795 || put_eol(fd) == FAIL)
10796 return FAIL;
10797 /* Restore the cursor column and left offset when not wrapping. */
10798 if (wp->w_cursor.col == 0)
10799 {
10800 if (put_line(fd, "normal! 0") == FAIL)
10801 return FAIL;
10802 }
10803 else
10804 {
10805 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10806 {
10807 if (fprintf(fd,
10808 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10809 (long)wp->w_cursor.col,
10810 (long)(wp->w_cursor.col - wp->w_leftcol),
10811 (long)wp->w_width / 2, (long)wp->w_width) < 0
10812 || put_eol(fd) == FAIL
10813 || put_line(fd, "if s:c > 0") == FAIL
10814 || fprintf(fd,
10815 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10816 (long)wp->w_cursor.col) < 0
10817 || put_eol(fd) == FAIL
10818 || put_line(fd, "else") == FAIL
10819 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10820 || put_eol(fd) == FAIL
10821 || put_line(fd, "endif") == FAIL)
10822 return FAIL;
10823 }
10824 else
10825 {
10826 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10827 || put_eol(fd) == FAIL)
10828 return FAIL;
10829 }
10830 }
10831 }
10832
10833 /*
10834 * Local directory.
10835 */
10836 if (wp->w_localdir != NULL)
10837 {
10838 if (fputs("lcd ", fd) < 0
10839 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10840 || put_eol(fd) == FAIL)
10841 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010842 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 }
10844
10845 return OK;
10846}
10847
10848/*
10849 * Write an argument list to the session file.
10850 * Returns FAIL if writing fails.
10851 */
10852 static int
10853ses_arglist(fd, cmd, gap, fullname, flagp)
10854 FILE *fd;
10855 char *cmd;
10856 garray_T *gap;
10857 int fullname; /* TRUE: use full path name */
10858 unsigned *flagp;
10859{
10860 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010861 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 char_u *s;
10863
10864 if (gap->ga_len == 0)
10865 return put_line(fd, "silent! argdel *");
10866 if (fputs(cmd, fd) < 0)
10867 return FAIL;
10868 for (i = 0; i < gap->ga_len; ++i)
10869 {
10870 /* NULL file names are skipped (only happens when out of memory). */
10871 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10872 if (s != NULL)
10873 {
10874 if (fullname)
10875 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010876 buf = alloc(MAXPATHL);
10877 if (buf != NULL)
10878 {
10879 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10880 s = buf;
10881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010884 {
10885 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010887 }
10888 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 }
10890 }
10891 return put_eol(fd);
10892}
10893
10894/*
10895 * Write a buffer name to the session file.
10896 * Also ends the line.
10897 * Returns FAIL if writing fails.
10898 */
10899 static int
10900ses_fname(fd, buf, flagp)
10901 FILE *fd;
10902 buf_T *buf;
10903 unsigned *flagp;
10904{
10905 char_u *name;
10906
10907 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010908 * the session file will be sourced.
10909 * Don't do this for ":mkview", we don't know the current directory.
10910 * Don't do this after ":lcd", we don't keep track of what the current
10911 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (buf->b_sfname != NULL
10913 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010914 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010915#ifdef FEAT_AUTOCHDIR
10916 && !p_acd
10917#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010918 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 name = buf->b_sfname;
10920 else
10921 name = buf->b_ffname;
10922 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10923 return FAIL;
10924 return OK;
10925}
10926
10927/*
10928 * Write a file name to the session file.
10929 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10930 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010931 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 */
10933 static int
10934ses_put_fname(fd, name, flagp)
10935 FILE *fd;
10936 char_u *name;
10937 unsigned *flagp;
10938{
10939 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010940 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942
10943 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010944 if (sname == NULL)
10945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010947 if (*flagp & SSOP_SLASH)
10948 {
10949 /* change all backslashes to forward slashes */
10950 for (p = sname; *p != NUL; mb_ptr_adv(p))
10951 if (*p == '\\')
10952 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010954
10955 /* escapse special characters */
10956 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010958 if (p == NULL)
10959 return FAIL;
10960
10961 /* write the result */
10962 if (fputs((char *)p, fd) < 0)
10963 retval = FAIL;
10964
10965 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 return retval;
10967}
10968
10969/*
10970 * ":loadview [nr]"
10971 */
10972 static void
10973ex_loadview(eap)
10974 exarg_T *eap;
10975{
10976 char_u *fname;
10977
10978 fname = get_view_file(*eap->arg);
10979 if (fname != NULL)
10980 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010981 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 vim_free(fname);
10983 }
10984}
10985
10986/*
10987 * Get the name of the view file for the current buffer.
10988 */
10989 static char_u *
10990get_view_file(c)
10991 int c;
10992{
10993 int len = 0;
10994 char_u *p, *s;
10995 char_u *retval;
10996 char_u *sname;
10997
10998 if (curbuf->b_ffname == NULL)
10999 {
11000 EMSG(_(e_noname));
11001 return NULL;
11002 }
11003 sname = home_replace_save(NULL, curbuf->b_ffname);
11004 if (sname == NULL)
11005 return NULL;
11006
11007 /*
11008 * We want a file name without separators, because we're not going to make
11009 * a directory.
11010 * "normal" path separator -> "=+"
11011 * "=" -> "=="
11012 * ":" path separator -> "=-"
11013 */
11014 for (p = sname; *p; ++p)
11015 if (*p == '=' || vim_ispathsep(*p))
11016 ++len;
11017 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11018 if (retval != NULL)
11019 {
11020 STRCPY(retval, p_vdir);
11021 add_pathsep(retval);
11022 s = retval + STRLEN(retval);
11023 for (p = sname; *p; ++p)
11024 {
11025 if (*p == '=')
11026 {
11027 *s++ = '=';
11028 *s++ = '=';
11029 }
11030 else if (vim_ispathsep(*p))
11031 {
11032 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011033#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 if (*p == ':')
11035 *s++ = '-';
11036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011038 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 }
11040 else
11041 *s++ = *p;
11042 }
11043 *s++ = '=';
11044 *s++ = c;
11045 STRCPY(s, ".vim");
11046 }
11047
11048 vim_free(sname);
11049 return retval;
11050}
11051
11052#endif /* FEAT_SESSION */
11053
11054/*
11055 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11056 * Return FAIL for a write error.
11057 */
11058 int
11059put_eol(fd)
11060 FILE *fd;
11061{
11062 if (
11063#ifdef USE_CRNL
11064 (
11065# ifdef MKSESSION_NL
11066 !mksession_nl &&
11067# endif
11068 (putc('\r', fd) < 0)) ||
11069#endif
11070 (putc('\n', fd) < 0))
11071 return FAIL;
11072 return OK;
11073}
11074
11075/*
11076 * Write a line to "fd".
11077 * Return FAIL for a write error.
11078 */
11079 int
11080put_line(fd, s)
11081 FILE *fd;
11082 char *s;
11083{
11084 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11085 return FAIL;
11086 return OK;
11087}
11088
11089#ifdef FEAT_VIMINFO
11090/*
11091 * ":rviminfo" and ":wviminfo".
11092 */
11093 static void
11094ex_viminfo(eap)
11095 exarg_T *eap;
11096{
11097 char_u *save_viminfo;
11098
11099 save_viminfo = p_viminfo;
11100 if (*p_viminfo == NUL)
11101 p_viminfo = (char_u *)"'100";
11102 if (eap->cmdidx == CMD_rviminfo)
11103 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011104 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11105 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 EMSG(_("E195: Cannot open viminfo file for reading"));
11107 }
11108 else
11109 write_viminfo(eap->arg, eap->forceit);
11110 p_viminfo = save_viminfo;
11111}
11112#endif
11113
11114#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011115/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011116 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011117 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 void
11120dialog_msg(buff, format, fname)
11121 char_u *buff;
11122 char *format;
11123 char_u *fname;
11124{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 if (fname == NULL)
11126 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011127 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128}
11129#endif
11130
11131/*
11132 * ":behave {mswin,xterm}"
11133 */
11134 static void
11135ex_behave(eap)
11136 exarg_T *eap;
11137{
11138 if (STRCMP(eap->arg, "mswin") == 0)
11139 {
11140 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11141 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11142 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11143 set_option_value((char_u *)"keymodel", 0L,
11144 (char_u *)"startsel,stopsel", 0);
11145 }
11146 else if (STRCMP(eap->arg, "xterm") == 0)
11147 {
11148 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11149 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11150 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11151 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11152 }
11153 else
11154 EMSG2(_(e_invarg2), eap->arg);
11155}
11156
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011157#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11158/*
11159 * Function given to ExpandGeneric() to obtain the possible arguments of the
11160 * ":behave {mswin,xterm}" command.
11161 */
11162 char_u *
11163get_behave_arg(xp, idx)
11164 expand_T *xp UNUSED;
11165 int idx;
11166{
11167 if (idx == 0)
11168 return (char_u *)"mswin";
11169 if (idx == 1)
11170 return (char_u *)"xterm";
11171 return NULL;
11172}
11173#endif
11174
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175#ifdef FEAT_AUTOCMD
11176static int filetype_detect = FALSE;
11177static int filetype_plugin = FALSE;
11178static int filetype_indent = FALSE;
11179
11180/*
11181 * ":filetype [plugin] [indent] {on,off,detect}"
11182 * on: Load the filetype.vim file to install autocommands for file types.
11183 * off: Load the ftoff.vim file to remove all autocommands for file types.
11184 * plugin on: load filetype.vim and ftplugin.vim
11185 * plugin off: load ftplugof.vim
11186 * indent on: load filetype.vim and indent.vim
11187 * indent off: load indoff.vim
11188 */
11189 static void
11190ex_filetype(eap)
11191 exarg_T *eap;
11192{
11193 char_u *arg = eap->arg;
11194 int plugin = FALSE;
11195 int indent = FALSE;
11196
11197 if (*eap->arg == NUL)
11198 {
11199 /* Print current status. */
11200 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11201 filetype_detect ? "ON" : "OFF",
11202 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11203 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11204 return;
11205 }
11206
11207 /* Accept "plugin" and "indent" in any order. */
11208 for (;;)
11209 {
11210 if (STRNCMP(arg, "plugin", 6) == 0)
11211 {
11212 plugin = TRUE;
11213 arg = skipwhite(arg + 6);
11214 continue;
11215 }
11216 if (STRNCMP(arg, "indent", 6) == 0)
11217 {
11218 indent = TRUE;
11219 arg = skipwhite(arg + 6);
11220 continue;
11221 }
11222 break;
11223 }
11224 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11225 {
11226 if (*arg == 'o' || !filetype_detect)
11227 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011228 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 filetype_detect = TRUE;
11230 if (plugin)
11231 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011232 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 filetype_plugin = TRUE;
11234 }
11235 if (indent)
11236 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011237 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 filetype_indent = TRUE;
11239 }
11240 }
11241 if (*arg == 'd')
11242 {
11243 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011244 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 }
11246 }
11247 else if (STRCMP(arg, "off") == 0)
11248 {
11249 if (plugin || indent)
11250 {
11251 if (plugin)
11252 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011253 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 filetype_plugin = FALSE;
11255 }
11256 if (indent)
11257 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011258 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 filetype_indent = FALSE;
11260 }
11261 }
11262 else
11263 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011264 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 filetype_detect = FALSE;
11266 }
11267 }
11268 else
11269 EMSG2(_(e_invarg2), arg);
11270}
11271
11272/*
11273 * ":setfiletype {name}"
11274 */
11275 static void
11276ex_setfiletype(eap)
11277 exarg_T *eap;
11278{
11279 if (!did_filetype)
11280 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11281}
11282#endif
11283
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 static void
11285ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011286 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
11288#ifdef FEAT_DIGRAPHS
11289 if (*eap->arg != NUL)
11290 putdigraph(eap->arg);
11291 else
11292 listdigraphs();
11293#else
11294 EMSG(_("E196: No digraphs in this version"));
11295#endif
11296}
11297
11298 static void
11299ex_set(eap)
11300 exarg_T *eap;
11301{
11302 int flags = 0;
11303
11304 if (eap->cmdidx == CMD_setlocal)
11305 flags = OPT_LOCAL;
11306 else if (eap->cmdidx == CMD_setglobal)
11307 flags = OPT_GLOBAL;
11308#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11309 if (cmdmod.browse && flags == 0)
11310 ex_options(eap);
11311 else
11312#endif
11313 (void)do_set(eap->arg, flags);
11314}
11315
11316#ifdef FEAT_SEARCH_EXTRA
11317/*
11318 * ":nohlsearch"
11319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 static void
11321ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011322 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323{
11324 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011325 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326}
11327
11328/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011329 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 * Sets nextcmd to the start of the next command, if any. Also called when
11331 * skipping commands to find the next command.
11332 */
11333 static void
11334ex_match(eap)
11335 exarg_T *eap;
11336{
11337 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011338 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 char_u *end;
11340 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011341 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011342
11343 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011344 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011345 else
11346 {
11347 EMSG(e_invcmd);
11348 return;
11349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350
11351 /* First clear any old pattern. */
11352 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011353 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354
11355 if (ends_excmd(*eap->arg))
11356 end = eap->arg;
11357 else if ((STRNICMP(eap->arg, "none", 4) == 0
11358 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11359 end = eap->arg + 4;
11360 else
11361 {
11362 p = skiptowhite(eap->arg);
11363 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011364 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 p = skipwhite(p);
11366 if (*p == NUL)
11367 {
11368 /* There must be two arguments. */
11369 EMSG2(_(e_invarg2), eap->arg);
11370 return;
11371 }
11372 end = skip_regexp(p + 1, *p, TRUE, NULL);
11373 if (!eap->skip)
11374 {
11375 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11376 {
11377 eap->errmsg = e_trailing;
11378 return;
11379 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011380 if (*end != *p)
11381 {
11382 EMSG2(_(e_invarg2), p);
11383 return;
11384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385
11386 c = *end;
11387 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011388 match_add(curwin, g, p + 1, 10, id);
11389 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011390 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 }
11392 }
11393 eap->nextcmd = find_nextcmd(end);
11394}
11395#endif
11396
11397#ifdef FEAT_CRYPT
11398/*
11399 * ":X": Get crypt key
11400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 static void
11402ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011403 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011405 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011406 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407}
11408#endif
11409
11410#ifdef FEAT_FOLDING
11411 static void
11412ex_fold(eap)
11413 exarg_T *eap;
11414{
11415 if (foldManualAllowed(TRUE))
11416 foldCreate(eap->line1, eap->line2);
11417}
11418
11419 static void
11420ex_foldopen(eap)
11421 exarg_T *eap;
11422{
11423 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11424 eap->forceit, FALSE);
11425}
11426
11427 static void
11428ex_folddo(eap)
11429 exarg_T *eap;
11430{
11431 linenr_T lnum;
11432
11433 /* First set the marks for all lines closed/open. */
11434 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11435 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11436 ml_setmarked(lnum);
11437
11438 /* Execute the command on the marked lines. */
11439 global_exe(eap->arg);
11440 ml_clearmarked(); /* clear rest of the marks */
11441}
11442#endif