blob: 4a372b5953413e21797ddfb6be3415f8261ad364 [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 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001096 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ++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 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001298 && !(did_emsg
1299#ifdef FEAT_EVAL
1300 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001301 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001302 * the :endtry to be missed. */
1303 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1304#endif
1305 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 && (getline_equal(fgetline, cookie, getexmodeline)
1307 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (next_cmdline != NULL
1309#ifdef FEAT_EVAL
1310 || cstack.cs_idx >= 0
1311#endif
1312 || (flags & DOCMD_REPEAT)));
1313
1314 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001315 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef FEAT_EVAL
1317 free_cmdlines(&lines_ga);
1318 ga_clear(&lines_ga);
1319
1320 if (cstack.cs_idx >= 0)
1321 {
1322 /*
1323 * If a sourced file or executed function ran to its end, report the
1324 * unclosed conditional.
1325 */
1326 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 && ((getline_equal(fgetline, cookie, getsourceline)
1328 && !source_finished(fgetline, cookie))
1329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && !func_has_ended(real_cookie))))
1331 {
1332 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1333 EMSG(_(e_endtry));
1334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1335 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001336 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1337 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 EMSG(_(e_endif));
1340 }
1341
1342 /*
1343 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1344 * ":endtry" in a sourced file or executed function. If the try
1345 * conditional is in its finally clause, ignore anything pending.
1346 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001350 {
1351 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1352
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001353 if (idx >= 0)
1354 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1356 &cstack.cs_looplevel);
1357 }
1358 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 trylevel = initial_trylevel;
1360 }
1361
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001362 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1363 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 ? (char_u *)"endfunction" : (char_u *)NULL);
1367
1368 if (trylevel == 0)
1369 {
1370 /*
1371 * When an exception is being thrown out of the outermost try
1372 * conditional, discard the uncaught exception, disable the conversion
1373 * of interrupts or errors to exceptions, and ensure that no more
1374 * commands are executed.
1375 */
1376 if (did_throw)
1377 {
1378 void *p = NULL;
1379 char_u *saved_sourcing_name;
1380 int saved_sourcing_lnum;
1381 struct msglist *messages = NULL, *next;
1382
1383 /*
1384 * If the uncaught exception is a user exception, report it as an
1385 * error. If it is an error exception, display the saved error
1386 * message now. For an interrupt exception, do nothing; the
1387 * interrupt message is given elsewhere.
1388 */
1389 switch (current_exception->type)
1390 {
1391 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001392 vim_snprintf((char *)IObuff, IOSIZE,
1393 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 current_exception->value);
1395 p = vim_strsave(IObuff);
1396 break;
1397 case ET_ERROR:
1398 messages = current_exception->messages;
1399 current_exception->messages = NULL;
1400 break;
1401 case ET_INTERRUPT:
1402 break;
1403 default:
1404 p = vim_strsave((char_u *)_(e_internal));
1405 }
1406
1407 saved_sourcing_name = sourcing_name;
1408 saved_sourcing_lnum = sourcing_lnum;
1409 sourcing_name = current_exception->throw_name;
1410 sourcing_lnum = current_exception->throw_lnum;
1411 current_exception->throw_name = NULL;
1412
1413 discard_current_exception(); /* uses IObuff if 'verbose' */
1414 suppress_errthrow = TRUE;
1415 force_abort = TRUE;
1416
1417 if (messages != NULL)
1418 {
1419 do
1420 {
1421 next = messages->next;
1422 emsg(messages->msg);
1423 vim_free(messages->msg);
1424 vim_free(messages);
1425 messages = next;
1426 }
1427 while (messages != NULL);
1428 }
1429 else if (p != NULL)
1430 {
1431 emsg(p);
1432 vim_free(p);
1433 }
1434 vim_free(sourcing_name);
1435 sourcing_name = saved_sourcing_name;
1436 sourcing_lnum = saved_sourcing_lnum;
1437 }
1438
1439 /*
1440 * On an interrupt or an aborting error not converted to an exception,
1441 * disable the conversion of errors to exceptions. (Interrupts are not
1442 * converted any more, here.) This enables also the interrupt message
1443 * when force_abort is set and did_emsg unset in case of an interrupt
1444 * from a finally clause after an error.
1445 */
1446 else if (got_int || (did_emsg && force_abort))
1447 suppress_errthrow = TRUE;
1448 }
1449
1450 /*
1451 * The current cstack will be freed when do_cmdline() returns. An uncaught
1452 * exception will have to be rethrown in the previous cstack. If a function
1453 * has just returned or a script file was just finished and the previous
1454 * cstack belongs to the same function or, respectively, script file, it
1455 * will have to be checked for finally clauses to be executed due to the
1456 * ":return" or ":finish". This is done in do_one_cmd().
1457 */
1458 if (did_throw)
1459 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001460 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 && ex_nesting_level > func_level(real_cookie) + 1))
1464 {
1465 if (!did_throw)
1466 check_cstack = TRUE;
1467 }
1468 else
1469 {
1470 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 --ex_nesting_level;
1473 /*
1474 * Go to debug mode when returning from a function in which we are
1475 * single-stepping.
1476 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001477 if ((getline_equal(fgetline, cookie, getsourceline)
1478 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001480 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 ? (char_u *)_("End of sourced file")
1482 : (char_u *)_("End of function"));
1483 }
1484
1485 /*
1486 * Restore the exception environment (done after returning from the
1487 * debugger).
1488 */
1489 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001490 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 msg_list = saved_msg_list;
1493#endif /* FEAT_EVAL */
1494
1495 /*
1496 * If there was too much output to fit on the command line, ask the user to
1497 * hit return before redrawing the screen. With the ":global" command we do
1498 * this only once after the command is finished.
1499 */
1500 if (did_inc)
1501 {
1502 --RedrawingDisabled;
1503 --no_wait_return;
1504 msg_scroll = FALSE;
1505
1506 /*
1507 * When just finished an ":if"-":else" which was typed, no need to
1508 * wait for hit-return. Also for an error situation.
1509 */
1510 if (retval == FAIL
1511#ifdef FEAT_EVAL
1512 || (did_endif && KeyTyped && !did_emsg)
1513#endif
1514 )
1515 {
1516 need_wait_return = FALSE;
1517 msg_didany = FALSE; /* don't wait when restarting edit */
1518 }
1519 else if (need_wait_return)
1520 {
1521 /*
1522 * The msg_start() above clears msg_didout. The wait_return we do
1523 * here should not overwrite the command that may be shown before
1524 * doing that.
1525 */
1526 msg_didout |= msg_didout_before_start;
1527 wait_return(FALSE);
1528 }
1529 }
1530
Bram Moolenaar2a942252012-11-28 23:03:07 +01001531#ifdef FEAT_EVAL
1532 did_endif = FALSE; /* in case do_cmdline used recursively */
1533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 /*
1535 * Reset if_level, in case a sourced script file contains more ":if" than
1536 * ":endif" (could be ":if x | foo | endif").
1537 */
1538 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001539#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 --call_depth;
1542 return retval;
1543}
1544
1545#ifdef FEAT_EVAL
1546/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 int c;
1552 void *cookie;
1553 int indent;
1554{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 wcmd_T *wp;
1557 char_u *line;
1558
1559 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1560 {
1561 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (cp->getline == NULL)
1566 line = getcmdline(c, 0L, indent);
1567 else
1568 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001569 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 ++cp->current_line;
1571
1572 return line;
1573 }
1574
1575 KeyTyped = FALSE;
1576 ++cp->current_line;
1577 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1578 sourcing_lnum = wp->lnum;
1579 return vim_strsave(wp->line);
1580}
1581
1582/*
1583 * Store a line in "gap" so that a ":while" loop can execute it again.
1584 */
1585 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001586store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 garray_T *gap;
1588 char_u *line;
1589{
1590 if (ga_grow(gap, 1) == FAIL)
1591 return FAIL;
1592 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1593 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1594 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 return OK;
1596}
1597
1598/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001599 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 static void
1602free_cmdlines(gap)
1603 garray_T *gap;
1604{
1605 while (gap->ga_len > 0)
1606 {
1607 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1608 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610}
1611#endif
1612
1613/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001614 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1615 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618getline_equal(fgetline, cookie, func)
1619 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001620 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 char_u *(*func) __ARGS((int, void *, int));
1622{
1623#ifdef FEAT_EVAL
1624 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001625 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001628 * function that's originally used to obtain the lines. This may be
1629 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 gp = cp->getline;
1635 cp = cp->cookie;
1636 }
1637 return gp == func;
1638#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641}
1642
1643#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1644/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001645 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 * getline function. Otherwise return "cookie".
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001650 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653# ifdef FEAT_EVAL
1654 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001655 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar89d40322006-08-29 15:30:07 +00001657 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001658 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001661 cp = (struct loop_cookie *)cookie;
1662 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 gp = cp->getline;
1665 cp = cp->cookie;
1666 }
1667 return cp;
1668# else
1669 return cookie;
1670# endif
1671}
1672#endif
1673
1674/*
1675 * Execute one Ex command.
1676 *
1677 * If 'sourcing' is TRUE, the command will be included in the error message.
1678 *
1679 * 1. skip comment lines and leading space
1680 * 2. handle command modifiers
1681 * 3. parse range
1682 * 4. parse command
1683 * 5. parse arguments
1684 * 6. switch on command name
1685 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001686 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
1688 * This function may be called recursively!
1689 */
1690#if (_MSC_VER == 1200)
1691/*
Bram Moolenaared203462004-06-16 11:19:22 +00001692 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001694 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
1696 static char_u *
1697do_one_cmd(cmdlinep, sourcing,
1698#ifdef FEAT_EVAL
1699 cstack,
1700#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001701 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 char_u **cmdlinep;
1703 int sourcing;
1704#ifdef FEAT_EVAL
1705 struct condstack *cstack;
1706#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 char_u *(*fgetline) __ARGS((int, void *, int));
1708 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 char_u *p;
1711 linenr_T lnum;
1712 long n;
1713 char_u *errormsg = NULL; /* error message */
1714 exarg_T ea; /* Ex command arguments */
1715 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001716 int save_msg_scroll = msg_scroll;
1717 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001719#ifdef HAVE_SANDBOX
1720 int did_sandbox = FALSE;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 cmdmod_T save_cmdmod;
1723 int ni; /* set when Not Implemented */
1724
1725 vim_memset(&ea, 0, sizeof(ea));
1726 ea.line1 = 1;
1727 ea.line2 = 1;
1728#ifdef FEAT_EVAL
1729 ++ex_nesting_level;
1730#endif
1731
Bram Moolenaar21691f82012-12-05 19:13:18 +01001732 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (quitmore
1734#ifdef FEAT_EVAL
1735 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001737#endif
1738#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001739 /* avoid that an autocommand, e.g. QuitPre, does this */
1740 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
1742 )
1743 --quitmore;
1744
1745 /*
1746 * Reset browse, confirm, etc.. They are restored when returning, for
1747 * recursive calls.
1748 */
1749 save_cmdmod = cmdmod;
1750 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1751
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001752 /* "#!anything" is handled like a comment. */
1753 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1754 goto doend;
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /*
1757 * Repeat until no more command modifiers are found.
1758 */
1759 ea.cmd = *cmdlinep;
1760 for (;;)
1761 {
1762/*
1763 * 1. skip comment lines and leading white space and colons
1764 */
1765 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1766 ++ea.cmd;
1767
1768 /* in ex mode, an empty line works like :+ */
1769 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 && (getline_equal(fgetline, cookie, getexmodeline)
1771 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 ea.cmd = (char_u *)"+";
1775 ex_pressedreturn = TRUE;
1776 }
1777
1778 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001779 if (*ea.cmd == '"')
1780 goto doend;
1781 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001782 {
1783 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787/*
1788 * 2. handle command modifiers.
1789 */
1790 p = ea.cmd;
1791 if (VIM_ISDIGIT(*ea.cmd))
1792 p = skipwhite(skipdigits(ea.cmd));
1793 switch (*p)
1794 {
1795 /* When adding an entry, also modify cmd_exists(). */
1796 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1797 break;
1798#ifdef FEAT_WINDOWS
1799 cmdmod.split |= WSP_ABOVE;
1800#endif
1801 continue;
1802
1803 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1804 {
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BELOW;
1807#endif
1808 continue;
1809 }
1810 if (checkforcmd(&ea.cmd, "browse", 3))
1811 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001812#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 cmdmod.browse = TRUE;
1814#endif
1815 continue;
1816 }
1817 if (!checkforcmd(&ea.cmd, "botright", 2))
1818 break;
1819#ifdef FEAT_WINDOWS
1820 cmdmod.split |= WSP_BOT;
1821#endif
1822 continue;
1823
1824 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1825 break;
1826#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1827 cmdmod.confirm = TRUE;
1828#endif
1829 continue;
1830
1831 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1832 {
1833 cmdmod.keepmarks = TRUE;
1834 continue;
1835 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001836 if (checkforcmd(&ea.cmd, "keepalt", 5))
1837 {
1838 cmdmod.keepalt = TRUE;
1839 continue;
1840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1842 break;
1843 cmdmod.keepjumps = TRUE;
1844 continue;
1845
1846 /* ":hide" and ":hide | cmd" are not modifiers */
1847 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1848 || *p == NUL || ends_excmd(*p))
1849 break;
1850 ea.cmd = p;
1851 cmdmod.hide = TRUE;
1852 continue;
1853
1854 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1855 {
1856 cmdmod.lockmarks = TRUE;
1857 continue;
1858 }
1859
1860 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1861 break;
1862#ifdef FEAT_WINDOWS
1863 cmdmod.split |= WSP_ABOVE;
1864#endif
1865 continue;
1866
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001867 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1868 break;
1869#ifdef FEAT_AUTOCMD
1870 if (cmdmod.save_ei == NULL)
1871 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001872 /* Set 'eventignore' to "all". Restore the
1873 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001874 cmdmod.save_ei = vim_strsave(p_ei);
1875 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001876 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 }
1878#endif
1879 continue;
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1882 break;
1883#ifdef FEAT_WINDOWS
1884 cmdmod.split |= WSP_BELOW;
1885#endif
1886 continue;
1887
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001888 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1889 {
1890#ifdef HAVE_SANDBOX
1891 if (!did_sandbox)
1892 ++sandbox;
1893 did_sandbox = TRUE;
1894#endif
1895 continue;
1896 }
1897 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001899 if (save_msg_silent == -1)
1900 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1903 {
1904 /* ":silent!", but not "silent !cmd" */
1905 ea.cmd = skipwhite(ea.cmd + 1);
1906 ++emsg_silent;
1907 ++did_esilent;
1908 }
1909 continue;
1910
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001911 case 't': if (checkforcmd(&p, "tab", 3))
1912 {
1913#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001914 if (vim_isdigit(*ea.cmd))
1915 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1916 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001917 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001918 ea.cmd = p;
1919#endif
1920 continue;
1921 }
1922 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 break;
1924#ifdef FEAT_WINDOWS
1925 cmdmod.split |= WSP_TOP;
1926#endif
1927 continue;
1928
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001929 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1930 break;
1931 if (save_msg_silent == -1)
1932 save_msg_silent = msg_silent;
1933 msg_silent = 0;
1934 continue;
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1937 {
1938#ifdef FEAT_VERTSPLIT
1939 cmdmod.split |= WSP_VERT;
1940#endif
1941 continue;
1942 }
1943 if (!checkforcmd(&p, "verbose", 4))
1944 break;
1945 if (verbose_save < 0)
1946 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001947 if (vim_isdigit(*ea.cmd))
1948 p_verbose = atoi((char *)ea.cmd);
1949 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 p_verbose = 1;
1951 ea.cmd = p;
1952 continue;
1953 }
1954 break;
1955 }
1956
1957#ifdef FEAT_EVAL
1958 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1959 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1960#else
1961 ea.skip = (if_level > 0);
1962#endif
1963
1964#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001965# ifdef FEAT_PROFILE
1966 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001967 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001969 if (getline_equal(fgetline, cookie, get_func_line))
1970 func_line_exec(getline_cookie(fgetline, cookie));
1971 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001972 script_line_exec();
1973 }
1974#endif
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 /* May go to debug mode. If this happens and the ">quit" debug command is
1977 * used, throw an interrupt exception and skip the next command. */
1978 dbg_check_breakpoint(&ea);
1979 if (!ea.skip && got_int)
1980 {
1981 ea.skip = TRUE;
1982 (void)do_intthrow(cstack);
1983 }
1984#endif
1985
1986/*
1987 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1988 *
1989 * where 'addr' is:
1990 *
1991 * % (entire file)
1992 * $ [+-NUM]
1993 * 'x [+-NUM] (where x denotes a currently defined mark)
1994 * . [+-NUM]
1995 * [+-NUM]..
1996 * NUM
1997 *
1998 * The ea.cmd pointer is updated to point to the first character following the
1999 * range spec. If an initial address is found, but no second, the upper bound
2000 * is equal to the lower.
2001 */
2002
2003 /* repeat for all ',' or ';' separated addresses */
2004 for (;;)
2005 {
2006 ea.line1 = ea.line2;
2007 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2008 ea.cmd = skipwhite(ea.cmd);
2009 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2010 if (ea.cmd == NULL) /* error detected */
2011 goto doend;
2012 if (lnum == MAXLNUM)
2013 {
2014 if (*ea.cmd == '%') /* '%' - all lines */
2015 {
2016 ++ea.cmd;
2017 ea.line1 = 1;
2018 ea.line2 = curbuf->b_ml.ml_line_count;
2019 ++ea.addr_count;
2020 }
2021 /* '*' - visual area */
2022 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2023 {
2024 pos_T *fp;
2025
2026 ++ea.cmd;
2027 if (!ea.skip)
2028 {
2029 fp = getmark('<', FALSE);
2030 if (check_mark(fp) == FAIL)
2031 goto doend;
2032 ea.line1 = fp->lnum;
2033 fp = getmark('>', FALSE);
2034 if (check_mark(fp) == FAIL)
2035 goto doend;
2036 ea.line2 = fp->lnum;
2037 ++ea.addr_count;
2038 }
2039 }
2040 }
2041 else
2042 ea.line2 = lnum;
2043 ea.addr_count++;
2044
2045 if (*ea.cmd == ';')
2046 {
2047 if (!ea.skip)
2048 curwin->w_cursor.lnum = ea.line2;
2049 }
2050 else if (*ea.cmd != ',')
2051 break;
2052 ++ea.cmd;
2053 }
2054
2055 /* One address given: set start and end lines */
2056 if (ea.addr_count == 1)
2057 {
2058 ea.line1 = ea.line2;
2059 /* ... but only implicit: really no address given */
2060 if (lnum == MAXLNUM)
2061 ea.addr_count = 0;
2062 }
2063
2064 /* Don't leave the cursor on an illegal line (caused by ';') */
2065 check_cursor_lnum();
2066
2067/*
2068 * 4. parse command
2069 */
2070
2071 /*
2072 * Skip ':' and any white space
2073 */
2074 ea.cmd = skipwhite(ea.cmd);
2075 while (*ea.cmd == ':')
2076 ea.cmd = skipwhite(ea.cmd + 1);
2077
2078 /*
2079 * If we got a line, but no command, then go to the line.
2080 * If we find a '|' or '\n' we set ea.nextcmd.
2081 */
2082 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2083 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2084 {
2085 /*
2086 * strange vi behaviour:
2087 * ":3" jumps to line 3
2088 * ":3|..." prints line 3
2089 * ":|" prints current line
2090 */
2091 if (ea.skip) /* skip this if inside :if */
2092 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002093 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 ea.cmdidx = CMD_print;
2096 ea.argt = RANGE+COUNT+TRLBAR;
2097 if ((errormsg = invalid_range(&ea)) == NULL)
2098 {
2099 correct_range(&ea);
2100 ex_print(&ea);
2101 }
2102 }
2103 else if (ea.addr_count != 0)
2104 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002105 if (ea.line2 > curbuf->b_ml.ml_line_count)
2106 {
2107 /* With '-' in 'cpoptions' a line number past the file is an
2108 * error, otherwise put it at the end of the file. */
2109 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2110 ea.line2 = -1;
2111 else
2112 ea.line2 = curbuf->b_ml.ml_line_count;
2113 }
2114
2115 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002116 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else
2118 {
2119 if (ea.line2 == 0)
2120 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
2122 curwin->w_cursor.lnum = ea.line2;
2123 beginline(BL_SOL | BL_FIX);
2124 }
2125 }
2126 goto doend;
2127 }
2128
2129 /* Find the command and let "p" point to after it. */
2130 p = find_command(&ea, NULL);
2131
2132#ifdef FEAT_USR_CMDS
2133 if (p == NULL)
2134 {
2135 if (!ea.skip)
2136 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2137 goto doend;
2138 }
2139 /* Check for wrong commands. */
2140 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2141 {
2142 errormsg = uc_fun_cmd();
2143 goto doend;
2144 }
2145#endif
2146 if (ea.cmdidx == CMD_SIZE)
2147 {
2148 if (!ea.skip)
2149 {
2150 STRCPY(IObuff, _("E492: Not an editor command"));
2151 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002152 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002154 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 goto doend;
2157 }
2158
2159 ni = (
2160#ifdef FEAT_USR_CMDS
2161 !USER_CMDIDX(ea.cmdidx) &&
2162#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002163 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002164#ifdef HAVE_EX_SCRIPT_NI
2165 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2166#endif
2167 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169#ifndef FEAT_EVAL
2170 /*
2171 * When the expression evaluation is disabled, recognize the ":if" and
2172 * ":endif" commands and ignore everything in between it.
2173 */
2174 if (ea.cmdidx == CMD_if)
2175 ++if_level;
2176 if (if_level)
2177 {
2178 if (ea.cmdidx == CMD_endif)
2179 --if_level;
2180 goto doend;
2181 }
2182
2183#endif
2184
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002185 /* forced commands */
2186 if (*p == '!' && ea.cmdidx != CMD_substitute
2187 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 ++p;
2190 ea.forceit = TRUE;
2191 }
2192 else
2193 ea.forceit = FALSE;
2194
2195/*
2196 * 5. parse arguments
2197 */
2198#ifdef FEAT_USR_CMDS
2199 if (!USER_CMDIDX(ea.cmdidx))
2200#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002201 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 if (!ea.skip)
2204 {
2205#ifdef HAVE_SANDBOX
2206 if (sandbox != 0 && !(ea.argt & SBOXOK))
2207 {
2208 /* Command not allowed in sandbox. */
2209 errormsg = (char_u *)_(e_sandbox);
2210 goto doend;
2211 }
2212#endif
2213 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2214 {
2215 /* Command not allowed in non-'modifiable' buffer */
2216 errormsg = (char_u *)_(e_modifiable);
2217 goto doend;
2218 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002219
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002220 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221# ifdef FEAT_USR_CMDS
2222 && !USER_CMDIDX(ea.cmdidx)
2223# endif
2224 )
2225 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002226 /* Command not allowed when editing the command line. */
2227#ifdef FEAT_CMDWIN
2228 if (cmdwin_type != 0)
2229 errormsg = (char_u *)_(e_cmdwin);
2230 else
2231#endif
2232 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 goto doend;
2234 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002236 /* Disallow editing another buffer when "curbuf_lock" is set.
2237 * Do allow ":edit" (check for argument later).
2238 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002240 && ea.cmdidx != CMD_edit
2241 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242# ifdef FEAT_USR_CMDS
2243 && !USER_CMDIDX(ea.cmdidx)
2244# endif
2245 && curbuf_locked())
2246 goto doend;
2247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2250 {
2251 /* no range allowed */
2252 errormsg = (char_u *)_(e_norange);
2253 goto doend;
2254 }
2255 }
2256
2257 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2258 {
2259 errormsg = (char_u *)_(e_nobang);
2260 goto doend;
2261 }
2262
2263 /*
2264 * Don't complain about the range if it is not used
2265 * (could happen if line_count is accidentally set to 0).
2266 */
2267 if (!ea.skip && !ni)
2268 {
2269 /*
2270 * If the range is backwards, ask for confirmation and, if given, swap
2271 * ea.line1 & ea.line2 so it's forwards again.
2272 * When global command is busy, don't ask, will fail below.
2273 */
2274 if (!global_busy && ea.line1 > ea.line2)
2275 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002276 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002278 if (sourcing || exmode_active)
2279 {
2280 errormsg = (char_u *)_("E493: Backwards range given");
2281 goto doend;
2282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (ask_yesno((char_u *)
2284 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002285 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 lnum = ea.line1;
2288 ea.line1 = ea.line2;
2289 ea.line2 = lnum;
2290 }
2291 if ((errormsg = invalid_range(&ea)) != NULL)
2292 goto doend;
2293 }
2294
2295 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2296 ea.line2 = 1;
2297
2298 correct_range(&ea);
2299
2300#ifdef FEAT_FOLDING
2301 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2302 {
2303 /* Put the first line at the start of a closed fold, put the last line
2304 * at the end of a closed fold. */
2305 (void)hasFolding(ea.line1, &ea.line1, NULL);
2306 (void)hasFolding(ea.line2, NULL, &ea.line2);
2307 }
2308#endif
2309
2310#ifdef FEAT_QUICKFIX
2311 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002312 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * option here, so things like % get expanded.
2314 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002315 p = replace_makeprg(&ea, p, cmdlinep);
2316 if (p == NULL)
2317 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319
2320 /*
2321 * Skip to start of argument.
2322 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2323 */
2324 if (ea.cmdidx == CMD_bang)
2325 ea.arg = p;
2326 else
2327 ea.arg = skipwhite(p);
2328
2329 /*
2330 * Check for "++opt=val" argument.
2331 * Must be first, allow ":w ++enc=utf8 !cmd"
2332 */
2333 if (ea.argt & ARGOPT)
2334 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2335 if (getargopt(&ea) == FAIL && !ni)
2336 {
2337 errormsg = (char_u *)_(e_invarg);
2338 goto doend;
2339 }
2340
2341 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2342 {
2343 if (*ea.arg == '>') /* append */
2344 {
2345 if (*++ea.arg != '>') /* typed wrong */
2346 {
2347 errormsg = (char_u *)_("E494: Use w or w>>");
2348 goto doend;
2349 }
2350 ea.arg = skipwhite(ea.arg + 1);
2351 ea.append = TRUE;
2352 }
2353 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2354 {
2355 ++ea.arg;
2356 ea.usefilter = TRUE;
2357 }
2358 }
2359
2360 if (ea.cmdidx == CMD_read)
2361 {
2362 if (ea.forceit)
2363 {
2364 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2365 ea.forceit = FALSE;
2366 }
2367 else if (*ea.arg == '!') /* :r !filter */
2368 {
2369 ++ea.arg;
2370 ea.usefilter = TRUE;
2371 }
2372 }
2373
2374 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2375 {
2376 ea.amount = 1;
2377 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2378 {
2379 ++ea.arg;
2380 ++ea.amount;
2381 }
2382 ea.arg = skipwhite(ea.arg);
2383 }
2384
2385 /*
2386 * Check for "+command" argument, before checking for next command.
2387 * Don't do this for ":read !cmd" and ":write !cmd".
2388 */
2389 if ((ea.argt & EDITCMD) && !ea.usefilter)
2390 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2391
2392 /*
2393 * Check for '|' to separate commands and '"' to start comments.
2394 * Don't do this for ":read !cmd" and ":write !cmd".
2395 */
2396 if ((ea.argt & TRLBAR) && !ea.usefilter)
2397 separate_nextcmd(&ea);
2398
2399 /*
2400 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002401 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2402 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002404 else if (ea.cmdidx == CMD_bang
2405 || ea.cmdidx == CMD_global
2406 || ea.cmdidx == CMD_vglobal
2407 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 for (p = ea.arg; *p; ++p)
2410 {
2411 /* Remove one backslash before a newline, so that it's possible to
2412 * pass a newline to the shell and also a newline that is preceded
2413 * with a backslash. This makes it impossible to end a shell
2414 * command in a backslash, but that doesn't appear useful.
2415 * Halving the number of backslashes is incompatible with previous
2416 * versions. */
2417 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002418 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else if (*p == '\n')
2420 {
2421 ea.nextcmd = p + 1;
2422 *p = NUL;
2423 break;
2424 }
2425 }
2426 }
2427
2428 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2429 {
2430 ea.line1 = 1;
2431 ea.line2 = curbuf->b_ml.ml_line_count;
2432 }
2433
2434 /* accept numbered register only when no count allowed (:put) */
2435 if ( (ea.argt & REGSTR)
2436 && *ea.arg != NUL
2437#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /* Do not allow register = for user commands */
2439 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#endif
2441 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2442 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002443#ifndef FEAT_CLIPBOARD
2444 /* check these explicitly for a more specific error message */
2445 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002447 errormsg = (char_u *)_(e_invalidreg);
2448 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002451 if (
2452#ifdef FEAT_USR_CMDS
2453 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2454 && USER_CMDIDX(ea.cmdidx)))
2455#else
2456 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2457#endif
2458 )
2459 {
2460 ea.regname = *ea.arg++;
2461#ifdef FEAT_EVAL
2462 /* for '=' register: accept the rest of the line as an expression */
2463 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2464 {
2465 set_expr_line(vim_strsave(ea.arg));
2466 ea.arg += STRLEN(ea.arg);
2467 }
2468#endif
2469 ea.arg = skipwhite(ea.arg);
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 /*
2474 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2475 * count, it's a buffer name.
2476 */
2477 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2478 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2479 || vim_iswhite(*p)))
2480 {
2481 n = getdigits(&ea.arg);
2482 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002483 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 errormsg = (char_u *)_(e_zerocount);
2486 goto doend;
2487 }
2488 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2489 {
2490 ea.line2 = n;
2491 if (ea.addr_count == 0)
2492 ea.addr_count = 1;
2493 }
2494 else
2495 {
2496 ea.line1 = ea.line2;
2497 ea.line2 += n - 1;
2498 ++ea.addr_count;
2499 /*
2500 * Be vi compatible: no error message for out of range.
2501 */
2502 if (ea.line2 > curbuf->b_ml.ml_line_count)
2503 ea.line2 = curbuf->b_ml.ml_line_count;
2504 }
2505 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002506
2507 /*
2508 * Check for flags: 'l', 'p' and '#'.
2509 */
2510 if (ea.argt & EXFLAGS)
2511 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002513 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002514 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 errormsg = (char_u *)_(e_trailing);
2517 goto doend;
2518 }
2519
2520 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2521 {
2522 errormsg = (char_u *)_(e_argreq);
2523 goto doend;
2524 }
2525
2526#ifdef FEAT_EVAL
2527 /*
2528 * Skip the command when it's not going to be executed.
2529 * The commands like :if, :endif, etc. always need to be executed.
2530 * Also make an exception for commands that handle a trailing command
2531 * themselves.
2532 */
2533 if (ea.skip)
2534 {
2535 switch (ea.cmdidx)
2536 {
2537 /* commands that need evaluation */
2538 case CMD_while:
2539 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002540 case CMD_for:
2541 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 case CMD_if:
2543 case CMD_elseif:
2544 case CMD_else:
2545 case CMD_endif:
2546 case CMD_try:
2547 case CMD_catch:
2548 case CMD_finally:
2549 case CMD_endtry:
2550 case CMD_function:
2551 break;
2552
2553 /* Commands that handle '|' themselves. Check: A command should
2554 * either have the TRLBAR flag, appear in this list or appear in
2555 * the list at ":help :bar". */
2556 case CMD_aboveleft:
2557 case CMD_and:
2558 case CMD_belowright:
2559 case CMD_botright:
2560 case CMD_browse:
2561 case CMD_call:
2562 case CMD_confirm:
2563 case CMD_delfunction:
2564 case CMD_djump:
2565 case CMD_dlist:
2566 case CMD_dsearch:
2567 case CMD_dsplit:
2568 case CMD_echo:
2569 case CMD_echoerr:
2570 case CMD_echomsg:
2571 case CMD_echon:
2572 case CMD_execute:
2573 case CMD_help:
2574 case CMD_hide:
2575 case CMD_ijump:
2576 case CMD_ilist:
2577 case CMD_isearch:
2578 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002579 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 case CMD_keepjumps:
2581 case CMD_keepmarks:
2582 case CMD_leftabove:
2583 case CMD_let:
2584 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002585 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002587 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 case CMD_perl:
2589 case CMD_psearch:
2590 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002591 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002592 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_return:
2594 case CMD_rightbelow:
2595 case CMD_ruby:
2596 case CMD_silent:
2597 case CMD_smagic:
2598 case CMD_snomagic:
2599 case CMD_substitute:
2600 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002601 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 case CMD_tcl:
2603 case CMD_throw:
2604 case CMD_tilde:
2605 case CMD_topleft:
2606 case CMD_unlet:
2607 case CMD_verbose:
2608 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002609 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
2611
2612 default: goto doend;
2613 }
2614 }
2615#endif
2616
2617 if (ea.argt & XFILE)
2618 {
2619 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2620 goto doend;
2621 }
2622
2623#ifdef FEAT_LISTCMDS
2624 /*
2625 * Accept buffer name. Cannot be used at the same time with a buffer
2626 * number. Don't do this for a user command.
2627 */
2628 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2629# ifdef FEAT_USR_CMDS
2630 && !USER_CMDIDX(ea.cmdidx)
2631# endif
2632 )
2633 {
2634 /*
2635 * :bdelete, :bwipeout and :bunload take several arguments, separated
2636 * by spaces: find next space (skipping over escaped characters).
2637 * The others take one argument: ignore trailing spaces.
2638 */
2639 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2640 || ea.cmdidx == CMD_bunload)
2641 p = skiptowhite_esc(ea.arg);
2642 else
2643 {
2644 p = ea.arg + STRLEN(ea.arg);
2645 while (p > ea.arg && vim_iswhite(p[-1]))
2646 --p;
2647 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002648 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2649 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (ea.line2 < 0) /* failed */
2651 goto doend;
2652 ea.addr_count = 1;
2653 ea.arg = skipwhite(p);
2654 }
2655#endif
2656
2657/*
2658 * 6. switch on command name
2659 *
2660 * The "ea" structure holds the arguments that can be used.
2661 */
2662 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 ea.cookie = cookie;
2665#ifdef FEAT_EVAL
2666 ea.cstack = cstack;
2667#endif
2668
2669#ifdef FEAT_USR_CMDS
2670 if (USER_CMDIDX(ea.cmdidx))
2671 {
2672 /*
2673 * Execute a user-defined command.
2674 */
2675 do_ucmd(&ea);
2676 }
2677 else
2678#endif
2679 {
2680 /*
2681 * Call the function to execute the command.
2682 */
2683 ea.errmsg = NULL;
2684 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2685 if (ea.errmsg != NULL)
2686 errormsg = (char_u *)_(ea.errmsg);
2687 }
2688
2689#ifdef FEAT_EVAL
2690 /*
2691 * If the command just executed called do_cmdline(), any throw or ":return"
2692 * or ":finish" encountered there must also check the cstack of the still
2693 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2694 * exception, or reanimate a returned function or finished script file and
2695 * return or finish it again.
2696 */
2697 if (need_rethrow)
2698 do_throw(cstack);
2699 else if (check_cstack)
2700 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002701 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002703 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 && current_func_returned())
2705 do_return(&ea, TRUE, FALSE, NULL);
2706 }
2707 need_rethrow = check_cstack = FALSE;
2708#endif
2709
2710doend:
2711 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2712 curwin->w_cursor.lnum = 1;
2713
2714 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2715 {
2716 if (sourcing)
2717 {
2718 if (errormsg != IObuff)
2719 {
2720 STRCPY(IObuff, errormsg);
2721 errormsg = IObuff;
2722 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002723 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 emsg(errormsg);
2726 }
2727#ifdef FEAT_EVAL
2728 do_errthrow(cstack,
2729 (ea.cmdidx != CMD_SIZE
2730# ifdef FEAT_USR_CMDS
2731 && !USER_CMDIDX(ea.cmdidx)
2732# endif
2733 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2734#endif
2735
2736 if (verbose_save >= 0)
2737 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002738#ifdef FEAT_AUTOCMD
2739 if (cmdmod.save_ei != NULL)
2740 {
2741 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002742 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2743 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002744 free_string_option(cmdmod.save_ei);
2745 }
2746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 cmdmod = save_cmdmod;
2749
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002750 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
2752 /* messages could be enabled for a serious error, need to check if the
2753 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002754 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002755 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 emsg_silent -= did_esilent;
2757 if (emsg_silent < 0)
2758 emsg_silent = 0;
2759 /* Restore msg_scroll, it's set by file I/O commands, even when no
2760 * message is actually displayed. */
2761 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002762
2763 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2764 * somewhere in the line. Put it back in the first column. */
2765 if (redirecting())
2766 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002769#ifdef HAVE_SANDBOX
2770 if (did_sandbox)
2771 --sandbox;
2772#endif
2773
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2775 ea.nextcmd = NULL;
2776
2777#ifdef FEAT_EVAL
2778 --ex_nesting_level;
2779#endif
2780
2781 return ea.nextcmd;
2782}
2783#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002784 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#endif
2786
2787/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002788 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * If there is a match advance "pp" to the argument and return TRUE.
2790 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002791 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002793 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 char *cmd; /* name of command */
2795 int len; /* required length */
2796{
2797 int i;
2798
2799 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002800 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 break;
2802 if (i >= len && !isalpha((*pp)[i]))
2803 {
2804 *pp = skipwhite(*pp + i);
2805 return TRUE;
2806 }
2807 return FALSE;
2808}
2809
2810/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002811 * Append "cmd" to the error message in IObuff.
2812 * Takes care of limiting the length and handling 0xa0, which would be
2813 * invisible otherwise.
2814 */
2815 static void
2816append_command(cmd)
2817 char_u *cmd;
2818{
2819 char_u *s = cmd;
2820 char_u *d;
2821
2822 STRCAT(IObuff, ": ");
2823 d = IObuff + STRLEN(IObuff);
2824 while (*s != NUL && d - IObuff < IOSIZE - 7)
2825 {
2826 if (
2827#ifdef FEAT_MBYTE
2828 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2829#endif
2830 *s == 0xa0)
2831 {
2832 s +=
2833#ifdef FEAT_MBYTE
2834 enc_utf8 ? 2 :
2835#endif
2836 1;
2837 STRCPY(d, "<a0>");
2838 d += 4;
2839 }
2840 else
2841 MB_COPY_CHAR(s, d);
2842 }
2843 *d = NUL;
2844}
2845
2846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002848 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 * Returns NULL for an ambiguous user command.
2852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 static char_u *
2854find_command(eap, full)
2855 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002856 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 int len;
2859 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002860 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 /*
2863 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002864 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * - the 'k' command can directly be followed by any character.
2866 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2867 * but :sre[wind] is another command, as are :scrip[tnames],
2868 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002869 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 */
2871 p = eap->cmd;
2872 if (*p == 'k')
2873 {
2874 eap->cmdidx = CMD_k;
2875 ++p;
2876 }
2877 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002878 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2879 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 || p[1] == 'g'
2881 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2882 || p[1] == 'I'
2883 || (p[1] == 'r' && p[2] != 'e')))
2884 {
2885 eap->cmdidx = CMD_substitute;
2886 ++p;
2887 }
2888 else
2889 {
2890 while (ASCII_ISALPHA(*p))
2891 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002892 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002893 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002894 while (ASCII_ISALNUM(*p))
2895 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /* check for non-alpha command */
2898 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2899 ++p;
2900 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002901 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2902 {
2903 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2904 * :delete with the 'l' flag. Same for 'p'. */
2905 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002906 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002907 break;
2908 if (i == len - 1)
2909 {
2910 --len;
2911 if (p[-1] == 'l')
2912 eap->flags |= EXFLAG_LIST;
2913 else
2914 eap->flags |= EXFLAG_PRINT;
2915 }
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 if (ASCII_ISLOWER(*eap->cmd))
2919 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2920 else
2921 eap->cmdidx = cmdidxs[26];
2922
2923 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2924 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2925 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2926 (size_t)len) == 0)
2927 {
2928#ifdef FEAT_EVAL
2929 if (full != NULL
2930 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2931 *full = TRUE;
2932#endif
2933 break;
2934 }
2935
2936#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002937 /* Look for a user defined command as a last resort. Let ":Print" be
2938 * overruled by a user defined command. */
2939 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2940 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002942 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 while (ASCII_ISALNUM(*p))
2944 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002945 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002948 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 eap->cmdidx = CMD_SIZE;
2950 }
2951
2952 return p;
2953}
2954
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955#ifdef FEAT_USR_CMDS
2956/*
2957 * Search for a user command that matches "eap->cmd".
2958 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2959 * Return a pointer to just after the command.
2960 * Return NULL if there is no matching command.
2961 */
2962 static char_u *
2963find_ucmd(eap, p, full, xp, compl)
2964 exarg_T *eap;
2965 char_u *p; /* end of the command (possibly including count) */
2966 int *full; /* set to TRUE for a full match */
2967 expand_T *xp; /* used for completion, NULL otherwise */
2968 int *compl; /* completion flags or NULL */
2969{
2970 int len = (int)(p - eap->cmd);
2971 int j, k, matchlen = 0;
2972 ucmd_T *uc;
2973 int found = FALSE;
2974 int possible = FALSE;
2975 char_u *cp, *np; /* Point into typed cmd and test name */
2976 garray_T *gap;
2977 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2978 only full match global is accepted. */
2979
2980 /*
2981 * Look for buffer-local user commands first, then global ones.
2982 */
2983 gap = &curbuf->b_ucmds;
2984 for (;;)
2985 {
2986 for (j = 0; j < gap->ga_len; ++j)
2987 {
2988 uc = USER_CMD_GA(gap, j);
2989 cp = eap->cmd;
2990 np = uc->uc_name;
2991 k = 0;
2992 while (k < len && *np != NUL && *cp++ == *np++)
2993 k++;
2994 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2995 {
2996 /* If finding a second match, the command is ambiguous. But
2997 * not if a buffer-local command wasn't a full match and a
2998 * global command is a full match. */
2999 if (k == len && found && *np != NUL)
3000 {
3001 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003002 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003003 amb_local = TRUE;
3004 }
3005
3006 if (!found || (k == len && *np == NUL))
3007 {
3008 /* If we matched up to a digit, then there could
3009 * be another command including the digit that we
3010 * should use instead.
3011 */
3012 if (k == len)
3013 found = TRUE;
3014 else
3015 possible = TRUE;
3016
3017 if (gap == &ucmds)
3018 eap->cmdidx = CMD_USER;
3019 else
3020 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003021 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003022 eap->useridx = j;
3023
3024# ifdef FEAT_CMDL_COMPL
3025 if (compl != NULL)
3026 *compl = uc->uc_compl;
3027# ifdef FEAT_EVAL
3028 if (xp != NULL)
3029 {
3030 xp->xp_arg = uc->uc_compl_arg;
3031 xp->xp_scriptID = uc->uc_scriptID;
3032 }
3033# endif
3034# endif
3035 /* Do not search for further abbreviations
3036 * if this is an exact match. */
3037 matchlen = k;
3038 if (k == len && *np == NUL)
3039 {
3040 if (full != NULL)
3041 *full = TRUE;
3042 amb_local = FALSE;
3043 break;
3044 }
3045 }
3046 }
3047 }
3048
3049 /* Stop if we found a full match or searched all. */
3050 if (j < gap->ga_len || gap == &ucmds)
3051 break;
3052 gap = &ucmds;
3053 }
3054
3055 /* Only found ambiguous matches. */
3056 if (amb_local)
3057 {
3058 if (xp != NULL)
3059 xp->xp_context = EXPAND_UNSUCCESSFUL;
3060 return NULL;
3061 }
3062
3063 /* The match we found may be followed immediately by a number. Move "p"
3064 * back to point to it. */
3065 if (found || possible)
3066 return p + (matchlen - len);
3067 return p;
3068}
3069#endif
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003072static struct cmdmod
3073{
3074 char *name;
3075 int minlen;
3076 int has_count; /* :123verbose :3tab */
3077} cmdmods[] = {
3078 {"aboveleft", 3, FALSE},
3079 {"belowright", 3, FALSE},
3080 {"botright", 2, FALSE},
3081 {"browse", 3, FALSE},
3082 {"confirm", 4, FALSE},
3083 {"hide", 3, FALSE},
3084 {"keepalt", 5, FALSE},
3085 {"keepjumps", 5, FALSE},
3086 {"keepmarks", 3, FALSE},
3087 {"leftabove", 5, FALSE},
3088 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003089 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003090 {"rightbelow", 6, FALSE},
3091 {"sandbox", 3, FALSE},
3092 {"silent", 3, FALSE},
3093 {"tab", 3, TRUE},
3094 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003095 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003096 {"verbose", 4, TRUE},
3097 {"vertical", 4, FALSE},
3098};
3099
3100/*
3101 * Return length of a command modifier (including optional count).
3102 * Return zero when it's not a modifier.
3103 */
3104 int
3105modifier_len(cmd)
3106 char_u *cmd;
3107{
3108 int i, j;
3109 char_u *p = cmd;
3110
3111 if (VIM_ISDIGIT(*cmd))
3112 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003113 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003114 {
3115 for (j = 0; p[j] != NUL; ++j)
3116 if (p[j] != cmdmods[i].name[j])
3117 break;
3118 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3119 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003120 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003121 }
3122 return 0;
3123}
3124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125/*
3126 * Return > 0 if an Ex command "name" exists.
3127 * Return 2 if there is an exact match.
3128 * Return 3 if there is an ambiguous match.
3129 */
3130 int
3131cmd_exists(name)
3132 char_u *name;
3133{
3134 exarg_T ea;
3135 int full = FALSE;
3136 int i;
3137 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003138 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003141 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 for (j = 0; name[j] != NUL; ++j)
3144 if (name[j] != cmdmods[i].name[j])
3145 break;
3146 if (name[j] == NUL && j >= cmdmods[i].minlen)
3147 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3148 }
3149
Bram Moolenaara9587612006-05-04 21:47:50 +00003150 /* Check built-in commands and user defined commands.
3151 * For ":2match" and ":3match" we need to skip the number. */
3152 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003154 p = find_command(&ea, &full);
3155 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003157 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3158 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003159 if (*skipwhite(p) != NUL)
3160 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3162}
3163#endif
3164
3165/*
3166 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3167 * we don't need/want deleted. Maybe this could be done better if we didn't
3168 * repeat all this stuff. The only problem is that they may not stay
3169 * perfectly compatible with each other, but then the command line syntax
3170 * probably won't change that much -- webb.
3171 */
3172 char_u *
3173set_one_cmd_context(xp, buff)
3174 expand_T *xp;
3175 char_u *buff; /* buffer for command string */
3176{
3177 char_u *p;
3178 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 int len = 0;
3180 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3182 int compl = EXPAND_NOTHING;
3183#endif
3184#ifdef FEAT_CMDL_COMPL
3185 int delim;
3186#endif
3187 int forceit = FALSE;
3188 int usefilter = FALSE; /* filter instead of file name */
3189
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003190 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 xp->xp_pattern = buff;
3192 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195/*
3196 * 2. skip comment lines and leading space, colons or bars
3197 */
3198 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3199 ;
3200 xp->xp_pattern = cmd;
3201
3202 if (*cmd == NUL)
3203 return NULL;
3204 if (*cmd == '"') /* ignore comment lines */
3205 {
3206 xp->xp_context = EXPAND_NOTHING;
3207 return NULL;
3208 }
3209
3210/*
3211 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3212 */
3213 cmd = skip_range(cmd, &xp->xp_context);
3214
3215/*
3216 * 4. parse command
3217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 xp->xp_pattern = cmd;
3219 if (*cmd == NUL)
3220 return NULL;
3221 if (*cmd == '"')
3222 {
3223 xp->xp_context = EXPAND_NOTHING;
3224 return NULL;
3225 }
3226
3227 if (*cmd == '|' || *cmd == '\n')
3228 return cmd + 1; /* There's another command */
3229
3230 /*
3231 * Isolate the command and search for it in the command table.
3232 * Exceptions:
3233 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003234 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3236 */
3237 if (*cmd == 'k' && cmd[1] != 'e')
3238 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 p = cmd + 1;
3241 }
3242 else
3243 {
3244 p = cmd;
3245 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3246 ++p;
3247 /* check for non-alpha command */
3248 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3249 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 xp->xp_context = EXPAND_UNSUCCESSFUL;
3255 return NULL;
3256 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003258 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3259 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3260 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 break;
3262
3263#ifdef FEAT_USR_CMDS
3264 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3266 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
3268 }
3269
3270 /*
3271 * If the cursor is touching the command, and it ends in an alpha-numeric
3272 * character, complete the command name.
3273 */
3274 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3275 return NULL;
3276
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3280 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003281 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 p = cmd + 1;
3283 }
3284#ifdef FEAT_USR_CMDS
3285 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3286 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287 ea.cmd = cmd;
3288 p = find_ucmd(&ea, p, NULL, xp,
3289# if defined(FEAT_CMDL_COMPL)
3290 &compl
3291# else
3292 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003295 if (p == NULL)
3296 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298#endif
3299 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003300 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
3302 /* Not still touching the command and it was an illegal one */
3303 xp->xp_context = EXPAND_UNSUCCESSFUL;
3304 return NULL;
3305 }
3306
3307 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3308
3309 if (*p == '!') /* forced commands */
3310 {
3311 forceit = TRUE;
3312 ++p;
3313 }
3314
3315/*
3316 * 5. parse arguments
3317 */
3318#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003321 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 arg = skipwhite(p);
3324
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003325 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
3327 if (*arg == '>') /* append */
3328 {
3329 if (*++arg == '>')
3330 ++arg;
3331 arg = skipwhite(arg);
3332 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 ++arg;
3336 usefilter = TRUE;
3337 }
3338 }
3339
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003340 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342 usefilter = forceit; /* :r! filter if forced */
3343 if (*arg == '!') /* :r !filter */
3344 {
3345 ++arg;
3346 usefilter = TRUE;
3347 }
3348 }
3349
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003350 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 while (*arg == *cmd) /* allow any number of '>' or '<' */
3353 ++arg;
3354 arg = skipwhite(arg);
3355 }
3356
3357 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 /* Check if we're in the +command */
3361 p = arg + 1;
3362 arg = skip_cmd_arg(arg, FALSE);
3363
3364 /* Still touching the command after '+'? */
3365 if (*arg == NUL)
3366 return p;
3367
3368 /* Skip space(s) after +command to get to the real argument */
3369 arg = skipwhite(arg);
3370 }
3371
3372 /*
3373 * Check for '|' to separate commands and '"' to start comments.
3374 * Don't do this for ":read !cmd" and ":write !cmd".
3375 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003376 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 p = arg;
3379 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003380 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 p += 2;
3382 while (*p)
3383 {
3384 if (*p == Ctrl_V)
3385 {
3386 if (p[1] != NUL)
3387 ++p;
3388 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003389 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 || *p == '|' || *p == '\n')
3391 {
3392 if (*(p - 1) != '\\')
3393 {
3394 if (*p == '|' || *p == '\n')
3395 return p + 1;
3396 return NULL; /* It's a comment */
3397 }
3398 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003399 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401 }
3402
3403 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 vim_strchr((char_u *)"|\"", *arg) == NULL)
3406 return NULL;
3407
3408 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003409 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003411 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003412 while (*p && p < buff + len)
3413 {
3414 if (*p == ' ' || *p == TAB)
3415 {
3416 /* argument starts after a space */
3417 xp->xp_pattern = ++p;
3418 }
3419 else
3420 {
3421 if (*p == '\\' && *(p + 1) != NUL)
3422 ++p; /* skip over escaped character */
3423 mb_ptr_adv(p);
3424 }
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003429 int c;
3430 int in_quote = FALSE;
3431 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433 /*
3434 * Allow spaces within back-quotes to count as part of the argument
3435 * being expanded.
3436 */
3437 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003438 p = xp->xp_pattern;
3439 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003441#ifdef FEAT_MBYTE
3442 if (has_mbyte)
3443 c = mb_ptr2char(p);
3444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003446 c = *p;
3447 if (c == '\\' && p[1] != NUL)
3448 ++p;
3449 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 if (!in_quote)
3452 {
3453 xp->xp_pattern = p;
3454 bow = p + 1;
3455 }
3456 in_quote = !in_quote;
3457 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003458 /* An argument can contain just about everything, except
3459 * characters that end the command and white space. */
3460 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003461#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003462 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003463#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003464 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003465 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003466 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003467 while (*p != NUL)
3468 {
3469#ifdef FEAT_MBYTE
3470 if (has_mbyte)
3471 c = mb_ptr2char(p);
3472 else
3473#endif
3474 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003475 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003476 break;
3477#ifdef FEAT_MBYTE
3478 if (has_mbyte)
3479 len = (*mb_ptr2len)(p);
3480 else
3481#endif
3482 len = 1;
3483 mb_ptr_adv(p);
3484 }
3485 if (in_quote)
3486 bow = p;
3487 else
3488 xp->xp_pattern = p;
3489 p -= len;
3490 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003491 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 /*
3495 * If we are still inside the quotes, and we passed a space, just
3496 * expand from there.
3497 */
3498 if (bow != NULL && in_quote)
3499 xp->xp_pattern = bow;
3500 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003502 /* For a shell command more chars need to be escaped. */
3503 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003504 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003505#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003506 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003507#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003508 /* When still after the command name expand executables. */
3509 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003510 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 /* Check for environment variable */
3514 if (*xp->xp_pattern == '$'
3515#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3516 || *xp->xp_pattern == '%'
3517#endif
3518 )
3519 {
3520 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3521 if (!vim_isIDc(*p))
3522 break;
3523 if (*p == NUL)
3524 {
3525 xp->xp_context = EXPAND_ENV_VARS;
3526 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003527#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3528 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003529 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003530 compl = EXPAND_ENV_VARS;
3531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003534#if defined(FEAT_CMDL_COMPL)
3535 /* Check for user names */
3536 if (*xp->xp_pattern == '~')
3537 {
3538 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3539 ;
3540 /* Complete ~user only if it partially matches a user name.
3541 * A full match ~user<Tab> will be replaced by user's home
3542 * directory i.e. something like ~user<Tab> -> /home/user/ */
3543 if (*p == NUL && p > xp->xp_pattern + 1
3544 && match_user(xp->xp_pattern + 1) == 1)
3545 {
3546 xp->xp_context = EXPAND_USER;
3547 ++xp->xp_pattern;
3548 }
3549 }
3550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553/*
3554 * 6. switch on command name
3555 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003556 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003558 case CMD_find:
3559 case CMD_sfind:
3560 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003561 if (xp->xp_context == EXPAND_FILES)
3562 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 case CMD_cd:
3565 case CMD_chdir:
3566 case CMD_lcd:
3567 case CMD_lchdir:
3568 if (xp->xp_context == EXPAND_FILES)
3569 xp->xp_context = EXPAND_DIRECTORIES;
3570 break;
3571 case CMD_help:
3572 xp->xp_context = EXPAND_HELP;
3573 xp->xp_pattern = arg;
3574 break;
3575
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003576 /* Command modifiers: return the argument.
3577 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003579 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 case CMD_belowright:
3581 case CMD_botright:
3582 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003583 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003585 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 case CMD_folddoclosed:
3587 case CMD_folddoopen:
3588 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003589 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 case CMD_keepjumps:
3591 case CMD_keepmarks:
3592 case CMD_leftabove:
3593 case CMD_lockmarks:
3594 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003595 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003597 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 case CMD_topleft:
3599 case CMD_verbose:
3600 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003601 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 return arg;
3603
Bram Moolenaar4f688582007-07-24 12:34:30 +00003604#ifdef FEAT_CMDL_COMPL
3605# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 case CMD_match:
3607 if (*arg == NUL || !ends_excmd(*arg))
3608 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003609 /* also complete "None" */
3610 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 arg = skipwhite(skiptowhite(arg));
3612 if (*arg != NUL)
3613 {
3614 xp->xp_context = EXPAND_NOTHING;
3615 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3616 }
3617 }
3618 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621/*
3622 * All completion for the +cmdline_compl feature goes here.
3623 */
3624
3625# ifdef FEAT_USR_CMDS
3626 case CMD_command:
3627 /* Check for attributes */
3628 while (*arg == '-')
3629 {
3630 arg++; /* Skip "-" */
3631 p = skiptowhite(arg);
3632 if (*p == NUL)
3633 {
3634 /* Cursor is still in the attribute */
3635 p = vim_strchr(arg, '=');
3636 if (p == NULL)
3637 {
3638 /* No "=", so complete attribute names */
3639 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3640 xp->xp_pattern = arg;
3641 return NULL;
3642 }
3643
3644 /* For the -complete and -nargs attributes, we complete
3645 * their arguments as well.
3646 */
3647 if (STRNICMP(arg, "complete", p - arg) == 0)
3648 {
3649 xp->xp_context = EXPAND_USER_COMPLETE;
3650 xp->xp_pattern = p + 1;
3651 return NULL;
3652 }
3653 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3654 {
3655 xp->xp_context = EXPAND_USER_NARGS;
3656 xp->xp_pattern = p + 1;
3657 return NULL;
3658 }
3659 return NULL;
3660 }
3661 arg = skipwhite(p);
3662 }
3663
3664 /* After the attributes comes the new command name */
3665 p = skiptowhite(arg);
3666 if (*p == NUL)
3667 {
3668 xp->xp_context = EXPAND_USER_COMMANDS;
3669 xp->xp_pattern = arg;
3670 break;
3671 }
3672
3673 /* And finally comes a normal command */
3674 return skipwhite(p);
3675
3676 case CMD_delcommand:
3677 xp->xp_context = EXPAND_USER_COMMANDS;
3678 xp->xp_pattern = arg;
3679 break;
3680# endif
3681
3682 case CMD_global:
3683 case CMD_vglobal:
3684 delim = *arg; /* get the delimiter */
3685 if (delim)
3686 ++arg; /* skip delimiter if there is one */
3687
3688 while (arg[0] != NUL && arg[0] != delim)
3689 {
3690 if (arg[0] == '\\' && arg[1] != NUL)
3691 ++arg;
3692 ++arg;
3693 }
3694 if (arg[0] != NUL)
3695 return arg + 1;
3696 break;
3697 case CMD_and:
3698 case CMD_substitute:
3699 delim = *arg;
3700 if (delim)
3701 {
3702 /* skip "from" part */
3703 ++arg;
3704 arg = skip_regexp(arg, delim, p_magic, NULL);
3705 }
3706 /* skip "to" part */
3707 while (arg[0] != NUL && arg[0] != delim)
3708 {
3709 if (arg[0] == '\\' && arg[1] != NUL)
3710 ++arg;
3711 ++arg;
3712 }
3713 if (arg[0] != NUL) /* skip delimiter */
3714 ++arg;
3715 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3716 ++arg;
3717 if (arg[0] != NUL)
3718 return arg;
3719 break;
3720 case CMD_isearch:
3721 case CMD_dsearch:
3722 case CMD_ilist:
3723 case CMD_dlist:
3724 case CMD_ijump:
3725 case CMD_psearch:
3726 case CMD_djump:
3727 case CMD_isplit:
3728 case CMD_dsplit:
3729 arg = skipwhite(skipdigits(arg)); /* skip count */
3730 if (*arg == '/') /* Match regexp, not just whole words */
3731 {
3732 for (++arg; *arg && *arg != '/'; arg++)
3733 if (*arg == '\\' && arg[1] != NUL)
3734 arg++;
3735 if (*arg)
3736 {
3737 arg = skipwhite(arg + 1);
3738
3739 /* Check for trailing illegal characters */
3740 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3741 xp->xp_context = EXPAND_NOTHING;
3742 else
3743 return arg;
3744 }
3745 }
3746 break;
3747#ifdef FEAT_AUTOCMD
3748 case CMD_autocmd:
3749 return set_context_in_autocmd(xp, arg, FALSE);
3750
3751 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003752 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 return set_context_in_autocmd(xp, arg, TRUE);
3754#endif
3755 case CMD_set:
3756 set_context_in_set_cmd(xp, arg, 0);
3757 break;
3758 case CMD_setglobal:
3759 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3760 break;
3761 case CMD_setlocal:
3762 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3763 break;
3764 case CMD_tag:
3765 case CMD_stag:
3766 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003767 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case CMD_tselect:
3769 case CMD_stselect:
3770 case CMD_ptselect:
3771 case CMD_tjump:
3772 case CMD_stjump:
3773 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003774 if (*p_wop != NUL)
3775 xp->xp_context = EXPAND_TAGS_LISTFILES;
3776 else
3777 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 xp->xp_pattern = arg;
3779 break;
3780 case CMD_augroup:
3781 xp->xp_context = EXPAND_AUGROUP;
3782 xp->xp_pattern = arg;
3783 break;
3784#ifdef FEAT_SYN_HL
3785 case CMD_syntax:
3786 set_context_in_syntax_cmd(xp, arg);
3787 break;
3788#endif
3789#ifdef FEAT_EVAL
3790 case CMD_let:
3791 case CMD_if:
3792 case CMD_elseif:
3793 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003794 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 case CMD_echo:
3796 case CMD_echon:
3797 case CMD_execute:
3798 case CMD_echomsg:
3799 case CMD_echoerr:
3800 case CMD_call:
3801 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003802 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 break;
3804
3805 case CMD_unlet:
3806 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3807 arg = xp->xp_pattern + 1;
3808 xp->xp_context = EXPAND_USER_VARS;
3809 xp->xp_pattern = arg;
3810 break;
3811
3812 case CMD_function:
3813 case CMD_delfunction:
3814 xp->xp_context = EXPAND_USER_FUNC;
3815 xp->xp_pattern = arg;
3816 break;
3817
3818 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003819 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 break;
3821#endif
3822 case CMD_highlight:
3823 set_context_in_highlight_cmd(xp, arg);
3824 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003825#ifdef FEAT_CSCOPE
3826 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003827 case CMD_lcscope:
3828 case CMD_scscope:
3829 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003830 break;
3831#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003832#ifdef FEAT_SIGNS
3833 case CMD_sign:
3834 set_context_in_sign_cmd(xp, arg);
3835 break;
3836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#ifdef FEAT_LISTCMDS
3838 case CMD_bdelete:
3839 case CMD_bwipeout:
3840 case CMD_bunload:
3841 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3842 arg = xp->xp_pattern + 1;
3843 /*FALLTHROUGH*/
3844 case CMD_buffer:
3845 case CMD_sbuffer:
3846 case CMD_checktime:
3847 xp->xp_context = EXPAND_BUFFERS;
3848 xp->xp_pattern = arg;
3849 break;
3850#endif
3851#ifdef FEAT_USR_CMDS
3852 case CMD_USER:
3853 case CMD_USER_BUF:
3854 if (compl != EXPAND_NOTHING)
3855 {
3856 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003857 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859# ifdef FEAT_MENU
3860 if (compl == EXPAND_MENUS)
3861 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3862# endif
3863 if (compl == EXPAND_COMMANDS)
3864 return arg;
3865 if (compl == EXPAND_MAPPINGS)
3866 return set_context_in_map_cmd(xp, (char_u *)"map",
3867 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003868 /* Find start of last argument. */
3869 p = arg;
3870 while (*p)
3871 {
3872 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003873 /* argument starts after a space */
3874 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003875 else if (*p == '\\' && *(p + 1) != NUL)
3876 ++p; /* skip over escaped character */
3877 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 xp->xp_pattern = arg;
3880 }
3881 xp->xp_context = compl;
3882 }
3883 break;
3884#endif
3885 case CMD_map: case CMD_noremap:
3886 case CMD_nmap: case CMD_nnoremap:
3887 case CMD_vmap: case CMD_vnoremap:
3888 case CMD_omap: case CMD_onoremap:
3889 case CMD_imap: case CMD_inoremap:
3890 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003891 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003893 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_unmap:
3895 case CMD_nunmap:
3896 case CMD_vunmap:
3897 case CMD_ounmap:
3898 case CMD_iunmap:
3899 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003900 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003902 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_abbreviate: case CMD_noreabbrev:
3904 case CMD_cabbrev: case CMD_cnoreabbrev:
3905 case CMD_iabbrev: case CMD_inoreabbrev:
3906 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003907 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_unabbreviate:
3909 case CMD_cunabbrev:
3910 case CMD_iunabbrev:
3911 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003912 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#ifdef FEAT_MENU
3914 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3915 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3916 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3917 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3918 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3919 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3920 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3921 case CMD_tmenu: case CMD_tunmenu:
3922 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3923 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3924#endif
3925
3926 case CMD_colorscheme:
3927 xp->xp_context = EXPAND_COLORS;
3928 xp->xp_pattern = arg;
3929 break;
3930
3931 case CMD_compiler:
3932 xp->xp_context = EXPAND_COMPILER;
3933 xp->xp_pattern = arg;
3934 break;
3935
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003936 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003937 xp->xp_context = EXPAND_OWNSYNTAX;
3938 xp->xp_pattern = arg;
3939 break;
3940
3941 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003942 xp->xp_context = EXPAND_FILETYPE;
3943 xp->xp_pattern = arg;
3944 break;
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3947 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3948 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003949 p = skiptowhite(arg);
3950 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 xp->xp_context = EXPAND_LANGUAGE;
3953 xp->xp_pattern = arg;
3954 }
3955 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003956 {
3957 if ( STRNCMP(arg, "messages", p - arg) == 0
3958 || STRNCMP(arg, "ctype", p - arg) == 0
3959 || STRNCMP(arg, "time", p - arg) == 0)
3960 {
3961 xp->xp_context = EXPAND_LOCALES;
3962 xp->xp_pattern = skipwhite(p);
3963 }
3964 else
3965 xp->xp_context = EXPAND_NOTHING;
3966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 break;
3968#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003969#if defined(FEAT_PROFILE)
3970 case CMD_profile:
3971 set_context_in_profile_cmd(xp, arg);
3972 break;
3973#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003974 case CMD_behave:
3975 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003976 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003979#if defined(FEAT_CMDHIST)
3980 case CMD_history:
3981 xp->xp_context = EXPAND_HISTORY;
3982 xp->xp_pattern = arg;
3983 break;
3984#endif
3985
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#endif /* FEAT_CMDL_COMPL */
3987
3988 default:
3989 break;
3990 }
3991 return NULL;
3992}
3993
3994/*
3995 * skip a range specifier of the form: addr [,addr] [;addr] ..
3996 *
3997 * Backslashed delimiters after / or ? will be skipped, and commands will
3998 * not be expanded between /'s and ?'s or after "'".
3999 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004000 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 * Returns the "cmd" pointer advanced to beyond the range.
4002 */
4003 char_u *
4004skip_range(cmd, ctx)
4005 char_u *cmd;
4006 int *ctx; /* pointer to xp_context or NULL */
4007{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004008 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaardf177f62005-02-22 08:39:57 +00004010 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 if (*cmd == '\'')
4013 {
4014 if (*++cmd == NUL && ctx != NULL)
4015 *ctx = EXPAND_NOTHING;
4016 }
4017 else if (*cmd == '/' || *cmd == '?')
4018 {
4019 delim = *cmd++;
4020 while (*cmd != NUL && *cmd != delim)
4021 if (*cmd++ == '\\' && *cmd != NUL)
4022 ++cmd;
4023 if (*cmd == NUL && ctx != NULL)
4024 *ctx = EXPAND_NOTHING;
4025 }
4026 if (*cmd != NUL)
4027 ++cmd;
4028 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004029
4030 /* Skip ":" and white space. */
4031 while (*cmd == ':')
4032 cmd = skipwhite(cmd + 1);
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return cmd;
4035}
4036
4037/*
4038 * get a single EX address
4039 *
4040 * Set ptr to the next character after the part that was interpreted.
4041 * Set ptr to NULL when an error is encountered.
4042 *
4043 * Return MAXLNUM when no Ex address was found.
4044 */
4045 static linenr_T
4046get_address(ptr, skip, to_other_file)
4047 char_u **ptr;
4048 int skip; /* only skip the address, don't use it */
4049 int to_other_file; /* flag: may jump to other file */
4050{
4051 int c;
4052 int i;
4053 long n;
4054 char_u *cmd;
4055 pos_T pos;
4056 pos_T *fp;
4057 linenr_T lnum;
4058
4059 cmd = skipwhite(*ptr);
4060 lnum = MAXLNUM;
4061 do
4062 {
4063 switch (*cmd)
4064 {
4065 case '.': /* '.' - Cursor position */
4066 ++cmd;
4067 lnum = curwin->w_cursor.lnum;
4068 break;
4069
4070 case '$': /* '$' - last line */
4071 ++cmd;
4072 lnum = curbuf->b_ml.ml_line_count;
4073 break;
4074
4075 case '\'': /* ''' - mark */
4076 if (*++cmd == NUL)
4077 {
4078 cmd = NULL;
4079 goto error;
4080 }
4081 if (skip)
4082 ++cmd;
4083 else
4084 {
4085 /* Only accept a mark in another file when it is
4086 * used by itself: ":'M". */
4087 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4088 ++cmd;
4089 if (fp == (pos_T *)-1)
4090 /* Jumped to another file. */
4091 lnum = curwin->w_cursor.lnum;
4092 else
4093 {
4094 if (check_mark(fp) == FAIL)
4095 {
4096 cmd = NULL;
4097 goto error;
4098 }
4099 lnum = fp->lnum;
4100 }
4101 }
4102 break;
4103
4104 case '/':
4105 case '?': /* '/' or '?' - search */
4106 c = *cmd++;
4107 if (skip) /* skip "/pat/" */
4108 {
4109 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4110 if (*cmd == c)
4111 ++cmd;
4112 }
4113 else
4114 {
4115 pos = curwin->w_cursor; /* save curwin->w_cursor */
4116 /*
4117 * When '/' or '?' follows another address, start
4118 * from there.
4119 */
4120 if (lnum != MAXLNUM)
4121 curwin->w_cursor.lnum = lnum;
4122 /*
4123 * Start a forward search at the end of the line.
4124 * Start a backward search at the start of the line.
4125 * This makes sure we never match in the current
4126 * line, and can match anywhere in the
4127 * next/previous line.
4128 */
4129 if (c == '/')
4130 curwin->w_cursor.col = MAXCOL;
4131 else
4132 curwin->w_cursor.col = 0;
4133 searchcmdlen = 0;
4134 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004135 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
4137 curwin->w_cursor = pos;
4138 cmd = NULL;
4139 goto error;
4140 }
4141 lnum = curwin->w_cursor.lnum;
4142 curwin->w_cursor = pos;
4143 /* adjust command string pointer */
4144 cmd += searchcmdlen;
4145 }
4146 break;
4147
4148 case '\\': /* "\?", "\/" or "\&", repeat search */
4149 ++cmd;
4150 if (*cmd == '&')
4151 i = RE_SUBST;
4152 else if (*cmd == '?' || *cmd == '/')
4153 i = RE_SEARCH;
4154 else
4155 {
4156 EMSG(_(e_backslash));
4157 cmd = NULL;
4158 goto error;
4159 }
4160
4161 if (!skip)
4162 {
4163 /*
4164 * When search follows another address, start from
4165 * there.
4166 */
4167 if (lnum != MAXLNUM)
4168 pos.lnum = lnum;
4169 else
4170 pos.lnum = curwin->w_cursor.lnum;
4171
4172 /*
4173 * Start the search just like for the above
4174 * do_search().
4175 */
4176 if (*cmd != '?')
4177 pos.col = MAXCOL;
4178 else
4179 pos.col = 0;
4180 if (searchit(curwin, curbuf, &pos,
4181 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004182 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004183 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 lnum = pos.lnum;
4185 else
4186 {
4187 cmd = NULL;
4188 goto error;
4189 }
4190 }
4191 ++cmd;
4192 break;
4193
4194 default:
4195 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4196 lnum = getdigits(&cmd);
4197 }
4198
4199 for (;;)
4200 {
4201 cmd = skipwhite(cmd);
4202 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4203 break;
4204
4205 if (lnum == MAXLNUM)
4206 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4207 if (VIM_ISDIGIT(*cmd))
4208 i = '+'; /* "number" is same as "+number" */
4209 else
4210 i = *cmd++;
4211 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4212 n = 1;
4213 else
4214 n = getdigits(&cmd);
4215 if (i == '-')
4216 lnum -= n;
4217 else
4218 lnum += n;
4219 }
4220 } while (*cmd == '/' || *cmd == '?');
4221
4222error:
4223 *ptr = cmd;
4224 return lnum;
4225}
4226
4227/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004228 * Get flags from an Ex command argument.
4229 */
4230 static void
4231get_flags(eap)
4232 exarg_T *eap;
4233{
4234 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4235 {
4236 if (*eap->arg == 'l')
4237 eap->flags |= EXFLAG_LIST;
4238 else if (*eap->arg == 'p')
4239 eap->flags |= EXFLAG_PRINT;
4240 else
4241 eap->flags |= EXFLAG_NR;
4242 eap->arg = skipwhite(eap->arg + 1);
4243 }
4244}
4245
4246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 * Function called for command which is Not Implemented. NI!
4248 */
4249 void
4250ex_ni(eap)
4251 exarg_T *eap;
4252{
4253 if (!eap->skip)
4254 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4255}
4256
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004257#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258/*
4259 * Function called for script command which is Not Implemented. NI!
4260 * Skips over ":perl <<EOF" constructs.
4261 */
4262 static void
4263ex_script_ni(eap)
4264 exarg_T *eap;
4265{
4266 if (!eap->skip)
4267 ex_ni(eap);
4268 else
4269 vim_free(script_get(eap, eap->arg));
4270}
4271#endif
4272
4273/*
4274 * Check range in Ex command for validity.
4275 * Return NULL when valid, error message when invalid.
4276 */
4277 static char_u *
4278invalid_range(eap)
4279 exarg_T *eap;
4280{
4281 if ( eap->line1 < 0
4282 || eap->line2 < 0
4283 || eap->line1 > eap->line2
4284 || ((eap->argt & RANGE)
4285 && !(eap->argt & NOTADR)
4286 && eap->line2 > curbuf->b_ml.ml_line_count
4287#ifdef FEAT_DIFF
4288 + (eap->cmdidx == CMD_diffget)
4289#endif
4290 ))
4291 return (char_u *)_(e_invrange);
4292 return NULL;
4293}
4294
4295/*
4296 * Correct the range for zero line number, if required.
4297 */
4298 static void
4299correct_range(eap)
4300 exarg_T *eap;
4301{
4302 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4303 {
4304 if (eap->line1 == 0)
4305 eap->line1 = 1;
4306 if (eap->line2 == 0)
4307 eap->line2 = 1;
4308 }
4309}
4310
Bram Moolenaar748bf032005-02-02 23:04:36 +00004311#ifdef FEAT_QUICKFIX
4312static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4313
4314/*
4315 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4316 * pattern. Otherwise return eap->arg.
4317 */
4318 static char_u *
4319skip_grep_pat(eap)
4320 exarg_T *eap;
4321{
4322 char_u *p = eap->arg;
4323
Bram Moolenaara37420f2006-02-04 22:37:47 +00004324 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4325 || eap->cmdidx == CMD_vimgrepadd
4326 || eap->cmdidx == CMD_lvimgrepadd
4327 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004328 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004329 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004330 if (p == NULL)
4331 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004332 }
4333 return p;
4334}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004335
4336/*
4337 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4338 * in the command line, so that things like % get expanded.
4339 */
4340 static char_u *
4341replace_makeprg(eap, p, cmdlinep)
4342 exarg_T *eap;
4343 char_u *p;
4344 char_u **cmdlinep;
4345{
4346 char_u *new_cmdline;
4347 char_u *program;
4348 char_u *pos;
4349 char_u *ptr;
4350 int len;
4351 int i;
4352
4353 /*
4354 * Don't do it when ":vimgrep" is used for ":grep".
4355 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004356 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4357 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4358 || eap->cmdidx == CMD_grepadd
4359 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004360 && !grep_internal(eap->cmdidx))
4361 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004362 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4363 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004364 {
4365 if (*curbuf->b_p_gp == NUL)
4366 program = p_gp;
4367 else
4368 program = curbuf->b_p_gp;
4369 }
4370 else
4371 {
4372 if (*curbuf->b_p_mp == NUL)
4373 program = p_mp;
4374 else
4375 program = curbuf->b_p_mp;
4376 }
4377
4378 p = skipwhite(p);
4379
4380 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4381 {
4382 /* replace $* by given arguments */
4383 i = 1;
4384 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4385 ++i;
4386 len = (int)STRLEN(p);
4387 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4388 if (new_cmdline == NULL)
4389 return NULL; /* out of memory */
4390 ptr = new_cmdline;
4391 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4392 {
4393 i = (int)(pos - program);
4394 STRNCPY(ptr, program, i);
4395 STRCPY(ptr += i, p);
4396 ptr += len;
4397 program = pos + 2;
4398 }
4399 STRCPY(ptr, program);
4400 }
4401 else
4402 {
4403 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4404 if (new_cmdline == NULL)
4405 return NULL; /* out of memory */
4406 STRCPY(new_cmdline, program);
4407 STRCAT(new_cmdline, " ");
4408 STRCAT(new_cmdline, p);
4409 }
4410 msg_make(p);
4411
4412 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4413 vim_free(*cmdlinep);
4414 *cmdlinep = new_cmdline;
4415 p = new_cmdline;
4416 }
4417 return p;
4418}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004419#endif
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421/*
4422 * Expand file name in Ex command argument.
4423 * Return FAIL for failure, OK otherwise.
4424 */
4425 int
4426expand_filename(eap, cmdlinep, errormsgp)
4427 exarg_T *eap;
4428 char_u **cmdlinep;
4429 char_u **errormsgp;
4430{
4431 int has_wildcards; /* need to expand wildcards */
4432 char_u *repl;
4433 int srclen;
4434 char_u *p;
4435 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004436 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
Bram Moolenaar748bf032005-02-02 23:04:36 +00004438#ifdef FEAT_QUICKFIX
4439 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4440 p = skip_grep_pat(eap);
4441#else
4442 p = eap->arg;
4443#endif
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /*
4446 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4447 * the file name contains a wildcard it should not cause expanding.
4448 * (it will be expanded anyway if there is a wildcard before replacing).
4449 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004450 has_wildcards = mch_has_wildcard(p);
4451 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004453#ifdef FEAT_EVAL
4454 /* Skip over `=expr`, wildcards in it are not expanded. */
4455 if (p[0] == '`' && p[1] == '=')
4456 {
4457 p += 2;
4458 (void)skip_expr(&p);
4459 if (*p == '`')
4460 ++p;
4461 continue;
4462 }
4463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /*
4465 * Quick check if this cannot be the start of a special string.
4466 * Also removes backslash before '%', '#' and '<'.
4467 */
4468 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4469 {
4470 ++p;
4471 continue;
4472 }
4473
4474 /*
4475 * Try to find a match at this position.
4476 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004477 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4478 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (*errormsgp != NULL) /* error detected */
4480 return FAIL;
4481 if (repl == NULL) /* no match found */
4482 {
4483 p += srclen;
4484 continue;
4485 }
4486
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004487 /* Wildcards won't be expanded below, the replacement is taken
4488 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4489 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4490 {
4491 char_u *l = repl;
4492
4493 repl = expand_env_save(repl);
4494 vim_free(l);
4495 }
4496
Bram Moolenaar63b92542007-03-27 14:57:09 +00004497 /* Need to escape white space et al. with a backslash.
4498 * Don't do this for:
4499 * - replacement that already has been escaped: "##"
4500 * - shell commands (may have to use quotes instead).
4501 * - non-unix systems when there is a single argument (spaces don't
4502 * separate arguments then).
4503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004505 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 && eap->cmdidx != CMD_bang
4507 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004508 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004510 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004512 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513#ifndef UNIX
4514 && !(eap->argt & NOSPC)
4515#endif
4516 )
4517 {
4518 char_u *l;
4519#ifdef BACKSLASH_IN_FILENAME
4520 /* Don't escape a backslash here, because rem_backslash() doesn't
4521 * remove it later. */
4522 static char_u *nobslash = (char_u *)" \t\"|";
4523# define ESCAPE_CHARS nobslash
4524#else
4525# define ESCAPE_CHARS escape_chars
4526#endif
4527
4528 for (l = repl; *l; ++l)
4529 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4530 {
4531 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4532 if (l != NULL)
4533 {
4534 vim_free(repl);
4535 repl = l;
4536 }
4537 break;
4538 }
4539 }
4540
4541 /* For a shell command a '!' must be escaped. */
4542 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004543 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 char_u *l;
4546
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004547 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (l != NULL)
4549 {
4550 vim_free(repl);
4551 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004552 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (strstr((char *)p_sh, "sh") != NULL)
4554 {
4555 l = vim_strsave_escaped(repl, (char_u *)"!");
4556 if (l != NULL)
4557 {
4558 vim_free(repl);
4559 repl = l;
4560 }
4561 }
4562 }
4563 }
4564
4565 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4566 vim_free(repl);
4567 if (p == NULL)
4568 return FAIL;
4569 }
4570
4571 /*
4572 * One file argument: Expand wildcards.
4573 * Don't do this with ":r !command" or ":w !command".
4574 */
4575 if ((eap->argt & NOSPC) && !eap->usefilter)
4576 {
4577 /*
4578 * May do this twice:
4579 * 1. Replace environment variables.
4580 * 2. Replace any other wildcards, remove backslashes.
4581 */
4582 for (n = 1; n <= 2; ++n)
4583 {
4584 if (n == 2)
4585 {
4586#ifdef UNIX
4587 /*
4588 * Only for Unix we check for more than one file name.
4589 * For other systems spaces are considered to be part
4590 * of the file name.
4591 * Only check here if there is no wildcard, otherwise
4592 * ExpandOne() will check for errors. This allows
4593 * ":e `ls ve*.c`" on Unix.
4594 */
4595 if (!has_wildcards)
4596 for (p = eap->arg; *p; ++p)
4597 {
4598 /* skip escaped characters */
4599 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4600 ++p;
4601 else if (vim_iswhite(*p))
4602 {
4603 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4604 return FAIL;
4605 }
4606 }
4607#endif
4608
4609 /*
4610 * Halve the number of backslashes (this is Vi compatible).
4611 * For Unix and OS/2, when wildcards are expanded, this is
4612 * done by ExpandOne() below.
4613 */
4614#if defined(UNIX) || defined(OS2)
4615 if (!has_wildcards)
4616#endif
4617 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619
4620 if (has_wildcards)
4621 {
4622 if (n == 1)
4623 {
4624 /*
4625 * First loop: May expand environment variables. This
4626 * can be done much faster with expand_env() than with
4627 * something else (e.g., calling a shell).
4628 * After expanding environment variables, check again
4629 * if there are still wildcards present.
4630 */
4631 if (vim_strchr(eap->arg, '$') != NULL
4632 || vim_strchr(eap->arg, '~') != NULL)
4633 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004634 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004635 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 has_wildcards = mch_has_wildcard(NameBuff);
4637 p = NameBuff;
4638 }
4639 else
4640 p = NULL;
4641 }
4642 else /* n == 2 */
4643 {
4644 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004645 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
4647 ExpandInit(&xpc);
4648 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004649 if (p_wic)
4650 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004652 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (p == NULL)
4654 return FAIL;
4655 }
4656 if (p != NULL)
4657 {
4658 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4659 p, cmdlinep);
4660 if (n == 2) /* p came from ExpandOne() */
4661 vim_free(p);
4662 }
4663 }
4664 }
4665 }
4666 return OK;
4667}
4668
4669/*
4670 * Replace part of the command line, keeping eap->cmd, eap->arg and
4671 * eap->nextcmd correct.
4672 * "src" points to the part that is to be replaced, of length "srclen".
4673 * "repl" is the replacement string.
4674 * Returns a pointer to the character after the replaced string.
4675 * Returns NULL for failure.
4676 */
4677 static char_u *
4678repl_cmdline(eap, src, srclen, repl, cmdlinep)
4679 exarg_T *eap;
4680 char_u *src;
4681 int srclen;
4682 char_u *repl;
4683 char_u **cmdlinep;
4684{
4685 int len;
4686 int i;
4687 char_u *new_cmdline;
4688
4689 /*
4690 * The new command line is build in new_cmdline[].
4691 * First allocate it.
4692 * Careful: a "+cmd" argument may have been NUL terminated.
4693 */
4694 len = (int)STRLEN(repl);
4695 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004696 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4698 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4699 return NULL; /* out of memory! */
4700
4701 /*
4702 * Copy the stuff before the expanded part.
4703 * Copy the expanded stuff.
4704 * Copy what came after the expanded part.
4705 * Copy the next commands, if there are any.
4706 */
4707 i = (int)(src - *cmdlinep); /* length of part before match */
4708 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 mch_memmove(new_cmdline + i, repl, (size_t)len);
4711 i += len; /* remember the end of the string */
4712 STRCPY(new_cmdline + i, src + srclen);
4713 src = new_cmdline + i; /* remember where to continue */
4714
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004715 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 i = (int)STRLEN(new_cmdline) + 1;
4718 STRCPY(new_cmdline + i, eap->nextcmd);
4719 eap->nextcmd = new_cmdline + i;
4720 }
4721 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4722 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4723 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4724 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4725 vim_free(*cmdlinep);
4726 *cmdlinep = new_cmdline;
4727
4728 return src;
4729}
4730
4731/*
4732 * Check for '|' to separate commands and '"' to start comments.
4733 */
4734 void
4735separate_nextcmd(eap)
4736 exarg_T *eap;
4737{
4738 char_u *p;
4739
Bram Moolenaar86b68352004-12-27 21:59:20 +00004740#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741 p = skip_grep_pat(eap);
4742#else
4743 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004744#endif
4745
4746 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 if (*p == Ctrl_V)
4749 {
4750 if (eap->argt & (USECTRLV | XFILE))
4751 ++p; /* skip CTRL-V and next char */
4752 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004753 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004754 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (*p == NUL) /* stop at NUL after CTRL-V */
4756 break;
4757 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004758
4759#ifdef FEAT_EVAL
4760 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004761 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004762 {
4763 p += 2;
4764 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004765 }
4766#endif
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 /* Check for '"': start of comment or '|': next command */
4769 /* :@" and :*" do not start a comment!
4770 * :redir @" doesn't either. */
4771 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4772 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4773 || p != eap->arg)
4774 && (eap->cmdidx != CMD_redir
4775 || p != eap->arg + 1 || p[-1] != '@'))
4776 || *p == '|' || *p == '\n')
4777 {
4778 /*
4779 * We remove the '\' before the '|', unless USECTRLV is used
4780 * AND 'b' is present in 'cpoptions'.
4781 */
4782 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4783 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4784 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004785 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 --p;
4787 }
4788 else
4789 {
4790 eap->nextcmd = check_nextcmd(p);
4791 *p = NUL;
4792 break;
4793 }
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4798 del_trailing_spaces(eap->arg);
4799}
4800
4801/*
4802 * get + command from ex argument
4803 */
4804 static char_u *
4805getargcmd(argp)
4806 char_u **argp;
4807{
4808 char_u *arg = *argp;
4809 char_u *command = NULL;
4810
4811 if (*arg == '+') /* +[command] */
4812 {
4813 ++arg;
4814 if (vim_isspace(*arg))
4815 command = dollar_command;
4816 else
4817 {
4818 command = arg;
4819 arg = skip_cmd_arg(command, TRUE);
4820 if (*arg != NUL)
4821 *arg++ = NUL; /* terminate command with NUL */
4822 }
4823
4824 arg = skipwhite(arg); /* skip over spaces */
4825 *argp = arg;
4826 }
4827 return command;
4828}
4829
4830/*
4831 * Find end of "+command" argument. Skip over "\ " and "\\".
4832 */
4833 static char_u *
4834skip_cmd_arg(p, rembs)
4835 char_u *p;
4836 int rembs; /* TRUE to halve the number of backslashes */
4837{
4838 while (*p && !vim_isspace(*p))
4839 {
4840 if (*p == '\\' && p[1] != NUL)
4841 {
4842 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004843 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 else
4845 ++p;
4846 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004847 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 return p;
4850}
4851
4852/*
4853 * Get "++opt=arg" argument.
4854 * Return FAIL or OK.
4855 */
4856 static int
4857getargopt(eap)
4858 exarg_T *eap;
4859{
4860 char_u *arg = eap->arg + 2;
4861 int *pp = NULL;
4862#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004863 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u *p;
4865#endif
4866
4867 /* ":edit ++[no]bin[ary] file" */
4868 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4869 {
4870 if (*arg == 'n')
4871 {
4872 arg += 2;
4873 eap->force_bin = FORCE_NOBIN;
4874 }
4875 else
4876 eap->force_bin = FORCE_BIN;
4877 if (!checkforcmd(&arg, "binary", 3))
4878 return FAIL;
4879 eap->arg = skipwhite(arg);
4880 return OK;
4881 }
4882
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004883 /* ":read ++edit file" */
4884 if (STRNCMP(arg, "edit", 4) == 0)
4885 {
4886 eap->read_edit = TRUE;
4887 eap->arg = skipwhite(arg + 4);
4888 return OK;
4889 }
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (STRNCMP(arg, "ff", 2) == 0)
4892 {
4893 arg += 2;
4894 pp = &eap->force_ff;
4895 }
4896 else if (STRNCMP(arg, "fileformat", 10) == 0)
4897 {
4898 arg += 10;
4899 pp = &eap->force_ff;
4900 }
4901#ifdef FEAT_MBYTE
4902 else if (STRNCMP(arg, "enc", 3) == 0)
4903 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004904 if (STRNCMP(arg, "encoding", 8) == 0)
4905 arg += 8;
4906 else
4907 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 pp = &eap->force_enc;
4909 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004910 else if (STRNCMP(arg, "bad", 3) == 0)
4911 {
4912 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004913 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
4916
4917 if (pp == NULL || *arg != '=')
4918 return FAIL;
4919
4920 ++arg;
4921 *pp = (int)(arg - eap->cmd);
4922 arg = skip_cmd_arg(arg, FALSE);
4923 eap->arg = skipwhite(arg);
4924 *arg = NUL;
4925
4926#ifdef FEAT_MBYTE
4927 if (pp == &eap->force_ff)
4928 {
4929#endif
4930 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4931 return FAIL;
4932#ifdef FEAT_MBYTE
4933 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004934 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
4936 /* Make 'fileencoding' lower case. */
4937 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4938 *p = TOLOWER_ASC(*p);
4939 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004940 else
4941 {
4942 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4943 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004944 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004945 if (STRICMP(p, "keep") == 0)
4946 eap->bad_char = BAD_KEEP;
4947 else if (STRICMP(p, "drop") == 0)
4948 eap->bad_char = BAD_DROP;
4949 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4950 eap->bad_char = *p;
4951 else
4952 return FAIL;
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#endif
4955
4956 return OK;
4957}
4958
4959/*
4960 * ":abbreviate" and friends.
4961 */
4962 static void
4963ex_abbreviate(eap)
4964 exarg_T *eap;
4965{
4966 do_exmap(eap, TRUE); /* almost the same as mapping */
4967}
4968
4969/*
4970 * ":map" and friends.
4971 */
4972 static void
4973ex_map(eap)
4974 exarg_T *eap;
4975{
4976 /*
4977 * If we are sourcing .exrc or .vimrc in current directory we
4978 * print the mappings for security reasons.
4979 */
4980 if (secure)
4981 {
4982 secure = 2;
4983 msg_outtrans(eap->cmd);
4984 msg_putchar('\n');
4985 }
4986 do_exmap(eap, FALSE);
4987}
4988
4989/*
4990 * ":unmap" and friends.
4991 */
4992 static void
4993ex_unmap(eap)
4994 exarg_T *eap;
4995{
4996 do_exmap(eap, FALSE);
4997}
4998
4999/*
5000 * ":mapclear" and friends.
5001 */
5002 static void
5003ex_mapclear(eap)
5004 exarg_T *eap;
5005{
5006 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5007}
5008
5009/*
5010 * ":abclear" and friends.
5011 */
5012 static void
5013ex_abclear(eap)
5014 exarg_T *eap;
5015{
5016 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5017}
5018
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005019#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 static void
5021ex_autocmd(eap)
5022 exarg_T *eap;
5023{
5024 /*
5025 * Disallow auto commands from .exrc and .vimrc in current
5026 * directory for security reasons.
5027 */
5028 if (secure)
5029 {
5030 secure = 2;
5031 eap->errmsg = e_curdir;
5032 }
5033 else if (eap->cmdidx == CMD_autocmd)
5034 do_autocmd(eap->arg, eap->forceit);
5035 else
5036 do_augroup(eap->arg, eap->forceit);
5037}
5038
5039/*
5040 * ":doautocmd": Apply the automatic commands to the current buffer.
5041 */
5042 static void
5043ex_doautocmd(eap)
5044 exarg_T *eap;
5045{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005046 char_u *arg = eap->arg;
5047 int call_do_modelines = check_nomodeline(&arg);
5048
5049 (void)do_doautocmd(arg, TRUE);
5050 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5051 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052}
5053#endif
5054
5055#ifdef FEAT_LISTCMDS
5056/*
5057 * :[N]bunload[!] [N] [bufname] unload buffer
5058 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5059 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5060 */
5061 static void
5062ex_bunload(eap)
5063 exarg_T *eap;
5064{
5065 eap->errmsg = do_bufdel(
5066 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5067 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5068 : DOBUF_UNLOAD, eap->arg,
5069 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5070}
5071
5072/*
5073 * :[N]buffer [N] to buffer N
5074 * :[N]sbuffer [N] to buffer N
5075 */
5076 static void
5077ex_buffer(eap)
5078 exarg_T *eap;
5079{
5080 if (*eap->arg)
5081 eap->errmsg = e_trailing;
5082 else
5083 {
5084 if (eap->addr_count == 0) /* default is current buffer */
5085 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5086 else
5087 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5088 }
5089}
5090
5091/*
5092 * :[N]bmodified [N] to next mod. buffer
5093 * :[N]sbmodified [N] to next mod. buffer
5094 */
5095 static void
5096ex_bmodified(eap)
5097 exarg_T *eap;
5098{
5099 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5100}
5101
5102/*
5103 * :[N]bnext [N] to next buffer
5104 * :[N]sbnext [N] split and to next buffer
5105 */
5106 static void
5107ex_bnext(eap)
5108 exarg_T *eap;
5109{
5110 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5111}
5112
5113/*
5114 * :[N]bNext [N] to previous buffer
5115 * :[N]bprevious [N] to previous buffer
5116 * :[N]sbNext [N] split and to previous buffer
5117 * :[N]sbprevious [N] split and to previous buffer
5118 */
5119 static void
5120ex_bprevious(eap)
5121 exarg_T *eap;
5122{
5123 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5124}
5125
5126/*
5127 * :brewind to first buffer
5128 * :bfirst to first buffer
5129 * :sbrewind split and to first buffer
5130 * :sbfirst split and to first buffer
5131 */
5132 static void
5133ex_brewind(eap)
5134 exarg_T *eap;
5135{
5136 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5137}
5138
5139/*
5140 * :blast to last buffer
5141 * :sblast split and to last buffer
5142 */
5143 static void
5144ex_blast(eap)
5145 exarg_T *eap;
5146{
5147 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5148}
5149#endif
5150
5151 int
5152ends_excmd(c)
5153 int c;
5154{
5155 return (c == NUL || c == '|' || c == '"' || c == '\n');
5156}
5157
5158#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5159 || defined(PROTO)
5160/*
5161 * Return the next command, after the first '|' or '\n'.
5162 * Return NULL if not found.
5163 */
5164 char_u *
5165find_nextcmd(p)
5166 char_u *p;
5167{
5168 while (*p != '|' && *p != '\n')
5169 {
5170 if (*p == NUL)
5171 return NULL;
5172 ++p;
5173 }
5174 return (p + 1);
5175}
5176#endif
5177
5178/*
5179 * Check if *p is a separator between Ex commands.
5180 * Return NULL if it isn't, (p + 1) if it is.
5181 */
5182 char_u *
5183check_nextcmd(p)
5184 char_u *p;
5185{
5186 p = skipwhite(p);
5187 if (*p == '|' || *p == '\n')
5188 return (p + 1);
5189 else
5190 return NULL;
5191}
5192
5193/*
5194 * - if there are more files to edit
5195 * - and this is the last window
5196 * - and forceit not used
5197 * - and not repeated twice on a row
5198 * return FAIL and give error message if 'message' TRUE
5199 * return OK otherwise
5200 */
5201 static int
5202check_more(message, forceit)
5203 int message; /* when FALSE check only, no messages */
5204 int forceit;
5205{
5206 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5207
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005208 if (!forceit && only_one_window()
5209 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 if (message)
5212 {
5213#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5214 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5215 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005216 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005219 vim_strncpy(buff,
5220 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005221 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005223 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 _("%d more files to edit. Quit anyway?"), n);
5225 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5226 return OK;
5227 return FAIL;
5228 }
5229#endif
5230 if (n == 1)
5231 EMSG(_("E173: 1 more file to edit"));
5232 else
5233 EMSGN(_("E173: %ld more files to edit"), n);
5234 quitmore = 2; /* next try to quit is allowed */
5235 }
5236 return FAIL;
5237 }
5238 return OK;
5239}
5240
5241#ifdef FEAT_CMDL_COMPL
5242/*
5243 * Function given to ExpandGeneric() to obtain the list of command names.
5244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 char_u *
5246get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005247 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 int idx;
5249{
5250 if (idx >= (int)CMD_SIZE)
5251# ifdef FEAT_USR_CMDS
5252 return get_user_command_name(idx);
5253# else
5254 return NULL;
5255# endif
5256 return cmdnames[idx].cmd_name;
5257}
5258#endif
5259
5260#if defined(FEAT_USR_CMDS) || defined(PROTO)
5261static 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));
5262static void uc_list __ARGS((char_u *name, size_t name_len));
5263static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5264static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5265static 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));
5266
5267 static int
5268uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5269 char_u *name;
5270 size_t name_len;
5271 char_u *rep;
5272 long argt;
5273 long def;
5274 int flags;
5275 int compl;
5276 char_u *compl_arg;
5277 int force;
5278{
5279 ucmd_T *cmd = NULL;
5280 char_u *p;
5281 int i;
5282 int cmp = 1;
5283 char_u *rep_buf = NULL;
5284 garray_T *gap;
5285
Bram Moolenaar9c102382006-05-03 21:26:49 +00005286 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (rep_buf == NULL)
5288 {
5289 /* Can't replace termcodes - try using the string as is */
5290 rep_buf = vim_strsave(rep);
5291
5292 /* Give up if out of memory */
5293 if (rep_buf == NULL)
5294 return FAIL;
5295 }
5296
5297 /* get address of growarray: global or in curbuf */
5298 if (flags & UC_BUFFER)
5299 {
5300 gap = &curbuf->b_ucmds;
5301 if (gap->ga_itemsize == 0)
5302 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5303 }
5304 else
5305 gap = &ucmds;
5306
5307 /* Search for the command in the already defined commands. */
5308 for (i = 0; i < gap->ga_len; ++i)
5309 {
5310 size_t len;
5311
5312 cmd = USER_CMD_GA(gap, i);
5313 len = STRLEN(cmd->uc_name);
5314 cmp = STRNCMP(name, cmd->uc_name, name_len);
5315 if (cmp == 0)
5316 {
5317 if (name_len < len)
5318 cmp = -1;
5319 else if (name_len > len)
5320 cmp = 1;
5321 }
5322
5323 if (cmp == 0)
5324 {
5325 if (!force)
5326 {
5327 EMSG(_("E174: Command already exists: add ! to replace it"));
5328 goto fail;
5329 }
5330
5331 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005332 cmd->uc_rep = NULL;
5333#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5334 vim_free(cmd->uc_compl_arg);
5335 cmd->uc_compl_arg = NULL;
5336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 break;
5338 }
5339
5340 /* Stop as soon as we pass the name to add */
5341 if (cmp < 0)
5342 break;
5343 }
5344
5345 /* Extend the array unless we're replacing an existing command */
5346 if (cmp != 0)
5347 {
5348 if (ga_grow(gap, 1) != OK)
5349 goto fail;
5350 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5351 goto fail;
5352
5353 cmd = USER_CMD_GA(gap, i);
5354 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5355
5356 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 cmd->uc_name = p;
5359 }
5360
5361 cmd->uc_rep = rep_buf;
5362 cmd->uc_argt = argt;
5363 cmd->uc_def = def;
5364 cmd->uc_compl = compl;
5365#ifdef FEAT_EVAL
5366 cmd->uc_scriptID = current_SID;
5367# ifdef FEAT_CMDL_COMPL
5368 cmd->uc_compl_arg = compl_arg;
5369# endif
5370#endif
5371
5372 return OK;
5373
5374fail:
5375 vim_free(rep_buf);
5376#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5377 vim_free(compl_arg);
5378#endif
5379 return FAIL;
5380}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005383#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384/*
5385 * List of names for completion for ":command" with the EXPAND_ flag.
5386 * Must be alphabetical for completion.
5387 */
5388static struct
5389{
5390 int expand;
5391 char *name;
5392} command_complete[] =
5393{
5394 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005395 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005397 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005399 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005400#if defined(FEAT_CSCOPE)
5401 {EXPAND_CSCOPE, "cscope"},
5402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5404 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005405 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406#endif
5407 {EXPAND_DIRECTORIES, "dir"},
5408 {EXPAND_ENV_VARS, "environment"},
5409 {EXPAND_EVENTS, "event"},
5410 {EXPAND_EXPRESSION, "expression"},
5411 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005412 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005413 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {EXPAND_FUNCTIONS, "function"},
5415 {EXPAND_HELP, "help"},
5416 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005417#if defined(FEAT_CMDHIST)
5418 {EXPAND_HISTORY, "history"},
5419#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005420#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005421 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005422 {EXPAND_LOCALES, "locale"},
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {EXPAND_MAPPINGS, "mapping"},
5425 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005426 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005428 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005429#if defined(FEAT_SIGNS)
5430 {EXPAND_SIGN, "sign"},
5431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {EXPAND_TAGS, "tag"},
5433 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005434 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {EXPAND_USER_VARS, "var"},
5436 {0, NULL}
5437};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005440#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 static void
5442uc_list(name, name_len)
5443 char_u *name;
5444 size_t name_len;
5445{
5446 int i, j;
5447 int found = FALSE;
5448 ucmd_T *cmd;
5449 int len;
5450 long a;
5451 garray_T *gap;
5452
5453 gap = &curbuf->b_ucmds;
5454 for (;;)
5455 {
5456 for (i = 0; i < gap->ga_len; ++i)
5457 {
5458 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005459 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461 /* Skip commands which don't match the requested prefix */
5462 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5463 continue;
5464
5465 /* Put out the title first time */
5466 if (!found)
5467 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5468 found = TRUE;
5469 msg_putchar('\n');
5470 if (got_int)
5471 break;
5472
5473 /* Special cases */
5474 msg_putchar(a & BANG ? '!' : ' ');
5475 msg_putchar(a & REGSTR ? '"' : ' ');
5476 msg_putchar(gap != &ucmds ? 'b' : ' ');
5477 msg_putchar(' ');
5478
5479 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5480 len = (int)STRLEN(cmd->uc_name) + 4;
5481
5482 do {
5483 msg_putchar(' ');
5484 ++len;
5485 } while (len < 16);
5486
5487 len = 0;
5488
5489 /* Arguments */
5490 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5491 {
5492 case 0: IObuff[len++] = '0'; break;
5493 case (EXTRA): IObuff[len++] = '*'; break;
5494 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5495 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5496 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5497 }
5498
5499 do {
5500 IObuff[len++] = ' ';
5501 } while (len < 5);
5502
5503 /* Range */
5504 if (a & (RANGE|COUNT))
5505 {
5506 if (a & COUNT)
5507 {
5508 /* -count=N */
5509 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5510 len += (int)STRLEN(IObuff + len);
5511 }
5512 else if (a & DFLALL)
5513 IObuff[len++] = '%';
5514 else if (cmd->uc_def >= 0)
5515 {
5516 /* -range=N */
5517 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5518 len += (int)STRLEN(IObuff + len);
5519 }
5520 else
5521 IObuff[len++] = '.';
5522 }
5523
5524 do {
5525 IObuff[len++] = ' ';
5526 } while (len < 11);
5527
5528 /* Completion */
5529 for (j = 0; command_complete[j].expand != 0; ++j)
5530 if (command_complete[j].expand == cmd->uc_compl)
5531 {
5532 STRCPY(IObuff + len, command_complete[j].name);
5533 len += (int)STRLEN(IObuff + len);
5534 break;
5535 }
5536
5537 do {
5538 IObuff[len++] = ' ';
5539 } while (len < 21);
5540
5541 IObuff[len] = '\0';
5542 msg_outtrans(IObuff);
5543
5544 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005545#ifdef FEAT_EVAL
5546 if (p_verbose > 0)
5547 last_set_msg(cmd->uc_scriptID);
5548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 out_flush();
5550 ui_breakcheck();
5551 if (got_int)
5552 break;
5553 }
5554 if (gap == &ucmds || i < gap->ga_len)
5555 break;
5556 gap = &ucmds;
5557 }
5558
5559 if (!found)
5560 MSG(_("No user-defined commands found"));
5561}
5562
5563 static char_u *
5564uc_fun_cmd()
5565{
5566 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5567 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5568 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5569 0xb9, 0x7f, 0};
5570 int i;
5571
5572 for (i = 0; fcmd[i]; ++i)
5573 IObuff[i] = fcmd[i] - 0x40;
5574 IObuff[i] = 0;
5575 return IObuff;
5576}
5577
5578 static int
5579uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5580 char_u *attr;
5581 size_t len;
5582 long *argt;
5583 long *def;
5584 int *flags;
5585 int *compl;
5586 char_u **compl_arg;
5587{
5588 char_u *p;
5589
5590 if (len == 0)
5591 {
5592 EMSG(_("E175: No attribute specified"));
5593 return FAIL;
5594 }
5595
5596 /* First, try the simple attributes (no arguments) */
5597 if (STRNICMP(attr, "bang", len) == 0)
5598 *argt |= BANG;
5599 else if (STRNICMP(attr, "buffer", len) == 0)
5600 *flags |= UC_BUFFER;
5601 else if (STRNICMP(attr, "register", len) == 0)
5602 *argt |= REGSTR;
5603 else if (STRNICMP(attr, "bar", len) == 0)
5604 *argt |= TRLBAR;
5605 else
5606 {
5607 int i;
5608 char_u *val = NULL;
5609 size_t vallen = 0;
5610 size_t attrlen = len;
5611
5612 /* Look for the attribute name - which is the part before any '=' */
5613 for (i = 0; i < (int)len; ++i)
5614 {
5615 if (attr[i] == '=')
5616 {
5617 val = &attr[i + 1];
5618 vallen = len - i - 1;
5619 attrlen = i;
5620 break;
5621 }
5622 }
5623
5624 if (STRNICMP(attr, "nargs", attrlen) == 0)
5625 {
5626 if (vallen == 1)
5627 {
5628 if (*val == '0')
5629 /* Do nothing - this is the default */;
5630 else if (*val == '1')
5631 *argt |= (EXTRA | NOSPC | NEEDARG);
5632 else if (*val == '*')
5633 *argt |= EXTRA;
5634 else if (*val == '?')
5635 *argt |= (EXTRA | NOSPC);
5636 else if (*val == '+')
5637 *argt |= (EXTRA | NEEDARG);
5638 else
5639 goto wrong_nargs;
5640 }
5641 else
5642 {
5643wrong_nargs:
5644 EMSG(_("E176: Invalid number of arguments"));
5645 return FAIL;
5646 }
5647 }
5648 else if (STRNICMP(attr, "range", attrlen) == 0)
5649 {
5650 *argt |= RANGE;
5651 if (vallen == 1 && *val == '%')
5652 *argt |= DFLALL;
5653 else if (val != NULL)
5654 {
5655 p = val;
5656 if (*def >= 0)
5657 {
5658two_count:
5659 EMSG(_("E177: Count cannot be specified twice"));
5660 return FAIL;
5661 }
5662
5663 *def = getdigits(&p);
5664 *argt |= (ZEROR | NOTADR);
5665
5666 if (p != val + vallen || vallen == 0)
5667 {
5668invalid_count:
5669 EMSG(_("E178: Invalid default value for count"));
5670 return FAIL;
5671 }
5672 }
5673 }
5674 else if (STRNICMP(attr, "count", attrlen) == 0)
5675 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005676 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 if (val != NULL)
5679 {
5680 p = val;
5681 if (*def >= 0)
5682 goto two_count;
5683
5684 *def = getdigits(&p);
5685
5686 if (p != val + vallen)
5687 goto invalid_count;
5688 }
5689
5690 if (*def < 0)
5691 *def = 0;
5692 }
5693 else if (STRNICMP(attr, "complete", attrlen) == 0)
5694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (val == NULL)
5696 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005697 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 return FAIL;
5699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005701 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5702 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705 else
5706 {
5707 char_u ch = attr[len];
5708 attr[len] = '\0';
5709 EMSG2(_("E181: Invalid attribute: %s"), attr);
5710 attr[len] = ch;
5711 return FAIL;
5712 }
5713 }
5714
5715 return OK;
5716}
5717
Bram Moolenaara850a712009-01-28 14:42:59 +00005718/*
5719 * ":command ..."
5720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 static void
5722ex_command(eap)
5723 exarg_T *eap;
5724{
5725 char_u *name;
5726 char_u *end;
5727 char_u *p;
5728 long argt = 0;
5729 long def = -1;
5730 int flags = 0;
5731 int compl = EXPAND_NOTHING;
5732 char_u *compl_arg = NULL;
5733 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005734 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 p = eap->arg;
5737
5738 /* Check for attributes */
5739 while (*p == '-')
5740 {
5741 ++p;
5742 end = skiptowhite(p);
5743 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5744 == FAIL)
5745 return;
5746 p = skipwhite(end);
5747 }
5748
5749 /* Get the name (if any) and skip to the following argument */
5750 name = p;
5751 if (ASCII_ISALPHA(*p))
5752 while (ASCII_ISALNUM(*p))
5753 ++p;
5754 if (!ends_excmd(*p) && !vim_iswhite(*p))
5755 {
5756 EMSG(_("E182: Invalid command name"));
5757 return;
5758 }
5759 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005760 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
5762 /* If there is nothing after the name, and no attributes were specified,
5763 * we are listing commands
5764 */
5765 p = skipwhite(end);
5766 if (!has_attr && ends_excmd(*p))
5767 {
5768 uc_list(name, end - name);
5769 }
5770 else if (!ASCII_ISUPPER(*name))
5771 {
5772 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5773 return;
5774 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005775 else if ((name_len == 1 && *name == 'X')
5776 || (name_len <= 4
5777 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5778 {
5779 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5780 return;
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 else
5783 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5784 eap->forceit);
5785}
5786
5787/*
5788 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 * Clear all user commands, global and for current buffer.
5790 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005791 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005793 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 uc_clear(&ucmds);
5796 uc_clear(&curbuf->b_ucmds);
5797}
5798
5799/*
5800 * Clear all user commands for "gap".
5801 */
5802 void
5803uc_clear(gap)
5804 garray_T *gap;
5805{
5806 int i;
5807 ucmd_T *cmd;
5808
5809 for (i = 0; i < gap->ga_len; ++i)
5810 {
5811 cmd = USER_CMD_GA(gap, i);
5812 vim_free(cmd->uc_name);
5813 vim_free(cmd->uc_rep);
5814# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5815 vim_free(cmd->uc_compl_arg);
5816# endif
5817 }
5818 ga_clear(gap);
5819}
5820
5821 static void
5822ex_delcommand(eap)
5823 exarg_T *eap;
5824{
5825 int i = 0;
5826 ucmd_T *cmd = NULL;
5827 int cmp = -1;
5828 garray_T *gap;
5829
5830 gap = &curbuf->b_ucmds;
5831 for (;;)
5832 {
5833 for (i = 0; i < gap->ga_len; ++i)
5834 {
5835 cmd = USER_CMD_GA(gap, i);
5836 cmp = STRCMP(eap->arg, cmd->uc_name);
5837 if (cmp <= 0)
5838 break;
5839 }
5840 if (gap == &ucmds || cmp == 0)
5841 break;
5842 gap = &ucmds;
5843 }
5844
5845 if (cmp != 0)
5846 {
5847 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5848 return;
5849 }
5850
5851 vim_free(cmd->uc_name);
5852 vim_free(cmd->uc_rep);
5853# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5854 vim_free(cmd->uc_compl_arg);
5855# endif
5856
5857 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
5859 if (i < gap->ga_len)
5860 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5861}
5862
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005863/*
5864 * split and quote args for <f-args>
5865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 static char_u *
5867uc_split_args(arg, lenp)
5868 char_u *arg;
5869 size_t *lenp;
5870{
5871 char_u *buf;
5872 char_u *p;
5873 char_u *q;
5874 int len;
5875
5876 /* Precalculate length */
5877 p = arg;
5878 len = 2; /* Initial and final quotes */
5879
5880 while (*p)
5881 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005882 if (p[0] == '\\' && p[1] == '\\')
5883 {
5884 len += 2;
5885 p += 2;
5886 }
5887 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
5889 len += 1;
5890 p += 2;
5891 }
5892 else if (*p == '\\' || *p == '"')
5893 {
5894 len += 2;
5895 p += 1;
5896 }
5897 else if (vim_iswhite(*p))
5898 {
5899 p = skipwhite(p);
5900 if (*p == NUL)
5901 break;
5902 len += 3; /* "," */
5903 }
5904 else
5905 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005906#ifdef FEAT_MBYTE
5907 int charlen = (*mb_ptr2len)(p);
5908 len += charlen;
5909 p += charlen;
5910#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 ++len;
5912 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915 }
5916
5917 buf = alloc(len + 1);
5918 if (buf == NULL)
5919 {
5920 *lenp = 0;
5921 return buf;
5922 }
5923
5924 p = arg;
5925 q = buf;
5926 *q++ = '"';
5927 while (*p)
5928 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005929 if (p[0] == '\\' && p[1] == '\\')
5930 {
5931 *q++ = '\\';
5932 *q++ = '\\';
5933 p += 2;
5934 }
5935 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
5937 *q++ = p[1];
5938 p += 2;
5939 }
5940 else if (*p == '\\' || *p == '"')
5941 {
5942 *q++ = '\\';
5943 *q++ = *p++;
5944 }
5945 else if (vim_iswhite(*p))
5946 {
5947 p = skipwhite(p);
5948 if (*p == NUL)
5949 break;
5950 *q++ = '"';
5951 *q++ = ',';
5952 *q++ = '"';
5953 }
5954 else
5955 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005956 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958 }
5959 *q++ = '"';
5960 *q = 0;
5961
5962 *lenp = len;
5963 return buf;
5964}
5965
5966/*
5967 * Check for a <> code in a user command.
5968 * "code" points to the '<'. "len" the length of the <> (inclusive).
5969 * "buf" is where the result is to be added.
5970 * "split_buf" points to a buffer used for splitting, caller should free it.
5971 * "split_len" is the length of what "split_buf" contains.
5972 * Returns the length of the replacement, which has been added to "buf".
5973 * Returns -1 if there was no match, and only the "<" has been copied.
5974 */
5975 static size_t
5976uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5977 char_u *code;
5978 size_t len;
5979 char_u *buf;
5980 ucmd_T *cmd; /* the user command we're expanding */
5981 exarg_T *eap; /* ex arguments */
5982 char_u **split_buf;
5983 size_t *split_len;
5984{
5985 size_t result = 0;
5986 char_u *p = code + 1;
5987 size_t l = len - 2;
5988 int quote = 0;
5989 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5990 ct_LT, ct_NONE } type = ct_NONE;
5991
5992 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5993 {
5994 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5995 p += 2;
5996 l -= 2;
5997 }
5998
Bram Moolenaar371d5402006-03-20 21:47:49 +00005999 ++l;
6000 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006002 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006004 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006006 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006008 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006010 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006012 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006014 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 type = ct_REGISTER;
6016
6017 switch (type)
6018 {
6019 case ct_ARGS:
6020 /* Simple case first */
6021 if (*eap->arg == NUL)
6022 {
6023 if (quote == 1)
6024 {
6025 result = 2;
6026 if (buf != NULL)
6027 STRCPY(buf, "''");
6028 }
6029 else
6030 result = 0;
6031 break;
6032 }
6033
6034 /* When specified there is a single argument don't split it.
6035 * Works for ":Cmd %" when % is "a b c". */
6036 if ((eap->argt & NOSPC) && quote == 2)
6037 quote = 1;
6038
6039 switch (quote)
6040 {
6041 case 0: /* No quoting, no splitting */
6042 result = STRLEN(eap->arg);
6043 if (buf != NULL)
6044 STRCPY(buf, eap->arg);
6045 break;
6046 case 1: /* Quote, but don't split */
6047 result = STRLEN(eap->arg) + 2;
6048 for (p = eap->arg; *p; ++p)
6049 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006050#ifdef FEAT_MBYTE
6051 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6052 /* DBCS can contain \ in a trail byte, skip the
6053 * double-byte character. */
6054 ++p;
6055 else
6056#endif
6057 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 ++result;
6059 }
6060
6061 if (buf != NULL)
6062 {
6063 *buf++ = '"';
6064 for (p = eap->arg; *p; ++p)
6065 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006066#ifdef FEAT_MBYTE
6067 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6068 /* DBCS can contain \ in a trail byte, copy the
6069 * double-byte character to avoid escaping. */
6070 *buf++ = *p++;
6071 else
6072#endif
6073 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 *buf++ = '\\';
6075 *buf++ = *p;
6076 }
6077 *buf = '"';
6078 }
6079
6080 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006081 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 /* This is hard, so only do it once, and cache the result */
6083 if (*split_buf == NULL)
6084 *split_buf = uc_split_args(eap->arg, split_len);
6085
6086 result = *split_len;
6087 if (buf != NULL && result != 0)
6088 STRCPY(buf, *split_buf);
6089
6090 break;
6091 }
6092 break;
6093
6094 case ct_BANG:
6095 result = eap->forceit ? 1 : 0;
6096 if (quote)
6097 result += 2;
6098 if (buf != NULL)
6099 {
6100 if (quote)
6101 *buf++ = '"';
6102 if (eap->forceit)
6103 *buf++ = '!';
6104 if (quote)
6105 *buf = '"';
6106 }
6107 break;
6108
6109 case ct_LINE1:
6110 case ct_LINE2:
6111 case ct_COUNT:
6112 {
6113 char num_buf[20];
6114 long num = (type == ct_LINE1) ? eap->line1 :
6115 (type == ct_LINE2) ? eap->line2 :
6116 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6117 size_t num_len;
6118
6119 sprintf(num_buf, "%ld", num);
6120 num_len = STRLEN(num_buf);
6121 result = num_len;
6122
6123 if (quote)
6124 result += 2;
6125
6126 if (buf != NULL)
6127 {
6128 if (quote)
6129 *buf++ = '"';
6130 STRCPY(buf, num_buf);
6131 buf += num_len;
6132 if (quote)
6133 *buf = '"';
6134 }
6135
6136 break;
6137 }
6138
6139 case ct_REGISTER:
6140 result = eap->regname ? 1 : 0;
6141 if (quote)
6142 result += 2;
6143 if (buf != NULL)
6144 {
6145 if (quote)
6146 *buf++ = '\'';
6147 if (eap->regname)
6148 *buf++ = eap->regname;
6149 if (quote)
6150 *buf = '\'';
6151 }
6152 break;
6153
6154 case ct_LT:
6155 result = 1;
6156 if (buf != NULL)
6157 *buf = '<';
6158 break;
6159
6160 default:
6161 /* Not recognized: just copy the '<' and return -1. */
6162 result = (size_t)-1;
6163 if (buf != NULL)
6164 *buf = '<';
6165 break;
6166 }
6167
6168 return result;
6169}
6170
6171 static void
6172do_ucmd(eap)
6173 exarg_T *eap;
6174{
6175 char_u *buf;
6176 char_u *p;
6177 char_u *q;
6178
6179 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006180 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006181 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 size_t len, totlen;
6183
6184 size_t split_len = 0;
6185 char_u *split_buf = NULL;
6186 ucmd_T *cmd;
6187#ifdef FEAT_EVAL
6188 scid_T save_current_SID = current_SID;
6189#endif
6190
6191 if (eap->cmdidx == CMD_USER)
6192 cmd = USER_CMD(eap->useridx);
6193 else
6194 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6195
6196 /*
6197 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006198 * First round: "buf" is NULL, compute length, allocate "buf".
6199 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 */
6201 buf = NULL;
6202 for (;;)
6203 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006204 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006205 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006207
6208 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006210 start = vim_strchr(p, '<');
6211 if (start != NULL)
6212 end = vim_strchr(start + 1, '>');
6213 if (buf != NULL)
6214 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006215 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6216 ;
6217 if (*ksp == K_SPECIAL
6218 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006219 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6220# ifdef FEAT_GUI
6221 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6222# endif
6223 ))
6224 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006225 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006226 * KS_SPECIAL KE_FILLER, like for mappings, but
6227 * do_cmdline() doesn't handle that, so convert it back.
6228 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6229 len = ksp - p;
6230 if (len > 0)
6231 {
6232 mch_memmove(q, p, len);
6233 q += len;
6234 }
6235 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6236 p = ksp + 3;
6237 continue;
6238 }
6239 }
6240
6241 /* break if there no <item> is found */
6242 if (start == NULL || end == NULL)
6243 break;
6244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 /* Include the '>' */
6246 ++end;
6247
6248 /* Take everything up to the '<' */
6249 len = start - p;
6250 if (buf == NULL)
6251 totlen += len;
6252 else
6253 {
6254 mch_memmove(q, p, len);
6255 q += len;
6256 }
6257
6258 len = uc_check_code(start, end - start, q, cmd, eap,
6259 &split_buf, &split_len);
6260 if (len == (size_t)-1)
6261 {
6262 /* no match, continue after '<' */
6263 p = start + 1;
6264 len = 1;
6265 }
6266 else
6267 p = end;
6268 if (buf == NULL)
6269 totlen += len;
6270 else
6271 q += len;
6272 }
6273 if (buf != NULL) /* second time here, finished */
6274 {
6275 STRCPY(q, p);
6276 break;
6277 }
6278
6279 totlen += STRLEN(p); /* Add on the trailing characters */
6280 buf = alloc((unsigned)(totlen + 1));
6281 if (buf == NULL)
6282 {
6283 vim_free(split_buf);
6284 return;
6285 }
6286 }
6287
6288#ifdef FEAT_EVAL
6289 current_SID = cmd->uc_scriptID;
6290#endif
6291 (void)do_cmdline(buf, eap->getline, eap->cookie,
6292 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6293#ifdef FEAT_EVAL
6294 current_SID = save_current_SID;
6295#endif
6296 vim_free(buf);
6297 vim_free(split_buf);
6298}
6299
6300# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6301 static char_u *
6302get_user_command_name(idx)
6303 int idx;
6304{
6305 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6306}
6307
6308/*
6309 * Function given to ExpandGeneric() to obtain the list of user command names.
6310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 char_u *
6312get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006313 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 int idx;
6315{
6316 if (idx < curbuf->b_ucmds.ga_len)
6317 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6318 idx -= curbuf->b_ucmds.ga_len;
6319 if (idx < ucmds.ga_len)
6320 return USER_CMD(idx)->uc_name;
6321 return NULL;
6322}
6323
6324/*
6325 * Function given to ExpandGeneric() to obtain the list of user command
6326 * attributes.
6327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 char_u *
6329get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006330 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 int idx;
6332{
6333 static char *user_cmd_flags[] =
6334 {"bang", "bar", "buffer", "complete", "count",
6335 "nargs", "range", "register"};
6336
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006337 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 return NULL;
6339 return (char_u *)user_cmd_flags[idx];
6340}
6341
6342/*
6343 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 char_u *
6346get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006347 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 int idx;
6349{
6350 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6351
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006352 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return NULL;
6354 return (char_u *)user_cmd_nargs[idx];
6355}
6356
6357/*
6358 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 char_u *
6361get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006362 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 int idx;
6364{
6365 return (char_u *)command_complete[idx].name;
6366}
6367# endif /* FEAT_CMDL_COMPL */
6368
6369#endif /* FEAT_USR_CMDS */
6370
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006371#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6372/*
6373 * Parse a completion argument "value[vallen]".
6374 * The detected completion goes in "*complp", argument type in "*argt".
6375 * When there is an argument, for function and user defined completion, it's
6376 * copied to allocated memory and stored in "*compl_arg".
6377 * Returns FAIL if something is wrong.
6378 */
6379 int
6380parse_compl_arg(value, vallen, complp, argt, compl_arg)
6381 char_u *value;
6382 int vallen;
6383 int *complp;
6384 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006385 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006386{
6387 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006388# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006389 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006390# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006391 int i;
6392 int valend = vallen;
6393
6394 /* Look for any argument part - which is the part after any ',' */
6395 for (i = 0; i < vallen; ++i)
6396 {
6397 if (value[i] == ',')
6398 {
6399 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006400# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006401 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006402# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006403 valend = i;
6404 break;
6405 }
6406 }
6407
6408 for (i = 0; command_complete[i].expand != 0; ++i)
6409 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006410 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006411 && STRNCMP(value, command_complete[i].name, valend) == 0)
6412 {
6413 *complp = command_complete[i].expand;
6414 if (command_complete[i].expand == EXPAND_BUFFERS)
6415 *argt |= BUFNAME;
6416 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6417 || command_complete[i].expand == EXPAND_FILES)
6418 *argt |= XFILE;
6419 break;
6420 }
6421 }
6422
6423 if (command_complete[i].expand == 0)
6424 {
6425 EMSG2(_("E180: Invalid complete value: %s"), value);
6426 return FAIL;
6427 }
6428
6429# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6430 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6431 && arg != NULL)
6432# else
6433 if (arg != NULL)
6434# endif
6435 {
6436 EMSG(_("E468: Completion argument only allowed for custom completion"));
6437 return FAIL;
6438 }
6439
6440# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6441 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6442 && arg == NULL)
6443 {
6444 EMSG(_("E467: Custom completion requires a function argument"));
6445 return FAIL;
6446 }
6447
6448 if (arg != NULL)
6449 *compl_arg = vim_strnsave(arg, (int)arglen);
6450# endif
6451 return OK;
6452}
6453#endif
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 static void
6456ex_colorscheme(eap)
6457 exarg_T *eap;
6458{
Bram Moolenaare6850792010-05-14 15:28:44 +02006459 if (*eap->arg == NUL)
6460 {
6461#ifdef FEAT_EVAL
6462 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6463 char_u *p = NULL;
6464
6465 if (expr != NULL)
6466 {
6467 ++emsg_off;
6468 p = eval_to_string(expr, NULL, FALSE);
6469 --emsg_off;
6470 vim_free(expr);
6471 }
6472 if (p != NULL)
6473 {
6474 MSG(p);
6475 vim_free(p);
6476 }
6477 else
6478 MSG("default");
6479#else
6480 MSG(_("unknown"));
6481#endif
6482 }
6483 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006484 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
6487 static void
6488ex_highlight(eap)
6489 exarg_T *eap;
6490{
6491 if (*eap->arg == NUL && eap->cmd[2] == '!')
6492 MSG(_("Greetings, Vim user!"));
6493 do_highlight(eap->arg, eap->forceit, FALSE);
6494}
6495
6496
6497/*
6498 * Call this function if we thought we were going to exit, but we won't
6499 * (because of an error). May need to restore the terminal mode.
6500 */
6501 void
6502not_exiting()
6503{
6504 exiting = FALSE;
6505 settmode(TMODE_RAW);
6506}
6507
6508/*
6509 * ":quit": quit current window, quit Vim if closed the last window.
6510 */
6511 static void
6512ex_quit(eap)
6513 exarg_T *eap;
6514{
6515#ifdef FEAT_CMDWIN
6516 if (cmdwin_type != 0)
6517 {
6518 cmdwin_result = Ctrl_C;
6519 return;
6520 }
6521#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006522 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006523 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006524 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006525 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006526 return;
6527 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006528#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006529 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006530 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006531 * being closed (can only happen in autocommands). */
6532 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006533 return;
6534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
6536#ifdef FEAT_NETBEANS_INTG
6537 netbeansForcedQuit = eap->forceit;
6538#endif
6539
6540 /*
6541 * If there are more files or windows we won't exit.
6542 */
6543 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6544 exiting = TRUE;
6545 if ((!P_HID(curbuf)
6546 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6547 || check_more(TRUE, eap->forceit) == FAIL
6548 || (only_one_window() && check_changed_any(eap->forceit)))
6549 {
6550 not_exiting();
6551 }
6552 else
6553 {
6554#ifdef FEAT_WINDOWS
6555 if (only_one_window()) /* quit last window */
6556#endif
6557 getout(0);
6558#ifdef FEAT_WINDOWS
6559# ifdef FEAT_GUI
6560 need_mouse_correct = TRUE;
6561# endif
6562 /* close window; may free buffer */
6563 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6564#endif
6565 }
6566}
6567
6568/*
6569 * ":cquit".
6570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 static void
6572ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006573 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 getout(1); /* this does not always pass on the exit code to the Manx
6576 compiler. why? */
6577}
6578
6579/*
6580 * ":qall": try to quit all windows
6581 */
6582 static void
6583ex_quit_all(eap)
6584 exarg_T *eap;
6585{
6586# ifdef FEAT_CMDWIN
6587 if (cmdwin_type != 0)
6588 {
6589 if (eap->forceit)
6590 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6591 else
6592 cmdwin_result = K_XF2;
6593 return;
6594 }
6595# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006596
6597 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006598 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006599 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006600 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006601 return;
6602 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006603#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006604 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6605 /* Refuse to quit when locked or when the buffer in the last window is
6606 * being closed (can only happen in autocommands). */
6607 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006608 return;
6609#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 exiting = TRUE;
6612 if (eap->forceit || !check_changed_any(FALSE))
6613 getout(0);
6614 not_exiting();
6615}
6616
Bram Moolenaard9967712006-03-11 21:18:15 +00006617#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618/*
6619 * ":close": close current window, unless it is the last one
6620 */
6621 static void
6622ex_close(eap)
6623 exarg_T *eap;
6624{
6625# ifdef FEAT_CMDWIN
6626 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006627 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 else
6629# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006630 if (!text_locked()
6631#ifdef FEAT_AUTOCMD
6632 && !curbuf_locked()
6633#endif
6634 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006635 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636}
6637
Bram Moolenaard9967712006-03-11 21:18:15 +00006638# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006639/*
6640 * ":pclose": Close any preview window.
6641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006643ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645{
6646 win_T *win;
6647
6648 for (win = firstwin; win != NULL; win = win->w_next)
6649 if (win->w_p_pvw)
6650 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006651 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006652 break;
6653 }
6654}
Bram Moolenaard9967712006-03-11 21:18:15 +00006655# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656
Bram Moolenaarf740b292006-02-16 22:11:02 +00006657/*
6658 * Close window "win" and take care of handling closing the last window for a
6659 * modified buffer.
6660 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006662ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006663 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006665 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 int need_hide;
6668 buf_T *buf = win->w_buffer;
6669
6670 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006673# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if ((p_confirm || cmdmod.confirm) && p_write)
6675 {
6676 dialog_changed(buf, FALSE);
6677 if (buf_valid(buf) && bufIsChanged(buf))
6678 return;
6679 need_hide = FALSE;
6680 }
6681 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
6684 EMSG(_(e_nowrtmsg));
6685 return;
6686 }
6687 }
6688
Bram Moolenaard9967712006-03-11 21:18:15 +00006689# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006691# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006694 if (tp == NULL)
6695 win_close(win, !need_hide && !P_HID(buf));
6696 else
6697 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 * ":tabclose": close current tab page, unless it is the last one.
6702 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
6704 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006705ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 exarg_T *eap;
6707{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006708 tabpage_T *tp;
6709
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710# ifdef FEAT_CMDWIN
6711 if (cmdwin_type != 0)
6712 cmdwin_result = K_IGNORE;
6713 else
6714# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006715 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006716 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006717 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006719 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006720 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006721 tp = find_tabpage((int)eap->line2);
6722 if (tp == NULL)
6723 {
6724 beep_flush();
6725 return;
6726 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006727 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006728 {
6729 tabpage_close_other(tp, eap->forceit);
6730 return;
6731 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006733 if (!text_locked()
6734#ifdef FEAT_AUTOCMD
6735 && !curbuf_locked()
6736#endif
6737 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006738 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006740}
6741
6742/*
6743 * ":tabonly": close all tab pages except the current one
6744 */
6745 static void
6746ex_tabonly(eap)
6747 exarg_T *eap;
6748{
6749 tabpage_T *tp;
6750 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006751
6752# ifdef FEAT_CMDWIN
6753 if (cmdwin_type != 0)
6754 cmdwin_result = K_IGNORE;
6755 else
6756# endif
6757 if (first_tabpage->tp_next == NULL)
6758 MSG(_("Already only one tab page"));
6759 else
6760 {
6761 /* Repeat this up to a 1000 times, because autocommands may mess
6762 * up the lists. */
6763 for (done = 0; done < 1000; ++done)
6764 {
6765 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6766 if (tp->tp_topframe != topframe)
6767 {
6768 tabpage_close_other(tp, eap->forceit);
6769 /* if we failed to close it quit */
6770 if (valid_tabpage(tp))
6771 done = 1000;
6772 /* start over, "tp" is now invalid */
6773 break;
6774 }
6775 if (first_tabpage->tp_next == NULL)
6776 break;
6777 }
6778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
6781/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006782 * Close the current tab page.
6783 */
6784 void
6785tabpage_close(forceit)
6786 int forceit;
6787{
6788 /* First close all the windows but the current one. If that worked then
6789 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006790 if (lastwin != firstwin)
6791 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006792 if (lastwin == firstwin)
6793 ex_win_close(forceit, curwin, NULL);
6794# ifdef FEAT_GUI
6795 need_mouse_correct = TRUE;
6796# endif
6797}
6798
6799/*
6800 * Close tab page "tp", which is not the current tab page.
6801 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006802 * Also takes care of the tab pages line disappearing when closing the
6803 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006804 */
6805 void
6806tabpage_close_other(tp, forceit)
6807 tabpage_T *tp;
6808 int forceit;
6809{
6810 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006811 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006812 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006813
6814 /* Limit to 1000 windows, autocommands may add a window while we close
6815 * one. OK, so I'm paranoid... */
6816 while (++done < 1000)
6817 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006818 wp = tp->tp_firstwin;
6819 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006820
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006821 /* Autocommands may delete the tab page under our fingers and we may
6822 * fail to close a window with a modified buffer. */
6823 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006824 break;
6825 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006826
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006827 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006828 if (h != tabline_height())
6829 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006830}
6831
6832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 * ":only".
6834 */
6835 static void
6836ex_only(eap)
6837 exarg_T *eap;
6838{
6839# ifdef FEAT_GUI
6840 need_mouse_correct = TRUE;
6841# endif
6842 close_others(TRUE, eap->forceit);
6843}
6844
6845/*
6846 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006847 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006849 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850ex_all(eap)
6851 exarg_T *eap;
6852{
6853 if (eap->addr_count == 0)
6854 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006855 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857#endif /* FEAT_WINDOWS */
6858
6859 static void
6860ex_hide(eap)
6861 exarg_T *eap;
6862{
6863 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6864 eap->errmsg = e_invarg;
6865 else
6866 {
6867 /* ":hide" or ":hide | cmd": hide current window */
6868 eap->nextcmd = check_nextcmd(eap->arg);
6869#ifdef FEAT_WINDOWS
6870 if (!eap->skip)
6871 {
6872# ifdef FEAT_GUI
6873 need_mouse_correct = TRUE;
6874# endif
6875 win_close(curwin, FALSE); /* don't free buffer */
6876 }
6877#endif
6878 }
6879}
6880
6881/*
6882 * ":stop" and ":suspend": Suspend Vim.
6883 */
6884 static void
6885ex_stop(eap)
6886 exarg_T *eap;
6887{
6888 /*
6889 * Disallow suspending for "rvim".
6890 */
6891 if (!check_restricted()
6892#ifdef WIN3264
6893 /*
6894 * Check if external commands are allowed now.
6895 */
6896 && can_end_termcap_mode(TRUE)
6897#endif
6898 )
6899 {
6900 if (!eap->forceit)
6901 autowrite_all();
6902 windgoto((int)Rows - 1, 0);
6903 out_char('\n');
6904 out_flush();
6905 stoptermcap();
6906 out_flush(); /* needed for SUN to restore xterm buffer */
6907#ifdef FEAT_TITLE
6908 mch_restore_title(3); /* restore window titles */
6909#endif
6910 ui_suspend(); /* call machine specific function */
6911#ifdef FEAT_TITLE
6912 maketitle();
6913 resettitle(); /* force updating the title */
6914#endif
6915 starttermcap();
6916 scroll_start(); /* scroll screen before redrawing */
6917 redraw_later_clear();
6918 shell_resized(); /* may have resized window */
6919 }
6920}
6921
6922/*
6923 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6924 */
6925 static void
6926ex_exit(eap)
6927 exarg_T *eap;
6928{
6929#ifdef FEAT_CMDWIN
6930 if (cmdwin_type != 0)
6931 {
6932 cmdwin_result = Ctrl_C;
6933 return;
6934 }
6935#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006936 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006937 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006938 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006939 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006940 return;
6941 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006942#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006943 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6944 /* Refuse to quit when locked or when the buffer in the last window is
6945 * being closed (can only happen in autocommands). */
6946 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006947 return;
6948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
6950 /*
6951 * if more files or windows we won't exit
6952 */
6953 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6954 exiting = TRUE;
6955 if ( ((eap->cmdidx == CMD_wq
6956 || curbufIsChanged())
6957 && do_write(eap) == FAIL)
6958 || check_more(TRUE, eap->forceit) == FAIL
6959 || (only_one_window() && check_changed_any(eap->forceit)))
6960 {
6961 not_exiting();
6962 }
6963 else
6964 {
6965#ifdef FEAT_WINDOWS
6966 if (only_one_window()) /* quit last window, exit Vim */
6967#endif
6968 getout(0);
6969#ifdef FEAT_WINDOWS
6970# ifdef FEAT_GUI
6971 need_mouse_correct = TRUE;
6972# endif
6973 /* quit current window, may free buffer */
6974 win_close(curwin, !P_HID(curwin->w_buffer));
6975#endif
6976 }
6977}
6978
6979/*
6980 * ":print", ":list", ":number".
6981 */
6982 static void
6983ex_print(eap)
6984 exarg_T *eap;
6985{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006986 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6987 EMSG(_(e_emptybuf));
6988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006990 for ( ;!got_int; ui_breakcheck())
6991 {
6992 print_line(eap->line1,
6993 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6994 || (eap->flags & EXFLAG_NR)),
6995 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6996 if (++eap->line1 > eap->line2)
6997 break;
6998 out_flush(); /* show one line at a time */
6999 }
7000 setpcmark();
7001 /* put cursor at last line */
7002 curwin->w_cursor.lnum = eap->line2;
7003 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
7005
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007}
7008
7009#ifdef FEAT_BYTEOFF
7010 static void
7011ex_goto(eap)
7012 exarg_T *eap;
7013{
7014 goto_byte(eap->line2);
7015}
7016#endif
7017
7018/*
7019 * ":shell".
7020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 static void
7022ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007023 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 do_shell(NULL, 0);
7026}
7027
7028#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7029 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007030 || defined(FEAT_GUI_MSWIN) \
7031 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 || defined(PROTO)
7033
7034/*
7035 * Handle a file drop. The code is here because a drop is *nearly* like an
7036 * :args command, but not quite (we have a list of exact filenames, so we
7037 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7038 * code is very similar to :args and hence needs access to a lot of the static
7039 * functions in this file.
7040 *
7041 * The list should be allocated using alloc(), as should each item in the
7042 * list. This function takes over responsibility for freeing the list.
7043 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007044 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7046 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7047 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7048 * file functionality is (currently) not in EMX this is not presently a
7049 * problem.
7050 */
7051 void
7052handle_drop(filec, filev, split)
7053 int filec; /* the number of files dropped */
7054 char_u **filev; /* the list of files dropped */
7055 int split; /* force splitting the window */
7056{
7057 exarg_T ea;
7058 int save_msg_scroll = msg_scroll;
7059
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007060 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007061 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007063#ifdef FEAT_AUTOCMD
7064 if (curbuf_locked())
7065 return;
7066#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007067 /* When the screen is being updated we should not change buffers and
7068 * windows structures, it may cause freed memory to be used. */
7069 if (updating_screen)
7070 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
7072 /* Check whether the current buffer is changed. If so, we will need
7073 * to split the current window or data could be lost.
7074 * We don't need to check if the 'hidden' option is set, as in this
7075 * case the buffer won't be lost.
7076 */
7077 if (!P_HID(curbuf) && !split)
7078 {
7079 ++emsg_off;
7080 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7081 --emsg_off;
7082 }
7083 if (split)
7084 {
7085# ifdef FEAT_WINDOWS
7086 if (win_split(0, 0) == FAIL)
7087 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007088 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
7090 /* When splitting the window, create a new alist. Otherwise the
7091 * existing one is overwritten. */
7092 alist_unlink(curwin->w_alist);
7093 alist_new();
7094# else
7095 return; /* can't split, always fail */
7096# endif
7097 }
7098
7099 /*
7100 * Set up the new argument list.
7101 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007102 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 /*
7105 * Move to the first file.
7106 */
7107 /* Fake up a minimal "next" command for do_argfile() */
7108 vim_memset(&ea, 0, sizeof(ea));
7109 ea.cmd = (char_u *)"next";
7110 do_argfile(&ea, 0);
7111
7112 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7113 * mode that is not needed here. */
7114 need_start_insertmode = FALSE;
7115
7116 /* Restore msg_scroll, otherwise a following command may cause scrolling
7117 * unexpectedly. The screen will be redrawn by the caller, thus
7118 * msg_scroll being set by displaying a message is irrelevant. */
7119 msg_scroll = save_msg_scroll;
7120}
7121#endif
7122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123/*
7124 * Clear an argument list: free all file names and reset it to zero entries.
7125 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007126 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127alist_clear(al)
7128 alist_T *al;
7129{
7130 while (--al->al_ga.ga_len >= 0)
7131 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7132 ga_clear(&al->al_ga);
7133}
7134
7135/*
7136 * Init an argument list.
7137 */
7138 void
7139alist_init(al)
7140 alist_T *al;
7141{
7142 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7143}
7144
7145#if defined(FEAT_WINDOWS) || defined(PROTO)
7146
7147/*
7148 * Remove a reference from an argument list.
7149 * Ignored when the argument list is the global one.
7150 * If the argument list is no longer used by any window, free it.
7151 */
7152 void
7153alist_unlink(al)
7154 alist_T *al;
7155{
7156 if (al != &global_alist && --al->al_refcount <= 0)
7157 {
7158 alist_clear(al);
7159 vim_free(al);
7160 }
7161}
7162
7163# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7164/*
7165 * Create a new argument list and use it for the current window.
7166 */
7167 void
7168alist_new()
7169{
7170 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7171 if (curwin->w_alist == NULL)
7172 {
7173 curwin->w_alist = &global_alist;
7174 ++global_alist.al_refcount;
7175 }
7176 else
7177 {
7178 curwin->w_alist->al_refcount = 1;
7179 alist_init(curwin->w_alist);
7180 }
7181}
7182# endif
7183#endif
7184
7185#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7186/*
7187 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007188 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7189 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 */
7191 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007192alist_expand(fnum_list, fnum_len)
7193 int *fnum_list;
7194 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007197 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 char_u **new_arg_files;
7199 int new_arg_file_count;
7200 char_u *save_p_su = p_su;
7201 int i;
7202
7203 /* Don't use 'suffixes' here. This should work like the shell did the
7204 * expansion. Also, the vimrc file isn't read yet, thus the user
7205 * can't set the options. */
7206 p_su = empty_option;
7207 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7208 if (old_arg_files != NULL)
7209 {
7210 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007211 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7212 old_arg_count = GARGCOUNT;
7213 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007215 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 && new_arg_file_count > 0)
7217 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007218 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7219 TRUE, fnum_list, fnum_len);
7220 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223 p_su = save_p_su;
7224}
7225#endif
7226
7227/*
7228 * Set the argument list for the current window.
7229 * Takes over the allocated files[] and the allocated fnames in it.
7230 */
7231 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007232alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 alist_T *al;
7234 int count;
7235 char_u **files;
7236 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007237 int *fnum_list;
7238 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
7240 int i;
7241
7242 alist_clear(al);
7243 if (ga_grow(&al->al_ga, count) == OK)
7244 {
7245 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007246 {
7247 if (got_int)
7248 {
7249 /* When adding many buffers this can take a long time. Allow
7250 * interrupting here. */
7251 while (i < count)
7252 vim_free(files[i++]);
7253 break;
7254 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007255
7256 /* May set buffer name of a buffer previously used for the
7257 * argument list, so that it's re-used by alist_add. */
7258 if (fnum_list != NULL && i < fnum_len)
7259 buf_set_name(fnum_list[i], files[i]);
7260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007262 ui_breakcheck();
7263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 vim_free(files);
7265 }
7266 else
7267 FreeWild(count, files);
7268#ifdef FEAT_WINDOWS
7269 if (al == &global_alist)
7270#endif
7271 arg_had_last = FALSE;
7272}
7273
7274/*
7275 * Add file "fname" to argument list "al".
7276 * "fname" must have been allocated and "al" must have been checked for room.
7277 */
7278 void
7279alist_add(al, fname, set_fnum)
7280 alist_T *al;
7281 char_u *fname;
7282 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7283{
7284 if (fname == NULL) /* don't add NULL file names */
7285 return;
7286#ifdef BACKSLASH_IN_FILENAME
7287 slash_adjust(fname);
7288#endif
7289 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7290 if (set_fnum > 0)
7291 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7292 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7293 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294}
7295
7296#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7297/*
7298 * Adjust slashes in file names. Called after 'shellslash' was set.
7299 */
7300 void
7301alist_slash_adjust()
7302{
7303 int i;
7304# ifdef FEAT_WINDOWS
7305 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007306 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307# endif
7308
7309 for (i = 0; i < GARGCOUNT; ++i)
7310 if (GARGLIST[i].ae_fname != NULL)
7311 slash_adjust(GARGLIST[i].ae_fname);
7312# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007313 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (wp->w_alist != &global_alist)
7315 for (i = 0; i < WARGCOUNT(wp); ++i)
7316 if (WARGLIST(wp)[i].ae_fname != NULL)
7317 slash_adjust(WARGLIST(wp)[i].ae_fname);
7318# endif
7319}
7320#endif
7321
7322/*
7323 * ":preserve".
7324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 static void
7326ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007327 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007329 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 ml_preserve(curbuf, TRUE);
7331}
7332
7333/*
7334 * ":recover".
7335 */
7336 static void
7337ex_recover(eap)
7338 exarg_T *eap;
7339{
7340 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7341 recoverymode = TRUE;
7342 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7343 && (*eap->arg == NUL
7344 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7345 ml_recover();
7346 recoverymode = FALSE;
7347}
7348
7349/*
7350 * Command modifier used in a wrong way.
7351 */
7352 static void
7353ex_wrongmodifier(eap)
7354 exarg_T *eap;
7355{
7356 eap->errmsg = e_invcmd;
7357}
7358
7359#ifdef FEAT_WINDOWS
7360/*
7361 * :sview [+command] file split window with new file, read-only
7362 * :split [[+command] file] split window with current or new file
7363 * :vsplit [[+command] file] split window vertically with current or new file
7364 * :new [[+command] file] split window with no or new file
7365 * :vnew [[+command] file] split vertically window with no or new file
7366 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007367 *
7368 * :tabedit open new Tab page with empty window
7369 * :tabedit [+command] file open new Tab page and edit "file"
7370 * :tabnew [[+command] file] just like :tabedit
7371 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 */
7373 void
7374ex_splitview(eap)
7375 exarg_T *eap;
7376{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007377 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380# endif
7381# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7387 {
7388 ex_ni(eap);
7389 return;
7390 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007393# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007399 * quickfix windows. But it's OK when doing ":tab split". */
7400 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
7402 if (eap->cmdidx == CMD_split)
7403 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if (eap->cmdidx == CMD_vsplit)
7406 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007412 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7415 FNAME_MESS, TRUE, curbuf->b_ffname);
7416 if (fname == NULL)
7417 goto theend;
7418 eap->arg = fname;
7419 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 && eap->cmdidx != CMD_new)
7430 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007431# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007432 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007433# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007434 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007435# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007436 au_has_group((char_u *)"FileExplorer"))
7437 {
7438 /* No browsing supported but we do have the file explorer:
7439 * Edit the directory. */
7440 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7441 eap->arg = (char_u *)".";
7442 }
7443 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007444# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007445 {
7446 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007448 if (fname == NULL)
7449 goto theend;
7450 eap->arg = fname;
7451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 }
7453 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007456 /*
7457 * Either open new tab page or split the window.
7458 */
7459 if (eap->cmdidx == CMD_tabedit
7460 || eap->cmdidx == CMD_tabfind
7461 || eap->cmdidx == CMD_tabnew)
7462 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007463 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7464 : eap->addr_count == 0 ? 0
7465 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007466 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007467 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007468
7469 /* set the alternate buffer for the window we came from */
7470 if (curwin != old_curwin
7471 && win_valid(old_curwin)
7472 && old_curwin->w_buffer != curbuf
7473 && !cmdmod.keepalt)
7474 old_curwin->w_alt_fnum = curbuf->b_fnum;
7475 }
7476 }
7477 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7479 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007480# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 /* Reset 'scrollbind' when editing another file, but keep it when
7482 * doing ":split" without arguments. */
7483 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007484# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007488 {
7489 RESET_BINDING(curwin);
7490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
7492 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 do_exedit(eap, old_curwin);
7495 }
7496
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007497# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007501# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502theend:
7503 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007506
7507/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007508 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007509 */
7510 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007511tabpage_new()
7512{
7513 exarg_T ea;
7514
7515 vim_memset(&ea, 0, sizeof(ea));
7516 ea.cmdidx = CMD_tabnew;
7517 ea.cmd = (char_u *)"tabn";
7518 ea.arg = (char_u *)"";
7519 ex_splitview(&ea);
7520}
7521
7522/*
7523 * :tabnext command
7524 */
7525 static void
7526ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007527 exarg_T *eap;
7528{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007529 switch (eap->cmdidx)
7530 {
7531 case CMD_tabfirst:
7532 case CMD_tabrewind:
7533 goto_tabpage(1);
7534 break;
7535 case CMD_tablast:
7536 goto_tabpage(9999);
7537 break;
7538 case CMD_tabprevious:
7539 case CMD_tabNext:
7540 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7541 break;
7542 default: /* CMD_tabnext */
7543 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7544 break;
7545 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007546}
7547
7548/*
7549 * :tabmove command
7550 */
7551 static void
7552ex_tabmove(eap)
7553 exarg_T *eap;
7554{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007555 int tab_number = 9999;
7556
7557 if (eap->arg && *eap->arg != NUL)
7558 {
7559 char_u *p = eap->arg;
7560 int relative = 0; /* argument +N/-N means: move N places to the
7561 * right/left relative to the current position. */
7562
7563 if (*eap->arg == '-')
7564 {
7565 relative = -1;
7566 p = eap->arg + 1;
7567 }
7568 else if (*eap->arg == '+')
7569 {
7570 relative = 1;
7571 p = eap->arg + 1;
7572 }
7573 else
7574 p = eap->arg;
7575
7576 if (p == skipdigits(p))
7577 {
7578 /* No numbers as argument. */
7579 eap->errmsg = e_invarg;
7580 return;
7581 }
7582
7583 tab_number = getdigits(&p);
7584 if (relative != 0)
7585 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7586 }
7587 else if (eap->addr_count != 0)
7588 tab_number = eap->line2;
7589
7590 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007591}
7592
7593/*
7594 * :tabs command: List tabs and their contents.
7595 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007596 static void
7597ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007598 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007599{
7600 tabpage_T *tp;
7601 win_T *wp;
7602 int tabcount = 1;
7603
7604 msg_start();
7605 msg_scroll = TRUE;
7606 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7607 {
7608 msg_putchar('\n');
7609 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7610 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7611 out_flush(); /* output one line at a time */
7612 ui_breakcheck();
7613
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007614 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007615 wp = firstwin;
7616 else
7617 wp = tp->tp_firstwin;
7618 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7619 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007620 msg_putchar('\n');
7621 msg_putchar(wp == curwin ? '>' : ' ');
7622 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007623 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7624 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007625 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007626 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007627 else
7628 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7629 IObuff, IOSIZE, TRUE);
7630 msg_outtrans(IObuff);
7631 out_flush(); /* output one line at a time */
7632 ui_breakcheck();
7633 }
7634 }
7635}
7636
7637#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
7639/*
7640 * ":mode": Set screen mode.
7641 * If no argument given, just get the screen size and redraw.
7642 */
7643 static void
7644ex_mode(eap)
7645 exarg_T *eap;
7646{
7647 if (*eap->arg == NUL)
7648 shell_resized();
7649 else
7650 mch_screenmode(eap->arg);
7651}
7652
7653#ifdef FEAT_WINDOWS
7654/*
7655 * ":resize".
7656 * set, increment or decrement current window height
7657 */
7658 static void
7659ex_resize(eap)
7660 exarg_T *eap;
7661{
7662 int n;
7663 win_T *wp = curwin;
7664
7665 if (eap->addr_count > 0)
7666 {
7667 n = eap->line2;
7668 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7669 ;
7670 }
7671
7672#ifdef FEAT_GUI
7673 need_mouse_correct = TRUE;
7674#endif
7675 n = atol((char *)eap->arg);
7676#ifdef FEAT_VERTSPLIT
7677 if (cmdmod.split & WSP_VERT)
7678 {
7679 if (*eap->arg == '-' || *eap->arg == '+')
7680 n += W_WIDTH(curwin);
7681 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7682 n = 9999;
7683 win_setwidth_win((int)n, wp);
7684 }
7685 else
7686#endif
7687 {
7688 if (*eap->arg == '-' || *eap->arg == '+')
7689 n += curwin->w_height;
7690 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7691 n = 9999;
7692 win_setheight_win((int)n, wp);
7693 }
7694}
7695#endif
7696
7697/*
7698 * ":find [+command] <file>" command.
7699 */
7700 static void
7701ex_find(eap)
7702 exarg_T *eap;
7703{
7704#ifdef FEAT_SEARCHPATH
7705 char_u *fname;
7706 int count;
7707
7708 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7709 TRUE, curbuf->b_ffname);
7710 if (eap->addr_count > 0)
7711 {
7712 /* Repeat finding the file "count" times. This matters when it
7713 * appears several times in the path. */
7714 count = eap->line2;
7715 while (fname != NULL && --count > 0)
7716 {
7717 vim_free(fname);
7718 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7719 FALSE, curbuf->b_ffname);
7720 }
7721 }
7722
7723 if (fname != NULL)
7724 {
7725 eap->arg = fname;
7726#endif
7727 do_exedit(eap, NULL);
7728#ifdef FEAT_SEARCHPATH
7729 vim_free(fname);
7730 }
7731#endif
7732}
7733
7734/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007735 * ":open" simulation: for now just work like ":visual".
7736 */
7737 static void
7738ex_open(eap)
7739 exarg_T *eap;
7740{
7741 regmatch_T regmatch;
7742 char_u *p;
7743
7744 curwin->w_cursor.lnum = eap->line2;
7745 beginline(BL_SOL | BL_FIX);
7746 if (*eap->arg == '/')
7747 {
7748 /* ":open /pattern/": put cursor in column found with pattern */
7749 ++eap->arg;
7750 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7751 *p = NUL;
7752 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7753 if (regmatch.regprog != NULL)
7754 {
7755 regmatch.rm_ic = p_ic;
7756 p = ml_get_curline();
7757 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007758 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007759 else
7760 EMSG(_(e_nomatch));
7761 vim_free(regmatch.regprog);
7762 }
7763 /* Move to the NUL, ignore any other arguments. */
7764 eap->arg += STRLEN(eap->arg);
7765 }
7766 check_cursor();
7767
7768 eap->cmdidx = CMD_visual;
7769 do_exedit(eap, NULL);
7770}
7771
7772/*
7773 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 */
7775 static void
7776ex_edit(eap)
7777 exarg_T *eap;
7778{
7779 do_exedit(eap, NULL);
7780}
7781
7782/*
7783 * ":edit <file>" command and alikes.
7784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 void
7786do_exedit(eap, old_curwin)
7787 exarg_T *eap;
7788 win_T *old_curwin; /* curwin before doing a split or NULL */
7789{
7790 int n;
7791#ifdef FEAT_WINDOWS
7792 int need_hide;
7793#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007794 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
7796 /*
7797 * ":vi" command ends Ex mode.
7798 */
7799 if (exmode_active && (eap->cmdidx == CMD_visual
7800 || eap->cmdidx == CMD_view))
7801 {
7802 exmode_active = FALSE;
7803 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007804 {
7805 /* Special case: ":global/pat/visual\NLvi-commands" */
7806 if (global_busy)
7807 {
7808 int rd = RedrawingDisabled;
7809 int nwr = no_wait_return;
7810 int ms = msg_scroll;
7811#ifdef FEAT_GUI
7812 int he = hold_gui_events;
7813#endif
7814
7815 if (eap->nextcmd != NULL)
7816 {
7817 stuffReadbuff(eap->nextcmd);
7818 eap->nextcmd = NULL;
7819 }
7820
7821 if (exmode_was != EXMODE_VIM)
7822 settmode(TMODE_RAW);
7823 RedrawingDisabled = 0;
7824 no_wait_return = 0;
7825 need_wait_return = FALSE;
7826 msg_scroll = 0;
7827#ifdef FEAT_GUI
7828 hold_gui_events = 0;
7829#endif
7830 must_redraw = CLEAR;
7831
7832 main_loop(FALSE, TRUE);
7833
7834 RedrawingDisabled = rd;
7835 no_wait_return = nwr;
7836 msg_scroll = ms;
7837#ifdef FEAT_GUI
7838 hold_gui_events = he;
7839#endif
7840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844
7845 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007846 || eap->cmdidx == CMD_tabnew
7847 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848#ifdef FEAT_VERTSPLIT
7849 || eap->cmdidx == CMD_vnew
7850#endif
7851 ) && *eap->arg == NUL)
7852 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007853 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 setpcmark();
7855 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007856 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7857 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 }
7859 else if ((eap->cmdidx != CMD_split
7860#ifdef FEAT_VERTSPLIT
7861 && eap->cmdidx != CMD_vsplit
7862#endif
7863 )
7864 || *eap->arg != NUL
7865#ifdef FEAT_BROWSE
7866 || cmdmod.browse
7867#endif
7868 )
7869 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007870#ifdef FEAT_AUTOCMD
7871 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7872 * can bring us here, others are stopped earlier. */
7873 if (*eap->arg != NUL && curbuf_locked())
7874 return;
7875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 n = readonlymode;
7877 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7878 readonlymode = TRUE;
7879 else if (eap->cmdidx == CMD_enew)
7880 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7881 empty buffer */
7882 setpcmark();
7883 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7884 NULL, eap,
7885 /* ":edit" goes to first line if Vi compatible */
7886 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7887 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7888 ? ECMD_ONE : eap->do_ecmd_lnum,
7889 (P_HID(curbuf) ? ECMD_HIDE : 0)
7890 + (eap->forceit ? ECMD_FORCEIT : 0)
7891#ifdef FEAT_LISTCMDS
7892 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7893#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007894 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
7896 /* Editing the file failed. If the window was split, close it. */
7897#ifdef FEAT_WINDOWS
7898 if (old_curwin != NULL)
7899 {
7900 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7901 if (!need_hide || P_HID(curbuf))
7902 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007903# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7904 cleanup_T cs;
7905
7906 /* Reset the error/interrupt/exception state here so that
7907 * aborting() returns FALSE when closing a window. */
7908 enter_cleanup(&cs);
7909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910# ifdef FEAT_GUI
7911 need_mouse_correct = TRUE;
7912# endif
7913 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007914
7915# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7916 /* Restore the error/interrupt/exception state if not
7917 * discarded by a new aborting error, interrupt, or
7918 * uncaught exception. */
7919 leave_cleanup(&cs);
7920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 }
7922 }
7923#endif
7924 }
7925 else if (readonlymode && curbuf->b_nwindows == 1)
7926 {
7927 /* When editing an already visited buffer, 'readonly' won't be set
7928 * but the previous value is kept. With ":view" and ":sview" we
7929 * want the file to be readonly, except when another window is
7930 * editing the same buffer. */
7931 curbuf->b_p_ro = TRUE;
7932 }
7933 readonlymode = n;
7934 }
7935 else
7936 {
7937 if (eap->do_ecmd_cmd != NULL)
7938 do_cmdline_cmd(eap->do_ecmd_cmd);
7939#ifdef FEAT_TITLE
7940 n = curwin->w_arg_idx_invalid;
7941#endif
7942 check_arg_idx(curwin);
7943#ifdef FEAT_TITLE
7944 if (n != curwin->w_arg_idx_invalid)
7945 maketitle();
7946#endif
7947 }
7948
7949#ifdef FEAT_WINDOWS
7950 /*
7951 * if ":split file" worked, set alternate file name in old window to new
7952 * file
7953 */
7954 if (old_curwin != NULL
7955 && *eap->arg != NUL
7956 && curwin != old_curwin
7957 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007958 && old_curwin->w_buffer != curbuf
7959 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 old_curwin->w_alt_fnum = curbuf->b_fnum;
7961#endif
7962
7963 ex_no_reprint = TRUE;
7964}
7965
7966#ifndef FEAT_GUI
7967/*
7968 * ":gui" and ":gvim" when there is no GUI.
7969 */
7970 static void
7971ex_nogui(eap)
7972 exarg_T *eap;
7973{
7974 eap->errmsg = e_nogvim;
7975}
7976#endif
7977
7978#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7979 static void
7980ex_tearoff(eap)
7981 exarg_T *eap;
7982{
7983 gui_make_tearoff(eap->arg);
7984}
7985#endif
7986
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007987#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 static void
7989ex_popup(eap)
7990 exarg_T *eap;
7991{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007992 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993}
7994#endif
7995
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 static void
7997ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007998 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999{
8000 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8001 MSG(_("No swap file"));
8002 else
8003 msg(curbuf->b_ml.ml_mfp->mf_fname);
8004}
8005
8006/*
8007 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8008 * offset.
8009 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 static void
8012ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008013 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015#ifdef FEAT_SCROLLBIND
8016 win_T *wp;
8017 long topline;
8018 long y;
8019 linenr_T old_linenr = curwin->w_cursor.lnum;
8020
8021 setpcmark();
8022
8023 /*
8024 * determine max topline
8025 */
8026 if (curwin->w_p_scb)
8027 {
8028 topline = curwin->w_topline;
8029 for (wp = firstwin; wp; wp = wp->w_next)
8030 {
8031 if (wp->w_p_scb && wp->w_buffer)
8032 {
8033 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8034 if (topline > y)
8035 topline = y;
8036 }
8037 }
8038 if (topline < 1)
8039 topline = 1;
8040 }
8041 else
8042 {
8043 topline = 1;
8044 }
8045
8046
8047 /*
8048 * set all scrollbind windows to the same topline
8049 */
8050 wp = curwin;
8051 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8052 {
8053 if (curwin->w_p_scb)
8054 {
8055 y = topline - curwin->w_topline;
8056 if (y > 0)
8057 scrollup(y, TRUE);
8058 else
8059 scrolldown(-y, TRUE);
8060 curwin->w_scbind_pos = topline;
8061 redraw_later(VALID);
8062 cursor_correct();
8063#ifdef FEAT_WINDOWS
8064 curwin->w_redr_status = TRUE;
8065#endif
8066 }
8067 }
8068 curwin = wp;
8069 if (curwin->w_p_scb)
8070 {
8071 did_syncbind = TRUE;
8072 checkpcmark();
8073 if (old_linenr != curwin->w_cursor.lnum)
8074 {
8075 char_u ctrl_o[2];
8076
8077 ctrl_o[0] = Ctrl_O;
8078 ctrl_o[1] = 0;
8079 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8080 }
8081 }
8082#endif
8083}
8084
8085
8086 static void
8087ex_read(eap)
8088 exarg_T *eap;
8089{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008090 int i;
8091 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8092 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093
8094 if (eap->usefilter) /* :r!cmd */
8095 do_bang(1, eap, FALSE, FALSE, TRUE);
8096 else
8097 {
8098 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8099 return;
8100
8101#ifdef FEAT_BROWSE
8102 if (cmdmod.browse)
8103 {
8104 char_u *browseFile;
8105
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008106 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 NULL, NULL, NULL, curbuf);
8108 if (browseFile != NULL)
8109 {
8110 i = readfile(browseFile, NULL,
8111 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8112 vim_free(browseFile);
8113 }
8114 else
8115 i = OK;
8116 }
8117 else
8118#endif
8119 if (*eap->arg == NUL)
8120 {
8121 if (check_fname() == FAIL) /* check for no file name */
8122 return;
8123 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8124 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8125 }
8126 else
8127 {
8128 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8129 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8130 i = readfile(eap->arg, NULL,
8131 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8132
8133 }
8134 if (i == FAIL)
8135 {
8136#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8137 if (!aborting())
8138#endif
8139 EMSG2(_(e_notopen), eap->arg);
8140 }
8141 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008142 {
8143 if (empty && exmode_active)
8144 {
8145 /* Delete the empty line that remains. Historically ex does
8146 * this but vi doesn't. */
8147 if (eap->line2 == 0)
8148 lnum = curbuf->b_ml.ml_line_count;
8149 else
8150 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008151 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008152 {
8153 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008154 if (curwin->w_cursor.lnum > 1
8155 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008156 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008157 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008158 }
8159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 }
8163}
8164
Bram Moolenaarea408852005-06-25 22:49:46 +00008165static char_u *prev_dir = NULL;
8166
8167#if defined(EXITFREE) || defined(PROTO)
8168 void
8169free_cd_dir()
8170{
8171 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008172 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008173
8174 vim_free(globaldir);
8175 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008176}
8177#endif
8178
8179
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180/*
8181 * ":cd", ":lcd", ":chdir" and ":lchdir".
8182 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008183 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184ex_cd(eap)
8185 exarg_T *eap;
8186{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 char_u *new_dir;
8188 char_u *tofree;
8189
8190 new_dir = eap->arg;
8191#if !defined(UNIX) && !defined(VMS)
8192 /* for non-UNIX ":cd" means: print current directory */
8193 if (*new_dir == NUL)
8194 ex_pwd(NULL);
8195 else
8196#endif
8197 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008198#ifdef FEAT_AUTOCMD
8199 if (allbuf_locked())
8200 return;
8201#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008202 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8203 && !eap->forceit)
8204 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008205 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008206 return;
8207 }
8208
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 /* ":cd -": Change to previous directory */
8210 if (STRCMP(new_dir, "-") == 0)
8211 {
8212 if (prev_dir == NULL)
8213 {
8214 EMSG(_("E186: No previous directory"));
8215 return;
8216 }
8217 new_dir = prev_dir;
8218 }
8219
8220 /* Save current directory for next ":cd -" */
8221 tofree = prev_dir;
8222 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8223 prev_dir = vim_strsave(NameBuff);
8224 else
8225 prev_dir = NULL;
8226
8227#if defined(UNIX) || defined(VMS)
8228 /* for UNIX ":cd" means: go to home directory */
8229 if (*new_dir == NUL)
8230 {
8231 /* use NameBuff for home directory name */
8232# ifdef VMS
8233 char_u *p;
8234
8235 p = mch_getenv((char_u *)"SYS$LOGIN");
8236 if (p == NULL || *p == NUL) /* empty is the same as not set */
8237 NameBuff[0] = NUL;
8238 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008239 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240# else
8241 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8242# endif
8243 new_dir = NameBuff;
8244 }
8245#endif
8246 if (new_dir == NULL || vim_chdir(new_dir))
8247 EMSG(_(e_failed));
8248 else
8249 {
8250 vim_free(curwin->w_localdir);
8251 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8252 {
8253 /* If still in global directory, need to remember current
8254 * directory as global directory. */
8255 if (globaldir == NULL && prev_dir != NULL)
8256 globaldir = vim_strsave(prev_dir);
8257 /* Remember this local directory for the window. */
8258 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8259 curwin->w_localdir = vim_strsave(NameBuff);
8260 }
8261 else
8262 {
8263 /* We are now in the global directory, no need to remember its
8264 * name. */
8265 vim_free(globaldir);
8266 globaldir = NULL;
8267 curwin->w_localdir = NULL;
8268 }
8269
8270 shorten_fnames(TRUE);
8271
8272 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008273 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 ex_pwd(eap);
8275 }
8276 vim_free(tofree);
8277 }
8278}
8279
8280/*
8281 * ":pwd".
8282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 static void
8284ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008285 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
8287 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8288 {
8289#ifdef BACKSLASH_IN_FILENAME
8290 slash_adjust(NameBuff);
8291#endif
8292 msg(NameBuff);
8293 }
8294 else
8295 EMSG(_("E187: Unknown"));
8296}
8297
8298/*
8299 * ":=".
8300 */
8301 static void
8302ex_equal(eap)
8303 exarg_T *eap;
8304{
8305 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307}
8308
8309 static void
8310ex_sleep(eap)
8311 exarg_T *eap;
8312{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008313 int n;
8314 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315
8316 if (cursor_valid())
8317 {
8318 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8319 if (n >= 0)
8320 windgoto((int)n, curwin->w_wcol);
8321 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008322
8323 len = eap->line2;
8324 switch (*eap->arg)
8325 {
8326 case 'm': break;
8327 case NUL: len *= 1000L; break;
8328 default: EMSG2(_(e_invarg2), eap->arg); return;
8329 }
8330 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333/*
8334 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8335 */
8336 void
8337do_sleep(msec)
8338 long msec;
8339{
8340 long done;
8341
8342 cursor_on();
8343 out_flush();
8344 for (done = 0; !got_int && done < msec; done += 1000L)
8345 {
8346 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8347 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008348#ifdef FEAT_NETBEANS_INTG
8349 /* Process the netbeans messages that may have been received in the
8350 * call to ui_breakcheck() when the GUI is in use. This may occur when
8351 * running a test case. */
8352 netbeans_parse_messages();
8353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 }
8355}
8356
8357 static void
8358do_exmap(eap, isabbrev)
8359 exarg_T *eap;
8360 int isabbrev;
8361{
8362 int mode;
8363 char_u *cmdp;
8364
8365 cmdp = eap->cmd;
8366 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8367
8368 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8369 eap->arg, mode, isabbrev))
8370 {
8371 case 1: EMSG(_(e_invarg));
8372 break;
8373 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8374 break;
8375 }
8376}
8377
8378/*
8379 * ":winsize" command (obsolete).
8380 */
8381 static void
8382ex_winsize(eap)
8383 exarg_T *eap;
8384{
8385 int w, h;
8386 char_u *arg = eap->arg;
8387 char_u *p;
8388
8389 w = getdigits(&arg);
8390 arg = skipwhite(arg);
8391 p = arg;
8392 h = getdigits(&arg);
8393 if (*p != NUL && *arg == NUL)
8394 set_shellsize(w, h, TRUE);
8395 else
8396 EMSG(_("E465: :winsize requires two number arguments"));
8397}
8398
8399#ifdef FEAT_WINDOWS
8400 static void
8401ex_wincmd(eap)
8402 exarg_T *eap;
8403{
8404 int xchar = NUL;
8405 char_u *p;
8406
8407 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8408 {
8409 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8410 if (eap->arg[1] == NUL)
8411 {
8412 EMSG(_(e_invarg));
8413 return;
8414 }
8415 xchar = eap->arg[1];
8416 p = eap->arg + 2;
8417 }
8418 else
8419 p = eap->arg + 1;
8420
8421 eap->nextcmd = check_nextcmd(p);
8422 p = skipwhite(p);
8423 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8424 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008425 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {
8427 /* Pass flags on for ":vertical wincmd ]". */
8428 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008429 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8431 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008432 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
8434}
8435#endif
8436
Bram Moolenaar843ee412004-06-30 16:16:41 +00008437#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438/*
8439 * ":winpos".
8440 */
8441 static void
8442ex_winpos(eap)
8443 exarg_T *eap;
8444{
8445 int x, y;
8446 char_u *arg = eap->arg;
8447 char_u *p;
8448
8449 if (*arg == NUL)
8450 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008451# if defined(FEAT_GUI) || defined(MSWIN)
8452# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008454# else
8455 if (mch_get_winpos(&x, &y) != FAIL)
8456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8459 msg(IObuff);
8460 }
8461 else
8462# endif
8463 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8464 }
8465 else
8466 {
8467 x = getdigits(&arg);
8468 arg = skipwhite(arg);
8469 p = arg;
8470 y = getdigits(&arg);
8471 if (*p == NUL || *arg != NUL)
8472 {
8473 EMSG(_("E466: :winpos requires two number arguments"));
8474 return;
8475 }
8476# ifdef FEAT_GUI
8477 if (gui.in_use)
8478 gui_mch_set_winpos(x, y);
8479 else if (gui.starting)
8480 {
8481 /* Remember the coordinates for when the window is opened. */
8482 gui_win_x = x;
8483 gui_win_y = y;
8484 }
8485# ifdef HAVE_TGETENT
8486 else
8487# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008488# else
8489# ifdef MSWIN
8490 mch_set_winpos(x, y);
8491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492# endif
8493# ifdef HAVE_TGETENT
8494 if (*T_CWP)
8495 term_set_winpos(x, y);
8496# endif
8497 }
8498}
8499#endif
8500
8501/*
8502 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8503 */
8504 static void
8505ex_operators(eap)
8506 exarg_T *eap;
8507{
8508 oparg_T oa;
8509
8510 clear_oparg(&oa);
8511 oa.regname = eap->regname;
8512 oa.start.lnum = eap->line1;
8513 oa.end.lnum = eap->line2;
8514 oa.line_count = eap->line2 - eap->line1 + 1;
8515 oa.motion_type = MLINE;
8516#ifdef FEAT_VIRTUALEDIT
8517 virtual_op = FALSE;
8518#endif
8519 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8520 {
8521 setpcmark();
8522 curwin->w_cursor.lnum = eap->line1;
8523 beginline(BL_SOL | BL_FIX);
8524 }
8525
8526 switch (eap->cmdidx)
8527 {
8528 case CMD_delete:
8529 oa.op_type = OP_DELETE;
8530 op_delete(&oa);
8531 break;
8532
8533 case CMD_yank:
8534 oa.op_type = OP_YANK;
8535 (void)op_yank(&oa, FALSE, TRUE);
8536 break;
8537
8538 default: /* CMD_rshift or CMD_lshift */
8539 if ((eap->cmdidx == CMD_rshift)
8540#ifdef FEAT_RIGHTLEFT
8541 ^ curwin->w_p_rl
8542#endif
8543 )
8544 oa.op_type = OP_RSHIFT;
8545 else
8546 oa.op_type = OP_LSHIFT;
8547 op_shift(&oa, FALSE, eap->amount);
8548 break;
8549 }
8550#ifdef FEAT_VIRTUALEDIT
8551 virtual_op = MAYBE;
8552#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008553 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554}
8555
8556/*
8557 * ":put".
8558 */
8559 static void
8560ex_put(eap)
8561 exarg_T *eap;
8562{
8563 /* ":0put" works like ":1put!". */
8564 if (eap->line2 == 0)
8565 {
8566 eap->line2 = 1;
8567 eap->forceit = TRUE;
8568 }
8569 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008570 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8571 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572}
8573
8574/*
8575 * Handle ":copy" and ":move".
8576 */
8577 static void
8578ex_copymove(eap)
8579 exarg_T *eap;
8580{
8581 long n;
8582
8583 n = get_address(&eap->arg, FALSE, FALSE);
8584 if (eap->arg == NULL) /* error detected */
8585 {
8586 eap->nextcmd = NULL;
8587 return;
8588 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008589 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
8591 /*
8592 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8593 */
8594 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8595 {
8596 EMSG(_(e_invaddr));
8597 return;
8598 }
8599
8600 if (eap->cmdidx == CMD_move)
8601 {
8602 if (do_move(eap->line1, eap->line2, n) == FAIL)
8603 return;
8604 }
8605 else
8606 ex_copy(eap->line1, eap->line2, n);
8607 u_clearline();
8608 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008609 ex_may_print(eap);
8610}
8611
8612/*
8613 * Print the current line if flags were given to the Ex command.
8614 */
8615 static void
8616ex_may_print(eap)
8617 exarg_T *eap;
8618{
8619 if (eap->flags != 0)
8620 {
8621 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8622 (eap->flags & EXFLAG_LIST));
8623 ex_no_reprint = TRUE;
8624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625}
8626
8627/*
8628 * ":smagic" and ":snomagic".
8629 */
8630 static void
8631ex_submagic(eap)
8632 exarg_T *eap;
8633{
8634 int magic_save = p_magic;
8635
8636 p_magic = (eap->cmdidx == CMD_smagic);
8637 do_sub(eap);
8638 p_magic = magic_save;
8639}
8640
8641/*
8642 * ":join".
8643 */
8644 static void
8645ex_join(eap)
8646 exarg_T *eap;
8647{
8648 curwin->w_cursor.lnum = eap->line1;
8649 if (eap->line1 == eap->line2)
8650 {
8651 if (eap->addr_count >= 2) /* :2,2join does nothing */
8652 return;
8653 if (eap->line2 == curbuf->b_ml.ml_line_count)
8654 {
8655 beep_flush();
8656 return;
8657 }
8658 ++eap->line2;
8659 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008660 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663}
8664
8665/*
8666 * ":[addr]@r" or ":[addr]*r": execute register
8667 */
8668 static void
8669ex_at(eap)
8670 exarg_T *eap;
8671{
8672 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008673 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
8675 curwin->w_cursor.lnum = eap->line2;
8676
8677#ifdef USE_ON_FLY_SCROLL
8678 dont_scroll = TRUE; /* disallow scrolling here */
8679#endif
8680
8681 /* get the register name. No name means to use the previous one */
8682 c = *eap->arg;
8683 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8684 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008685 /* Put the register in the typeahead buffer with the "silent" flag. */
8686 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8687 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 else
8692 {
8693 int save_efr = exec_from_reg;
8694
8695 exec_from_reg = TRUE;
8696
8697 /*
8698 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008699 * Continue until the stuff buffer is empty and all added characters
8700 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008702 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8704
8705 exec_from_reg = save_efr;
8706 }
8707}
8708
8709/*
8710 * ":!".
8711 */
8712 static void
8713ex_bang(eap)
8714 exarg_T *eap;
8715{
8716 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8717}
8718
8719/*
8720 * ":undo".
8721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 static void
8723ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008724 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008726 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008727 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008728 else
8729 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730}
8731
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008732#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008733 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008734ex_wundo(eap)
8735 exarg_T *eap;
8736{
8737 char_u hash[UNDO_HASH_SIZE];
8738
8739 u_compute_hash(hash);
8740 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8741}
8742
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008743 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008744ex_rundo(eap)
8745 exarg_T *eap;
8746{
8747 char_u hash[UNDO_HASH_SIZE];
8748
8749 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008750 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008751}
8752#endif
8753
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754/*
8755 * ":redo".
8756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 static void
8758ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008759 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 u_redo(1);
8762}
8763
8764/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008765 * ":earlier" and ":later".
8766 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008767 static void
8768ex_later(eap)
8769 exarg_T *eap;
8770{
8771 long count = 0;
8772 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008773 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008774 char_u *p = eap->arg;
8775
8776 if (*p == NUL)
8777 count = 1;
8778 else if (isdigit(*p))
8779 {
8780 count = getdigits(&p);
8781 switch (*p)
8782 {
8783 case 's': ++p; sec = TRUE; break;
8784 case 'm': ++p; sec = TRUE; count *= 60; break;
8785 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008786 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8787 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008788 }
8789 }
8790
8791 if (*p != NUL)
8792 EMSG2(_(e_invarg2), eap->arg);
8793 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008794 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8795 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008796}
8797
8798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 * ":redir": start/stop redirection.
8800 */
8801 static void
8802ex_redir(eap)
8803 exarg_T *eap;
8804{
8805 char *mode;
8806 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008807 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
8809 if (STRICMP(eap->arg, "END") == 0)
8810 close_redir();
8811 else
8812 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008813 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008815 ++arg;
8816 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008818 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 mode = "a";
8820 }
8821 else
8822 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008823 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
8825 close_redir();
8826
8827 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008828 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (fname == NULL)
8830 return;
8831#ifdef FEAT_BROWSE
8832 if (cmdmod.browse)
8833 {
8834 char_u *browseFile;
8835
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008836 browseFile = do_browse(BROWSE_SAVE,
8837 (char_u *)_("Save Redirection"),
8838 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (browseFile == NULL)
8840 return; /* operation cancelled */
8841 vim_free(fname);
8842 fname = browseFile;
8843 eap->forceit = TRUE; /* since dialog already asked */
8844 }
8845#endif
8846
8847 redir_fd = open_exfile(fname, eap->forceit, mode);
8848 vim_free(fname);
8849 }
8850#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008851 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {
8853 /* redirect to a register a-z (resp. A-Z for appending) */
8854 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008855 ++arg;
8856 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008858 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008859 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008861 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008863 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008864 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008865 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008868 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008869 if (*arg == '>')
8870 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008871 /* Make register empty when not using @A-@Z and the
8872 * command is valid. */
8873 if (*arg == NUL && !isupper(redir_reg))
8874 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008876 }
8877 if (*arg != NUL)
8878 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008879 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008880 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008881 }
8882 }
8883 else if (*arg == '=' && arg[1] == '>')
8884 {
8885 int append;
8886
8887 /* redirect to a variable */
8888 close_redir();
8889 arg += 2;
8890
8891 if (*arg == '>')
8892 {
8893 ++arg;
8894 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
8896 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008897 append = FALSE;
8898
8899 if (var_redir_start(skipwhite(arg), append) == OK)
8900 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
8902#endif
8903
8904 /* TODO: redirect to a buffer */
8905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008907 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008909
8910 /* Make sure redirection is not off. Can happen for cmdline completion
8911 * that indirectly invokes a command to catch its output. */
8912 if (redir_fd != NULL
8913#ifdef FEAT_EVAL
8914 || redir_reg || redir_vname
8915#endif
8916 )
8917 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
8919
8920/*
8921 * ":redraw": force redraw
8922 */
8923 static void
8924ex_redraw(eap)
8925 exarg_T *eap;
8926{
8927 int r = RedrawingDisabled;
8928 int p = p_lz;
8929
8930 RedrawingDisabled = 0;
8931 p_lz = FALSE;
8932 update_topline();
8933 update_screen(eap->forceit ? CLEAR :
8934#ifdef FEAT_VISUAL
8935 VIsual_active ? INVERTED :
8936#endif
8937 0);
8938#ifdef FEAT_TITLE
8939 if (need_maketitle)
8940 maketitle();
8941#endif
8942 RedrawingDisabled = r;
8943 p_lz = p;
8944
8945 /* Reset msg_didout, so that a message that's there is overwritten. */
8946 msg_didout = FALSE;
8947 msg_col = 0;
8948
8949 out_flush();
8950}
8951
8952/*
8953 * ":redrawstatus": force redraw of status line(s)
8954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 static void
8956ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008957 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959#if defined(FEAT_WINDOWS)
8960 int r = RedrawingDisabled;
8961 int p = p_lz;
8962
8963 RedrawingDisabled = 0;
8964 p_lz = FALSE;
8965 if (eap->forceit)
8966 status_redraw_all();
8967 else
8968 status_redraw_curbuf();
8969 update_screen(
8970# ifdef FEAT_VISUAL
8971 VIsual_active ? INVERTED :
8972# endif
8973 0);
8974 RedrawingDisabled = r;
8975 p_lz = p;
8976 out_flush();
8977#endif
8978}
8979
8980 static void
8981close_redir()
8982{
8983 if (redir_fd != NULL)
8984 {
8985 fclose(redir_fd);
8986 redir_fd = NULL;
8987 }
8988#ifdef FEAT_EVAL
8989 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008990 if (redir_vname)
8991 {
8992 var_redir_stop();
8993 redir_vname = 0;
8994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#endif
8996}
8997
8998#if defined(FEAT_SESSION) && defined(USE_CRNL)
8999# define MKSESSION_NL
9000static int mksession_nl = FALSE; /* use NL only in put_eol() */
9001#endif
9002
9003/*
9004 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9005 */
9006 static void
9007ex_mkrc(eap)
9008 exarg_T *eap;
9009{
9010 FILE *fd;
9011 int failed = FALSE;
9012 char_u *fname;
9013#ifdef FEAT_BROWSE
9014 char_u *browseFile = NULL;
9015#endif
9016#ifdef FEAT_SESSION
9017 int view_session = FALSE;
9018 int using_vdir = FALSE; /* using 'viewdir'? */
9019 char_u *viewFile = NULL;
9020 unsigned *flagp;
9021#endif
9022
9023 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9024 {
9025#ifdef FEAT_SESSION
9026 view_session = TRUE;
9027#else
9028 ex_ni(eap);
9029 return;
9030#endif
9031 }
9032
9033#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009034 /* Use the short file name until ":lcd" is used. We also don't use the
9035 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009036 did_lcd = FALSE;
9037
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9039 if (eap->cmdidx == CMD_mkview
9040 && (*eap->arg == NUL
9041 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9042 {
9043 eap->forceit = TRUE;
9044 fname = get_view_file(*eap->arg);
9045 if (fname == NULL)
9046 return;
9047 viewFile = fname;
9048 using_vdir = TRUE;
9049 }
9050 else
9051#endif
9052 if (*eap->arg != NUL)
9053 fname = eap->arg;
9054 else if (eap->cmdidx == CMD_mkvimrc)
9055 fname = (char_u *)VIMRC_FILE;
9056#ifdef FEAT_SESSION
9057 else if (eap->cmdidx == CMD_mksession)
9058 fname = (char_u *)SESSION_FILE;
9059#endif
9060 else
9061 fname = (char_u *)EXRC_FILE;
9062
9063#ifdef FEAT_BROWSE
9064 if (cmdmod.browse)
9065 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009066 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067# ifdef FEAT_SESSION
9068 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9069 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9070# endif
9071 (char_u *)_("Save Setup"),
9072 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9073 if (browseFile == NULL)
9074 goto theend;
9075 fname = browseFile;
9076 eap->forceit = TRUE; /* since dialog already asked */
9077 }
9078#endif
9079
9080#if defined(FEAT_SESSION) && defined(vim_mkdir)
9081 /* When using 'viewdir' may have to create the directory. */
9082 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009083 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084#endif
9085
9086 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9087 if (fd != NULL)
9088 {
9089#ifdef FEAT_SESSION
9090 if (eap->cmdidx == CMD_mkview)
9091 flagp = &vop_flags;
9092 else
9093 flagp = &ssop_flags;
9094#endif
9095
9096#ifdef MKSESSION_NL
9097 /* "unix" in 'sessionoptions': use NL line separator */
9098 if (view_session && (*flagp & SSOP_UNIX))
9099 mksession_nl = TRUE;
9100#endif
9101
9102 /* Write the version command for :mkvimrc */
9103 if (eap->cmdidx == CMD_mkvimrc)
9104 (void)put_line(fd, "version 6.0");
9105
9106#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009107 if (eap->cmdidx == CMD_mksession)
9108 {
9109 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9110 failed = TRUE;
9111 }
9112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 if (eap->cmdidx != CMD_mkview)
9114#endif
9115 {
9116 /* Write setting 'compatible' first, because it has side effects.
9117 * For that same reason only do it when needed. */
9118 if (p_cp)
9119 (void)put_line(fd, "if !&cp | set cp | endif");
9120 else
9121 (void)put_line(fd, "if &cp | set nocp | endif");
9122 }
9123
9124#ifdef FEAT_SESSION
9125 if (!view_session
9126 || (eap->cmdidx == CMD_mksession
9127 && (*flagp & SSOP_OPTIONS)))
9128#endif
9129 failed |= (makemap(fd, NULL) == FAIL
9130 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9131
9132#ifdef FEAT_SESSION
9133 if (!failed && view_session)
9134 {
9135 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9136 failed = TRUE;
9137 if (eap->cmdidx == CMD_mksession)
9138 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009139 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
Bram Moolenaard9462e32011-04-11 21:35:11 +02009141 dirnow = alloc(MAXPATHL);
9142 if (dirnow == NULL)
9143 failed = TRUE;
9144 else
9145 {
9146 /*
9147 * Change to session file's dir.
9148 */
9149 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009151 *dirnow = NUL;
9152 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9153 {
9154 if (vim_chdirfile(fname) == OK)
9155 shorten_fnames(TRUE);
9156 }
9157 else if (*dirnow != NUL
9158 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9159 {
9160 if (mch_chdir((char *)globaldir) == 0)
9161 shorten_fnames(TRUE);
9162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163
Bram Moolenaard9462e32011-04-11 21:35:11 +02009164 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165
Bram Moolenaard9462e32011-04-11 21:35:11 +02009166 /* restore original dir */
9167 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009169 {
9170 if (mch_chdir((char *)dirnow) != 0)
9171 EMSG(_(e_prev_dir));
9172 shorten_fnames(TRUE);
9173 }
9174 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
9176 }
9177 else
9178 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009179 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9180 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
9182 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9183 == FAIL)
9184 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009185 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9186 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009187 if (eap->cmdidx == CMD_mksession)
9188 {
9189 if (put_line(fd, "unlet SessionLoad") == FAIL)
9190 failed = TRUE;
9191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
9193#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009194 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9195 failed = TRUE;
9196
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 failed |= fclose(fd);
9198
9199 if (failed)
9200 EMSG(_(e_write));
9201#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9202 else if (eap->cmdidx == CMD_mksession)
9203 {
9204 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009205 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
Bram Moolenaard9462e32011-04-11 21:35:11 +02009207 tbuf = alloc(MAXPATHL);
9208 if (tbuf != NULL)
9209 {
9210 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9211 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9212 vim_free(tbuf);
9213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
9215#endif
9216#ifdef MKSESSION_NL
9217 mksession_nl = FALSE;
9218#endif
9219 }
9220
9221#ifdef FEAT_BROWSE
9222theend:
9223 vim_free(browseFile);
9224#endif
9225#ifdef FEAT_SESSION
9226 vim_free(viewFile);
9227#endif
9228}
9229
Bram Moolenaardf177f62005-02-22 08:39:57 +00009230#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9231 || defined(PROTO)
9232 int
9233vim_mkdir_emsg(name, prot)
9234 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009235 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009236{
9237 if (vim_mkdir(name, prot) != 0)
9238 {
9239 EMSG2(_("E739: Cannot create directory: %s"), name);
9240 return FAIL;
9241 }
9242 return OK;
9243}
9244#endif
9245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246/*
9247 * Open a file for writing for an Ex command, with some checks.
9248 * Return file descriptor, or NULL on failure.
9249 */
9250 FILE *
9251open_exfile(fname, forceit, mode)
9252 char_u *fname;
9253 int forceit;
9254 char *mode; /* "w" for create new file or "a" for append */
9255{
9256 FILE *fd;
9257
9258#ifdef UNIX
9259 /* with Unix it is possible to open a directory */
9260 if (mch_isdir(fname))
9261 {
9262 EMSG2(_(e_isadir2), fname);
9263 return NULL;
9264 }
9265#endif
9266 if (!forceit && *mode != 'a' && vim_fexists(fname))
9267 {
9268 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9269 return NULL;
9270 }
9271
9272 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9273 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9274
9275 return fd;
9276}
9277
9278/*
9279 * ":mark" and ":k".
9280 */
9281 static void
9282ex_mark(eap)
9283 exarg_T *eap;
9284{
9285 pos_T pos;
9286
9287 if (*eap->arg == NUL) /* No argument? */
9288 EMSG(_(e_argreq));
9289 else if (eap->arg[1] != NUL) /* more than one character? */
9290 EMSG(_(e_trailing));
9291 else
9292 {
9293 pos = curwin->w_cursor; /* save curwin->w_cursor */
9294 curwin->w_cursor.lnum = eap->line2;
9295 beginline(BL_WHITE | BL_FIX);
9296 if (setmark(*eap->arg) == FAIL) /* set mark */
9297 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9298 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9299 }
9300}
9301
9302/*
9303 * Update w_topline, w_leftcol and the cursor position.
9304 */
9305 void
9306update_topline_cursor()
9307{
9308 check_cursor(); /* put cursor on valid line */
9309 update_topline();
9310 if (!curwin->w_p_wrap)
9311 validate_cursor();
9312 update_curswant();
9313}
9314
9315#ifdef FEAT_EX_EXTRA
9316/*
9317 * ":normal[!] {commands}": Execute normal mode commands.
9318 */
9319 static void
9320ex_normal(eap)
9321 exarg_T *eap;
9322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 int save_msg_scroll = msg_scroll;
9324 int save_restart_edit = restart_edit;
9325 int save_msg_didout = msg_didout;
9326 int save_State = State;
9327 tasave_T tabuf;
9328 int save_insertmode = p_im;
9329 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009330 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331#ifdef FEAT_MBYTE
9332 char_u *arg = NULL;
9333 int l;
9334 char_u *p;
9335#endif
9336
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009337 if (ex_normal_lock > 0)
9338 {
9339 EMSG(_(e_secure));
9340 return;
9341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 if (ex_normal_busy >= p_mmd)
9343 {
9344 EMSG(_("E192: Recursive use of :normal too deep"));
9345 return;
9346 }
9347 ++ex_normal_busy;
9348
9349 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9350 restart_edit = 0; /* don't go to Insert mode */
9351 p_im = FALSE; /* don't use 'insertmode' */
9352
9353#ifdef FEAT_MBYTE
9354 /*
9355 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9356 * this for the K_SPECIAL leading byte, otherwise special keys will not
9357 * work.
9358 */
9359 if (has_mbyte)
9360 {
9361 int len = 0;
9362
9363 /* Count the number of characters to be escaped. */
9364 for (p = eap->arg; *p != NUL; ++p)
9365 {
9366# ifdef FEAT_GUI
9367 if (*p == CSI) /* leadbyte CSI */
9368 len += 2;
9369# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009370 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9372# ifdef FEAT_GUI
9373 || *p == CSI
9374# endif
9375 )
9376 len += 2;
9377 }
9378 if (len > 0)
9379 {
9380 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9381 if (arg != NULL)
9382 {
9383 len = 0;
9384 for (p = eap->arg; *p != NUL; ++p)
9385 {
9386 arg[len++] = *p;
9387# ifdef FEAT_GUI
9388 if (*p == CSI)
9389 {
9390 arg[len++] = KS_EXTRA;
9391 arg[len++] = (int)KE_CSI;
9392 }
9393# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009394 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 {
9396 arg[len++] = *++p;
9397 if (*p == K_SPECIAL)
9398 {
9399 arg[len++] = KS_SPECIAL;
9400 arg[len++] = KE_FILLER;
9401 }
9402# ifdef FEAT_GUI
9403 else if (*p == CSI)
9404 {
9405 arg[len++] = KS_EXTRA;
9406 arg[len++] = (int)KE_CSI;
9407 }
9408# endif
9409 }
9410 arg[len] = NUL;
9411 }
9412 }
9413 }
9414 }
9415#endif
9416
9417 /*
9418 * Save the current typeahead. This is required to allow using ":normal"
9419 * from an event handler and makes sure we don't hang when the argument
9420 * ends with half a command.
9421 */
9422 save_typeahead(&tabuf);
9423 if (tabuf.typebuf_valid)
9424 {
9425 /*
9426 * Repeat the :normal command for each line in the range. When no
9427 * range given, execute it just once, without positioning the cursor
9428 * first.
9429 */
9430 do
9431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 if (eap->addr_count != 0)
9433 {
9434 curwin->w_cursor.lnum = eap->line1++;
9435 curwin->w_cursor.col = 0;
9436 }
9437
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009438 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#ifdef FEAT_MBYTE
9440 arg != NULL ? arg :
9441#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009442 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 }
9444 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9445 }
9446
9447 /* Might not return to the main loop when in an event handler. */
9448 update_topline_cursor();
9449
9450 /* Restore the previous typeahead. */
9451 restore_typeahead(&tabuf);
9452
9453 --ex_normal_busy;
9454 msg_scroll = save_msg_scroll;
9455 restart_edit = save_restart_edit;
9456 p_im = save_insertmode;
9457 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009458 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9460
9461 /* Restore the state (needed when called from a function executed for
9462 * 'indentexpr'). */
9463 State = save_State;
9464#ifdef FEAT_MBYTE
9465 vim_free(arg);
9466#endif
9467}
9468
9469/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009470 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 */
9472 static void
9473ex_startinsert(eap)
9474 exarg_T *eap;
9475{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009476 if (eap->forceit)
9477 {
9478 coladvance((colnr_T)MAXCOL);
9479 curwin->w_curswant = MAXCOL;
9480 curwin->w_set_curswant = FALSE;
9481 }
9482
Bram Moolenaara40c5002005-01-09 21:16:21 +00009483 /* Ignore the command when already in Insert mode. Inserting an
9484 * expression register that invokes a function can do this. */
9485 if (State & INSERT)
9486 return;
9487
Bram Moolenaar64969662005-12-14 21:59:55 +00009488 if (eap->cmdidx == CMD_startinsert)
9489 restart_edit = 'a';
9490 else if (eap->cmdidx == CMD_startreplace)
9491 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009493 restart_edit = 'V';
9494
9495 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009497 if (eap->cmdidx == CMD_startinsert)
9498 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 curwin->w_curswant = 0; /* avoid MAXCOL */
9500 }
9501}
9502
9503/*
9504 * ":stopinsert"
9505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 static void
9507ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009508 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510 restart_edit = 0;
9511 stop_insert_mode = TRUE;
9512}
9513#endif
9514
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009515#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9516/*
9517 * Execute normal mode command "cmd".
9518 * "remap" can be REMAP_NONE or REMAP_YES.
9519 */
9520 void
9521exec_normal_cmd(cmd, remap, silent)
9522 char_u *cmd;
9523 int remap;
9524 int silent;
9525{
9526 oparg_T oa;
9527
9528 /*
9529 * Stuff the argument into the typeahead buffer.
9530 * Execute normal_cmd() until there is no typeahead left.
9531 */
9532 clear_oparg(&oa);
9533 finish_op = FALSE;
9534 ins_typebuf(cmd, remap, 0, TRUE, silent);
9535 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9536 && !got_int)
9537 {
9538 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009539 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009540 }
9541}
9542#endif
9543
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544#ifdef FEAT_FIND_ID
9545 static void
9546ex_checkpath(eap)
9547 exarg_T *eap;
9548{
9549 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9550 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9551 (linenr_T)1, (linenr_T)MAXLNUM);
9552}
9553
9554#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9555/*
9556 * ":psearch"
9557 */
9558 static void
9559ex_psearch(eap)
9560 exarg_T *eap;
9561{
9562 g_do_tagpreview = p_pvh;
9563 ex_findpat(eap);
9564 g_do_tagpreview = 0;
9565}
9566#endif
9567
9568 static void
9569ex_findpat(eap)
9570 exarg_T *eap;
9571{
9572 int whole = TRUE;
9573 long n;
9574 char_u *p;
9575 int action;
9576
9577 switch (cmdnames[eap->cmdidx].cmd_name[2])
9578 {
9579 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9580 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9581 action = ACTION_GOTO;
9582 else
9583 action = ACTION_SHOW;
9584 break;
9585 case 'i': /* ":ilist" and ":dlist" */
9586 action = ACTION_SHOW_ALL;
9587 break;
9588 case 'u': /* ":ijump" and ":djump" */
9589 action = ACTION_GOTO;
9590 break;
9591 default: /* ":isplit" and ":dsplit" */
9592 action = ACTION_SPLIT;
9593 break;
9594 }
9595
9596 n = 1;
9597 if (vim_isdigit(*eap->arg)) /* get count */
9598 {
9599 n = getdigits(&eap->arg);
9600 eap->arg = skipwhite(eap->arg);
9601 }
9602 if (*eap->arg == '/') /* Match regexp, not just whole words */
9603 {
9604 whole = FALSE;
9605 ++eap->arg;
9606 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9607 if (*p)
9608 {
9609 *p++ = NUL;
9610 p = skipwhite(p);
9611
9612 /* Check for trailing illegal characters */
9613 if (!ends_excmd(*p))
9614 eap->errmsg = e_trailing;
9615 else
9616 eap->nextcmd = check_nextcmd(p);
9617 }
9618 }
9619 if (!eap->skip)
9620 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9621 whole, !eap->forceit,
9622 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9623 n, action, eap->line1, eap->line2);
9624}
9625#endif
9626
9627#ifdef FEAT_WINDOWS
9628
9629# ifdef FEAT_QUICKFIX
9630/*
9631 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9632 */
9633 static void
9634ex_ptag(eap)
9635 exarg_T *eap;
9636{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009637 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9639}
9640
9641/*
9642 * ":pedit"
9643 */
9644 static void
9645ex_pedit(eap)
9646 exarg_T *eap;
9647{
9648 win_T *curwin_save = curwin;
9649
9650 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009651 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 keep_help_flag = curwin_save->w_buffer->b_help;
9653 do_exedit(eap, NULL);
9654 keep_help_flag = FALSE;
9655 if (curwin != curwin_save && win_valid(curwin_save))
9656 {
9657 /* Return cursor to where we were */
9658 validate_cursor();
9659 redraw_later(VALID);
9660 win_enter(curwin_save, TRUE);
9661 }
9662 g_do_tagpreview = 0;
9663}
9664# endif
9665
9666/*
9667 * ":stag", ":stselect" and ":stjump".
9668 */
9669 static void
9670ex_stag(eap)
9671 exarg_T *eap;
9672{
9673 postponed_split = -1;
9674 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009675 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9677 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009678 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679}
9680#endif
9681
9682/*
9683 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9684 */
9685 static void
9686ex_tag(eap)
9687 exarg_T *eap;
9688{
9689 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9690}
9691
9692 static void
9693ex_tag_cmd(eap, name)
9694 exarg_T *eap;
9695 char_u *name;
9696{
9697 int cmd;
9698
9699 switch (name[1])
9700 {
9701 case 'j': cmd = DT_JUMP; /* ":tjump" */
9702 break;
9703 case 's': cmd = DT_SELECT; /* ":tselect" */
9704 break;
9705 case 'p': cmd = DT_PREV; /* ":tprevious" */
9706 break;
9707 case 'N': cmd = DT_PREV; /* ":tNext" */
9708 break;
9709 case 'n': cmd = DT_NEXT; /* ":tnext" */
9710 break;
9711 case 'o': cmd = DT_POP; /* ":pop" */
9712 break;
9713 case 'f': /* ":tfirst" */
9714 case 'r': cmd = DT_FIRST; /* ":trewind" */
9715 break;
9716 case 'l': cmd = DT_LAST; /* ":tlast" */
9717 break;
9718 default: /* ":tag" */
9719#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009720 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 {
9722 do_cstag(eap);
9723 return;
9724 }
9725#endif
9726 cmd = DT_TAG;
9727 break;
9728 }
9729
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009730 if (name[0] == 'l')
9731 {
9732#ifndef FEAT_QUICKFIX
9733 ex_ni(eap);
9734 return;
9735#else
9736 cmd = DT_LTAG;
9737#endif
9738 }
9739
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9741 eap->forceit, TRUE);
9742}
9743
9744/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009745 * Check "str" for starting with a special cmdline variable.
9746 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9747 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9748 */
9749 int
9750find_cmdline_var(src, usedlen)
9751 char_u *src;
9752 int *usedlen;
9753{
9754 int len;
9755 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009756 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009757 "%",
9758#define SPEC_PERC 0
9759 "#",
9760#define SPEC_HASH 1
9761 "<cword>", /* cursor word */
9762#define SPEC_CWORD 2
9763 "<cWORD>", /* cursor WORD */
9764#define SPEC_CCWORD 3
9765 "<cfile>", /* cursor path name */
9766#define SPEC_CFILE 4
9767 "<sfile>", /* ":so" file name */
9768#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009769 "<slnum>", /* ":so" file line number */
9770#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009771#ifdef FEAT_AUTOCMD
9772 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009773# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009774 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009775# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009776 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009777# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009778#endif
9779#ifdef FEAT_CLIENTSERVER
9780 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009781# ifdef FEAT_AUTOCMD
9782# define SPEC_CLIENT 10
9783# else
9784# define SPEC_CLIENT 7
9785# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009786#endif
9787 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009788
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009789 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009790 {
9791 len = (int)STRLEN(spec_str[i]);
9792 if (STRNCMP(src, spec_str[i], len) == 0)
9793 {
9794 *usedlen = len;
9795 return i;
9796 }
9797 }
9798 return -1;
9799}
9800
9801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 * Evaluate cmdline variables.
9803 *
9804 * change '%' to curbuf->b_ffname
9805 * '#' to curwin->w_altfile
9806 * '<cword>' to word under the cursor
9807 * '<cWORD>' to WORD under the cursor
9808 * '<cfile>' to path name under the cursor
9809 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009810 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 * '<afile>' to file name for autocommand
9812 * '<abuf>' to buffer number for autocommand
9813 * '<amatch>' to matching name for autocommand
9814 *
9815 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9816 * "" for error without a message) and NULL is returned.
9817 * Returns an allocated string if a valid match was found.
9818 * Returns NULL if no match was found. "usedlen" then still contains the
9819 * number of characters to skip.
9820 */
9821 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009822eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009824 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 int *usedlen; /* characters after src that are used */
9826 linenr_T *lnump; /* line number for :e command, or NULL */
9827 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009828 int *escaped; /* return value has escaped white space (can
9829 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 int i;
9832 char_u *s;
9833 char_u *result;
9834 char_u *resultbuf = NULL;
9835 int resultlen;
9836 buf_T *buf;
9837 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9838 int spec_idx;
9839#ifdef FEAT_MODIFY_FNAME
9840 int skip_mod = FALSE;
9841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
9844 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009845 if (escaped != NULL)
9846 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
9848 /*
9849 * Check if there is something to do.
9850 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009851 spec_idx = find_cmdline_var(src, usedlen);
9852 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 {
9854 *usedlen = 1;
9855 return NULL;
9856 }
9857
9858 /*
9859 * Skip when preceded with a backslash "\%" and "\#".
9860 * Note: In "\\%" the % is also not recognized!
9861 */
9862 if (src > srcstart && src[-1] == '\\')
9863 {
9864 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009865 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 return NULL;
9867 }
9868
9869 /*
9870 * word or WORD under cursor
9871 */
9872 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9873 {
9874 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9875 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9876 if (resultlen == 0)
9877 {
9878 *errormsg = (char_u *)"";
9879 return NULL;
9880 }
9881 }
9882
9883 /*
9884 * '#': Alternate file name
9885 * '%': Current file name
9886 * File name under the cursor
9887 * File name for autocommand
9888 * and following modifiers
9889 */
9890 else
9891 {
9892 switch (spec_idx)
9893 {
9894 case SPEC_PERC: /* '%': current file */
9895 if (curbuf->b_fname == NULL)
9896 {
9897 result = (char_u *)"";
9898 valid = 0; /* Must have ":p:h" to be valid */
9899 }
9900 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 break;
9903
9904 case SPEC_HASH: /* '#' or "#99": alternate file */
9905 if (src[1] == '#') /* "##": the argument list */
9906 {
9907 result = arg_all();
9908 resultbuf = result;
9909 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009910 if (escaped != NULL)
9911 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#ifdef FEAT_MODIFY_FNAME
9913 skip_mod = TRUE;
9914#endif
9915 break;
9916 }
9917 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009918 if (*s == '<') /* "#<99" uses v:oldfiles */
9919 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 i = (int)getdigits(&s);
9921 *usedlen = (int)(s - src); /* length of what we expand */
9922
Bram Moolenaard812df62008-11-09 12:46:09 +00009923 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009925 if (*usedlen < 2)
9926 {
9927 /* Should we give an error message for #<text? */
9928 *usedlen = 1;
9929 return NULL;
9930 }
9931#ifdef FEAT_EVAL
9932 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9933 (long)i);
9934 if (result == NULL)
9935 {
9936 *errormsg = (char_u *)"";
9937 return NULL;
9938 }
9939#else
9940 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009945 {
9946 buf = buflist_findnr(i);
9947 if (buf == NULL)
9948 {
9949 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9950 return NULL;
9951 }
9952 if (lnump != NULL)
9953 *lnump = ECMD_LAST;
9954 if (buf->b_fname == NULL)
9955 {
9956 result = (char_u *)"";
9957 valid = 0; /* Must have ":p:h" to be valid */
9958 }
9959 else
9960 result = buf->b_fname;
9961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 break;
9963
9964#ifdef FEAT_SEARCHPATH
9965 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009966 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 if (result == NULL)
9968 {
9969 *errormsg = (char_u *)"";
9970 return NULL;
9971 }
9972 resultbuf = result; /* remember allocated string */
9973 break;
9974#endif
9975
9976#ifdef FEAT_AUTOCMD
9977 case SPEC_AFILE: /* file name for autocommand */
9978 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009979 if (result != NULL && !autocmd_fname_full)
9980 {
9981 /* Still need to turn the fname into a full path. It is
9982 * postponed to avoid a delay when <afile> is not used. */
9983 autocmd_fname_full = TRUE;
9984 result = FullName_save(autocmd_fname, FALSE);
9985 vim_free(autocmd_fname);
9986 autocmd_fname = result;
9987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 if (result == NULL)
9989 {
9990 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9991 return NULL;
9992 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009993 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 break;
9995
9996 case SPEC_ABUF: /* buffer number for autocommand */
9997 if (autocmd_bufnr <= 0)
9998 {
9999 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10000 return NULL;
10001 }
10002 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10003 result = strbuf;
10004 break;
10005
10006 case SPEC_AMATCH: /* match name for autocommand */
10007 result = autocmd_match;
10008 if (result == NULL)
10009 {
10010 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10011 return NULL;
10012 }
10013 break;
10014
10015#endif
10016 case SPEC_SFILE: /* file name for ":so" command */
10017 result = sourcing_name;
10018 if (result == NULL)
10019 {
10020 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10021 return NULL;
10022 }
10023 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010024 case SPEC_SLNUM: /* line in file for ":so" command */
10025 if (sourcing_name == NULL || sourcing_lnum == 0)
10026 {
10027 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10028 return NULL;
10029 }
10030 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10031 result = strbuf;
10032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033#if defined(FEAT_CLIENTSERVER)
10034 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010035 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10036 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 result = strbuf;
10038 break;
10039#endif
10040 }
10041
10042 resultlen = (int)STRLEN(result); /* length of new string */
10043 if (src[*usedlen] == '<') /* remove the file name extension */
10044 {
10045 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 resultlen = (int)(s - result);
10048 }
10049#ifdef FEAT_MODIFY_FNAME
10050 else if (!skip_mod)
10051 {
10052 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10053 &resultlen);
10054 if (result == NULL)
10055 {
10056 *errormsg = (char_u *)"";
10057 return NULL;
10058 }
10059 }
10060#endif
10061 }
10062
10063 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10064 {
10065 if (valid != VALID_HEAD + VALID_PATH)
10066 /* xgettext:no-c-format */
10067 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10068 else
10069 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10070 result = NULL;
10071 }
10072 else
10073 result = vim_strnsave(result, resultlen);
10074 vim_free(resultbuf);
10075 return result;
10076}
10077
10078/*
10079 * Concatenate all files in the argument list, separated by spaces, and return
10080 * it in one allocated string.
10081 * Spaces and backslashes in the file names are escaped with a backslash.
10082 * Returns NULL when out of memory.
10083 */
10084 static char_u *
10085arg_all()
10086{
10087 int len;
10088 int idx;
10089 char_u *retval = NULL;
10090 char_u *p;
10091
10092 /*
10093 * Do this loop two times:
10094 * first time: compute the total length
10095 * second time: concatenate the names
10096 */
10097 for (;;)
10098 {
10099 len = 0;
10100 for (idx = 0; idx < ARGCOUNT; ++idx)
10101 {
10102 p = alist_name(&ARGLIST[idx]);
10103 if (p != NULL)
10104 {
10105 if (len > 0)
10106 {
10107 /* insert a space in between names */
10108 if (retval != NULL)
10109 retval[len] = ' ';
10110 ++len;
10111 }
10112 for ( ; *p != NUL; ++p)
10113 {
10114 if (*p == ' ' || *p == '\\')
10115 {
10116 /* insert a backslash */
10117 if (retval != NULL)
10118 retval[len] = '\\';
10119 ++len;
10120 }
10121 if (retval != NULL)
10122 retval[len] = *p;
10123 ++len;
10124 }
10125 }
10126 }
10127
10128 /* second time: break here */
10129 if (retval != NULL)
10130 {
10131 retval[len] = NUL;
10132 break;
10133 }
10134
10135 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010136 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (retval == NULL)
10138 break;
10139 }
10140
10141 return retval;
10142}
10143
10144#if defined(FEAT_AUTOCMD) || defined(PROTO)
10145/*
10146 * Expand the <sfile> string in "arg".
10147 *
10148 * Returns an allocated string, or NULL for any error.
10149 */
10150 char_u *
10151expand_sfile(arg)
10152 char_u *arg;
10153{
10154 char_u *errormsg;
10155 int len;
10156 char_u *result;
10157 char_u *newres;
10158 char_u *repl;
10159 int srclen;
10160 char_u *p;
10161
10162 result = vim_strsave(arg);
10163 if (result == NULL)
10164 return NULL;
10165
10166 for (p = result; *p; )
10167 {
10168 if (STRNCMP(p, "<sfile>", 7) != 0)
10169 ++p;
10170 else
10171 {
10172 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010173 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 if (errormsg != NULL)
10175 {
10176 if (*errormsg)
10177 emsg(errormsg);
10178 vim_free(result);
10179 return NULL;
10180 }
10181 if (repl == NULL) /* no match (cannot happen) */
10182 {
10183 p += srclen;
10184 continue;
10185 }
10186 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10187 newres = alloc(len);
10188 if (newres == NULL)
10189 {
10190 vim_free(repl);
10191 vim_free(result);
10192 return NULL;
10193 }
10194 mch_memmove(newres, result, (size_t)(p - result));
10195 STRCPY(newres + (p - result), repl);
10196 len = (int)STRLEN(newres);
10197 STRCAT(newres, p + srclen);
10198 vim_free(repl);
10199 vim_free(result);
10200 result = newres;
10201 p = newres + len; /* continue after the match */
10202 }
10203 }
10204
10205 return result;
10206}
10207#endif
10208
10209#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010210static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10211 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10213static frame_T *ses_skipframe __ARGS((frame_T *fr));
10214static int ses_do_frame __ARGS((frame_T *fr));
10215static int ses_do_win __ARGS((win_T *wp));
10216static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10217static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10218static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10219
10220/*
10221 * Write openfile commands for the current buffers to an .exrc file.
10222 * Return FAIL on error, OK otherwise.
10223 */
10224 static int
10225makeopens(fd, dirnow)
10226 FILE *fd;
10227 char_u *dirnow; /* Current directory name */
10228{
10229 buf_T *buf;
10230 int only_save_windows = TRUE;
10231 int nr;
10232 int cnr = 1;
10233 int restore_size = TRUE;
10234 win_T *wp;
10235 char_u *sname;
10236 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010237 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010238 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010239 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010240 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010241 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
10243 if (ssop_flags & SSOP_BUFFERS)
10244 only_save_windows = FALSE; /* Save ALL buffers */
10245
10246 /*
10247 * Begin by setting the this_session variable, and then other
10248 * sessionable variables.
10249 */
10250#ifdef FEAT_EVAL
10251 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10252 return FAIL;
10253 if (ssop_flags & SSOP_GLOBALS)
10254 if (store_session_globals(fd) == FAIL)
10255 return FAIL;
10256#endif
10257
10258 /*
10259 * Close all windows but one.
10260 */
10261 if (put_line(fd, "silent only") == FAIL)
10262 return FAIL;
10263
10264 /*
10265 * Now a :cd command to the session directory or the current directory
10266 */
10267 if (ssop_flags & SSOP_SESDIR)
10268 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010269 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10270 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 return FAIL;
10272 }
10273 else if (ssop_flags & SSOP_CURDIR)
10274 {
10275 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10276 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010277 || fputs("cd ", fd) < 0
10278 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10279 || put_eol(fd) == FAIL)
10280 {
10281 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 vim_free(sname);
10285 }
10286
10287 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010288 * If there is an empty, unnamed buffer we will wipe it out later.
10289 * Remember the buffer number.
10290 */
10291 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10292 return FAIL;
10293 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10294 return FAIL;
10295 if (put_line(fd, "endif") == FAIL)
10296 return FAIL;
10297
10298 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 * Now save the current files, current buffer first.
10300 */
10301 if (put_line(fd, "set shortmess=aoO") == FAIL)
10302 return FAIL;
10303
10304 /* Now put the other buffers into the buffer list */
10305 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10306 {
10307 if (!(only_save_windows && buf->b_nwindows == 0)
10308 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10309 && buf->b_fname != NULL
10310 && buf->b_p_bl)
10311 {
10312 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10313 : buf->b_wininfo->wi_fpos.lnum) < 0
10314 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10315 return FAIL;
10316 }
10317 }
10318
10319 /* the global argument list */
10320 if (ses_arglist(fd, "args", &global_alist.al_ga,
10321 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10322 return FAIL;
10323
10324 if (ssop_flags & SSOP_RESIZE)
10325 {
10326 /* Note: after the restore we still check it worked!*/
10327 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10328 || put_eol(fd) == FAIL)
10329 return FAIL;
10330 }
10331
10332#ifdef FEAT_GUI
10333 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10334 {
10335 int x, y;
10336
10337 if (gui_mch_get_winpos(&x, &y) == OK)
10338 {
10339 /* Note: after the restore we still check it worked!*/
10340 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10341 return FAIL;
10342 }
10343 }
10344#endif
10345
10346 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010347 * May repeat putting Windows for each tab, when "tabpages" is in
10348 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010349 * Don't use goto_tabpage(), it may change directory and trigger
10350 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010352 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010353 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010354 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010356 int need_tabnew = FALSE;
10357
Bram Moolenaar18144c82006-04-12 21:52:12 +000010358 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010360 tabpage_T *tp = find_tabpage(tabnr);
10361
10362 if (tp == NULL)
10363 break; /* done all tab pages */
10364 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010365 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010366 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010367 tab_topframe = topframe;
10368 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010369 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010370 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010371 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010372 tab_topframe = tp->tp_topframe;
10373 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010374 if (tabnr > 1)
10375 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
Bram Moolenaar18144c82006-04-12 21:52:12 +000010378 /*
10379 * Before creating the window layout, try loading one file. If this
10380 * is aborted we don't end up with a number of useless windows.
10381 * This may have side effects! (e.g., compressed or network file).
10382 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010383 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010384 {
10385 if (ses_do_win(wp)
10386 && wp->w_buffer->b_ffname != NULL
10387 && !wp->w_buffer->b_help
10388#ifdef FEAT_QUICKFIX
10389 && !bt_nofile(wp->w_buffer)
10390#endif
10391 )
10392 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010393 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010394 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10395 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010396 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010397 if (!wp->w_arg_idx_invalid)
10398 edited_win = wp;
10399 break;
10400 }
10401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010403 /* If no file got edited create an empty tab page. */
10404 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10405 return FAIL;
10406
Bram Moolenaar18144c82006-04-12 21:52:12 +000010407 /*
10408 * Save current window layout.
10409 */
10410 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010412 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010414 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10415 return FAIL;
10416 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
Bram Moolenaar18144c82006-04-12 21:52:12 +000010419 /*
10420 * Check if window sizes can be restored (no windows omitted).
10421 * Remember the window number of the current window after restoring.
10422 */
10423 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010424 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010425 {
10426 if (ses_do_win(wp))
10427 ++nr;
10428 else
10429 restore_size = FALSE;
10430 if (curwin == wp)
10431 cnr = nr;
10432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
Bram Moolenaar18144c82006-04-12 21:52:12 +000010434 /* Go to the first window. */
10435 if (put_line(fd, "wincmd t") == FAIL)
10436 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010437
Bram Moolenaar18144c82006-04-12 21:52:12 +000010438 /*
10439 * If more than one window, see if sizes can be restored.
10440 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10441 * resized when moving between windows.
10442 * Do this before restoring the view, so that the topline and the
10443 * cursor can be set. This is done again below.
10444 */
10445 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10446 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010447 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010448 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449
Bram Moolenaar18144c82006-04-12 21:52:12 +000010450 /*
10451 * Restore the view of the window (options, file, cursor, etc.).
10452 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010453 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010454 {
10455 if (!ses_do_win(wp))
10456 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010457 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10458 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010459 return FAIL;
10460 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10461 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010462 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 }
10464
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010465 /* The argument index in the first tab page is zero, need to set it in
10466 * each window. For further tab pages it's the window where we do
10467 * "tabedit". */
10468 cur_arg_idx = next_arg_idx;
10469
Bram Moolenaar18144c82006-04-12 21:52:12 +000010470 /*
10471 * Restore cursor to the current window if it's not the first one.
10472 */
10473 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10474 || put_eol(fd) == FAIL))
10475 return FAIL;
10476
10477 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010478 * Restore window sizes again after jumping around in windows, because
10479 * the current window has a minimum size while others may not.
10480 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010481 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010482 return FAIL;
10483
Bram Moolenaar18144c82006-04-12 21:52:12 +000010484 /* Don't continue in another tab page when doing only the current one
10485 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010486 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010487 break;
10488 }
10489
10490 if (ssop_flags & SSOP_TABPAGES)
10491 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010492 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10493 || put_eol(fd) == FAIL)
10494 return FAIL;
10495 }
10496
Bram Moolenaar9c102382006-05-03 21:26:49 +000010497 /*
10498 * Wipe out an empty unnamed buffer we started in.
10499 */
10500 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10501 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010502 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010503 return FAIL;
10504 if (put_line(fd, "endif") == FAIL)
10505 return FAIL;
10506 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10507 return FAIL;
10508
10509 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10510 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10511 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10512 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
10514 /*
10515 * Lastly, execute the x.vim file if it exists.
10516 */
10517 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10518 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010519 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 || put_line(fd, "endif") == FAIL)
10521 return FAIL;
10522
10523 return OK;
10524}
10525
10526 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010527ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 FILE *fd;
10529 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010530 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 int n = 0;
10533 win_T *wp;
10534
10535 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10536 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010537 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 {
10539 if (!ses_do_win(wp))
10540 continue;
10541 ++n;
10542
10543 /* restore height when not full height */
10544 if (wp->w_height + wp->w_status_height < topframe->fr_height
10545 && (fprintf(fd,
10546 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10547 n, (long)wp->w_height, Rows / 2, Rows) < 0
10548 || put_eol(fd) == FAIL))
10549 return FAIL;
10550
10551 /* restore width when not full width */
10552 if (wp->w_width < Columns && (fprintf(fd,
10553 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10554 n, (long)wp->w_width, Columns / 2, Columns) < 0
10555 || put_eol(fd) == FAIL))
10556 return FAIL;
10557 }
10558 }
10559 else
10560 {
10561 /* Just equalise window sizes */
10562 if (put_line(fd, "wincmd =") == FAIL)
10563 return FAIL;
10564 }
10565 return OK;
10566}
10567
10568/*
10569 * Write commands to "fd" to recursively create windows for frame "fr",
10570 * horizontally and vertically split.
10571 * After the commands the last window in the frame is the current window.
10572 * Returns FAIL when writing the commands to "fd" fails.
10573 */
10574 static int
10575ses_win_rec(fd, fr)
10576 FILE *fd;
10577 frame_T *fr;
10578{
10579 frame_T *frc;
10580 int count = 0;
10581
10582 if (fr->fr_layout != FR_LEAF)
10583 {
10584 /* Find first frame that's not skipped and then create a window for
10585 * each following one (first frame is already there). */
10586 frc = ses_skipframe(fr->fr_child);
10587 if (frc != NULL)
10588 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10589 {
10590 /* Make window as big as possible so that we have lots of room
10591 * to split. */
10592 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10593 || put_line(fd, fr->fr_layout == FR_COL
10594 ? "split" : "vsplit") == FAIL)
10595 return FAIL;
10596 ++count;
10597 }
10598
10599 /* Go back to the first window. */
10600 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10601 ? "%dwincmd k" : "%dwincmd h", count) < 0
10602 || put_eol(fd) == FAIL))
10603 return FAIL;
10604
10605 /* Recursively create frames/windows in each window of this column or
10606 * row. */
10607 frc = ses_skipframe(fr->fr_child);
10608 while (frc != NULL)
10609 {
10610 ses_win_rec(fd, frc);
10611 frc = ses_skipframe(frc->fr_next);
10612 /* Go to next window. */
10613 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10614 return FAIL;
10615 }
10616 }
10617 return OK;
10618}
10619
10620/*
10621 * Skip frames that don't contain windows we want to save in the Session.
10622 * Returns NULL when there none.
10623 */
10624 static frame_T *
10625ses_skipframe(fr)
10626 frame_T *fr;
10627{
10628 frame_T *frc;
10629
10630 for (frc = fr; frc != NULL; frc = frc->fr_next)
10631 if (ses_do_frame(frc))
10632 break;
10633 return frc;
10634}
10635
10636/*
10637 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10638 * the Session.
10639 */
10640 static int
10641ses_do_frame(fr)
10642 frame_T *fr;
10643{
10644 frame_T *frc;
10645
10646 if (fr->fr_layout == FR_LEAF)
10647 return ses_do_win(fr->fr_win);
10648 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10649 if (ses_do_frame(frc))
10650 return TRUE;
10651 return FALSE;
10652}
10653
10654/*
10655 * Return non-zero if window "wp" is to be stored in the Session.
10656 */
10657 static int
10658ses_do_win(wp)
10659 win_T *wp;
10660{
10661 if (wp->w_buffer->b_fname == NULL
10662#ifdef FEAT_QUICKFIX
10663 /* When 'buftype' is "nofile" can't restore the window contents. */
10664 || bt_nofile(wp->w_buffer)
10665#endif
10666 )
10667 return (ssop_flags & SSOP_BLANK);
10668 if (wp->w_buffer->b_help)
10669 return (ssop_flags & SSOP_HELP);
10670 return TRUE;
10671}
10672
10673/*
10674 * Write commands to "fd" to restore the view of a window.
10675 * Caller must make sure 'scrolloff' is zero.
10676 */
10677 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010678put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 FILE *fd;
10680 win_T *wp;
10681 int add_edit; /* add ":edit" command to view */
10682 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010683 int current_arg_idx; /* current argument index of the window, use
10684 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 win_T *save_curwin;
10687 int f;
10688 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010689 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690
10691 /* Always restore cursor position for ":mksession". For ":mkview" only
10692 * when 'viewoptions' contains "cursor". */
10693 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10694
10695 /*
10696 * Local argument list.
10697 */
10698 if (wp->w_alist == &global_alist)
10699 {
10700 if (put_line(fd, "argglobal") == FAIL)
10701 return FAIL;
10702 }
10703 else
10704 {
10705 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10706 flagp == &vop_flags
10707 || !(*flagp & SSOP_CURDIR)
10708 || wp->w_localdir != NULL, flagp) == FAIL)
10709 return FAIL;
10710 }
10711
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010712 /* Only when part of a session: restore the argument index. Some
10713 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010714 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010715 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010717 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 || put_eol(fd) == FAIL)
10719 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010720 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 }
10722
10723 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010724 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 {
10726 /*
10727 * Load the file.
10728 */
10729 if (wp->w_buffer->b_ffname != NULL
10730#ifdef FEAT_QUICKFIX
10731 && !bt_nofile(wp->w_buffer)
10732#endif
10733 )
10734 {
10735 /*
10736 * Editing a file in this buffer: use ":edit file".
10737 * This may have side effects! (e.g., compressed or network file).
10738 */
10739 if (fputs("edit ", fd) < 0
10740 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10741 return FAIL;
10742 }
10743 else
10744 {
10745 /* No file in this buffer, just make it empty. */
10746 if (put_line(fd, "enew") == FAIL)
10747 return FAIL;
10748#ifdef FEAT_QUICKFIX
10749 if (wp->w_buffer->b_ffname != NULL)
10750 {
10751 /* The buffer does have a name, but it's not a file name. */
10752 if (fputs("file ", fd) < 0
10753 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10754 return FAIL;
10755 }
10756#endif
10757 do_cursor = FALSE;
10758 }
10759 }
10760
10761 /*
10762 * Local mappings and abbreviations.
10763 */
10764 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10765 && makemap(fd, wp->w_buffer) == FAIL)
10766 return FAIL;
10767
10768 /*
10769 * Local options. Need to go to the window temporarily.
10770 * Store only local values when using ":mkview" and when ":mksession" is
10771 * used and 'sessionoptions' doesn't include "options".
10772 * Some folding options are always stored when "folds" is included,
10773 * otherwise the folds would not be restored correctly.
10774 */
10775 save_curwin = curwin;
10776 curwin = wp;
10777 curbuf = curwin->w_buffer;
10778 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10779 f = makeset(fd, OPT_LOCAL,
10780 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10781#ifdef FEAT_FOLDING
10782 else if (*flagp & SSOP_FOLDS)
10783 f = makefoldset(fd);
10784#endif
10785 else
10786 f = OK;
10787 curwin = save_curwin;
10788 curbuf = curwin->w_buffer;
10789 if (f == FAIL)
10790 return FAIL;
10791
10792#ifdef FEAT_FOLDING
10793 /*
10794 * Save Folds when 'buftype' is empty and for help files.
10795 */
10796 if ((*flagp & SSOP_FOLDS)
10797 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010798# ifdef FEAT_QUICKFIX
10799 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10800# endif
10801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 {
10803 if (put_folds(fd, wp) == FAIL)
10804 return FAIL;
10805 }
10806#endif
10807
10808 /*
10809 * Set the cursor after creating folds, since that moves the cursor.
10810 */
10811 if (do_cursor)
10812 {
10813
10814 /* Restore the cursor line in the file and relatively in the
10815 * window. Don't use "G", it changes the jumplist. */
10816 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10817 (long)wp->w_cursor.lnum,
10818 (long)(wp->w_cursor.lnum - wp->w_topline),
10819 (long)wp->w_height / 2, (long)wp->w_height) < 0
10820 || put_eol(fd) == FAIL
10821 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10822 || put_line(fd, "exe s:l") == FAIL
10823 || put_line(fd, "normal! zt") == FAIL
10824 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10825 || put_eol(fd) == FAIL)
10826 return FAIL;
10827 /* Restore the cursor column and left offset when not wrapping. */
10828 if (wp->w_cursor.col == 0)
10829 {
10830 if (put_line(fd, "normal! 0") == FAIL)
10831 return FAIL;
10832 }
10833 else
10834 {
10835 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10836 {
10837 if (fprintf(fd,
10838 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010839 (long)wp->w_virtcol + 1,
10840 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 (long)wp->w_width / 2, (long)wp->w_width) < 0
10842 || put_eol(fd) == FAIL
10843 || put_line(fd, "if s:c > 0") == FAIL
10844 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010845 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10846 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 || put_eol(fd) == FAIL
10848 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010849 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 || put_eol(fd) == FAIL
10851 || put_line(fd, "endif") == FAIL)
10852 return FAIL;
10853 }
10854 else
10855 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010856 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 || put_eol(fd) == FAIL)
10858 return FAIL;
10859 }
10860 }
10861 }
10862
10863 /*
10864 * Local directory.
10865 */
10866 if (wp->w_localdir != NULL)
10867 {
10868 if (fputs("lcd ", fd) < 0
10869 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10870 || put_eol(fd) == FAIL)
10871 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010872 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 }
10874
10875 return OK;
10876}
10877
10878/*
10879 * Write an argument list to the session file.
10880 * Returns FAIL if writing fails.
10881 */
10882 static int
10883ses_arglist(fd, cmd, gap, fullname, flagp)
10884 FILE *fd;
10885 char *cmd;
10886 garray_T *gap;
10887 int fullname; /* TRUE: use full path name */
10888 unsigned *flagp;
10889{
10890 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010891 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 char_u *s;
10893
10894 if (gap->ga_len == 0)
10895 return put_line(fd, "silent! argdel *");
10896 if (fputs(cmd, fd) < 0)
10897 return FAIL;
10898 for (i = 0; i < gap->ga_len; ++i)
10899 {
10900 /* NULL file names are skipped (only happens when out of memory). */
10901 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10902 if (s != NULL)
10903 {
10904 if (fullname)
10905 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010906 buf = alloc(MAXPATHL);
10907 if (buf != NULL)
10908 {
10909 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10910 s = buf;
10911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 }
10913 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010914 {
10915 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010917 }
10918 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920 }
10921 return put_eol(fd);
10922}
10923
10924/*
10925 * Write a buffer name to the session file.
10926 * Also ends the line.
10927 * Returns FAIL if writing fails.
10928 */
10929 static int
10930ses_fname(fd, buf, flagp)
10931 FILE *fd;
10932 buf_T *buf;
10933 unsigned *flagp;
10934{
10935 char_u *name;
10936
10937 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010938 * the session file will be sourced.
10939 * Don't do this for ":mkview", we don't know the current directory.
10940 * Don't do this after ":lcd", we don't keep track of what the current
10941 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (buf->b_sfname != NULL
10943 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010944 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010945#ifdef FEAT_AUTOCHDIR
10946 && !p_acd
10947#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010948 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 name = buf->b_sfname;
10950 else
10951 name = buf->b_ffname;
10952 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10953 return FAIL;
10954 return OK;
10955}
10956
10957/*
10958 * Write a file name to the session file.
10959 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10960 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010961 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 */
10963 static int
10964ses_put_fname(fd, name, flagp)
10965 FILE *fd;
10966 char_u *name;
10967 unsigned *flagp;
10968{
10969 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010970 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972
10973 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010974 if (sname == NULL)
10975 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010977 if (*flagp & SSOP_SLASH)
10978 {
10979 /* change all backslashes to forward slashes */
10980 for (p = sname; *p != NUL; mb_ptr_adv(p))
10981 if (*p == '\\')
10982 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010984
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010985 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010986 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010988 if (p == NULL)
10989 return FAIL;
10990
10991 /* write the result */
10992 if (fputs((char *)p, fd) < 0)
10993 retval = FAIL;
10994
10995 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 return retval;
10997}
10998
10999/*
11000 * ":loadview [nr]"
11001 */
11002 static void
11003ex_loadview(eap)
11004 exarg_T *eap;
11005{
11006 char_u *fname;
11007
11008 fname = get_view_file(*eap->arg);
11009 if (fname != NULL)
11010 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011011 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 vim_free(fname);
11013 }
11014}
11015
11016/*
11017 * Get the name of the view file for the current buffer.
11018 */
11019 static char_u *
11020get_view_file(c)
11021 int c;
11022{
11023 int len = 0;
11024 char_u *p, *s;
11025 char_u *retval;
11026 char_u *sname;
11027
11028 if (curbuf->b_ffname == NULL)
11029 {
11030 EMSG(_(e_noname));
11031 return NULL;
11032 }
11033 sname = home_replace_save(NULL, curbuf->b_ffname);
11034 if (sname == NULL)
11035 return NULL;
11036
11037 /*
11038 * We want a file name without separators, because we're not going to make
11039 * a directory.
11040 * "normal" path separator -> "=+"
11041 * "=" -> "=="
11042 * ":" path separator -> "=-"
11043 */
11044 for (p = sname; *p; ++p)
11045 if (*p == '=' || vim_ispathsep(*p))
11046 ++len;
11047 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11048 if (retval != NULL)
11049 {
11050 STRCPY(retval, p_vdir);
11051 add_pathsep(retval);
11052 s = retval + STRLEN(retval);
11053 for (p = sname; *p; ++p)
11054 {
11055 if (*p == '=')
11056 {
11057 *s++ = '=';
11058 *s++ = '=';
11059 }
11060 else if (vim_ispathsep(*p))
11061 {
11062 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011063#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 if (*p == ':')
11065 *s++ = '-';
11066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011068 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 }
11070 else
11071 *s++ = *p;
11072 }
11073 *s++ = '=';
11074 *s++ = c;
11075 STRCPY(s, ".vim");
11076 }
11077
11078 vim_free(sname);
11079 return retval;
11080}
11081
11082#endif /* FEAT_SESSION */
11083
11084/*
11085 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11086 * Return FAIL for a write error.
11087 */
11088 int
11089put_eol(fd)
11090 FILE *fd;
11091{
11092 if (
11093#ifdef USE_CRNL
11094 (
11095# ifdef MKSESSION_NL
11096 !mksession_nl &&
11097# endif
11098 (putc('\r', fd) < 0)) ||
11099#endif
11100 (putc('\n', fd) < 0))
11101 return FAIL;
11102 return OK;
11103}
11104
11105/*
11106 * Write a line to "fd".
11107 * Return FAIL for a write error.
11108 */
11109 int
11110put_line(fd, s)
11111 FILE *fd;
11112 char *s;
11113{
11114 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11115 return FAIL;
11116 return OK;
11117}
11118
11119#ifdef FEAT_VIMINFO
11120/*
11121 * ":rviminfo" and ":wviminfo".
11122 */
11123 static void
11124ex_viminfo(eap)
11125 exarg_T *eap;
11126{
11127 char_u *save_viminfo;
11128
11129 save_viminfo = p_viminfo;
11130 if (*p_viminfo == NUL)
11131 p_viminfo = (char_u *)"'100";
11132 if (eap->cmdidx == CMD_rviminfo)
11133 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011134 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11135 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 EMSG(_("E195: Cannot open viminfo file for reading"));
11137 }
11138 else
11139 write_viminfo(eap->arg, eap->forceit);
11140 p_viminfo = save_viminfo;
11141}
11142#endif
11143
11144#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011145/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011146 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011147 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 void
11150dialog_msg(buff, format, fname)
11151 char_u *buff;
11152 char *format;
11153 char_u *fname;
11154{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 if (fname == NULL)
11156 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011157 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159#endif
11160
11161/*
11162 * ":behave {mswin,xterm}"
11163 */
11164 static void
11165ex_behave(eap)
11166 exarg_T *eap;
11167{
11168 if (STRCMP(eap->arg, "mswin") == 0)
11169 {
11170 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11171 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11172 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11173 set_option_value((char_u *)"keymodel", 0L,
11174 (char_u *)"startsel,stopsel", 0);
11175 }
11176 else if (STRCMP(eap->arg, "xterm") == 0)
11177 {
11178 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11179 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11180 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11181 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11182 }
11183 else
11184 EMSG2(_(e_invarg2), eap->arg);
11185}
11186
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011187#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11188/*
11189 * Function given to ExpandGeneric() to obtain the possible arguments of the
11190 * ":behave {mswin,xterm}" command.
11191 */
11192 char_u *
11193get_behave_arg(xp, idx)
11194 expand_T *xp UNUSED;
11195 int idx;
11196{
11197 if (idx == 0)
11198 return (char_u *)"mswin";
11199 if (idx == 1)
11200 return (char_u *)"xterm";
11201 return NULL;
11202}
11203#endif
11204
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205#ifdef FEAT_AUTOCMD
11206static int filetype_detect = FALSE;
11207static int filetype_plugin = FALSE;
11208static int filetype_indent = FALSE;
11209
11210/*
11211 * ":filetype [plugin] [indent] {on,off,detect}"
11212 * on: Load the filetype.vim file to install autocommands for file types.
11213 * off: Load the ftoff.vim file to remove all autocommands for file types.
11214 * plugin on: load filetype.vim and ftplugin.vim
11215 * plugin off: load ftplugof.vim
11216 * indent on: load filetype.vim and indent.vim
11217 * indent off: load indoff.vim
11218 */
11219 static void
11220ex_filetype(eap)
11221 exarg_T *eap;
11222{
11223 char_u *arg = eap->arg;
11224 int plugin = FALSE;
11225 int indent = FALSE;
11226
11227 if (*eap->arg == NUL)
11228 {
11229 /* Print current status. */
11230 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11231 filetype_detect ? "ON" : "OFF",
11232 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11233 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11234 return;
11235 }
11236
11237 /* Accept "plugin" and "indent" in any order. */
11238 for (;;)
11239 {
11240 if (STRNCMP(arg, "plugin", 6) == 0)
11241 {
11242 plugin = TRUE;
11243 arg = skipwhite(arg + 6);
11244 continue;
11245 }
11246 if (STRNCMP(arg, "indent", 6) == 0)
11247 {
11248 indent = TRUE;
11249 arg = skipwhite(arg + 6);
11250 continue;
11251 }
11252 break;
11253 }
11254 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11255 {
11256 if (*arg == 'o' || !filetype_detect)
11257 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011258 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 filetype_detect = TRUE;
11260 if (plugin)
11261 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011262 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 filetype_plugin = TRUE;
11264 }
11265 if (indent)
11266 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011267 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 filetype_indent = TRUE;
11269 }
11270 }
11271 if (*arg == 'd')
11272 {
11273 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011274 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 }
11276 }
11277 else if (STRCMP(arg, "off") == 0)
11278 {
11279 if (plugin || indent)
11280 {
11281 if (plugin)
11282 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011283 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 filetype_plugin = FALSE;
11285 }
11286 if (indent)
11287 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011288 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 filetype_indent = FALSE;
11290 }
11291 }
11292 else
11293 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011294 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 filetype_detect = FALSE;
11296 }
11297 }
11298 else
11299 EMSG2(_(e_invarg2), arg);
11300}
11301
11302/*
11303 * ":setfiletype {name}"
11304 */
11305 static void
11306ex_setfiletype(eap)
11307 exarg_T *eap;
11308{
11309 if (!did_filetype)
11310 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11311}
11312#endif
11313
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 static void
11315ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011316 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
11318#ifdef FEAT_DIGRAPHS
11319 if (*eap->arg != NUL)
11320 putdigraph(eap->arg);
11321 else
11322 listdigraphs();
11323#else
11324 EMSG(_("E196: No digraphs in this version"));
11325#endif
11326}
11327
11328 static void
11329ex_set(eap)
11330 exarg_T *eap;
11331{
11332 int flags = 0;
11333
11334 if (eap->cmdidx == CMD_setlocal)
11335 flags = OPT_LOCAL;
11336 else if (eap->cmdidx == CMD_setglobal)
11337 flags = OPT_GLOBAL;
11338#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11339 if (cmdmod.browse && flags == 0)
11340 ex_options(eap);
11341 else
11342#endif
11343 (void)do_set(eap->arg, flags);
11344}
11345
11346#ifdef FEAT_SEARCH_EXTRA
11347/*
11348 * ":nohlsearch"
11349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 static void
11351ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011352 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011355 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356}
11357
11358/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011359 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 * Sets nextcmd to the start of the next command, if any. Also called when
11361 * skipping commands to find the next command.
11362 */
11363 static void
11364ex_match(eap)
11365 exarg_T *eap;
11366{
11367 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011368 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 char_u *end;
11370 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011371 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011372
11373 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011374 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011375 else
11376 {
11377 EMSG(e_invcmd);
11378 return;
11379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380
11381 /* First clear any old pattern. */
11382 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011383 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384
11385 if (ends_excmd(*eap->arg))
11386 end = eap->arg;
11387 else if ((STRNICMP(eap->arg, "none", 4) == 0
11388 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11389 end = eap->arg + 4;
11390 else
11391 {
11392 p = skiptowhite(eap->arg);
11393 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011394 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 p = skipwhite(p);
11396 if (*p == NUL)
11397 {
11398 /* There must be two arguments. */
11399 EMSG2(_(e_invarg2), eap->arg);
11400 return;
11401 }
11402 end = skip_regexp(p + 1, *p, TRUE, NULL);
11403 if (!eap->skip)
11404 {
11405 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11406 {
11407 eap->errmsg = e_trailing;
11408 return;
11409 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011410 if (*end != *p)
11411 {
11412 EMSG2(_(e_invarg2), p);
11413 return;
11414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415
11416 c = *end;
11417 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011418 match_add(curwin, g, p + 1, 10, id);
11419 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011420 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 }
11422 }
11423 eap->nextcmd = find_nextcmd(end);
11424}
11425#endif
11426
11427#ifdef FEAT_CRYPT
11428/*
11429 * ":X": Get crypt key
11430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 static void
11432ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011433 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011435 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011436 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437}
11438#endif
11439
11440#ifdef FEAT_FOLDING
11441 static void
11442ex_fold(eap)
11443 exarg_T *eap;
11444{
11445 if (foldManualAllowed(TRUE))
11446 foldCreate(eap->line1, eap->line2);
11447}
11448
11449 static void
11450ex_foldopen(eap)
11451 exarg_T *eap;
11452{
11453 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11454 eap->forceit, FALSE);
11455}
11456
11457 static void
11458ex_folddo(eap)
11459 exarg_T *eap;
11460{
11461 linenr_T lnum;
11462
11463 /* First set the marks for all lines closed/open. */
11464 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11465 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11466 ml_setmarked(lnum);
11467
11468 /* Execute the command on the marked lines. */
11469 global_exe(eap->arg);
11470 ml_clearmarked(); /* clear rest of the marks */
11471}
11472#endif