blob: d0d9f560598bba2573c6e06be3e5a167df382e30 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
287#ifndef FEAT_SNIFF
288# define ex_sniff ex_ni
289#endif
290#ifndef FEAT_KEYMAP
291# define ex_loadkeymap ex_ni
292#endif
293static void ex_swapname __ARGS((exarg_T *eap));
294static void ex_syncbind __ARGS((exarg_T *eap));
295static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_pwd __ARGS((exarg_T *eap));
297static void ex_equal __ARGS((exarg_T *eap));
298static void ex_sleep __ARGS((exarg_T *eap));
299static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
300static void ex_winsize __ARGS((exarg_T *eap));
301#ifdef FEAT_WINDOWS
302static void ex_wincmd __ARGS((exarg_T *eap));
303#else
304# define ex_wincmd ex_ni
305#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_winpos __ARGS((exarg_T *eap));
308#else
309# define ex_winpos ex_ni
310#endif
311static void ex_operators __ARGS((exarg_T *eap));
312static void ex_put __ARGS((exarg_T *eap));
313static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000314static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_submagic __ARGS((exarg_T *eap));
316static void ex_join __ARGS((exarg_T *eap));
317static void ex_at __ARGS((exarg_T *eap));
318static void ex_bang __ARGS((exarg_T *eap));
319static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#ifdef FEAT_PERSISTENT_UNDO
321static void ex_wundo __ARGS((exarg_T *eap));
322static void ex_rundo __ARGS((exarg_T *eap));
323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000325static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redir __ARGS((exarg_T *eap));
327static void ex_redraw __ARGS((exarg_T *eap));
328static void ex_redrawstatus __ARGS((exarg_T *eap));
329static void close_redir __ARGS((void));
330static void ex_mkrc __ARGS((exarg_T *eap));
331static void ex_mark __ARGS((exarg_T *eap));
332#ifdef FEAT_USR_CMDS
333static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000334static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336#ifdef FEAT_EX_EXTRA
337static void ex_normal __ARGS((exarg_T *eap));
338static void ex_startinsert __ARGS((exarg_T *eap));
339static void ex_stopinsert __ARGS((exarg_T *eap));
340#else
341# define ex_normal ex_ni
342# define ex_align ex_ni
343# define ex_retab ex_ni
344# define ex_startinsert ex_ni
345# define ex_stopinsert ex_ni
346# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000347# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349#ifdef FEAT_FIND_ID
350static void ex_checkpath __ARGS((exarg_T *eap));
351static void ex_findpat __ARGS((exarg_T *eap));
352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357static void ex_psearch __ARGS((exarg_T *eap));
358#else
359# define ex_psearch ex_ni
360#endif
361static void ex_tag __ARGS((exarg_T *eap));
362static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392static char_u *arg_all __ARGS((void));
393#ifdef FEAT_SESSION
394static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000395static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static void ex_loadview __ARGS((exarg_T *eap));
397static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
406static void ex_viminfo __ARGS((exarg_T *eap));
407#else
408# define ex_viminfo ex_ni
409#endif
410static void ex_behave __ARGS((exarg_T *eap));
411#ifdef FEAT_AUTOCMD
412static void ex_filetype __ARGS((exarg_T *eap));
413static void ex_setfiletype __ARGS((exarg_T *eap));
414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
426static void ex_digraphs __ARGS((exarg_T *eap));
427static void ex_set __ARGS((exarg_T *eap));
428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
432static void ex_nohlsearch __ARGS((exarg_T *eap));
433static void ex_match __ARGS((exarg_T *eap));
434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
439static void ex_X __ARGS((exarg_T *eap));
440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
444static void ex_fold __ARGS((exarg_T *eap));
445static void ex_foldopen __ARGS((exarg_T *eap));
446static void ex_folddo __ARGS((exarg_T *eap));
447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
550 char_u *(*getline) __ARGS((int, void *, int));
551 void *cookie;
552};
553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
555static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
574static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
575static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576
577 static void
578save_dbg_stuff(dsp)
579 struct dbg_stuff *dsp;
580{
581 dsp->trylevel = trylevel; trylevel = 0;
582 dsp->force_abort = force_abort; force_abort = FALSE;
583 dsp->caught_stack = caught_stack; caught_stack = NULL;
584 dsp->vv_exception = v_exception(NULL);
585 dsp->vv_throwpoint = v_throwpoint(NULL);
586
587 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
588 dsp->did_emsg = did_emsg; did_emsg = FALSE;
589 dsp->got_int = got_int; got_int = FALSE;
590 dsp->did_throw = did_throw; did_throw = FALSE;
591 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
592 dsp->check_cstack = check_cstack; check_cstack = FALSE;
593 dsp->current_exception = current_exception; current_exception = NULL;
594}
595
596 static void
597restore_dbg_stuff(dsp)
598 struct dbg_stuff *dsp;
599{
600 suppress_errthrow = FALSE;
601 trylevel = dsp->trylevel;
602 force_abort = dsp->force_abort;
603 caught_stack = dsp->caught_stack;
604 (void)v_exception(dsp->vv_exception);
605 (void)v_throwpoint(dsp->vv_throwpoint);
606 did_emsg = dsp->did_emsg;
607 got_int = dsp->got_int;
608 did_throw = dsp->did_throw;
609 need_rethrow = dsp->need_rethrow;
610 check_cstack = dsp->check_cstack;
611 current_exception = dsp->current_exception;
612}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614
615
616/*
617 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
618 * command is given.
619 */
620 void
621do_exmode(improved)
622 int improved; /* TRUE for "improved Ex" mode */
623{
624 int save_msg_scroll;
625 int prev_msg_row;
626 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 int changedtick;
628
629 if (improved)
630 exmode_active = EXMODE_VIM;
631 else
632 exmode_active = EXMODE_NORMAL;
633 State = NORMAL;
634
635 /* When using ":global /pat/ visual" and then "Q" we return to continue
636 * the :global command. */
637 if (global_busy)
638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 save_msg_scroll = msg_scroll;
641 ++RedrawingDisabled; /* don't redisplay the window */
642 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_GUI
644 /* Ignore scrollbar and mouse events in Ex mode */
645 ++hold_gui_events;
646#endif
647#ifdef FEAT_SNIFF
648 want_sniff_request = 0; /* No K_SNIFF wanted */
649#endif
650
651 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
652 while (exmode_active)
653 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000654#ifdef FEAT_EX_EXTRA
655 /* Check for a ":normal" command and no more characters left. */
656 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
657 {
658 exmode_active = FALSE;
659 break;
660 }
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 msg_scroll = TRUE;
663 need_wait_return = FALSE;
664 ex_pressedreturn = FALSE;
665 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000666 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 prev_msg_row = msg_row;
668 prev_line = curwin->w_cursor.lnum;
669#ifdef FEAT_SNIFF
670 ProcessSniffRequests();
671#endif
672 if (improved)
673 {
674 cmdline_row = msg_row;
675 do_cmdline(NULL, getexline, NULL, 0);
676 }
677 else
678 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
679 lines_left = Rows - 1;
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if ((prev_line != curwin->w_cursor.lnum
682 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (curbuf->b_ml.ml_flags & ML_EMPTY)
685 EMSG(_(e_emptybuf));
686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (ex_pressedreturn)
689 {
690 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000691 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 msg_row = prev_msg_row;
693 if (prev_msg_row == Rows - 1)
694 msg_row--;
695 }
696 msg_col = 0;
697 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
698 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
702 {
703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
704 EMSG(_(e_emptybuf));
705 else
706 EMSG(_("E501: At end-of-file"));
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710#ifdef FEAT_GUI
711 --hold_gui_events;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 --RedrawingDisabled;
714 --no_wait_return;
715 update_screen(CLEAR);
716 need_wait_return = FALSE;
717 msg_scroll = save_msg_scroll;
718}
719
720/*
721 * Execute a simple command line. Used for translated commands like "*".
722 */
723 int
724do_cmdline_cmd(cmd)
725 char_u *cmd;
726{
727 return do_cmdline(cmd, NULL, NULL,
728 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
729}
730
731/*
732 * do_cmdline(): execute one Ex command line
733 *
734 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * 2. Split up in parts separated with '|'.
737 *
738 * This function can be called recursively!
739 *
740 * flags:
741 * DOCMD_VERBOSE - The command will be included in the error message.
742 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * DOCMD_KEYTYPED - Don't reset KeyTyped.
745 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
746 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
747 *
748 * return FAIL if cmdline could not be executed, OK otherwise
749 */
750 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 char_u *(*fgetline) __ARGS((int, void *, int));
754 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int flags;
756{
757 char_u *next_cmdline; /* next cmd to execute */
758 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 static int recursive = 0; /* recursive depth */
761 int msg_didout_before_start = 0;
762 int count = 0; /* line number count */
763 int did_inc = FALSE; /* incremented RedrawingDisabled */
764 int retval = OK;
765#ifdef FEAT_EVAL
766 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int current_line = 0; /* active line in lines_ga */
769 char_u *fname = NULL; /* function or script name */
770 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
771 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000772 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int initial_trylevel;
774 struct msglist **saved_msg_list = NULL;
775 struct msglist *private_msg_list;
776
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 char_u *(*cmd_getline) __ARGS((int, void *, int));
779 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# define cmd_cookie cookie
786#endif
787 static int call_depth = 0; /* recursiveness */
788
789#ifdef FEAT_EVAL
790 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
791 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000792 * This ensures that the do_errthrow() call in do_one_cmd() does not
793 * combine the messages stored by an earlier invocation of do_one_cmd()
794 * with the command name of the later one. This would happen when
795 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 saved_msg_list = msg_list;
797 msg_list = &private_msg_list;
798 private_msg_list = NULL;
799#endif
800
801 /* It's possible to create an endless loop with ":execute", catch that
802 * here. The value of 200 allows nested function calls, ":source", etc. */
803 if (call_depth == 200)
804 {
805 EMSG(_("E169: Command too recursive"));
806#ifdef FEAT_EVAL
807 /* When converting to an exception, we do not include the command name
808 * since this is not an error of the specific command. */
809 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
810 msg_list = saved_msg_list;
811#endif
812 return FAIL;
813 }
814 ++call_depth;
815
816#ifdef FEAT_EVAL
817 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 cstack.cs_trylevel = 0;
820 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000821 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
823
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++ex_nesting_level;
830
831 /* Get the function or script name and the address where the next breakpoint
832 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = func_name(real_cookie);
836 breakpoint = func_breakpoint(real_cookie);
837 dbg_tick = func_dbg_tick(real_cookie);
838 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 fname = sourcing_name;
842 breakpoint = source_breakpoint(real_cookie);
843 dbg_tick = source_dbg_tick(real_cookie);
844 }
845
846 /*
847 * Initialize "force_abort" and "suppress_errthrow" at the top level.
848 */
849 if (!recursive)
850 {
851 force_abort = FALSE;
852 suppress_errthrow = FALSE;
853 }
854
855 /*
856 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 * exception handling (used when debugging). Otherwise clear it to avoid
858 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000860 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000861 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200863 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 initial_trylevel = trylevel;
866
867 /*
868 * "did_throw" will be set to TRUE when an exception is being thrown.
869 */
870 did_throw = FALSE;
871#endif
872 /*
873 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * If force_abort is set, we cancel everything.
876 */
877 did_emsg = FALSE;
878
879 /*
880 * KeyTyped is only set when calling vgetc(). Reset it here when not
881 * calling vgetc() (sourced command lines).
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (!(flags & DOCMD_KEYTYPED)
884 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100972 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cmd_cookie = cookie;
1006 }
1007#endif
1008
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001145 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001166 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
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
1301 * dealth with, except when it is a syntax error, it may cause
1302 * 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 Moolenaar21691f82012-12-05 19:13:18 +01001737 /* avoid that an autocommand, e.g. QuitPre, does this */
1738 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#endif
1740 )
1741 --quitmore;
1742
1743 /*
1744 * Reset browse, confirm, etc.. They are restored when returning, for
1745 * recursive calls.
1746 */
1747 save_cmdmod = cmdmod;
1748 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1749
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001750 /* "#!anything" is handled like a comment. */
1751 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1752 goto doend;
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 /*
1755 * Repeat until no more command modifiers are found.
1756 */
1757 ea.cmd = *cmdlinep;
1758 for (;;)
1759 {
1760/*
1761 * 1. skip comment lines and leading white space and colons
1762 */
1763 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1764 ++ea.cmd;
1765
1766 /* in ex mode, an empty line works like :+ */
1767 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 && (getline_equal(fgetline, cookie, getexmodeline)
1769 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001770 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
1772 ea.cmd = (char_u *)"+";
1773 ex_pressedreturn = TRUE;
1774 }
1775
1776 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001777 if (*ea.cmd == '"')
1778 goto doend;
1779 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001780 {
1781 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785/*
1786 * 2. handle command modifiers.
1787 */
1788 p = ea.cmd;
1789 if (VIM_ISDIGIT(*ea.cmd))
1790 p = skipwhite(skipdigits(ea.cmd));
1791 switch (*p)
1792 {
1793 /* When adding an entry, also modify cmd_exists(). */
1794 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1795 break;
1796#ifdef FEAT_WINDOWS
1797 cmdmod.split |= WSP_ABOVE;
1798#endif
1799 continue;
1800
1801 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1802 {
1803#ifdef FEAT_WINDOWS
1804 cmdmod.split |= WSP_BELOW;
1805#endif
1806 continue;
1807 }
1808 if (checkforcmd(&ea.cmd, "browse", 3))
1809 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001810#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 cmdmod.browse = TRUE;
1812#endif
1813 continue;
1814 }
1815 if (!checkforcmd(&ea.cmd, "botright", 2))
1816 break;
1817#ifdef FEAT_WINDOWS
1818 cmdmod.split |= WSP_BOT;
1819#endif
1820 continue;
1821
1822 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1823 break;
1824#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1825 cmdmod.confirm = TRUE;
1826#endif
1827 continue;
1828
1829 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1830 {
1831 cmdmod.keepmarks = TRUE;
1832 continue;
1833 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001834 if (checkforcmd(&ea.cmd, "keepalt", 5))
1835 {
1836 cmdmod.keepalt = TRUE;
1837 continue;
1838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1840 break;
1841 cmdmod.keepjumps = TRUE;
1842 continue;
1843
1844 /* ":hide" and ":hide | cmd" are not modifiers */
1845 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1846 || *p == NUL || ends_excmd(*p))
1847 break;
1848 ea.cmd = p;
1849 cmdmod.hide = TRUE;
1850 continue;
1851
1852 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1853 {
1854 cmdmod.lockmarks = TRUE;
1855 continue;
1856 }
1857
1858 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1859 break;
1860#ifdef FEAT_WINDOWS
1861 cmdmod.split |= WSP_ABOVE;
1862#endif
1863 continue;
1864
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001865 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1866 break;
1867#ifdef FEAT_AUTOCMD
1868 if (cmdmod.save_ei == NULL)
1869 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001870 /* Set 'eventignore' to "all". Restore the
1871 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001872 cmdmod.save_ei = vim_strsave(p_ei);
1873 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001874 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001875 }
1876#endif
1877 continue;
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1880 break;
1881#ifdef FEAT_WINDOWS
1882 cmdmod.split |= WSP_BELOW;
1883#endif
1884 continue;
1885
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001886 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1887 {
1888#ifdef HAVE_SANDBOX
1889 if (!did_sandbox)
1890 ++sandbox;
1891 did_sandbox = TRUE;
1892#endif
1893 continue;
1894 }
1895 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001897 if (save_msg_silent == -1)
1898 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1901 {
1902 /* ":silent!", but not "silent !cmd" */
1903 ea.cmd = skipwhite(ea.cmd + 1);
1904 ++emsg_silent;
1905 ++did_esilent;
1906 }
1907 continue;
1908
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001909 case 't': if (checkforcmd(&p, "tab", 3))
1910 {
1911#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001912 if (vim_isdigit(*ea.cmd))
1913 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1914 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001915 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001916 ea.cmd = p;
1917#endif
1918 continue;
1919 }
1920 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 break;
1922#ifdef FEAT_WINDOWS
1923 cmdmod.split |= WSP_TOP;
1924#endif
1925 continue;
1926
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001927 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1928 break;
1929 if (save_msg_silent == -1)
1930 save_msg_silent = msg_silent;
1931 msg_silent = 0;
1932 continue;
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1935 {
1936#ifdef FEAT_VERTSPLIT
1937 cmdmod.split |= WSP_VERT;
1938#endif
1939 continue;
1940 }
1941 if (!checkforcmd(&p, "verbose", 4))
1942 break;
1943 if (verbose_save < 0)
1944 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001945 if (vim_isdigit(*ea.cmd))
1946 p_verbose = atoi((char *)ea.cmd);
1947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 p_verbose = 1;
1949 ea.cmd = p;
1950 continue;
1951 }
1952 break;
1953 }
1954
1955#ifdef FEAT_EVAL
1956 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1957 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1958#else
1959 ea.skip = (if_level > 0);
1960#endif
1961
1962#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001963# ifdef FEAT_PROFILE
1964 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001965 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001966 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001967 if (getline_equal(fgetline, cookie, get_func_line))
1968 func_line_exec(getline_cookie(fgetline, cookie));
1969 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970 script_line_exec();
1971 }
1972#endif
1973
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 /* May go to debug mode. If this happens and the ">quit" debug command is
1975 * used, throw an interrupt exception and skip the next command. */
1976 dbg_check_breakpoint(&ea);
1977 if (!ea.skip && got_int)
1978 {
1979 ea.skip = TRUE;
1980 (void)do_intthrow(cstack);
1981 }
1982#endif
1983
1984/*
1985 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1986 *
1987 * where 'addr' is:
1988 *
1989 * % (entire file)
1990 * $ [+-NUM]
1991 * 'x [+-NUM] (where x denotes a currently defined mark)
1992 * . [+-NUM]
1993 * [+-NUM]..
1994 * NUM
1995 *
1996 * The ea.cmd pointer is updated to point to the first character following the
1997 * range spec. If an initial address is found, but no second, the upper bound
1998 * is equal to the lower.
1999 */
2000
2001 /* repeat for all ',' or ';' separated addresses */
2002 for (;;)
2003 {
2004 ea.line1 = ea.line2;
2005 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2006 ea.cmd = skipwhite(ea.cmd);
2007 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2008 if (ea.cmd == NULL) /* error detected */
2009 goto doend;
2010 if (lnum == MAXLNUM)
2011 {
2012 if (*ea.cmd == '%') /* '%' - all lines */
2013 {
2014 ++ea.cmd;
2015 ea.line1 = 1;
2016 ea.line2 = curbuf->b_ml.ml_line_count;
2017 ++ea.addr_count;
2018 }
2019 /* '*' - visual area */
2020 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2021 {
2022 pos_T *fp;
2023
2024 ++ea.cmd;
2025 if (!ea.skip)
2026 {
2027 fp = getmark('<', FALSE);
2028 if (check_mark(fp) == FAIL)
2029 goto doend;
2030 ea.line1 = fp->lnum;
2031 fp = getmark('>', FALSE);
2032 if (check_mark(fp) == FAIL)
2033 goto doend;
2034 ea.line2 = fp->lnum;
2035 ++ea.addr_count;
2036 }
2037 }
2038 }
2039 else
2040 ea.line2 = lnum;
2041 ea.addr_count++;
2042
2043 if (*ea.cmd == ';')
2044 {
2045 if (!ea.skip)
2046 curwin->w_cursor.lnum = ea.line2;
2047 }
2048 else if (*ea.cmd != ',')
2049 break;
2050 ++ea.cmd;
2051 }
2052
2053 /* One address given: set start and end lines */
2054 if (ea.addr_count == 1)
2055 {
2056 ea.line1 = ea.line2;
2057 /* ... but only implicit: really no address given */
2058 if (lnum == MAXLNUM)
2059 ea.addr_count = 0;
2060 }
2061
2062 /* Don't leave the cursor on an illegal line (caused by ';') */
2063 check_cursor_lnum();
2064
2065/*
2066 * 4. parse command
2067 */
2068
2069 /*
2070 * Skip ':' and any white space
2071 */
2072 ea.cmd = skipwhite(ea.cmd);
2073 while (*ea.cmd == ':')
2074 ea.cmd = skipwhite(ea.cmd + 1);
2075
2076 /*
2077 * If we got a line, but no command, then go to the line.
2078 * If we find a '|' or '\n' we set ea.nextcmd.
2079 */
2080 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2081 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2082 {
2083 /*
2084 * strange vi behaviour:
2085 * ":3" jumps to line 3
2086 * ":3|..." prints line 3
2087 * ":|" prints current line
2088 */
2089 if (ea.skip) /* skip this if inside :if */
2090 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002091 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 ea.cmdidx = CMD_print;
2094 ea.argt = RANGE+COUNT+TRLBAR;
2095 if ((errormsg = invalid_range(&ea)) == NULL)
2096 {
2097 correct_range(&ea);
2098 ex_print(&ea);
2099 }
2100 }
2101 else if (ea.addr_count != 0)
2102 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002103 if (ea.line2 > curbuf->b_ml.ml_line_count)
2104 {
2105 /* With '-' in 'cpoptions' a line number past the file is an
2106 * error, otherwise put it at the end of the file. */
2107 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2108 ea.line2 = -1;
2109 else
2110 ea.line2 = curbuf->b_ml.ml_line_count;
2111 }
2112
2113 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002114 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 else
2116 {
2117 if (ea.line2 == 0)
2118 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
2120 curwin->w_cursor.lnum = ea.line2;
2121 beginline(BL_SOL | BL_FIX);
2122 }
2123 }
2124 goto doend;
2125 }
2126
2127 /* Find the command and let "p" point to after it. */
2128 p = find_command(&ea, NULL);
2129
2130#ifdef FEAT_USR_CMDS
2131 if (p == NULL)
2132 {
2133 if (!ea.skip)
2134 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2135 goto doend;
2136 }
2137 /* Check for wrong commands. */
2138 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2139 {
2140 errormsg = uc_fun_cmd();
2141 goto doend;
2142 }
2143#endif
2144 if (ea.cmdidx == CMD_SIZE)
2145 {
2146 if (!ea.skip)
2147 {
2148 STRCPY(IObuff, _("E492: Not an editor command"));
2149 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002150 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002152 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 goto doend;
2155 }
2156
2157 ni = (
2158#ifdef FEAT_USR_CMDS
2159 !USER_CMDIDX(ea.cmdidx) &&
2160#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002161 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002162#ifdef HAVE_EX_SCRIPT_NI
2163 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2164#endif
2165 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
2167#ifndef FEAT_EVAL
2168 /*
2169 * When the expression evaluation is disabled, recognize the ":if" and
2170 * ":endif" commands and ignore everything in between it.
2171 */
2172 if (ea.cmdidx == CMD_if)
2173 ++if_level;
2174 if (if_level)
2175 {
2176 if (ea.cmdidx == CMD_endif)
2177 --if_level;
2178 goto doend;
2179 }
2180
2181#endif
2182
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002183 /* forced commands */
2184 if (*p == '!' && ea.cmdidx != CMD_substitute
2185 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 ++p;
2188 ea.forceit = TRUE;
2189 }
2190 else
2191 ea.forceit = FALSE;
2192
2193/*
2194 * 5. parse arguments
2195 */
2196#ifdef FEAT_USR_CMDS
2197 if (!USER_CMDIDX(ea.cmdidx))
2198#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002199 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 if (!ea.skip)
2202 {
2203#ifdef HAVE_SANDBOX
2204 if (sandbox != 0 && !(ea.argt & SBOXOK))
2205 {
2206 /* Command not allowed in sandbox. */
2207 errormsg = (char_u *)_(e_sandbox);
2208 goto doend;
2209 }
2210#endif
2211 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2212 {
2213 /* Command not allowed in non-'modifiable' buffer */
2214 errormsg = (char_u *)_(e_modifiable);
2215 goto doend;
2216 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002217
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002218 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219# ifdef FEAT_USR_CMDS
2220 && !USER_CMDIDX(ea.cmdidx)
2221# endif
2222 )
2223 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002224 /* Command not allowed when editing the command line. */
2225#ifdef FEAT_CMDWIN
2226 if (cmdwin_type != 0)
2227 errormsg = (char_u *)_(e_cmdwin);
2228 else
2229#endif
2230 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 goto doend;
2232 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002234 /* Disallow editing another buffer when "curbuf_lock" is set.
2235 * Do allow ":edit" (check for argument later).
2236 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002238 && ea.cmdidx != CMD_edit
2239 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240# ifdef FEAT_USR_CMDS
2241 && !USER_CMDIDX(ea.cmdidx)
2242# endif
2243 && curbuf_locked())
2244 goto doend;
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2248 {
2249 /* no range allowed */
2250 errormsg = (char_u *)_(e_norange);
2251 goto doend;
2252 }
2253 }
2254
2255 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2256 {
2257 errormsg = (char_u *)_(e_nobang);
2258 goto doend;
2259 }
2260
2261 /*
2262 * Don't complain about the range if it is not used
2263 * (could happen if line_count is accidentally set to 0).
2264 */
2265 if (!ea.skip && !ni)
2266 {
2267 /*
2268 * If the range is backwards, ask for confirmation and, if given, swap
2269 * ea.line1 & ea.line2 so it's forwards again.
2270 * When global command is busy, don't ask, will fail below.
2271 */
2272 if (!global_busy && ea.line1 > ea.line2)
2273 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002274 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002276 if (sourcing || exmode_active)
2277 {
2278 errormsg = (char_u *)_("E493: Backwards range given");
2279 goto doend;
2280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (ask_yesno((char_u *)
2282 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002283 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285 lnum = ea.line1;
2286 ea.line1 = ea.line2;
2287 ea.line2 = lnum;
2288 }
2289 if ((errormsg = invalid_range(&ea)) != NULL)
2290 goto doend;
2291 }
2292
2293 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2294 ea.line2 = 1;
2295
2296 correct_range(&ea);
2297
2298#ifdef FEAT_FOLDING
2299 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2300 {
2301 /* Put the first line at the start of a closed fold, put the last line
2302 * at the end of a closed fold. */
2303 (void)hasFolding(ea.line1, &ea.line1, NULL);
2304 (void)hasFolding(ea.line2, NULL, &ea.line2);
2305 }
2306#endif
2307
2308#ifdef FEAT_QUICKFIX
2309 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002310 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 * option here, so things like % get expanded.
2312 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002313 p = replace_makeprg(&ea, p, cmdlinep);
2314 if (p == NULL)
2315 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#endif
2317
2318 /*
2319 * Skip to start of argument.
2320 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2321 */
2322 if (ea.cmdidx == CMD_bang)
2323 ea.arg = p;
2324 else
2325 ea.arg = skipwhite(p);
2326
2327 /*
2328 * Check for "++opt=val" argument.
2329 * Must be first, allow ":w ++enc=utf8 !cmd"
2330 */
2331 if (ea.argt & ARGOPT)
2332 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2333 if (getargopt(&ea) == FAIL && !ni)
2334 {
2335 errormsg = (char_u *)_(e_invarg);
2336 goto doend;
2337 }
2338
2339 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2340 {
2341 if (*ea.arg == '>') /* append */
2342 {
2343 if (*++ea.arg != '>') /* typed wrong */
2344 {
2345 errormsg = (char_u *)_("E494: Use w or w>>");
2346 goto doend;
2347 }
2348 ea.arg = skipwhite(ea.arg + 1);
2349 ea.append = TRUE;
2350 }
2351 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2352 {
2353 ++ea.arg;
2354 ea.usefilter = TRUE;
2355 }
2356 }
2357
2358 if (ea.cmdidx == CMD_read)
2359 {
2360 if (ea.forceit)
2361 {
2362 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2363 ea.forceit = FALSE;
2364 }
2365 else if (*ea.arg == '!') /* :r !filter */
2366 {
2367 ++ea.arg;
2368 ea.usefilter = TRUE;
2369 }
2370 }
2371
2372 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2373 {
2374 ea.amount = 1;
2375 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2376 {
2377 ++ea.arg;
2378 ++ea.amount;
2379 }
2380 ea.arg = skipwhite(ea.arg);
2381 }
2382
2383 /*
2384 * Check for "+command" argument, before checking for next command.
2385 * Don't do this for ":read !cmd" and ":write !cmd".
2386 */
2387 if ((ea.argt & EDITCMD) && !ea.usefilter)
2388 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2389
2390 /*
2391 * Check for '|' to separate commands and '"' to start comments.
2392 * Don't do this for ":read !cmd" and ":write !cmd".
2393 */
2394 if ((ea.argt & TRLBAR) && !ea.usefilter)
2395 separate_nextcmd(&ea);
2396
2397 /*
2398 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002399 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2400 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002402 else if (ea.cmdidx == CMD_bang
2403 || ea.cmdidx == CMD_global
2404 || ea.cmdidx == CMD_vglobal
2405 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 for (p = ea.arg; *p; ++p)
2408 {
2409 /* Remove one backslash before a newline, so that it's possible to
2410 * pass a newline to the shell and also a newline that is preceded
2411 * with a backslash. This makes it impossible to end a shell
2412 * command in a backslash, but that doesn't appear useful.
2413 * Halving the number of backslashes is incompatible with previous
2414 * versions. */
2415 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002416 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 else if (*p == '\n')
2418 {
2419 ea.nextcmd = p + 1;
2420 *p = NUL;
2421 break;
2422 }
2423 }
2424 }
2425
2426 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2427 {
2428 ea.line1 = 1;
2429 ea.line2 = curbuf->b_ml.ml_line_count;
2430 }
2431
2432 /* accept numbered register only when no count allowed (:put) */
2433 if ( (ea.argt & REGSTR)
2434 && *ea.arg != NUL
2435#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 /* Do not allow register = for user commands */
2437 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#endif
2439 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2440 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002441#ifndef FEAT_CLIPBOARD
2442 /* check these explicitly for a more specific error message */
2443 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002445 errormsg = (char_u *)_(e_invalidreg);
2446 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002449 if (
2450#ifdef FEAT_USR_CMDS
2451 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2452 && USER_CMDIDX(ea.cmdidx)))
2453#else
2454 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2455#endif
2456 )
2457 {
2458 ea.regname = *ea.arg++;
2459#ifdef FEAT_EVAL
2460 /* for '=' register: accept the rest of the line as an expression */
2461 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2462 {
2463 set_expr_line(vim_strsave(ea.arg));
2464 ea.arg += STRLEN(ea.arg);
2465 }
2466#endif
2467 ea.arg = skipwhite(ea.arg);
2468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470
2471 /*
2472 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2473 * count, it's a buffer name.
2474 */
2475 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2476 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2477 || vim_iswhite(*p)))
2478 {
2479 n = getdigits(&ea.arg);
2480 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002481 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
2483 errormsg = (char_u *)_(e_zerocount);
2484 goto doend;
2485 }
2486 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2487 {
2488 ea.line2 = n;
2489 if (ea.addr_count == 0)
2490 ea.addr_count = 1;
2491 }
2492 else
2493 {
2494 ea.line1 = ea.line2;
2495 ea.line2 += n - 1;
2496 ++ea.addr_count;
2497 /*
2498 * Be vi compatible: no error message for out of range.
2499 */
2500 if (ea.line2 > curbuf->b_ml.ml_line_count)
2501 ea.line2 = curbuf->b_ml.ml_line_count;
2502 }
2503 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002504
2505 /*
2506 * Check for flags: 'l', 'p' and '#'.
2507 */
2508 if (ea.argt & EXFLAGS)
2509 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002511 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002512 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 errormsg = (char_u *)_(e_trailing);
2515 goto doend;
2516 }
2517
2518 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2519 {
2520 errormsg = (char_u *)_(e_argreq);
2521 goto doend;
2522 }
2523
2524#ifdef FEAT_EVAL
2525 /*
2526 * Skip the command when it's not going to be executed.
2527 * The commands like :if, :endif, etc. always need to be executed.
2528 * Also make an exception for commands that handle a trailing command
2529 * themselves.
2530 */
2531 if (ea.skip)
2532 {
2533 switch (ea.cmdidx)
2534 {
2535 /* commands that need evaluation */
2536 case CMD_while:
2537 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002538 case CMD_for:
2539 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 case CMD_if:
2541 case CMD_elseif:
2542 case CMD_else:
2543 case CMD_endif:
2544 case CMD_try:
2545 case CMD_catch:
2546 case CMD_finally:
2547 case CMD_endtry:
2548 case CMD_function:
2549 break;
2550
2551 /* Commands that handle '|' themselves. Check: A command should
2552 * either have the TRLBAR flag, appear in this list or appear in
2553 * the list at ":help :bar". */
2554 case CMD_aboveleft:
2555 case CMD_and:
2556 case CMD_belowright:
2557 case CMD_botright:
2558 case CMD_browse:
2559 case CMD_call:
2560 case CMD_confirm:
2561 case CMD_delfunction:
2562 case CMD_djump:
2563 case CMD_dlist:
2564 case CMD_dsearch:
2565 case CMD_dsplit:
2566 case CMD_echo:
2567 case CMD_echoerr:
2568 case CMD_echomsg:
2569 case CMD_echon:
2570 case CMD_execute:
2571 case CMD_help:
2572 case CMD_hide:
2573 case CMD_ijump:
2574 case CMD_ilist:
2575 case CMD_isearch:
2576 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002577 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 case CMD_keepjumps:
2579 case CMD_keepmarks:
2580 case CMD_leftabove:
2581 case CMD_let:
2582 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002583 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002585 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 case CMD_perl:
2587 case CMD_psearch:
2588 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002589 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002590 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 case CMD_return:
2592 case CMD_rightbelow:
2593 case CMD_ruby:
2594 case CMD_silent:
2595 case CMD_smagic:
2596 case CMD_snomagic:
2597 case CMD_substitute:
2598 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002599 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 case CMD_tcl:
2601 case CMD_throw:
2602 case CMD_tilde:
2603 case CMD_topleft:
2604 case CMD_unlet:
2605 case CMD_verbose:
2606 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002607 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 break;
2609
2610 default: goto doend;
2611 }
2612 }
2613#endif
2614
2615 if (ea.argt & XFILE)
2616 {
2617 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2618 goto doend;
2619 }
2620
2621#ifdef FEAT_LISTCMDS
2622 /*
2623 * Accept buffer name. Cannot be used at the same time with a buffer
2624 * number. Don't do this for a user command.
2625 */
2626 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2627# ifdef FEAT_USR_CMDS
2628 && !USER_CMDIDX(ea.cmdidx)
2629# endif
2630 )
2631 {
2632 /*
2633 * :bdelete, :bwipeout and :bunload take several arguments, separated
2634 * by spaces: find next space (skipping over escaped characters).
2635 * The others take one argument: ignore trailing spaces.
2636 */
2637 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2638 || ea.cmdidx == CMD_bunload)
2639 p = skiptowhite_esc(ea.arg);
2640 else
2641 {
2642 p = ea.arg + STRLEN(ea.arg);
2643 while (p > ea.arg && vim_iswhite(p[-1]))
2644 --p;
2645 }
2646 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2647 if (ea.line2 < 0) /* failed */
2648 goto doend;
2649 ea.addr_count = 1;
2650 ea.arg = skipwhite(p);
2651 }
2652#endif
2653
2654/*
2655 * 6. switch on command name
2656 *
2657 * The "ea" structure holds the arguments that can be used.
2658 */
2659 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002660 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 ea.cookie = cookie;
2662#ifdef FEAT_EVAL
2663 ea.cstack = cstack;
2664#endif
2665
2666#ifdef FEAT_USR_CMDS
2667 if (USER_CMDIDX(ea.cmdidx))
2668 {
2669 /*
2670 * Execute a user-defined command.
2671 */
2672 do_ucmd(&ea);
2673 }
2674 else
2675#endif
2676 {
2677 /*
2678 * Call the function to execute the command.
2679 */
2680 ea.errmsg = NULL;
2681 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2682 if (ea.errmsg != NULL)
2683 errormsg = (char_u *)_(ea.errmsg);
2684 }
2685
2686#ifdef FEAT_EVAL
2687 /*
2688 * If the command just executed called do_cmdline(), any throw or ":return"
2689 * or ":finish" encountered there must also check the cstack of the still
2690 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2691 * exception, or reanimate a returned function or finished script file and
2692 * return or finish it again.
2693 */
2694 if (need_rethrow)
2695 do_throw(cstack);
2696 else if (check_cstack)
2697 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002698 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 && current_func_returned())
2702 do_return(&ea, TRUE, FALSE, NULL);
2703 }
2704 need_rethrow = check_cstack = FALSE;
2705#endif
2706
2707doend:
2708 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2709 curwin->w_cursor.lnum = 1;
2710
2711 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2712 {
2713 if (sourcing)
2714 {
2715 if (errormsg != IObuff)
2716 {
2717 STRCPY(IObuff, errormsg);
2718 errormsg = IObuff;
2719 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002720 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 emsg(errormsg);
2723 }
2724#ifdef FEAT_EVAL
2725 do_errthrow(cstack,
2726 (ea.cmdidx != CMD_SIZE
2727# ifdef FEAT_USR_CMDS
2728 && !USER_CMDIDX(ea.cmdidx)
2729# endif
2730 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2731#endif
2732
2733 if (verbose_save >= 0)
2734 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002735#ifdef FEAT_AUTOCMD
2736 if (cmdmod.save_ei != NULL)
2737 {
2738 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002739 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2740 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002741 free_string_option(cmdmod.save_ei);
2742 }
2743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 cmdmod = save_cmdmod;
2746
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002747 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
2749 /* messages could be enabled for a serious error, need to check if the
2750 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002751 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002752 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 emsg_silent -= did_esilent;
2754 if (emsg_silent < 0)
2755 emsg_silent = 0;
2756 /* Restore msg_scroll, it's set by file I/O commands, even when no
2757 * message is actually displayed. */
2758 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002759
2760 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2761 * somewhere in the line. Put it back in the first column. */
2762 if (redirecting())
2763 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002766#ifdef HAVE_SANDBOX
2767 if (did_sandbox)
2768 --sandbox;
2769#endif
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2772 ea.nextcmd = NULL;
2773
2774#ifdef FEAT_EVAL
2775 --ex_nesting_level;
2776#endif
2777
2778 return ea.nextcmd;
2779}
2780#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002781 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782#endif
2783
2784/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002785 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 * If there is a match advance "pp" to the argument and return TRUE.
2787 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002788 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002790 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 char *cmd; /* name of command */
2792 int len; /* required length */
2793{
2794 int i;
2795
2796 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002797 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 break;
2799 if (i >= len && !isalpha((*pp)[i]))
2800 {
2801 *pp = skipwhite(*pp + i);
2802 return TRUE;
2803 }
2804 return FALSE;
2805}
2806
2807/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002808 * Append "cmd" to the error message in IObuff.
2809 * Takes care of limiting the length and handling 0xa0, which would be
2810 * invisible otherwise.
2811 */
2812 static void
2813append_command(cmd)
2814 char_u *cmd;
2815{
2816 char_u *s = cmd;
2817 char_u *d;
2818
2819 STRCAT(IObuff, ": ");
2820 d = IObuff + STRLEN(IObuff);
2821 while (*s != NUL && d - IObuff < IOSIZE - 7)
2822 {
2823 if (
2824#ifdef FEAT_MBYTE
2825 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2826#endif
2827 *s == 0xa0)
2828 {
2829 s +=
2830#ifdef FEAT_MBYTE
2831 enc_utf8 ? 2 :
2832#endif
2833 1;
2834 STRCPY(d, "<a0>");
2835 d += 4;
2836 }
2837 else
2838 MB_COPY_CHAR(s, d);
2839 }
2840 *d = NUL;
2841}
2842
2843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002845 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002847 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 * Returns NULL for an ambiguous user command.
2849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 static char_u *
2851find_command(eap, full)
2852 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002853 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 int len;
2856 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002857 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /*
2860 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002861 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 * - the 'k' command can directly be followed by any character.
2863 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2864 * but :sre[wind] is another command, as are :scrip[tnames],
2865 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002866 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
2868 p = eap->cmd;
2869 if (*p == 'k')
2870 {
2871 eap->cmdidx = CMD_k;
2872 ++p;
2873 }
2874 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002875 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2876 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 || p[1] == 'g'
2878 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2879 || p[1] == 'I'
2880 || (p[1] == 'r' && p[2] != 'e')))
2881 {
2882 eap->cmdidx = CMD_substitute;
2883 ++p;
2884 }
2885 else
2886 {
2887 while (ASCII_ISALPHA(*p))
2888 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002889 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002890 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002891 while (ASCII_ISALNUM(*p))
2892 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 /* check for non-alpha command */
2895 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2896 ++p;
2897 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002898 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2899 {
2900 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2901 * :delete with the 'l' flag. Same for 'p'. */
2902 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002903 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002904 break;
2905 if (i == len - 1)
2906 {
2907 --len;
2908 if (p[-1] == 'l')
2909 eap->flags |= EXFLAG_LIST;
2910 else
2911 eap->flags |= EXFLAG_PRINT;
2912 }
2913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 if (ASCII_ISLOWER(*eap->cmd))
2916 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2917 else
2918 eap->cmdidx = cmdidxs[26];
2919
2920 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2921 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2922 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2923 (size_t)len) == 0)
2924 {
2925#ifdef FEAT_EVAL
2926 if (full != NULL
2927 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2928 *full = TRUE;
2929#endif
2930 break;
2931 }
2932
2933#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002934 /* Look for a user defined command as a last resort. Let ":Print" be
2935 * overruled by a user defined command. */
2936 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2937 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002939 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 while (ASCII_ISALNUM(*p))
2941 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002942 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002945 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 eap->cmdidx = CMD_SIZE;
2947 }
2948
2949 return p;
2950}
2951
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002952#ifdef FEAT_USR_CMDS
2953/*
2954 * Search for a user command that matches "eap->cmd".
2955 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2956 * Return a pointer to just after the command.
2957 * Return NULL if there is no matching command.
2958 */
2959 static char_u *
2960find_ucmd(eap, p, full, xp, compl)
2961 exarg_T *eap;
2962 char_u *p; /* end of the command (possibly including count) */
2963 int *full; /* set to TRUE for a full match */
2964 expand_T *xp; /* used for completion, NULL otherwise */
2965 int *compl; /* completion flags or NULL */
2966{
2967 int len = (int)(p - eap->cmd);
2968 int j, k, matchlen = 0;
2969 ucmd_T *uc;
2970 int found = FALSE;
2971 int possible = FALSE;
2972 char_u *cp, *np; /* Point into typed cmd and test name */
2973 garray_T *gap;
2974 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2975 only full match global is accepted. */
2976
2977 /*
2978 * Look for buffer-local user commands first, then global ones.
2979 */
2980 gap = &curbuf->b_ucmds;
2981 for (;;)
2982 {
2983 for (j = 0; j < gap->ga_len; ++j)
2984 {
2985 uc = USER_CMD_GA(gap, j);
2986 cp = eap->cmd;
2987 np = uc->uc_name;
2988 k = 0;
2989 while (k < len && *np != NUL && *cp++ == *np++)
2990 k++;
2991 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2992 {
2993 /* If finding a second match, the command is ambiguous. But
2994 * not if a buffer-local command wasn't a full match and a
2995 * global command is a full match. */
2996 if (k == len && found && *np != NUL)
2997 {
2998 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002999 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003000 amb_local = TRUE;
3001 }
3002
3003 if (!found || (k == len && *np == NUL))
3004 {
3005 /* If we matched up to a digit, then there could
3006 * be another command including the digit that we
3007 * should use instead.
3008 */
3009 if (k == len)
3010 found = TRUE;
3011 else
3012 possible = TRUE;
3013
3014 if (gap == &ucmds)
3015 eap->cmdidx = CMD_USER;
3016 else
3017 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003018 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003019 eap->useridx = j;
3020
3021# ifdef FEAT_CMDL_COMPL
3022 if (compl != NULL)
3023 *compl = uc->uc_compl;
3024# ifdef FEAT_EVAL
3025 if (xp != NULL)
3026 {
3027 xp->xp_arg = uc->uc_compl_arg;
3028 xp->xp_scriptID = uc->uc_scriptID;
3029 }
3030# endif
3031# endif
3032 /* Do not search for further abbreviations
3033 * if this is an exact match. */
3034 matchlen = k;
3035 if (k == len && *np == NUL)
3036 {
3037 if (full != NULL)
3038 *full = TRUE;
3039 amb_local = FALSE;
3040 break;
3041 }
3042 }
3043 }
3044 }
3045
3046 /* Stop if we found a full match or searched all. */
3047 if (j < gap->ga_len || gap == &ucmds)
3048 break;
3049 gap = &ucmds;
3050 }
3051
3052 /* Only found ambiguous matches. */
3053 if (amb_local)
3054 {
3055 if (xp != NULL)
3056 xp->xp_context = EXPAND_UNSUCCESSFUL;
3057 return NULL;
3058 }
3059
3060 /* The match we found may be followed immediately by a number. Move "p"
3061 * back to point to it. */
3062 if (found || possible)
3063 return p + (matchlen - len);
3064 return p;
3065}
3066#endif
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003069static struct cmdmod
3070{
3071 char *name;
3072 int minlen;
3073 int has_count; /* :123verbose :3tab */
3074} cmdmods[] = {
3075 {"aboveleft", 3, FALSE},
3076 {"belowright", 3, FALSE},
3077 {"botright", 2, FALSE},
3078 {"browse", 3, FALSE},
3079 {"confirm", 4, FALSE},
3080 {"hide", 3, FALSE},
3081 {"keepalt", 5, FALSE},
3082 {"keepjumps", 5, FALSE},
3083 {"keepmarks", 3, FALSE},
3084 {"leftabove", 5, FALSE},
3085 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003086 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003087 {"rightbelow", 6, FALSE},
3088 {"sandbox", 3, FALSE},
3089 {"silent", 3, FALSE},
3090 {"tab", 3, TRUE},
3091 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003092 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003093 {"verbose", 4, TRUE},
3094 {"vertical", 4, FALSE},
3095};
3096
3097/*
3098 * Return length of a command modifier (including optional count).
3099 * Return zero when it's not a modifier.
3100 */
3101 int
3102modifier_len(cmd)
3103 char_u *cmd;
3104{
3105 int i, j;
3106 char_u *p = cmd;
3107
3108 if (VIM_ISDIGIT(*cmd))
3109 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003110 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003111 {
3112 for (j = 0; p[j] != NUL; ++j)
3113 if (p[j] != cmdmods[i].name[j])
3114 break;
3115 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3116 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003117 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003118 }
3119 return 0;
3120}
3121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122/*
3123 * Return > 0 if an Ex command "name" exists.
3124 * Return 2 if there is an exact match.
3125 * Return 3 if there is an ambiguous match.
3126 */
3127 int
3128cmd_exists(name)
3129 char_u *name;
3130{
3131 exarg_T ea;
3132 int full = FALSE;
3133 int i;
3134 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003135 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003138 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 for (j = 0; name[j] != NUL; ++j)
3141 if (name[j] != cmdmods[i].name[j])
3142 break;
3143 if (name[j] == NUL && j >= cmdmods[i].minlen)
3144 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3145 }
3146
Bram Moolenaara9587612006-05-04 21:47:50 +00003147 /* Check built-in commands and user defined commands.
3148 * For ":2match" and ":3match" we need to skip the number. */
3149 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003151 p = find_command(&ea, &full);
3152 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003154 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3155 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003156 if (*skipwhite(p) != NUL)
3157 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3159}
3160#endif
3161
3162/*
3163 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3164 * we don't need/want deleted. Maybe this could be done better if we didn't
3165 * repeat all this stuff. The only problem is that they may not stay
3166 * perfectly compatible with each other, but then the command line syntax
3167 * probably won't change that much -- webb.
3168 */
3169 char_u *
3170set_one_cmd_context(xp, buff)
3171 expand_T *xp;
3172 char_u *buff; /* buffer for command string */
3173{
3174 char_u *p;
3175 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003176 int len = 0;
3177 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3179 int compl = EXPAND_NOTHING;
3180#endif
3181#ifdef FEAT_CMDL_COMPL
3182 int delim;
3183#endif
3184 int forceit = FALSE;
3185 int usefilter = FALSE; /* filter instead of file name */
3186
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003187 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 xp->xp_pattern = buff;
3189 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192/*
3193 * 2. skip comment lines and leading space, colons or bars
3194 */
3195 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3196 ;
3197 xp->xp_pattern = cmd;
3198
3199 if (*cmd == NUL)
3200 return NULL;
3201 if (*cmd == '"') /* ignore comment lines */
3202 {
3203 xp->xp_context = EXPAND_NOTHING;
3204 return NULL;
3205 }
3206
3207/*
3208 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3209 */
3210 cmd = skip_range(cmd, &xp->xp_context);
3211
3212/*
3213 * 4. parse command
3214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 xp->xp_pattern = cmd;
3216 if (*cmd == NUL)
3217 return NULL;
3218 if (*cmd == '"')
3219 {
3220 xp->xp_context = EXPAND_NOTHING;
3221 return NULL;
3222 }
3223
3224 if (*cmd == '|' || *cmd == '\n')
3225 return cmd + 1; /* There's another command */
3226
3227 /*
3228 * Isolate the command and search for it in the command table.
3229 * Exceptions:
3230 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003231 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3233 */
3234 if (*cmd == 'k' && cmd[1] != 'e')
3235 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 p = cmd + 1;
3238 }
3239 else
3240 {
3241 p = cmd;
3242 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3243 ++p;
3244 /* check for non-alpha command */
3245 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3246 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
3251 xp->xp_context = EXPAND_UNSUCCESSFUL;
3252 return NULL;
3253 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003255 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3256 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3257 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 break;
3259
3260#ifdef FEAT_USR_CMDS
3261 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3263 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#endif
3265 }
3266
3267 /*
3268 * If the cursor is touching the command, and it ends in an alpha-numeric
3269 * character, complete the command name.
3270 */
3271 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3272 return NULL;
3273
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003274 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
3276 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3277 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 p = cmd + 1;
3280 }
3281#ifdef FEAT_USR_CMDS
3282 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3283 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 ea.cmd = cmd;
3285 p = find_ucmd(&ea, p, NULL, xp,
3286# if defined(FEAT_CMDL_COMPL)
3287 &compl
3288# else
3289 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003291 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003292 if (p == NULL)
3293 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295#endif
3296 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003297 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
3299 /* Not still touching the command and it was an illegal one */
3300 xp->xp_context = EXPAND_UNSUCCESSFUL;
3301 return NULL;
3302 }
3303
3304 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3305
3306 if (*p == '!') /* forced commands */
3307 {
3308 forceit = TRUE;
3309 ++p;
3310 }
3311
3312/*
3313 * 5. parse arguments
3314 */
3315#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003316 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003318 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
3320 arg = skipwhite(p);
3321
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 if (*arg == '>') /* append */
3325 {
3326 if (*++arg == '>')
3327 ++arg;
3328 arg = skipwhite(arg);
3329 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
3332 ++arg;
3333 usefilter = TRUE;
3334 }
3335 }
3336
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003337 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
3339 usefilter = forceit; /* :r! filter if forced */
3340 if (*arg == '!') /* :r !filter */
3341 {
3342 ++arg;
3343 usefilter = TRUE;
3344 }
3345 }
3346
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003347 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
3349 while (*arg == *cmd) /* allow any number of '>' or '<' */
3350 ++arg;
3351 arg = skipwhite(arg);
3352 }
3353
3354 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003355 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 /* Check if we're in the +command */
3358 p = arg + 1;
3359 arg = skip_cmd_arg(arg, FALSE);
3360
3361 /* Still touching the command after '+'? */
3362 if (*arg == NUL)
3363 return p;
3364
3365 /* Skip space(s) after +command to get to the real argument */
3366 arg = skipwhite(arg);
3367 }
3368
3369 /*
3370 * Check for '|' to separate commands and '"' to start comments.
3371 * Don't do this for ":read !cmd" and ":write !cmd".
3372 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003373 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
3375 p = arg;
3376 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003377 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 p += 2;
3379 while (*p)
3380 {
3381 if (*p == Ctrl_V)
3382 {
3383 if (p[1] != NUL)
3384 ++p;
3385 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003386 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 || *p == '|' || *p == '\n')
3388 {
3389 if (*(p - 1) != '\\')
3390 {
3391 if (*p == '|' || *p == '\n')
3392 return p + 1;
3393 return NULL; /* It's a comment */
3394 }
3395 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003396 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 }
3399
3400 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003401 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 vim_strchr((char_u *)"|\"", *arg) == NULL)
3403 return NULL;
3404
3405 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003406 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003408 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003409 while (*p && p < buff + len)
3410 {
3411 if (*p == ' ' || *p == TAB)
3412 {
3413 /* argument starts after a space */
3414 xp->xp_pattern = ++p;
3415 }
3416 else
3417 {
3418 if (*p == '\\' && *(p + 1) != NUL)
3419 ++p; /* skip over escaped character */
3420 mb_ptr_adv(p);
3421 }
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003424 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003426 int c;
3427 int in_quote = FALSE;
3428 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /*
3431 * Allow spaces within back-quotes to count as part of the argument
3432 * being expanded.
3433 */
3434 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003435 p = xp->xp_pattern;
3436 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003438#ifdef FEAT_MBYTE
3439 if (has_mbyte)
3440 c = mb_ptr2char(p);
3441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003443 c = *p;
3444 if (c == '\\' && p[1] != NUL)
3445 ++p;
3446 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 if (!in_quote)
3449 {
3450 xp->xp_pattern = p;
3451 bow = p + 1;
3452 }
3453 in_quote = !in_quote;
3454 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003455 /* An argument can contain just about everything, except
3456 * characters that end the command and white space. */
3457 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003458#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003459 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003460#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003461 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003462 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003463 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003464 while (*p != NUL)
3465 {
3466#ifdef FEAT_MBYTE
3467 if (has_mbyte)
3468 c = mb_ptr2char(p);
3469 else
3470#endif
3471 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003472 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003473 break;
3474#ifdef FEAT_MBYTE
3475 if (has_mbyte)
3476 len = (*mb_ptr2len)(p);
3477 else
3478#endif
3479 len = 1;
3480 mb_ptr_adv(p);
3481 }
3482 if (in_quote)
3483 bow = p;
3484 else
3485 xp->xp_pattern = p;
3486 p -= len;
3487 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003488 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490
3491 /*
3492 * If we are still inside the quotes, and we passed a space, just
3493 * expand from there.
3494 */
3495 if (bow != NULL && in_quote)
3496 xp->xp_pattern = bow;
3497 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003498
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003499 /* For a shell command more chars need to be escaped. */
3500 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003502#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003503 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003504#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003505 /* When still after the command name expand executables. */
3506 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003507 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 /* Check for environment variable */
3511 if (*xp->xp_pattern == '$'
3512#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3513 || *xp->xp_pattern == '%'
3514#endif
3515 )
3516 {
3517 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3518 if (!vim_isIDc(*p))
3519 break;
3520 if (*p == NUL)
3521 {
3522 xp->xp_context = EXPAND_ENV_VARS;
3523 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003524#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3525 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003526 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003527 compl = EXPAND_ENV_VARS;
3528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003531#if defined(FEAT_CMDL_COMPL)
3532 /* Check for user names */
3533 if (*xp->xp_pattern == '~')
3534 {
3535 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3536 ;
3537 /* Complete ~user only if it partially matches a user name.
3538 * A full match ~user<Tab> will be replaced by user's home
3539 * directory i.e. something like ~user<Tab> -> /home/user/ */
3540 if (*p == NUL && p > xp->xp_pattern + 1
3541 && match_user(xp->xp_pattern + 1) == 1)
3542 {
3543 xp->xp_context = EXPAND_USER;
3544 ++xp->xp_pattern;
3545 }
3546 }
3547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549
3550/*
3551 * 6. switch on command name
3552 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003553 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003555 case CMD_find:
3556 case CMD_sfind:
3557 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003558 if (xp->xp_context == EXPAND_FILES)
3559 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 case CMD_cd:
3562 case CMD_chdir:
3563 case CMD_lcd:
3564 case CMD_lchdir:
3565 if (xp->xp_context == EXPAND_FILES)
3566 xp->xp_context = EXPAND_DIRECTORIES;
3567 break;
3568 case CMD_help:
3569 xp->xp_context = EXPAND_HELP;
3570 xp->xp_pattern = arg;
3571 break;
3572
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003573 /* Command modifiers: return the argument.
3574 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003576 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 case CMD_belowright:
3578 case CMD_botright:
3579 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003580 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003582 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_folddoclosed:
3584 case CMD_folddoopen:
3585 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003586 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 case CMD_keepjumps:
3588 case CMD_keepmarks:
3589 case CMD_leftabove:
3590 case CMD_lockmarks:
3591 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003592 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003594 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 case CMD_topleft:
3596 case CMD_verbose:
3597 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003598 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 return arg;
3600
Bram Moolenaar4f688582007-07-24 12:34:30 +00003601#ifdef FEAT_CMDL_COMPL
3602# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 case CMD_match:
3604 if (*arg == NUL || !ends_excmd(*arg))
3605 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003606 /* also complete "None" */
3607 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 arg = skipwhite(skiptowhite(arg));
3609 if (*arg != NUL)
3610 {
3611 xp->xp_context = EXPAND_NOTHING;
3612 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3613 }
3614 }
3615 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618/*
3619 * All completion for the +cmdline_compl feature goes here.
3620 */
3621
3622# ifdef FEAT_USR_CMDS
3623 case CMD_command:
3624 /* Check for attributes */
3625 while (*arg == '-')
3626 {
3627 arg++; /* Skip "-" */
3628 p = skiptowhite(arg);
3629 if (*p == NUL)
3630 {
3631 /* Cursor is still in the attribute */
3632 p = vim_strchr(arg, '=');
3633 if (p == NULL)
3634 {
3635 /* No "=", so complete attribute names */
3636 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3637 xp->xp_pattern = arg;
3638 return NULL;
3639 }
3640
3641 /* For the -complete and -nargs attributes, we complete
3642 * their arguments as well.
3643 */
3644 if (STRNICMP(arg, "complete", p - arg) == 0)
3645 {
3646 xp->xp_context = EXPAND_USER_COMPLETE;
3647 xp->xp_pattern = p + 1;
3648 return NULL;
3649 }
3650 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3651 {
3652 xp->xp_context = EXPAND_USER_NARGS;
3653 xp->xp_pattern = p + 1;
3654 return NULL;
3655 }
3656 return NULL;
3657 }
3658 arg = skipwhite(p);
3659 }
3660
3661 /* After the attributes comes the new command name */
3662 p = skiptowhite(arg);
3663 if (*p == NUL)
3664 {
3665 xp->xp_context = EXPAND_USER_COMMANDS;
3666 xp->xp_pattern = arg;
3667 break;
3668 }
3669
3670 /* And finally comes a normal command */
3671 return skipwhite(p);
3672
3673 case CMD_delcommand:
3674 xp->xp_context = EXPAND_USER_COMMANDS;
3675 xp->xp_pattern = arg;
3676 break;
3677# endif
3678
3679 case CMD_global:
3680 case CMD_vglobal:
3681 delim = *arg; /* get the delimiter */
3682 if (delim)
3683 ++arg; /* skip delimiter if there is one */
3684
3685 while (arg[0] != NUL && arg[0] != delim)
3686 {
3687 if (arg[0] == '\\' && arg[1] != NUL)
3688 ++arg;
3689 ++arg;
3690 }
3691 if (arg[0] != NUL)
3692 return arg + 1;
3693 break;
3694 case CMD_and:
3695 case CMD_substitute:
3696 delim = *arg;
3697 if (delim)
3698 {
3699 /* skip "from" part */
3700 ++arg;
3701 arg = skip_regexp(arg, delim, p_magic, NULL);
3702 }
3703 /* skip "to" part */
3704 while (arg[0] != NUL && arg[0] != delim)
3705 {
3706 if (arg[0] == '\\' && arg[1] != NUL)
3707 ++arg;
3708 ++arg;
3709 }
3710 if (arg[0] != NUL) /* skip delimiter */
3711 ++arg;
3712 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3713 ++arg;
3714 if (arg[0] != NUL)
3715 return arg;
3716 break;
3717 case CMD_isearch:
3718 case CMD_dsearch:
3719 case CMD_ilist:
3720 case CMD_dlist:
3721 case CMD_ijump:
3722 case CMD_psearch:
3723 case CMD_djump:
3724 case CMD_isplit:
3725 case CMD_dsplit:
3726 arg = skipwhite(skipdigits(arg)); /* skip count */
3727 if (*arg == '/') /* Match regexp, not just whole words */
3728 {
3729 for (++arg; *arg && *arg != '/'; arg++)
3730 if (*arg == '\\' && arg[1] != NUL)
3731 arg++;
3732 if (*arg)
3733 {
3734 arg = skipwhite(arg + 1);
3735
3736 /* Check for trailing illegal characters */
3737 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3738 xp->xp_context = EXPAND_NOTHING;
3739 else
3740 return arg;
3741 }
3742 }
3743 break;
3744#ifdef FEAT_AUTOCMD
3745 case CMD_autocmd:
3746 return set_context_in_autocmd(xp, arg, FALSE);
3747
3748 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003749 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 return set_context_in_autocmd(xp, arg, TRUE);
3751#endif
3752 case CMD_set:
3753 set_context_in_set_cmd(xp, arg, 0);
3754 break;
3755 case CMD_setglobal:
3756 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3757 break;
3758 case CMD_setlocal:
3759 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3760 break;
3761 case CMD_tag:
3762 case CMD_stag:
3763 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003764 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_tselect:
3766 case CMD_stselect:
3767 case CMD_ptselect:
3768 case CMD_tjump:
3769 case CMD_stjump:
3770 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003771 if (*p_wop != NUL)
3772 xp->xp_context = EXPAND_TAGS_LISTFILES;
3773 else
3774 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 xp->xp_pattern = arg;
3776 break;
3777 case CMD_augroup:
3778 xp->xp_context = EXPAND_AUGROUP;
3779 xp->xp_pattern = arg;
3780 break;
3781#ifdef FEAT_SYN_HL
3782 case CMD_syntax:
3783 set_context_in_syntax_cmd(xp, arg);
3784 break;
3785#endif
3786#ifdef FEAT_EVAL
3787 case CMD_let:
3788 case CMD_if:
3789 case CMD_elseif:
3790 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003791 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 case CMD_echo:
3793 case CMD_echon:
3794 case CMD_execute:
3795 case CMD_echomsg:
3796 case CMD_echoerr:
3797 case CMD_call:
3798 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003799 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 break;
3801
3802 case CMD_unlet:
3803 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3804 arg = xp->xp_pattern + 1;
3805 xp->xp_context = EXPAND_USER_VARS;
3806 xp->xp_pattern = arg;
3807 break;
3808
3809 case CMD_function:
3810 case CMD_delfunction:
3811 xp->xp_context = EXPAND_USER_FUNC;
3812 xp->xp_pattern = arg;
3813 break;
3814
3815 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003816 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 break;
3818#endif
3819 case CMD_highlight:
3820 set_context_in_highlight_cmd(xp, arg);
3821 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003822#ifdef FEAT_CSCOPE
3823 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003824 case CMD_lcscope:
3825 case CMD_scscope:
3826 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003827 break;
3828#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003829#ifdef FEAT_SIGNS
3830 case CMD_sign:
3831 set_context_in_sign_cmd(xp, arg);
3832 break;
3833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834#ifdef FEAT_LISTCMDS
3835 case CMD_bdelete:
3836 case CMD_bwipeout:
3837 case CMD_bunload:
3838 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3839 arg = xp->xp_pattern + 1;
3840 /*FALLTHROUGH*/
3841 case CMD_buffer:
3842 case CMD_sbuffer:
3843 case CMD_checktime:
3844 xp->xp_context = EXPAND_BUFFERS;
3845 xp->xp_pattern = arg;
3846 break;
3847#endif
3848#ifdef FEAT_USR_CMDS
3849 case CMD_USER:
3850 case CMD_USER_BUF:
3851 if (compl != EXPAND_NOTHING)
3852 {
3853 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003854 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856# ifdef FEAT_MENU
3857 if (compl == EXPAND_MENUS)
3858 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3859# endif
3860 if (compl == EXPAND_COMMANDS)
3861 return arg;
3862 if (compl == EXPAND_MAPPINGS)
3863 return set_context_in_map_cmd(xp, (char_u *)"map",
3864 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003865 /* Find start of last argument. */
3866 p = arg;
3867 while (*p)
3868 {
3869 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003870 /* argument starts after a space */
3871 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003872 else if (*p == '\\' && *(p + 1) != NUL)
3873 ++p; /* skip over escaped character */
3874 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 xp->xp_pattern = arg;
3877 }
3878 xp->xp_context = compl;
3879 }
3880 break;
3881#endif
3882 case CMD_map: case CMD_noremap:
3883 case CMD_nmap: case CMD_nnoremap:
3884 case CMD_vmap: case CMD_vnoremap:
3885 case CMD_omap: case CMD_onoremap:
3886 case CMD_imap: case CMD_inoremap:
3887 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003888 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003890 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case CMD_unmap:
3892 case CMD_nunmap:
3893 case CMD_vunmap:
3894 case CMD_ounmap:
3895 case CMD_iunmap:
3896 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003897 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003899 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_abbreviate: case CMD_noreabbrev:
3901 case CMD_cabbrev: case CMD_cnoreabbrev:
3902 case CMD_iabbrev: case CMD_inoreabbrev:
3903 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003904 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_unabbreviate:
3906 case CMD_cunabbrev:
3907 case CMD_iunabbrev:
3908 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003909 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910#ifdef FEAT_MENU
3911 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3912 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3913 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3914 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3915 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3916 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3917 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3918 case CMD_tmenu: case CMD_tunmenu:
3919 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3920 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3921#endif
3922
3923 case CMD_colorscheme:
3924 xp->xp_context = EXPAND_COLORS;
3925 xp->xp_pattern = arg;
3926 break;
3927
3928 case CMD_compiler:
3929 xp->xp_context = EXPAND_COMPILER;
3930 xp->xp_pattern = arg;
3931 break;
3932
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003933 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003934 xp->xp_context = EXPAND_OWNSYNTAX;
3935 xp->xp_pattern = arg;
3936 break;
3937
3938 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003939 xp->xp_context = EXPAND_FILETYPE;
3940 xp->xp_pattern = arg;
3941 break;
3942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3944 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3945 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003946 p = skiptowhite(arg);
3947 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 xp->xp_context = EXPAND_LANGUAGE;
3950 xp->xp_pattern = arg;
3951 }
3952 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003953 {
3954 if ( STRNCMP(arg, "messages", p - arg) == 0
3955 || STRNCMP(arg, "ctype", p - arg) == 0
3956 || STRNCMP(arg, "time", p - arg) == 0)
3957 {
3958 xp->xp_context = EXPAND_LOCALES;
3959 xp->xp_pattern = skipwhite(p);
3960 }
3961 else
3962 xp->xp_context = EXPAND_NOTHING;
3963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 break;
3965#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003966#if defined(FEAT_PROFILE)
3967 case CMD_profile:
3968 set_context_in_profile_cmd(xp, arg);
3969 break;
3970#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003971 case CMD_behave:
3972 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003973 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003976#if defined(FEAT_CMDHIST)
3977 case CMD_history:
3978 xp->xp_context = EXPAND_HISTORY;
3979 xp->xp_pattern = arg;
3980 break;
3981#endif
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#endif /* FEAT_CMDL_COMPL */
3984
3985 default:
3986 break;
3987 }
3988 return NULL;
3989}
3990
3991/*
3992 * skip a range specifier of the form: addr [,addr] [;addr] ..
3993 *
3994 * Backslashed delimiters after / or ? will be skipped, and commands will
3995 * not be expanded between /'s and ?'s or after "'".
3996 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003997 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * Returns the "cmd" pointer advanced to beyond the range.
3999 */
4000 char_u *
4001skip_range(cmd, ctx)
4002 char_u *cmd;
4003 int *ctx; /* pointer to xp_context or NULL */
4004{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004005 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaardf177f62005-02-22 08:39:57 +00004007 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 if (*cmd == '\'')
4010 {
4011 if (*++cmd == NUL && ctx != NULL)
4012 *ctx = EXPAND_NOTHING;
4013 }
4014 else if (*cmd == '/' || *cmd == '?')
4015 {
4016 delim = *cmd++;
4017 while (*cmd != NUL && *cmd != delim)
4018 if (*cmd++ == '\\' && *cmd != NUL)
4019 ++cmd;
4020 if (*cmd == NUL && ctx != NULL)
4021 *ctx = EXPAND_NOTHING;
4022 }
4023 if (*cmd != NUL)
4024 ++cmd;
4025 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004026
4027 /* Skip ":" and white space. */
4028 while (*cmd == ':')
4029 cmd = skipwhite(cmd + 1);
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 return cmd;
4032}
4033
4034/*
4035 * get a single EX address
4036 *
4037 * Set ptr to the next character after the part that was interpreted.
4038 * Set ptr to NULL when an error is encountered.
4039 *
4040 * Return MAXLNUM when no Ex address was found.
4041 */
4042 static linenr_T
4043get_address(ptr, skip, to_other_file)
4044 char_u **ptr;
4045 int skip; /* only skip the address, don't use it */
4046 int to_other_file; /* flag: may jump to other file */
4047{
4048 int c;
4049 int i;
4050 long n;
4051 char_u *cmd;
4052 pos_T pos;
4053 pos_T *fp;
4054 linenr_T lnum;
4055
4056 cmd = skipwhite(*ptr);
4057 lnum = MAXLNUM;
4058 do
4059 {
4060 switch (*cmd)
4061 {
4062 case '.': /* '.' - Cursor position */
4063 ++cmd;
4064 lnum = curwin->w_cursor.lnum;
4065 break;
4066
4067 case '$': /* '$' - last line */
4068 ++cmd;
4069 lnum = curbuf->b_ml.ml_line_count;
4070 break;
4071
4072 case '\'': /* ''' - mark */
4073 if (*++cmd == NUL)
4074 {
4075 cmd = NULL;
4076 goto error;
4077 }
4078 if (skip)
4079 ++cmd;
4080 else
4081 {
4082 /* Only accept a mark in another file when it is
4083 * used by itself: ":'M". */
4084 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4085 ++cmd;
4086 if (fp == (pos_T *)-1)
4087 /* Jumped to another file. */
4088 lnum = curwin->w_cursor.lnum;
4089 else
4090 {
4091 if (check_mark(fp) == FAIL)
4092 {
4093 cmd = NULL;
4094 goto error;
4095 }
4096 lnum = fp->lnum;
4097 }
4098 }
4099 break;
4100
4101 case '/':
4102 case '?': /* '/' or '?' - search */
4103 c = *cmd++;
4104 if (skip) /* skip "/pat/" */
4105 {
4106 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4107 if (*cmd == c)
4108 ++cmd;
4109 }
4110 else
4111 {
4112 pos = curwin->w_cursor; /* save curwin->w_cursor */
4113 /*
4114 * When '/' or '?' follows another address, start
4115 * from there.
4116 */
4117 if (lnum != MAXLNUM)
4118 curwin->w_cursor.lnum = lnum;
4119 /*
4120 * Start a forward search at the end of the line.
4121 * Start a backward search at the start of the line.
4122 * This makes sure we never match in the current
4123 * line, and can match anywhere in the
4124 * next/previous line.
4125 */
4126 if (c == '/')
4127 curwin->w_cursor.col = MAXCOL;
4128 else
4129 curwin->w_cursor.col = 0;
4130 searchcmdlen = 0;
4131 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004132 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134 curwin->w_cursor = pos;
4135 cmd = NULL;
4136 goto error;
4137 }
4138 lnum = curwin->w_cursor.lnum;
4139 curwin->w_cursor = pos;
4140 /* adjust command string pointer */
4141 cmd += searchcmdlen;
4142 }
4143 break;
4144
4145 case '\\': /* "\?", "\/" or "\&", repeat search */
4146 ++cmd;
4147 if (*cmd == '&')
4148 i = RE_SUBST;
4149 else if (*cmd == '?' || *cmd == '/')
4150 i = RE_SEARCH;
4151 else
4152 {
4153 EMSG(_(e_backslash));
4154 cmd = NULL;
4155 goto error;
4156 }
4157
4158 if (!skip)
4159 {
4160 /*
4161 * When search follows another address, start from
4162 * there.
4163 */
4164 if (lnum != MAXLNUM)
4165 pos.lnum = lnum;
4166 else
4167 pos.lnum = curwin->w_cursor.lnum;
4168
4169 /*
4170 * Start the search just like for the above
4171 * do_search().
4172 */
4173 if (*cmd != '?')
4174 pos.col = MAXCOL;
4175 else
4176 pos.col = 0;
4177 if (searchit(curwin, curbuf, &pos,
4178 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004179 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004180 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 lnum = pos.lnum;
4182 else
4183 {
4184 cmd = NULL;
4185 goto error;
4186 }
4187 }
4188 ++cmd;
4189 break;
4190
4191 default:
4192 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4193 lnum = getdigits(&cmd);
4194 }
4195
4196 for (;;)
4197 {
4198 cmd = skipwhite(cmd);
4199 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4200 break;
4201
4202 if (lnum == MAXLNUM)
4203 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4204 if (VIM_ISDIGIT(*cmd))
4205 i = '+'; /* "number" is same as "+number" */
4206 else
4207 i = *cmd++;
4208 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4209 n = 1;
4210 else
4211 n = getdigits(&cmd);
4212 if (i == '-')
4213 lnum -= n;
4214 else
4215 lnum += n;
4216 }
4217 } while (*cmd == '/' || *cmd == '?');
4218
4219error:
4220 *ptr = cmd;
4221 return lnum;
4222}
4223
4224/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004225 * Get flags from an Ex command argument.
4226 */
4227 static void
4228get_flags(eap)
4229 exarg_T *eap;
4230{
4231 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4232 {
4233 if (*eap->arg == 'l')
4234 eap->flags |= EXFLAG_LIST;
4235 else if (*eap->arg == 'p')
4236 eap->flags |= EXFLAG_PRINT;
4237 else
4238 eap->flags |= EXFLAG_NR;
4239 eap->arg = skipwhite(eap->arg + 1);
4240 }
4241}
4242
4243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * Function called for command which is Not Implemented. NI!
4245 */
4246 void
4247ex_ni(eap)
4248 exarg_T *eap;
4249{
4250 if (!eap->skip)
4251 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4252}
4253
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004254#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255/*
4256 * Function called for script command which is Not Implemented. NI!
4257 * Skips over ":perl <<EOF" constructs.
4258 */
4259 static void
4260ex_script_ni(eap)
4261 exarg_T *eap;
4262{
4263 if (!eap->skip)
4264 ex_ni(eap);
4265 else
4266 vim_free(script_get(eap, eap->arg));
4267}
4268#endif
4269
4270/*
4271 * Check range in Ex command for validity.
4272 * Return NULL when valid, error message when invalid.
4273 */
4274 static char_u *
4275invalid_range(eap)
4276 exarg_T *eap;
4277{
4278 if ( eap->line1 < 0
4279 || eap->line2 < 0
4280 || eap->line1 > eap->line2
4281 || ((eap->argt & RANGE)
4282 && !(eap->argt & NOTADR)
4283 && eap->line2 > curbuf->b_ml.ml_line_count
4284#ifdef FEAT_DIFF
4285 + (eap->cmdidx == CMD_diffget)
4286#endif
4287 ))
4288 return (char_u *)_(e_invrange);
4289 return NULL;
4290}
4291
4292/*
4293 * Correct the range for zero line number, if required.
4294 */
4295 static void
4296correct_range(eap)
4297 exarg_T *eap;
4298{
4299 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4300 {
4301 if (eap->line1 == 0)
4302 eap->line1 = 1;
4303 if (eap->line2 == 0)
4304 eap->line2 = 1;
4305 }
4306}
4307
Bram Moolenaar748bf032005-02-02 23:04:36 +00004308#ifdef FEAT_QUICKFIX
4309static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4310
4311/*
4312 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4313 * pattern. Otherwise return eap->arg.
4314 */
4315 static char_u *
4316skip_grep_pat(eap)
4317 exarg_T *eap;
4318{
4319 char_u *p = eap->arg;
4320
Bram Moolenaara37420f2006-02-04 22:37:47 +00004321 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4322 || eap->cmdidx == CMD_vimgrepadd
4323 || eap->cmdidx == CMD_lvimgrepadd
4324 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004325 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004326 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004327 if (p == NULL)
4328 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004329 }
4330 return p;
4331}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004332
4333/*
4334 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4335 * in the command line, so that things like % get expanded.
4336 */
4337 static char_u *
4338replace_makeprg(eap, p, cmdlinep)
4339 exarg_T *eap;
4340 char_u *p;
4341 char_u **cmdlinep;
4342{
4343 char_u *new_cmdline;
4344 char_u *program;
4345 char_u *pos;
4346 char_u *ptr;
4347 int len;
4348 int i;
4349
4350 /*
4351 * Don't do it when ":vimgrep" is used for ":grep".
4352 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004353 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4354 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4355 || eap->cmdidx == CMD_grepadd
4356 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004357 && !grep_internal(eap->cmdidx))
4358 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004359 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4360 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004361 {
4362 if (*curbuf->b_p_gp == NUL)
4363 program = p_gp;
4364 else
4365 program = curbuf->b_p_gp;
4366 }
4367 else
4368 {
4369 if (*curbuf->b_p_mp == NUL)
4370 program = p_mp;
4371 else
4372 program = curbuf->b_p_mp;
4373 }
4374
4375 p = skipwhite(p);
4376
4377 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4378 {
4379 /* replace $* by given arguments */
4380 i = 1;
4381 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4382 ++i;
4383 len = (int)STRLEN(p);
4384 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4385 if (new_cmdline == NULL)
4386 return NULL; /* out of memory */
4387 ptr = new_cmdline;
4388 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4389 {
4390 i = (int)(pos - program);
4391 STRNCPY(ptr, program, i);
4392 STRCPY(ptr += i, p);
4393 ptr += len;
4394 program = pos + 2;
4395 }
4396 STRCPY(ptr, program);
4397 }
4398 else
4399 {
4400 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4401 if (new_cmdline == NULL)
4402 return NULL; /* out of memory */
4403 STRCPY(new_cmdline, program);
4404 STRCAT(new_cmdline, " ");
4405 STRCAT(new_cmdline, p);
4406 }
4407 msg_make(p);
4408
4409 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4410 vim_free(*cmdlinep);
4411 *cmdlinep = new_cmdline;
4412 p = new_cmdline;
4413 }
4414 return p;
4415}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004416#endif
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418/*
4419 * Expand file name in Ex command argument.
4420 * Return FAIL for failure, OK otherwise.
4421 */
4422 int
4423expand_filename(eap, cmdlinep, errormsgp)
4424 exarg_T *eap;
4425 char_u **cmdlinep;
4426 char_u **errormsgp;
4427{
4428 int has_wildcards; /* need to expand wildcards */
4429 char_u *repl;
4430 int srclen;
4431 char_u *p;
4432 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004433 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaar748bf032005-02-02 23:04:36 +00004435#ifdef FEAT_QUICKFIX
4436 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4437 p = skip_grep_pat(eap);
4438#else
4439 p = eap->arg;
4440#endif
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 /*
4443 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4444 * the file name contains a wildcard it should not cause expanding.
4445 * (it will be expanded anyway if there is a wildcard before replacing).
4446 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004447 has_wildcards = mch_has_wildcard(p);
4448 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004450#ifdef FEAT_EVAL
4451 /* Skip over `=expr`, wildcards in it are not expanded. */
4452 if (p[0] == '`' && p[1] == '=')
4453 {
4454 p += 2;
4455 (void)skip_expr(&p);
4456 if (*p == '`')
4457 ++p;
4458 continue;
4459 }
4460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 /*
4462 * Quick check if this cannot be the start of a special string.
4463 * Also removes backslash before '%', '#' and '<'.
4464 */
4465 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4466 {
4467 ++p;
4468 continue;
4469 }
4470
4471 /*
4472 * Try to find a match at this position.
4473 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004474 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4475 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (*errormsgp != NULL) /* error detected */
4477 return FAIL;
4478 if (repl == NULL) /* no match found */
4479 {
4480 p += srclen;
4481 continue;
4482 }
4483
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004484 /* Wildcards won't be expanded below, the replacement is taken
4485 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4486 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4487 {
4488 char_u *l = repl;
4489
4490 repl = expand_env_save(repl);
4491 vim_free(l);
4492 }
4493
Bram Moolenaar63b92542007-03-27 14:57:09 +00004494 /* Need to escape white space et al. with a backslash.
4495 * Don't do this for:
4496 * - replacement that already has been escaped: "##"
4497 * - shell commands (may have to use quotes instead).
4498 * - non-unix systems when there is a single argument (spaces don't
4499 * separate arguments then).
4500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004502 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 && eap->cmdidx != CMD_bang
4504 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004505 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004507 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004509 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510#ifndef UNIX
4511 && !(eap->argt & NOSPC)
4512#endif
4513 )
4514 {
4515 char_u *l;
4516#ifdef BACKSLASH_IN_FILENAME
4517 /* Don't escape a backslash here, because rem_backslash() doesn't
4518 * remove it later. */
4519 static char_u *nobslash = (char_u *)" \t\"|";
4520# define ESCAPE_CHARS nobslash
4521#else
4522# define ESCAPE_CHARS escape_chars
4523#endif
4524
4525 for (l = repl; *l; ++l)
4526 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4527 {
4528 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4529 if (l != NULL)
4530 {
4531 vim_free(repl);
4532 repl = l;
4533 }
4534 break;
4535 }
4536 }
4537
4538 /* For a shell command a '!' must be escaped. */
4539 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004540 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
4542 char_u *l;
4543
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004544 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (l != NULL)
4546 {
4547 vim_free(repl);
4548 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004549 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (strstr((char *)p_sh, "sh") != NULL)
4551 {
4552 l = vim_strsave_escaped(repl, (char_u *)"!");
4553 if (l != NULL)
4554 {
4555 vim_free(repl);
4556 repl = l;
4557 }
4558 }
4559 }
4560 }
4561
4562 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4563 vim_free(repl);
4564 if (p == NULL)
4565 return FAIL;
4566 }
4567
4568 /*
4569 * One file argument: Expand wildcards.
4570 * Don't do this with ":r !command" or ":w !command".
4571 */
4572 if ((eap->argt & NOSPC) && !eap->usefilter)
4573 {
4574 /*
4575 * May do this twice:
4576 * 1. Replace environment variables.
4577 * 2. Replace any other wildcards, remove backslashes.
4578 */
4579 for (n = 1; n <= 2; ++n)
4580 {
4581 if (n == 2)
4582 {
4583#ifdef UNIX
4584 /*
4585 * Only for Unix we check for more than one file name.
4586 * For other systems spaces are considered to be part
4587 * of the file name.
4588 * Only check here if there is no wildcard, otherwise
4589 * ExpandOne() will check for errors. This allows
4590 * ":e `ls ve*.c`" on Unix.
4591 */
4592 if (!has_wildcards)
4593 for (p = eap->arg; *p; ++p)
4594 {
4595 /* skip escaped characters */
4596 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4597 ++p;
4598 else if (vim_iswhite(*p))
4599 {
4600 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4601 return FAIL;
4602 }
4603 }
4604#endif
4605
4606 /*
4607 * Halve the number of backslashes (this is Vi compatible).
4608 * For Unix and OS/2, when wildcards are expanded, this is
4609 * done by ExpandOne() below.
4610 */
4611#if defined(UNIX) || defined(OS2)
4612 if (!has_wildcards)
4613#endif
4614 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616
4617 if (has_wildcards)
4618 {
4619 if (n == 1)
4620 {
4621 /*
4622 * First loop: May expand environment variables. This
4623 * can be done much faster with expand_env() than with
4624 * something else (e.g., calling a shell).
4625 * After expanding environment variables, check again
4626 * if there are still wildcards present.
4627 */
4628 if (vim_strchr(eap->arg, '$') != NULL
4629 || vim_strchr(eap->arg, '~') != NULL)
4630 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004631 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004632 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 has_wildcards = mch_has_wildcard(NameBuff);
4634 p = NameBuff;
4635 }
4636 else
4637 p = NULL;
4638 }
4639 else /* n == 2 */
4640 {
4641 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004642 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 ExpandInit(&xpc);
4645 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004646 if (p_wic)
4647 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004649 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (p == NULL)
4651 return FAIL;
4652 }
4653 if (p != NULL)
4654 {
4655 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4656 p, cmdlinep);
4657 if (n == 2) /* p came from ExpandOne() */
4658 vim_free(p);
4659 }
4660 }
4661 }
4662 }
4663 return OK;
4664}
4665
4666/*
4667 * Replace part of the command line, keeping eap->cmd, eap->arg and
4668 * eap->nextcmd correct.
4669 * "src" points to the part that is to be replaced, of length "srclen".
4670 * "repl" is the replacement string.
4671 * Returns a pointer to the character after the replaced string.
4672 * Returns NULL for failure.
4673 */
4674 static char_u *
4675repl_cmdline(eap, src, srclen, repl, cmdlinep)
4676 exarg_T *eap;
4677 char_u *src;
4678 int srclen;
4679 char_u *repl;
4680 char_u **cmdlinep;
4681{
4682 int len;
4683 int i;
4684 char_u *new_cmdline;
4685
4686 /*
4687 * The new command line is build in new_cmdline[].
4688 * First allocate it.
4689 * Careful: a "+cmd" argument may have been NUL terminated.
4690 */
4691 len = (int)STRLEN(repl);
4692 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4695 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4696 return NULL; /* out of memory! */
4697
4698 /*
4699 * Copy the stuff before the expanded part.
4700 * Copy the expanded stuff.
4701 * Copy what came after the expanded part.
4702 * Copy the next commands, if there are any.
4703 */
4704 i = (int)(src - *cmdlinep); /* length of part before match */
4705 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 mch_memmove(new_cmdline + i, repl, (size_t)len);
4708 i += len; /* remember the end of the string */
4709 STRCPY(new_cmdline + i, src + srclen);
4710 src = new_cmdline + i; /* remember where to continue */
4711
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004712 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 i = (int)STRLEN(new_cmdline) + 1;
4715 STRCPY(new_cmdline + i, eap->nextcmd);
4716 eap->nextcmd = new_cmdline + i;
4717 }
4718 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4719 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4720 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4721 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4722 vim_free(*cmdlinep);
4723 *cmdlinep = new_cmdline;
4724
4725 return src;
4726}
4727
4728/*
4729 * Check for '|' to separate commands and '"' to start comments.
4730 */
4731 void
4732separate_nextcmd(eap)
4733 exarg_T *eap;
4734{
4735 char_u *p;
4736
Bram Moolenaar86b68352004-12-27 21:59:20 +00004737#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004738 p = skip_grep_pat(eap);
4739#else
4740 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004741#endif
4742
4743 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
4745 if (*p == Ctrl_V)
4746 {
4747 if (eap->argt & (USECTRLV | XFILE))
4748 ++p; /* skip CTRL-V and next char */
4749 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004750 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004751 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (*p == NUL) /* stop at NUL after CTRL-V */
4753 break;
4754 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004755
4756#ifdef FEAT_EVAL
4757 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004759 {
4760 p += 2;
4761 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004762 }
4763#endif
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 /* Check for '"': start of comment or '|': next command */
4766 /* :@" and :*" do not start a comment!
4767 * :redir @" doesn't either. */
4768 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4769 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4770 || p != eap->arg)
4771 && (eap->cmdidx != CMD_redir
4772 || p != eap->arg + 1 || p[-1] != '@'))
4773 || *p == '|' || *p == '\n')
4774 {
4775 /*
4776 * We remove the '\' before the '|', unless USECTRLV is used
4777 * AND 'b' is present in 'cpoptions'.
4778 */
4779 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4780 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4781 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004782 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 --p;
4784 }
4785 else
4786 {
4787 eap->nextcmd = check_nextcmd(p);
4788 *p = NUL;
4789 break;
4790 }
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4795 del_trailing_spaces(eap->arg);
4796}
4797
4798/*
4799 * get + command from ex argument
4800 */
4801 static char_u *
4802getargcmd(argp)
4803 char_u **argp;
4804{
4805 char_u *arg = *argp;
4806 char_u *command = NULL;
4807
4808 if (*arg == '+') /* +[command] */
4809 {
4810 ++arg;
4811 if (vim_isspace(*arg))
4812 command = dollar_command;
4813 else
4814 {
4815 command = arg;
4816 arg = skip_cmd_arg(command, TRUE);
4817 if (*arg != NUL)
4818 *arg++ = NUL; /* terminate command with NUL */
4819 }
4820
4821 arg = skipwhite(arg); /* skip over spaces */
4822 *argp = arg;
4823 }
4824 return command;
4825}
4826
4827/*
4828 * Find end of "+command" argument. Skip over "\ " and "\\".
4829 */
4830 static char_u *
4831skip_cmd_arg(p, rembs)
4832 char_u *p;
4833 int rembs; /* TRUE to halve the number of backslashes */
4834{
4835 while (*p && !vim_isspace(*p))
4836 {
4837 if (*p == '\\' && p[1] != NUL)
4838 {
4839 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004840 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 else
4842 ++p;
4843 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004844 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 return p;
4847}
4848
4849/*
4850 * Get "++opt=arg" argument.
4851 * Return FAIL or OK.
4852 */
4853 static int
4854getargopt(eap)
4855 exarg_T *eap;
4856{
4857 char_u *arg = eap->arg + 2;
4858 int *pp = NULL;
4859#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004860 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 char_u *p;
4862#endif
4863
4864 /* ":edit ++[no]bin[ary] file" */
4865 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4866 {
4867 if (*arg == 'n')
4868 {
4869 arg += 2;
4870 eap->force_bin = FORCE_NOBIN;
4871 }
4872 else
4873 eap->force_bin = FORCE_BIN;
4874 if (!checkforcmd(&arg, "binary", 3))
4875 return FAIL;
4876 eap->arg = skipwhite(arg);
4877 return OK;
4878 }
4879
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004880 /* ":read ++edit file" */
4881 if (STRNCMP(arg, "edit", 4) == 0)
4882 {
4883 eap->read_edit = TRUE;
4884 eap->arg = skipwhite(arg + 4);
4885 return OK;
4886 }
4887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (STRNCMP(arg, "ff", 2) == 0)
4889 {
4890 arg += 2;
4891 pp = &eap->force_ff;
4892 }
4893 else if (STRNCMP(arg, "fileformat", 10) == 0)
4894 {
4895 arg += 10;
4896 pp = &eap->force_ff;
4897 }
4898#ifdef FEAT_MBYTE
4899 else if (STRNCMP(arg, "enc", 3) == 0)
4900 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004901 if (STRNCMP(arg, "encoding", 8) == 0)
4902 arg += 8;
4903 else
4904 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 pp = &eap->force_enc;
4906 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004907 else if (STRNCMP(arg, "bad", 3) == 0)
4908 {
4909 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004910 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#endif
4913
4914 if (pp == NULL || *arg != '=')
4915 return FAIL;
4916
4917 ++arg;
4918 *pp = (int)(arg - eap->cmd);
4919 arg = skip_cmd_arg(arg, FALSE);
4920 eap->arg = skipwhite(arg);
4921 *arg = NUL;
4922
4923#ifdef FEAT_MBYTE
4924 if (pp == &eap->force_ff)
4925 {
4926#endif
4927 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4928 return FAIL;
4929#ifdef FEAT_MBYTE
4930 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004931 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
4933 /* Make 'fileencoding' lower case. */
4934 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4935 *p = TOLOWER_ASC(*p);
4936 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004937 else
4938 {
4939 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4940 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004941 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004942 if (STRICMP(p, "keep") == 0)
4943 eap->bad_char = BAD_KEEP;
4944 else if (STRICMP(p, "drop") == 0)
4945 eap->bad_char = BAD_DROP;
4946 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4947 eap->bad_char = *p;
4948 else
4949 return FAIL;
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951#endif
4952
4953 return OK;
4954}
4955
4956/*
4957 * ":abbreviate" and friends.
4958 */
4959 static void
4960ex_abbreviate(eap)
4961 exarg_T *eap;
4962{
4963 do_exmap(eap, TRUE); /* almost the same as mapping */
4964}
4965
4966/*
4967 * ":map" and friends.
4968 */
4969 static void
4970ex_map(eap)
4971 exarg_T *eap;
4972{
4973 /*
4974 * If we are sourcing .exrc or .vimrc in current directory we
4975 * print the mappings for security reasons.
4976 */
4977 if (secure)
4978 {
4979 secure = 2;
4980 msg_outtrans(eap->cmd);
4981 msg_putchar('\n');
4982 }
4983 do_exmap(eap, FALSE);
4984}
4985
4986/*
4987 * ":unmap" and friends.
4988 */
4989 static void
4990ex_unmap(eap)
4991 exarg_T *eap;
4992{
4993 do_exmap(eap, FALSE);
4994}
4995
4996/*
4997 * ":mapclear" and friends.
4998 */
4999 static void
5000ex_mapclear(eap)
5001 exarg_T *eap;
5002{
5003 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5004}
5005
5006/*
5007 * ":abclear" and friends.
5008 */
5009 static void
5010ex_abclear(eap)
5011 exarg_T *eap;
5012{
5013 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5014}
5015
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005016#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 static void
5018ex_autocmd(eap)
5019 exarg_T *eap;
5020{
5021 /*
5022 * Disallow auto commands from .exrc and .vimrc in current
5023 * directory for security reasons.
5024 */
5025 if (secure)
5026 {
5027 secure = 2;
5028 eap->errmsg = e_curdir;
5029 }
5030 else if (eap->cmdidx == CMD_autocmd)
5031 do_autocmd(eap->arg, eap->forceit);
5032 else
5033 do_augroup(eap->arg, eap->forceit);
5034}
5035
5036/*
5037 * ":doautocmd": Apply the automatic commands to the current buffer.
5038 */
5039 static void
5040ex_doautocmd(eap)
5041 exarg_T *eap;
5042{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005043 char_u *arg = eap->arg;
5044 int call_do_modelines = check_nomodeline(&arg);
5045
5046 (void)do_doautocmd(arg, TRUE);
5047 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5048 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049}
5050#endif
5051
5052#ifdef FEAT_LISTCMDS
5053/*
5054 * :[N]bunload[!] [N] [bufname] unload buffer
5055 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5056 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5057 */
5058 static void
5059ex_bunload(eap)
5060 exarg_T *eap;
5061{
5062 eap->errmsg = do_bufdel(
5063 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5064 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5065 : DOBUF_UNLOAD, eap->arg,
5066 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5067}
5068
5069/*
5070 * :[N]buffer [N] to buffer N
5071 * :[N]sbuffer [N] to buffer N
5072 */
5073 static void
5074ex_buffer(eap)
5075 exarg_T *eap;
5076{
5077 if (*eap->arg)
5078 eap->errmsg = e_trailing;
5079 else
5080 {
5081 if (eap->addr_count == 0) /* default is current buffer */
5082 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5083 else
5084 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5085 }
5086}
5087
5088/*
5089 * :[N]bmodified [N] to next mod. buffer
5090 * :[N]sbmodified [N] to next mod. buffer
5091 */
5092 static void
5093ex_bmodified(eap)
5094 exarg_T *eap;
5095{
5096 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5097}
5098
5099/*
5100 * :[N]bnext [N] to next buffer
5101 * :[N]sbnext [N] split and to next buffer
5102 */
5103 static void
5104ex_bnext(eap)
5105 exarg_T *eap;
5106{
5107 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5108}
5109
5110/*
5111 * :[N]bNext [N] to previous buffer
5112 * :[N]bprevious [N] to previous buffer
5113 * :[N]sbNext [N] split and to previous buffer
5114 * :[N]sbprevious [N] split and to previous buffer
5115 */
5116 static void
5117ex_bprevious(eap)
5118 exarg_T *eap;
5119{
5120 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5121}
5122
5123/*
5124 * :brewind to first buffer
5125 * :bfirst to first buffer
5126 * :sbrewind split and to first buffer
5127 * :sbfirst split and to first buffer
5128 */
5129 static void
5130ex_brewind(eap)
5131 exarg_T *eap;
5132{
5133 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5134}
5135
5136/*
5137 * :blast to last buffer
5138 * :sblast split and to last buffer
5139 */
5140 static void
5141ex_blast(eap)
5142 exarg_T *eap;
5143{
5144 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5145}
5146#endif
5147
5148 int
5149ends_excmd(c)
5150 int c;
5151{
5152 return (c == NUL || c == '|' || c == '"' || c == '\n');
5153}
5154
5155#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5156 || defined(PROTO)
5157/*
5158 * Return the next command, after the first '|' or '\n'.
5159 * Return NULL if not found.
5160 */
5161 char_u *
5162find_nextcmd(p)
5163 char_u *p;
5164{
5165 while (*p != '|' && *p != '\n')
5166 {
5167 if (*p == NUL)
5168 return NULL;
5169 ++p;
5170 }
5171 return (p + 1);
5172}
5173#endif
5174
5175/*
5176 * Check if *p is a separator between Ex commands.
5177 * Return NULL if it isn't, (p + 1) if it is.
5178 */
5179 char_u *
5180check_nextcmd(p)
5181 char_u *p;
5182{
5183 p = skipwhite(p);
5184 if (*p == '|' || *p == '\n')
5185 return (p + 1);
5186 else
5187 return NULL;
5188}
5189
5190/*
5191 * - if there are more files to edit
5192 * - and this is the last window
5193 * - and forceit not used
5194 * - and not repeated twice on a row
5195 * return FAIL and give error message if 'message' TRUE
5196 * return OK otherwise
5197 */
5198 static int
5199check_more(message, forceit)
5200 int message; /* when FALSE check only, no messages */
5201 int forceit;
5202{
5203 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5204
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005205 if (!forceit && only_one_window()
5206 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 if (message)
5209 {
5210#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5211 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5212 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005213 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005216 vim_strncpy(buff,
5217 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005218 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005220 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 _("%d more files to edit. Quit anyway?"), n);
5222 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5223 return OK;
5224 return FAIL;
5225 }
5226#endif
5227 if (n == 1)
5228 EMSG(_("E173: 1 more file to edit"));
5229 else
5230 EMSGN(_("E173: %ld more files to edit"), n);
5231 quitmore = 2; /* next try to quit is allowed */
5232 }
5233 return FAIL;
5234 }
5235 return OK;
5236}
5237
5238#ifdef FEAT_CMDL_COMPL
5239/*
5240 * Function given to ExpandGeneric() to obtain the list of command names.
5241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 char_u *
5243get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005244 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 int idx;
5246{
5247 if (idx >= (int)CMD_SIZE)
5248# ifdef FEAT_USR_CMDS
5249 return get_user_command_name(idx);
5250# else
5251 return NULL;
5252# endif
5253 return cmdnames[idx].cmd_name;
5254}
5255#endif
5256
5257#if defined(FEAT_USR_CMDS) || defined(PROTO)
5258static 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));
5259static void uc_list __ARGS((char_u *name, size_t name_len));
5260static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5261static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5262static 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));
5263
5264 static int
5265uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5266 char_u *name;
5267 size_t name_len;
5268 char_u *rep;
5269 long argt;
5270 long def;
5271 int flags;
5272 int compl;
5273 char_u *compl_arg;
5274 int force;
5275{
5276 ucmd_T *cmd = NULL;
5277 char_u *p;
5278 int i;
5279 int cmp = 1;
5280 char_u *rep_buf = NULL;
5281 garray_T *gap;
5282
Bram Moolenaar9c102382006-05-03 21:26:49 +00005283 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (rep_buf == NULL)
5285 {
5286 /* Can't replace termcodes - try using the string as is */
5287 rep_buf = vim_strsave(rep);
5288
5289 /* Give up if out of memory */
5290 if (rep_buf == NULL)
5291 return FAIL;
5292 }
5293
5294 /* get address of growarray: global or in curbuf */
5295 if (flags & UC_BUFFER)
5296 {
5297 gap = &curbuf->b_ucmds;
5298 if (gap->ga_itemsize == 0)
5299 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5300 }
5301 else
5302 gap = &ucmds;
5303
5304 /* Search for the command in the already defined commands. */
5305 for (i = 0; i < gap->ga_len; ++i)
5306 {
5307 size_t len;
5308
5309 cmd = USER_CMD_GA(gap, i);
5310 len = STRLEN(cmd->uc_name);
5311 cmp = STRNCMP(name, cmd->uc_name, name_len);
5312 if (cmp == 0)
5313 {
5314 if (name_len < len)
5315 cmp = -1;
5316 else if (name_len > len)
5317 cmp = 1;
5318 }
5319
5320 if (cmp == 0)
5321 {
5322 if (!force)
5323 {
5324 EMSG(_("E174: Command already exists: add ! to replace it"));
5325 goto fail;
5326 }
5327
5328 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005329 cmd->uc_rep = NULL;
5330#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5331 vim_free(cmd->uc_compl_arg);
5332 cmd->uc_compl_arg = NULL;
5333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 break;
5335 }
5336
5337 /* Stop as soon as we pass the name to add */
5338 if (cmp < 0)
5339 break;
5340 }
5341
5342 /* Extend the array unless we're replacing an existing command */
5343 if (cmp != 0)
5344 {
5345 if (ga_grow(gap, 1) != OK)
5346 goto fail;
5347 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5348 goto fail;
5349
5350 cmd = USER_CMD_GA(gap, i);
5351 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5352
5353 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 cmd->uc_name = p;
5356 }
5357
5358 cmd->uc_rep = rep_buf;
5359 cmd->uc_argt = argt;
5360 cmd->uc_def = def;
5361 cmd->uc_compl = compl;
5362#ifdef FEAT_EVAL
5363 cmd->uc_scriptID = current_SID;
5364# ifdef FEAT_CMDL_COMPL
5365 cmd->uc_compl_arg = compl_arg;
5366# endif
5367#endif
5368
5369 return OK;
5370
5371fail:
5372 vim_free(rep_buf);
5373#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5374 vim_free(compl_arg);
5375#endif
5376 return FAIL;
5377}
5378
5379/*
5380 * List of names for completion for ":command" with the EXPAND_ flag.
5381 * Must be alphabetical for completion.
5382 */
5383static struct
5384{
5385 int expand;
5386 char *name;
5387} command_complete[] =
5388{
5389 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005390 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005392 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005394 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005395#if defined(FEAT_CSCOPE)
5396 {EXPAND_CSCOPE, "cscope"},
5397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5399 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005400 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401#endif
5402 {EXPAND_DIRECTORIES, "dir"},
5403 {EXPAND_ENV_VARS, "environment"},
5404 {EXPAND_EVENTS, "event"},
5405 {EXPAND_EXPRESSION, "expression"},
5406 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005407 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005408 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {EXPAND_FUNCTIONS, "function"},
5410 {EXPAND_HELP, "help"},
5411 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005412#if defined(FEAT_CMDHIST)
5413 {EXPAND_HISTORY, "history"},
5414#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005415#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005416 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005417 {EXPAND_LOCALES, "locale"},
5418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {EXPAND_MAPPINGS, "mapping"},
5420 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005421 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005423 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005424#if defined(FEAT_SIGNS)
5425 {EXPAND_SIGN, "sign"},
5426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {EXPAND_TAGS, "tag"},
5428 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005429 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {EXPAND_USER_VARS, "var"},
5431 {0, NULL}
5432};
5433
5434 static void
5435uc_list(name, name_len)
5436 char_u *name;
5437 size_t name_len;
5438{
5439 int i, j;
5440 int found = FALSE;
5441 ucmd_T *cmd;
5442 int len;
5443 long a;
5444 garray_T *gap;
5445
5446 gap = &curbuf->b_ucmds;
5447 for (;;)
5448 {
5449 for (i = 0; i < gap->ga_len; ++i)
5450 {
5451 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005452 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454 /* Skip commands which don't match the requested prefix */
5455 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5456 continue;
5457
5458 /* Put out the title first time */
5459 if (!found)
5460 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5461 found = TRUE;
5462 msg_putchar('\n');
5463 if (got_int)
5464 break;
5465
5466 /* Special cases */
5467 msg_putchar(a & BANG ? '!' : ' ');
5468 msg_putchar(a & REGSTR ? '"' : ' ');
5469 msg_putchar(gap != &ucmds ? 'b' : ' ');
5470 msg_putchar(' ');
5471
5472 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5473 len = (int)STRLEN(cmd->uc_name) + 4;
5474
5475 do {
5476 msg_putchar(' ');
5477 ++len;
5478 } while (len < 16);
5479
5480 len = 0;
5481
5482 /* Arguments */
5483 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5484 {
5485 case 0: IObuff[len++] = '0'; break;
5486 case (EXTRA): IObuff[len++] = '*'; break;
5487 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5488 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5489 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5490 }
5491
5492 do {
5493 IObuff[len++] = ' ';
5494 } while (len < 5);
5495
5496 /* Range */
5497 if (a & (RANGE|COUNT))
5498 {
5499 if (a & COUNT)
5500 {
5501 /* -count=N */
5502 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5503 len += (int)STRLEN(IObuff + len);
5504 }
5505 else if (a & DFLALL)
5506 IObuff[len++] = '%';
5507 else if (cmd->uc_def >= 0)
5508 {
5509 /* -range=N */
5510 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5511 len += (int)STRLEN(IObuff + len);
5512 }
5513 else
5514 IObuff[len++] = '.';
5515 }
5516
5517 do {
5518 IObuff[len++] = ' ';
5519 } while (len < 11);
5520
5521 /* Completion */
5522 for (j = 0; command_complete[j].expand != 0; ++j)
5523 if (command_complete[j].expand == cmd->uc_compl)
5524 {
5525 STRCPY(IObuff + len, command_complete[j].name);
5526 len += (int)STRLEN(IObuff + len);
5527 break;
5528 }
5529
5530 do {
5531 IObuff[len++] = ' ';
5532 } while (len < 21);
5533
5534 IObuff[len] = '\0';
5535 msg_outtrans(IObuff);
5536
5537 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005538#ifdef FEAT_EVAL
5539 if (p_verbose > 0)
5540 last_set_msg(cmd->uc_scriptID);
5541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 out_flush();
5543 ui_breakcheck();
5544 if (got_int)
5545 break;
5546 }
5547 if (gap == &ucmds || i < gap->ga_len)
5548 break;
5549 gap = &ucmds;
5550 }
5551
5552 if (!found)
5553 MSG(_("No user-defined commands found"));
5554}
5555
5556 static char_u *
5557uc_fun_cmd()
5558{
5559 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5560 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5561 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5562 0xb9, 0x7f, 0};
5563 int i;
5564
5565 for (i = 0; fcmd[i]; ++i)
5566 IObuff[i] = fcmd[i] - 0x40;
5567 IObuff[i] = 0;
5568 return IObuff;
5569}
5570
5571 static int
5572uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5573 char_u *attr;
5574 size_t len;
5575 long *argt;
5576 long *def;
5577 int *flags;
5578 int *compl;
5579 char_u **compl_arg;
5580{
5581 char_u *p;
5582
5583 if (len == 0)
5584 {
5585 EMSG(_("E175: No attribute specified"));
5586 return FAIL;
5587 }
5588
5589 /* First, try the simple attributes (no arguments) */
5590 if (STRNICMP(attr, "bang", len) == 0)
5591 *argt |= BANG;
5592 else if (STRNICMP(attr, "buffer", len) == 0)
5593 *flags |= UC_BUFFER;
5594 else if (STRNICMP(attr, "register", len) == 0)
5595 *argt |= REGSTR;
5596 else if (STRNICMP(attr, "bar", len) == 0)
5597 *argt |= TRLBAR;
5598 else
5599 {
5600 int i;
5601 char_u *val = NULL;
5602 size_t vallen = 0;
5603 size_t attrlen = len;
5604
5605 /* Look for the attribute name - which is the part before any '=' */
5606 for (i = 0; i < (int)len; ++i)
5607 {
5608 if (attr[i] == '=')
5609 {
5610 val = &attr[i + 1];
5611 vallen = len - i - 1;
5612 attrlen = i;
5613 break;
5614 }
5615 }
5616
5617 if (STRNICMP(attr, "nargs", attrlen) == 0)
5618 {
5619 if (vallen == 1)
5620 {
5621 if (*val == '0')
5622 /* Do nothing - this is the default */;
5623 else if (*val == '1')
5624 *argt |= (EXTRA | NOSPC | NEEDARG);
5625 else if (*val == '*')
5626 *argt |= EXTRA;
5627 else if (*val == '?')
5628 *argt |= (EXTRA | NOSPC);
5629 else if (*val == '+')
5630 *argt |= (EXTRA | NEEDARG);
5631 else
5632 goto wrong_nargs;
5633 }
5634 else
5635 {
5636wrong_nargs:
5637 EMSG(_("E176: Invalid number of arguments"));
5638 return FAIL;
5639 }
5640 }
5641 else if (STRNICMP(attr, "range", attrlen) == 0)
5642 {
5643 *argt |= RANGE;
5644 if (vallen == 1 && *val == '%')
5645 *argt |= DFLALL;
5646 else if (val != NULL)
5647 {
5648 p = val;
5649 if (*def >= 0)
5650 {
5651two_count:
5652 EMSG(_("E177: Count cannot be specified twice"));
5653 return FAIL;
5654 }
5655
5656 *def = getdigits(&p);
5657 *argt |= (ZEROR | NOTADR);
5658
5659 if (p != val + vallen || vallen == 0)
5660 {
5661invalid_count:
5662 EMSG(_("E178: Invalid default value for count"));
5663 return FAIL;
5664 }
5665 }
5666 }
5667 else if (STRNICMP(attr, "count", attrlen) == 0)
5668 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005669 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 if (val != NULL)
5672 {
5673 p = val;
5674 if (*def >= 0)
5675 goto two_count;
5676
5677 *def = getdigits(&p);
5678
5679 if (p != val + vallen)
5680 goto invalid_count;
5681 }
5682
5683 if (*def < 0)
5684 *def = 0;
5685 }
5686 else if (STRNICMP(attr, "complete", attrlen) == 0)
5687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 if (val == NULL)
5689 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005690 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 return FAIL;
5692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005694 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5695 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698 else
5699 {
5700 char_u ch = attr[len];
5701 attr[len] = '\0';
5702 EMSG2(_("E181: Invalid attribute: %s"), attr);
5703 attr[len] = ch;
5704 return FAIL;
5705 }
5706 }
5707
5708 return OK;
5709}
5710
Bram Moolenaara850a712009-01-28 14:42:59 +00005711/*
5712 * ":command ..."
5713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 static void
5715ex_command(eap)
5716 exarg_T *eap;
5717{
5718 char_u *name;
5719 char_u *end;
5720 char_u *p;
5721 long argt = 0;
5722 long def = -1;
5723 int flags = 0;
5724 int compl = EXPAND_NOTHING;
5725 char_u *compl_arg = NULL;
5726 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005727 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 p = eap->arg;
5730
5731 /* Check for attributes */
5732 while (*p == '-')
5733 {
5734 ++p;
5735 end = skiptowhite(p);
5736 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5737 == FAIL)
5738 return;
5739 p = skipwhite(end);
5740 }
5741
5742 /* Get the name (if any) and skip to the following argument */
5743 name = p;
5744 if (ASCII_ISALPHA(*p))
5745 while (ASCII_ISALNUM(*p))
5746 ++p;
5747 if (!ends_excmd(*p) && !vim_iswhite(*p))
5748 {
5749 EMSG(_("E182: Invalid command name"));
5750 return;
5751 }
5752 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005753 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755 /* If there is nothing after the name, and no attributes were specified,
5756 * we are listing commands
5757 */
5758 p = skipwhite(end);
5759 if (!has_attr && ends_excmd(*p))
5760 {
5761 uc_list(name, end - name);
5762 }
5763 else if (!ASCII_ISUPPER(*name))
5764 {
5765 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5766 return;
5767 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005768 else if ((name_len == 1 && *name == 'X')
5769 || (name_len <= 4
5770 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5771 {
5772 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5773 return;
5774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
5776 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5777 eap->forceit);
5778}
5779
5780/*
5781 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 * Clear all user commands, global and for current buffer.
5783 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005784 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005786 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 uc_clear(&ucmds);
5789 uc_clear(&curbuf->b_ucmds);
5790}
5791
5792/*
5793 * Clear all user commands for "gap".
5794 */
5795 void
5796uc_clear(gap)
5797 garray_T *gap;
5798{
5799 int i;
5800 ucmd_T *cmd;
5801
5802 for (i = 0; i < gap->ga_len; ++i)
5803 {
5804 cmd = USER_CMD_GA(gap, i);
5805 vim_free(cmd->uc_name);
5806 vim_free(cmd->uc_rep);
5807# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5808 vim_free(cmd->uc_compl_arg);
5809# endif
5810 }
5811 ga_clear(gap);
5812}
5813
5814 static void
5815ex_delcommand(eap)
5816 exarg_T *eap;
5817{
5818 int i = 0;
5819 ucmd_T *cmd = NULL;
5820 int cmp = -1;
5821 garray_T *gap;
5822
5823 gap = &curbuf->b_ucmds;
5824 for (;;)
5825 {
5826 for (i = 0; i < gap->ga_len; ++i)
5827 {
5828 cmd = USER_CMD_GA(gap, i);
5829 cmp = STRCMP(eap->arg, cmd->uc_name);
5830 if (cmp <= 0)
5831 break;
5832 }
5833 if (gap == &ucmds || cmp == 0)
5834 break;
5835 gap = &ucmds;
5836 }
5837
5838 if (cmp != 0)
5839 {
5840 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5841 return;
5842 }
5843
5844 vim_free(cmd->uc_name);
5845 vim_free(cmd->uc_rep);
5846# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5847 vim_free(cmd->uc_compl_arg);
5848# endif
5849
5850 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852 if (i < gap->ga_len)
5853 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5854}
5855
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005856/*
5857 * split and quote args for <f-args>
5858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 static char_u *
5860uc_split_args(arg, lenp)
5861 char_u *arg;
5862 size_t *lenp;
5863{
5864 char_u *buf;
5865 char_u *p;
5866 char_u *q;
5867 int len;
5868
5869 /* Precalculate length */
5870 p = arg;
5871 len = 2; /* Initial and final quotes */
5872
5873 while (*p)
5874 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005875 if (p[0] == '\\' && p[1] == '\\')
5876 {
5877 len += 2;
5878 p += 2;
5879 }
5880 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
5882 len += 1;
5883 p += 2;
5884 }
5885 else if (*p == '\\' || *p == '"')
5886 {
5887 len += 2;
5888 p += 1;
5889 }
5890 else if (vim_iswhite(*p))
5891 {
5892 p = skipwhite(p);
5893 if (*p == NUL)
5894 break;
5895 len += 3; /* "," */
5896 }
5897 else
5898 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005899#ifdef FEAT_MBYTE
5900 int charlen = (*mb_ptr2len)(p);
5901 len += charlen;
5902 p += charlen;
5903#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 ++len;
5905 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 }
5909
5910 buf = alloc(len + 1);
5911 if (buf == NULL)
5912 {
5913 *lenp = 0;
5914 return buf;
5915 }
5916
5917 p = arg;
5918 q = buf;
5919 *q++ = '"';
5920 while (*p)
5921 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005922 if (p[0] == '\\' && p[1] == '\\')
5923 {
5924 *q++ = '\\';
5925 *q++ = '\\';
5926 p += 2;
5927 }
5928 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
5930 *q++ = p[1];
5931 p += 2;
5932 }
5933 else if (*p == '\\' || *p == '"')
5934 {
5935 *q++ = '\\';
5936 *q++ = *p++;
5937 }
5938 else if (vim_iswhite(*p))
5939 {
5940 p = skipwhite(p);
5941 if (*p == NUL)
5942 break;
5943 *q++ = '"';
5944 *q++ = ',';
5945 *q++ = '"';
5946 }
5947 else
5948 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005949 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 }
5951 }
5952 *q++ = '"';
5953 *q = 0;
5954
5955 *lenp = len;
5956 return buf;
5957}
5958
5959/*
5960 * Check for a <> code in a user command.
5961 * "code" points to the '<'. "len" the length of the <> (inclusive).
5962 * "buf" is where the result is to be added.
5963 * "split_buf" points to a buffer used for splitting, caller should free it.
5964 * "split_len" is the length of what "split_buf" contains.
5965 * Returns the length of the replacement, which has been added to "buf".
5966 * Returns -1 if there was no match, and only the "<" has been copied.
5967 */
5968 static size_t
5969uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5970 char_u *code;
5971 size_t len;
5972 char_u *buf;
5973 ucmd_T *cmd; /* the user command we're expanding */
5974 exarg_T *eap; /* ex arguments */
5975 char_u **split_buf;
5976 size_t *split_len;
5977{
5978 size_t result = 0;
5979 char_u *p = code + 1;
5980 size_t l = len - 2;
5981 int quote = 0;
5982 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5983 ct_LT, ct_NONE } type = ct_NONE;
5984
5985 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5986 {
5987 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5988 p += 2;
5989 l -= 2;
5990 }
5991
Bram Moolenaar371d5402006-03-20 21:47:49 +00005992 ++l;
5993 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005995 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005997 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005999 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006001 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006003 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006005 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006007 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 type = ct_REGISTER;
6009
6010 switch (type)
6011 {
6012 case ct_ARGS:
6013 /* Simple case first */
6014 if (*eap->arg == NUL)
6015 {
6016 if (quote == 1)
6017 {
6018 result = 2;
6019 if (buf != NULL)
6020 STRCPY(buf, "''");
6021 }
6022 else
6023 result = 0;
6024 break;
6025 }
6026
6027 /* When specified there is a single argument don't split it.
6028 * Works for ":Cmd %" when % is "a b c". */
6029 if ((eap->argt & NOSPC) && quote == 2)
6030 quote = 1;
6031
6032 switch (quote)
6033 {
6034 case 0: /* No quoting, no splitting */
6035 result = STRLEN(eap->arg);
6036 if (buf != NULL)
6037 STRCPY(buf, eap->arg);
6038 break;
6039 case 1: /* Quote, but don't split */
6040 result = STRLEN(eap->arg) + 2;
6041 for (p = eap->arg; *p; ++p)
6042 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006043#ifdef FEAT_MBYTE
6044 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6045 /* DBCS can contain \ in a trail byte, skip the
6046 * double-byte character. */
6047 ++p;
6048 else
6049#endif
6050 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 ++result;
6052 }
6053
6054 if (buf != NULL)
6055 {
6056 *buf++ = '"';
6057 for (p = eap->arg; *p; ++p)
6058 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006059#ifdef FEAT_MBYTE
6060 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6061 /* DBCS can contain \ in a trail byte, copy the
6062 * double-byte character to avoid escaping. */
6063 *buf++ = *p++;
6064 else
6065#endif
6066 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 *buf++ = '\\';
6068 *buf++ = *p;
6069 }
6070 *buf = '"';
6071 }
6072
6073 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006074 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 /* This is hard, so only do it once, and cache the result */
6076 if (*split_buf == NULL)
6077 *split_buf = uc_split_args(eap->arg, split_len);
6078
6079 result = *split_len;
6080 if (buf != NULL && result != 0)
6081 STRCPY(buf, *split_buf);
6082
6083 break;
6084 }
6085 break;
6086
6087 case ct_BANG:
6088 result = eap->forceit ? 1 : 0;
6089 if (quote)
6090 result += 2;
6091 if (buf != NULL)
6092 {
6093 if (quote)
6094 *buf++ = '"';
6095 if (eap->forceit)
6096 *buf++ = '!';
6097 if (quote)
6098 *buf = '"';
6099 }
6100 break;
6101
6102 case ct_LINE1:
6103 case ct_LINE2:
6104 case ct_COUNT:
6105 {
6106 char num_buf[20];
6107 long num = (type == ct_LINE1) ? eap->line1 :
6108 (type == ct_LINE2) ? eap->line2 :
6109 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6110 size_t num_len;
6111
6112 sprintf(num_buf, "%ld", num);
6113 num_len = STRLEN(num_buf);
6114 result = num_len;
6115
6116 if (quote)
6117 result += 2;
6118
6119 if (buf != NULL)
6120 {
6121 if (quote)
6122 *buf++ = '"';
6123 STRCPY(buf, num_buf);
6124 buf += num_len;
6125 if (quote)
6126 *buf = '"';
6127 }
6128
6129 break;
6130 }
6131
6132 case ct_REGISTER:
6133 result = eap->regname ? 1 : 0;
6134 if (quote)
6135 result += 2;
6136 if (buf != NULL)
6137 {
6138 if (quote)
6139 *buf++ = '\'';
6140 if (eap->regname)
6141 *buf++ = eap->regname;
6142 if (quote)
6143 *buf = '\'';
6144 }
6145 break;
6146
6147 case ct_LT:
6148 result = 1;
6149 if (buf != NULL)
6150 *buf = '<';
6151 break;
6152
6153 default:
6154 /* Not recognized: just copy the '<' and return -1. */
6155 result = (size_t)-1;
6156 if (buf != NULL)
6157 *buf = '<';
6158 break;
6159 }
6160
6161 return result;
6162}
6163
6164 static void
6165do_ucmd(eap)
6166 exarg_T *eap;
6167{
6168 char_u *buf;
6169 char_u *p;
6170 char_u *q;
6171
6172 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006173 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006174 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 size_t len, totlen;
6176
6177 size_t split_len = 0;
6178 char_u *split_buf = NULL;
6179 ucmd_T *cmd;
6180#ifdef FEAT_EVAL
6181 scid_T save_current_SID = current_SID;
6182#endif
6183
6184 if (eap->cmdidx == CMD_USER)
6185 cmd = USER_CMD(eap->useridx);
6186 else
6187 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6188
6189 /*
6190 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006191 * First round: "buf" is NULL, compute length, allocate "buf".
6192 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 */
6194 buf = NULL;
6195 for (;;)
6196 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006197 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006198 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006200
6201 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006203 start = vim_strchr(p, '<');
6204 if (start != NULL)
6205 end = vim_strchr(start + 1, '>');
6206 if (buf != NULL)
6207 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006208 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6209 ;
6210 if (*ksp == K_SPECIAL
6211 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006212 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6213# ifdef FEAT_GUI
6214 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6215# endif
6216 ))
6217 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006218 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006219 * KS_SPECIAL KE_FILLER, like for mappings, but
6220 * do_cmdline() doesn't handle that, so convert it back.
6221 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6222 len = ksp - p;
6223 if (len > 0)
6224 {
6225 mch_memmove(q, p, len);
6226 q += len;
6227 }
6228 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6229 p = ksp + 3;
6230 continue;
6231 }
6232 }
6233
6234 /* break if there no <item> is found */
6235 if (start == NULL || end == NULL)
6236 break;
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 /* Include the '>' */
6239 ++end;
6240
6241 /* Take everything up to the '<' */
6242 len = start - p;
6243 if (buf == NULL)
6244 totlen += len;
6245 else
6246 {
6247 mch_memmove(q, p, len);
6248 q += len;
6249 }
6250
6251 len = uc_check_code(start, end - start, q, cmd, eap,
6252 &split_buf, &split_len);
6253 if (len == (size_t)-1)
6254 {
6255 /* no match, continue after '<' */
6256 p = start + 1;
6257 len = 1;
6258 }
6259 else
6260 p = end;
6261 if (buf == NULL)
6262 totlen += len;
6263 else
6264 q += len;
6265 }
6266 if (buf != NULL) /* second time here, finished */
6267 {
6268 STRCPY(q, p);
6269 break;
6270 }
6271
6272 totlen += STRLEN(p); /* Add on the trailing characters */
6273 buf = alloc((unsigned)(totlen + 1));
6274 if (buf == NULL)
6275 {
6276 vim_free(split_buf);
6277 return;
6278 }
6279 }
6280
6281#ifdef FEAT_EVAL
6282 current_SID = cmd->uc_scriptID;
6283#endif
6284 (void)do_cmdline(buf, eap->getline, eap->cookie,
6285 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6286#ifdef FEAT_EVAL
6287 current_SID = save_current_SID;
6288#endif
6289 vim_free(buf);
6290 vim_free(split_buf);
6291}
6292
6293# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6294 static char_u *
6295get_user_command_name(idx)
6296 int idx;
6297{
6298 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6299}
6300
6301/*
6302 * Function given to ExpandGeneric() to obtain the list of user command names.
6303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 char_u *
6305get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006306 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 int idx;
6308{
6309 if (idx < curbuf->b_ucmds.ga_len)
6310 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6311 idx -= curbuf->b_ucmds.ga_len;
6312 if (idx < ucmds.ga_len)
6313 return USER_CMD(idx)->uc_name;
6314 return NULL;
6315}
6316
6317/*
6318 * Function given to ExpandGeneric() to obtain the list of user command
6319 * attributes.
6320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 char_u *
6322get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006323 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 int idx;
6325{
6326 static char *user_cmd_flags[] =
6327 {"bang", "bar", "buffer", "complete", "count",
6328 "nargs", "range", "register"};
6329
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006330 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 return NULL;
6332 return (char_u *)user_cmd_flags[idx];
6333}
6334
6335/*
6336 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 char_u *
6339get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006340 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 int idx;
6342{
6343 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6344
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006345 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 return NULL;
6347 return (char_u *)user_cmd_nargs[idx];
6348}
6349
6350/*
6351 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 char_u *
6354get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006355 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 int idx;
6357{
6358 return (char_u *)command_complete[idx].name;
6359}
6360# endif /* FEAT_CMDL_COMPL */
6361
6362#endif /* FEAT_USR_CMDS */
6363
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006364#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6365/*
6366 * Parse a completion argument "value[vallen]".
6367 * The detected completion goes in "*complp", argument type in "*argt".
6368 * When there is an argument, for function and user defined completion, it's
6369 * copied to allocated memory and stored in "*compl_arg".
6370 * Returns FAIL if something is wrong.
6371 */
6372 int
6373parse_compl_arg(value, vallen, complp, argt, compl_arg)
6374 char_u *value;
6375 int vallen;
6376 int *complp;
6377 long *argt;
6378 char_u **compl_arg;
6379{
6380 char_u *arg = NULL;
6381 size_t arglen = 0;
6382 int i;
6383 int valend = vallen;
6384
6385 /* Look for any argument part - which is the part after any ',' */
6386 for (i = 0; i < vallen; ++i)
6387 {
6388 if (value[i] == ',')
6389 {
6390 arg = &value[i + 1];
6391 arglen = vallen - i - 1;
6392 valend = i;
6393 break;
6394 }
6395 }
6396
6397 for (i = 0; command_complete[i].expand != 0; ++i)
6398 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006399 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006400 && STRNCMP(value, command_complete[i].name, valend) == 0)
6401 {
6402 *complp = command_complete[i].expand;
6403 if (command_complete[i].expand == EXPAND_BUFFERS)
6404 *argt |= BUFNAME;
6405 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6406 || command_complete[i].expand == EXPAND_FILES)
6407 *argt |= XFILE;
6408 break;
6409 }
6410 }
6411
6412 if (command_complete[i].expand == 0)
6413 {
6414 EMSG2(_("E180: Invalid complete value: %s"), value);
6415 return FAIL;
6416 }
6417
6418# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6419 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6420 && arg != NULL)
6421# else
6422 if (arg != NULL)
6423# endif
6424 {
6425 EMSG(_("E468: Completion argument only allowed for custom completion"));
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 {
6433 EMSG(_("E467: Custom completion requires a function argument"));
6434 return FAIL;
6435 }
6436
6437 if (arg != NULL)
6438 *compl_arg = vim_strnsave(arg, (int)arglen);
6439# endif
6440 return OK;
6441}
6442#endif
6443
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 static void
6445ex_colorscheme(eap)
6446 exarg_T *eap;
6447{
Bram Moolenaare6850792010-05-14 15:28:44 +02006448 if (*eap->arg == NUL)
6449 {
6450#ifdef FEAT_EVAL
6451 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6452 char_u *p = NULL;
6453
6454 if (expr != NULL)
6455 {
6456 ++emsg_off;
6457 p = eval_to_string(expr, NULL, FALSE);
6458 --emsg_off;
6459 vim_free(expr);
6460 }
6461 if (p != NULL)
6462 {
6463 MSG(p);
6464 vim_free(p);
6465 }
6466 else
6467 MSG("default");
6468#else
6469 MSG(_("unknown"));
6470#endif
6471 }
6472 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006473 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475
6476 static void
6477ex_highlight(eap)
6478 exarg_T *eap;
6479{
6480 if (*eap->arg == NUL && eap->cmd[2] == '!')
6481 MSG(_("Greetings, Vim user!"));
6482 do_highlight(eap->arg, eap->forceit, FALSE);
6483}
6484
6485
6486/*
6487 * Call this function if we thought we were going to exit, but we won't
6488 * (because of an error). May need to restore the terminal mode.
6489 */
6490 void
6491not_exiting()
6492{
6493 exiting = FALSE;
6494 settmode(TMODE_RAW);
6495}
6496
6497/*
6498 * ":quit": quit current window, quit Vim if closed the last window.
6499 */
6500 static void
6501ex_quit(eap)
6502 exarg_T *eap;
6503{
6504#ifdef FEAT_CMDWIN
6505 if (cmdwin_type != 0)
6506 {
6507 cmdwin_result = Ctrl_C;
6508 return;
6509 }
6510#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006511 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006512 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006513 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006514 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006515 return;
6516 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006517#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006518 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02006519 /* Refuse to quick when locked or when the buffer in the last window is
6520 * being closed (can only happen in autocommands). */
6521 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006522 return;
6523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
6525#ifdef FEAT_NETBEANS_INTG
6526 netbeansForcedQuit = eap->forceit;
6527#endif
6528
6529 /*
6530 * If there are more files or windows we won't exit.
6531 */
6532 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6533 exiting = TRUE;
6534 if ((!P_HID(curbuf)
6535 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6536 || check_more(TRUE, eap->forceit) == FAIL
6537 || (only_one_window() && check_changed_any(eap->forceit)))
6538 {
6539 not_exiting();
6540 }
6541 else
6542 {
6543#ifdef FEAT_WINDOWS
6544 if (only_one_window()) /* quit last window */
6545#endif
6546 getout(0);
6547#ifdef FEAT_WINDOWS
6548# ifdef FEAT_GUI
6549 need_mouse_correct = TRUE;
6550# endif
6551 /* close window; may free buffer */
6552 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6553#endif
6554 }
6555}
6556
6557/*
6558 * ":cquit".
6559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 static void
6561ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006562 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 getout(1); /* this does not always pass on the exit code to the Manx
6565 compiler. why? */
6566}
6567
6568/*
6569 * ":qall": try to quit all windows
6570 */
6571 static void
6572ex_quit_all(eap)
6573 exarg_T *eap;
6574{
6575# ifdef FEAT_CMDWIN
6576 if (cmdwin_type != 0)
6577 {
6578 if (eap->forceit)
6579 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6580 else
6581 cmdwin_result = K_XF2;
6582 return;
6583 }
6584# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006585
6586 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006587 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006588 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006589 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006590 return;
6591 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006592#ifdef FEAT_AUTOCMD
6593 if (curbuf_locked())
6594 return;
6595#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 exiting = TRUE;
6598 if (eap->forceit || !check_changed_any(FALSE))
6599 getout(0);
6600 not_exiting();
6601}
6602
Bram Moolenaard9967712006-03-11 21:18:15 +00006603#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604/*
6605 * ":close": close current window, unless it is the last one
6606 */
6607 static void
6608ex_close(eap)
6609 exarg_T *eap;
6610{
6611# ifdef FEAT_CMDWIN
6612 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006613 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 else
6615# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006616 if (!text_locked()
6617#ifdef FEAT_AUTOCMD
6618 && !curbuf_locked()
6619#endif
6620 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006621 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622}
6623
Bram Moolenaard9967712006-03-11 21:18:15 +00006624# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006625/*
6626 * ":pclose": Close any preview window.
6627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006631{
6632 win_T *win;
6633
6634 for (win = firstwin; win != NULL; win = win->w_next)
6635 if (win->w_p_pvw)
6636 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006637 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006638 break;
6639 }
6640}
Bram Moolenaard9967712006-03-11 21:18:15 +00006641# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006642
Bram Moolenaarf740b292006-02-16 22:11:02 +00006643/*
6644 * Close window "win" and take care of handling closing the last window for a
6645 * modified buffer.
6646 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006648ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006651 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 int need_hide;
6654 buf_T *buf = win->w_buffer;
6655
6656 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006657 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006659# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if ((p_confirm || cmdmod.confirm) && p_write)
6661 {
6662 dialog_changed(buf, FALSE);
6663 if (buf_valid(buf) && bufIsChanged(buf))
6664 return;
6665 need_hide = FALSE;
6666 }
6667 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
6670 EMSG(_(e_nowrtmsg));
6671 return;
6672 }
6673 }
6674
Bram Moolenaard9967712006-03-11 21:18:15 +00006675# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006677# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006678
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006680 if (tp == NULL)
6681 win_close(win, !need_hide && !P_HID(buf));
6682 else
6683 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684}
6685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006687 * ":tabclose": close current tab page, unless it is the last one.
6688 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 */
6690 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 exarg_T *eap;
6693{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006694 tabpage_T *tp;
6695
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006696# ifdef FEAT_CMDWIN
6697 if (cmdwin_type != 0)
6698 cmdwin_result = K_IGNORE;
6699 else
6700# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006702 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006705 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006707 tp = find_tabpage((int)eap->line2);
6708 if (tp == NULL)
6709 {
6710 beep_flush();
6711 return;
6712 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006713 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006714 {
6715 tabpage_close_other(tp, eap->forceit);
6716 return;
6717 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006718 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006719 if (!text_locked()
6720#ifdef FEAT_AUTOCMD
6721 && !curbuf_locked()
6722#endif
6723 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006726}
6727
6728/*
6729 * ":tabonly": close all tab pages except the current one
6730 */
6731 static void
6732ex_tabonly(eap)
6733 exarg_T *eap;
6734{
6735 tabpage_T *tp;
6736 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006737
6738# ifdef FEAT_CMDWIN
6739 if (cmdwin_type != 0)
6740 cmdwin_result = K_IGNORE;
6741 else
6742# endif
6743 if (first_tabpage->tp_next == NULL)
6744 MSG(_("Already only one tab page"));
6745 else
6746 {
6747 /* Repeat this up to a 1000 times, because autocommands may mess
6748 * up the lists. */
6749 for (done = 0; done < 1000; ++done)
6750 {
6751 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6752 if (tp->tp_topframe != topframe)
6753 {
6754 tabpage_close_other(tp, eap->forceit);
6755 /* if we failed to close it quit */
6756 if (valid_tabpage(tp))
6757 done = 1000;
6758 /* start over, "tp" is now invalid */
6759 break;
6760 }
6761 if (first_tabpage->tp_next == NULL)
6762 break;
6763 }
6764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006768 * Close the current tab page.
6769 */
6770 void
6771tabpage_close(forceit)
6772 int forceit;
6773{
6774 /* First close all the windows but the current one. If that worked then
6775 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006776 if (lastwin != firstwin)
6777 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006778 if (lastwin == firstwin)
6779 ex_win_close(forceit, curwin, NULL);
6780# ifdef FEAT_GUI
6781 need_mouse_correct = TRUE;
6782# endif
6783}
6784
6785/*
6786 * Close tab page "tp", which is not the current tab page.
6787 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006788 * Also takes care of the tab pages line disappearing when closing the
6789 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006790 */
6791 void
6792tabpage_close_other(tp, forceit)
6793 tabpage_T *tp;
6794 int forceit;
6795{
6796 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006797 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006798 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006799
6800 /* Limit to 1000 windows, autocommands may add a window while we close
6801 * one. OK, so I'm paranoid... */
6802 while (++done < 1000)
6803 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006804 wp = tp->tp_firstwin;
6805 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006806
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006807 /* Autocommands may delete the tab page under our fingers and we may
6808 * fail to close a window with a modified buffer. */
6809 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006810 break;
6811 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006812
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006813 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006814 if (h != tabline_height())
6815 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006816}
6817
6818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 * ":only".
6820 */
6821 static void
6822ex_only(eap)
6823 exarg_T *eap;
6824{
6825# ifdef FEAT_GUI
6826 need_mouse_correct = TRUE;
6827# endif
6828 close_others(TRUE, eap->forceit);
6829}
6830
6831/*
6832 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006833 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006835 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836ex_all(eap)
6837 exarg_T *eap;
6838{
6839 if (eap->addr_count == 0)
6840 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006841 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842}
6843#endif /* FEAT_WINDOWS */
6844
6845 static void
6846ex_hide(eap)
6847 exarg_T *eap;
6848{
6849 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6850 eap->errmsg = e_invarg;
6851 else
6852 {
6853 /* ":hide" or ":hide | cmd": hide current window */
6854 eap->nextcmd = check_nextcmd(eap->arg);
6855#ifdef FEAT_WINDOWS
6856 if (!eap->skip)
6857 {
6858# ifdef FEAT_GUI
6859 need_mouse_correct = TRUE;
6860# endif
6861 win_close(curwin, FALSE); /* don't free buffer */
6862 }
6863#endif
6864 }
6865}
6866
6867/*
6868 * ":stop" and ":suspend": Suspend Vim.
6869 */
6870 static void
6871ex_stop(eap)
6872 exarg_T *eap;
6873{
6874 /*
6875 * Disallow suspending for "rvim".
6876 */
6877 if (!check_restricted()
6878#ifdef WIN3264
6879 /*
6880 * Check if external commands are allowed now.
6881 */
6882 && can_end_termcap_mode(TRUE)
6883#endif
6884 )
6885 {
6886 if (!eap->forceit)
6887 autowrite_all();
6888 windgoto((int)Rows - 1, 0);
6889 out_char('\n');
6890 out_flush();
6891 stoptermcap();
6892 out_flush(); /* needed for SUN to restore xterm buffer */
6893#ifdef FEAT_TITLE
6894 mch_restore_title(3); /* restore window titles */
6895#endif
6896 ui_suspend(); /* call machine specific function */
6897#ifdef FEAT_TITLE
6898 maketitle();
6899 resettitle(); /* force updating the title */
6900#endif
6901 starttermcap();
6902 scroll_start(); /* scroll screen before redrawing */
6903 redraw_later_clear();
6904 shell_resized(); /* may have resized window */
6905 }
6906}
6907
6908/*
6909 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6910 */
6911 static void
6912ex_exit(eap)
6913 exarg_T *eap;
6914{
6915#ifdef FEAT_CMDWIN
6916 if (cmdwin_type != 0)
6917 {
6918 cmdwin_result = Ctrl_C;
6919 return;
6920 }
6921#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006922 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006923 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006924 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006925 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006926 return;
6927 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006928#ifdef FEAT_AUTOCMD
6929 if (curbuf_locked())
6930 return;
6931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
6933 /*
6934 * if more files or windows we won't exit
6935 */
6936 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6937 exiting = TRUE;
6938 if ( ((eap->cmdidx == CMD_wq
6939 || curbufIsChanged())
6940 && do_write(eap) == FAIL)
6941 || check_more(TRUE, eap->forceit) == FAIL
6942 || (only_one_window() && check_changed_any(eap->forceit)))
6943 {
6944 not_exiting();
6945 }
6946 else
6947 {
6948#ifdef FEAT_WINDOWS
6949 if (only_one_window()) /* quit last window, exit Vim */
6950#endif
6951 getout(0);
6952#ifdef FEAT_WINDOWS
6953# ifdef FEAT_GUI
6954 need_mouse_correct = TRUE;
6955# endif
6956 /* quit current window, may free buffer */
6957 win_close(curwin, !P_HID(curwin->w_buffer));
6958#endif
6959 }
6960}
6961
6962/*
6963 * ":print", ":list", ":number".
6964 */
6965 static void
6966ex_print(eap)
6967 exarg_T *eap;
6968{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006969 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6970 EMSG(_(e_emptybuf));
6971 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006973 for ( ;!got_int; ui_breakcheck())
6974 {
6975 print_line(eap->line1,
6976 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6977 || (eap->flags & EXFLAG_NR)),
6978 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6979 if (++eap->line1 > eap->line2)
6980 break;
6981 out_flush(); /* show one line at a time */
6982 }
6983 setpcmark();
6984 /* put cursor at last line */
6985 curwin->w_cursor.lnum = eap->line2;
6986 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990}
6991
6992#ifdef FEAT_BYTEOFF
6993 static void
6994ex_goto(eap)
6995 exarg_T *eap;
6996{
6997 goto_byte(eap->line2);
6998}
6999#endif
7000
7001/*
7002 * ":shell".
7003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 static void
7005ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007006 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 do_shell(NULL, 0);
7009}
7010
7011#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7012 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007013 || defined(FEAT_GUI_MSWIN) \
7014 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 || defined(PROTO)
7016
7017/*
7018 * Handle a file drop. The code is here because a drop is *nearly* like an
7019 * :args command, but not quite (we have a list of exact filenames, so we
7020 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7021 * code is very similar to :args and hence needs access to a lot of the static
7022 * functions in this file.
7023 *
7024 * The list should be allocated using alloc(), as should each item in the
7025 * list. This function takes over responsibility for freeing the list.
7026 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007027 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7029 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7030 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7031 * file functionality is (currently) not in EMX this is not presently a
7032 * problem.
7033 */
7034 void
7035handle_drop(filec, filev, split)
7036 int filec; /* the number of files dropped */
7037 char_u **filev; /* the list of files dropped */
7038 int split; /* force splitting the window */
7039{
7040 exarg_T ea;
7041 int save_msg_scroll = msg_scroll;
7042
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007043 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007044 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007046#ifdef FEAT_AUTOCMD
7047 if (curbuf_locked())
7048 return;
7049#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007050 /* When the screen is being updated we should not change buffers and
7051 * windows structures, it may cause freed memory to be used. */
7052 if (updating_screen)
7053 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055 /* Check whether the current buffer is changed. If so, we will need
7056 * to split the current window or data could be lost.
7057 * We don't need to check if the 'hidden' option is set, as in this
7058 * case the buffer won't be lost.
7059 */
7060 if (!P_HID(curbuf) && !split)
7061 {
7062 ++emsg_off;
7063 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7064 --emsg_off;
7065 }
7066 if (split)
7067 {
7068# ifdef FEAT_WINDOWS
7069 if (win_split(0, 0) == FAIL)
7070 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007071 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
7073 /* When splitting the window, create a new alist. Otherwise the
7074 * existing one is overwritten. */
7075 alist_unlink(curwin->w_alist);
7076 alist_new();
7077# else
7078 return; /* can't split, always fail */
7079# endif
7080 }
7081
7082 /*
7083 * Set up the new argument list.
7084 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007085 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
7087 /*
7088 * Move to the first file.
7089 */
7090 /* Fake up a minimal "next" command for do_argfile() */
7091 vim_memset(&ea, 0, sizeof(ea));
7092 ea.cmd = (char_u *)"next";
7093 do_argfile(&ea, 0);
7094
7095 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7096 * mode that is not needed here. */
7097 need_start_insertmode = FALSE;
7098
7099 /* Restore msg_scroll, otherwise a following command may cause scrolling
7100 * unexpectedly. The screen will be redrawn by the caller, thus
7101 * msg_scroll being set by displaying a message is irrelevant. */
7102 msg_scroll = save_msg_scroll;
7103}
7104#endif
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106/*
7107 * Clear an argument list: free all file names and reset it to zero entries.
7108 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007109 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110alist_clear(al)
7111 alist_T *al;
7112{
7113 while (--al->al_ga.ga_len >= 0)
7114 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7115 ga_clear(&al->al_ga);
7116}
7117
7118/*
7119 * Init an argument list.
7120 */
7121 void
7122alist_init(al)
7123 alist_T *al;
7124{
7125 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7126}
7127
7128#if defined(FEAT_WINDOWS) || defined(PROTO)
7129
7130/*
7131 * Remove a reference from an argument list.
7132 * Ignored when the argument list is the global one.
7133 * If the argument list is no longer used by any window, free it.
7134 */
7135 void
7136alist_unlink(al)
7137 alist_T *al;
7138{
7139 if (al != &global_alist && --al->al_refcount <= 0)
7140 {
7141 alist_clear(al);
7142 vim_free(al);
7143 }
7144}
7145
7146# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7147/*
7148 * Create a new argument list and use it for the current window.
7149 */
7150 void
7151alist_new()
7152{
7153 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7154 if (curwin->w_alist == NULL)
7155 {
7156 curwin->w_alist = &global_alist;
7157 ++global_alist.al_refcount;
7158 }
7159 else
7160 {
7161 curwin->w_alist->al_refcount = 1;
7162 alist_init(curwin->w_alist);
7163 }
7164}
7165# endif
7166#endif
7167
7168#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7169/*
7170 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007171 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7172 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 */
7174 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007175alist_expand(fnum_list, fnum_len)
7176 int *fnum_list;
7177 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007180 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 char_u **new_arg_files;
7182 int new_arg_file_count;
7183 char_u *save_p_su = p_su;
7184 int i;
7185
7186 /* Don't use 'suffixes' here. This should work like the shell did the
7187 * expansion. Also, the vimrc file isn't read yet, thus the user
7188 * can't set the options. */
7189 p_su = empty_option;
7190 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7191 if (old_arg_files != NULL)
7192 {
7193 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007194 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7195 old_arg_count = GARGCOUNT;
7196 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007198 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 && new_arg_file_count > 0)
7200 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007201 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7202 TRUE, fnum_list, fnum_len);
7203 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206 p_su = save_p_su;
7207}
7208#endif
7209
7210/*
7211 * Set the argument list for the current window.
7212 * Takes over the allocated files[] and the allocated fnames in it.
7213 */
7214 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007215alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 alist_T *al;
7217 int count;
7218 char_u **files;
7219 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007220 int *fnum_list;
7221 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 int i;
7224
7225 alist_clear(al);
7226 if (ga_grow(&al->al_ga, count) == OK)
7227 {
7228 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007229 {
7230 if (got_int)
7231 {
7232 /* When adding many buffers this can take a long time. Allow
7233 * interrupting here. */
7234 while (i < count)
7235 vim_free(files[i++]);
7236 break;
7237 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007238
7239 /* May set buffer name of a buffer previously used for the
7240 * argument list, so that it's re-used by alist_add. */
7241 if (fnum_list != NULL && i < fnum_len)
7242 buf_set_name(fnum_list[i], files[i]);
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007245 ui_breakcheck();
7246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 vim_free(files);
7248 }
7249 else
7250 FreeWild(count, files);
7251#ifdef FEAT_WINDOWS
7252 if (al == &global_alist)
7253#endif
7254 arg_had_last = FALSE;
7255}
7256
7257/*
7258 * Add file "fname" to argument list "al".
7259 * "fname" must have been allocated and "al" must have been checked for room.
7260 */
7261 void
7262alist_add(al, fname, set_fnum)
7263 alist_T *al;
7264 char_u *fname;
7265 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7266{
7267 if (fname == NULL) /* don't add NULL file names */
7268 return;
7269#ifdef BACKSLASH_IN_FILENAME
7270 slash_adjust(fname);
7271#endif
7272 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7273 if (set_fnum > 0)
7274 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7275 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7276 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277}
7278
7279#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7280/*
7281 * Adjust slashes in file names. Called after 'shellslash' was set.
7282 */
7283 void
7284alist_slash_adjust()
7285{
7286 int i;
7287# ifdef FEAT_WINDOWS
7288 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007289 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290# endif
7291
7292 for (i = 0; i < GARGCOUNT; ++i)
7293 if (GARGLIST[i].ae_fname != NULL)
7294 slash_adjust(GARGLIST[i].ae_fname);
7295# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007296 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (wp->w_alist != &global_alist)
7298 for (i = 0; i < WARGCOUNT(wp); ++i)
7299 if (WARGLIST(wp)[i].ae_fname != NULL)
7300 slash_adjust(WARGLIST(wp)[i].ae_fname);
7301# endif
7302}
7303#endif
7304
7305/*
7306 * ":preserve".
7307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 static void
7309ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007310 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007312 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 ml_preserve(curbuf, TRUE);
7314}
7315
7316/*
7317 * ":recover".
7318 */
7319 static void
7320ex_recover(eap)
7321 exarg_T *eap;
7322{
7323 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7324 recoverymode = TRUE;
7325 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7326 && (*eap->arg == NUL
7327 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7328 ml_recover();
7329 recoverymode = FALSE;
7330}
7331
7332/*
7333 * Command modifier used in a wrong way.
7334 */
7335 static void
7336ex_wrongmodifier(eap)
7337 exarg_T *eap;
7338{
7339 eap->errmsg = e_invcmd;
7340}
7341
7342#ifdef FEAT_WINDOWS
7343/*
7344 * :sview [+command] file split window with new file, read-only
7345 * :split [[+command] file] split window with current or new file
7346 * :vsplit [[+command] file] split window vertically with current or new file
7347 * :new [[+command] file] split window with no or new file
7348 * :vnew [[+command] file] split vertically window with no or new file
7349 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007350 *
7351 * :tabedit open new Tab page with empty window
7352 * :tabedit [+command] file open new Tab page and edit "file"
7353 * :tabnew [[+command] file] just like :tabedit
7354 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
7356 void
7357ex_splitview(eap)
7358 exarg_T *eap;
7359{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007360 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363# endif
7364# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007368# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7370 {
7371 ex_ni(eap);
7372 return;
7373 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007382 * quickfix windows. But it's OK when doing ":tab split". */
7383 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
7385 if (eap->cmdidx == CMD_split)
7386 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (eap->cmdidx == CMD_vsplit)
7389 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007395 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
7397 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7398 FNAME_MESS, TRUE, curbuf->b_ffname);
7399 if (fname == NULL)
7400 goto theend;
7401 eap->arg = fname;
7402 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007403# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 && eap->cmdidx != CMD_new)
7413 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007414# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007415 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007416# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007417 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007418# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007419 au_has_group((char_u *)"FileExplorer"))
7420 {
7421 /* No browsing supported but we do have the file explorer:
7422 * Edit the directory. */
7423 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7424 eap->arg = (char_u *)".";
7425 }
7426 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007427# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007428 {
7429 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007431 if (fname == NULL)
7432 goto theend;
7433 eap->arg = fname;
7434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007439 /*
7440 * Either open new tab page or split the window.
7441 */
7442 if (eap->cmdidx == CMD_tabedit
7443 || eap->cmdidx == CMD_tabfind
7444 || eap->cmdidx == CMD_tabnew)
7445 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007446 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7447 : eap->addr_count == 0 ? 0
7448 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007449 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007450 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007451
7452 /* set the alternate buffer for the window we came from */
7453 if (curwin != old_curwin
7454 && win_valid(old_curwin)
7455 && old_curwin->w_buffer != curbuf
7456 && !cmdmod.keepalt)
7457 old_curwin->w_alt_fnum = curbuf->b_fnum;
7458 }
7459 }
7460 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7462 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 /* Reset 'scrollbind' when editing another file, but keep it when
7465 * doing ":split" without arguments. */
7466 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007471 {
7472 RESET_BINDING(curwin);
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 else
7475 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 do_exedit(eap, old_curwin);
7478 }
7479
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007480# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007484# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485theend:
7486 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007489
7490/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007491 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007492 */
7493 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007494tabpage_new()
7495{
7496 exarg_T ea;
7497
7498 vim_memset(&ea, 0, sizeof(ea));
7499 ea.cmdidx = CMD_tabnew;
7500 ea.cmd = (char_u *)"tabn";
7501 ea.arg = (char_u *)"";
7502 ex_splitview(&ea);
7503}
7504
7505/*
7506 * :tabnext command
7507 */
7508 static void
7509ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007510 exarg_T *eap;
7511{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007512 switch (eap->cmdidx)
7513 {
7514 case CMD_tabfirst:
7515 case CMD_tabrewind:
7516 goto_tabpage(1);
7517 break;
7518 case CMD_tablast:
7519 goto_tabpage(9999);
7520 break;
7521 case CMD_tabprevious:
7522 case CMD_tabNext:
7523 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7524 break;
7525 default: /* CMD_tabnext */
7526 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7527 break;
7528 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007529}
7530
7531/*
7532 * :tabmove command
7533 */
7534 static void
7535ex_tabmove(eap)
7536 exarg_T *eap;
7537{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007538 int tab_number = 9999;
7539
7540 if (eap->arg && *eap->arg != NUL)
7541 {
7542 char_u *p = eap->arg;
7543 int relative = 0; /* argument +N/-N means: move N places to the
7544 * right/left relative to the current position. */
7545
7546 if (*eap->arg == '-')
7547 {
7548 relative = -1;
7549 p = eap->arg + 1;
7550 }
7551 else if (*eap->arg == '+')
7552 {
7553 relative = 1;
7554 p = eap->arg + 1;
7555 }
7556 else
7557 p = eap->arg;
7558
7559 if (p == skipdigits(p))
7560 {
7561 /* No numbers as argument. */
7562 eap->errmsg = e_invarg;
7563 return;
7564 }
7565
7566 tab_number = getdigits(&p);
7567 if (relative != 0)
7568 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7569 }
7570 else if (eap->addr_count != 0)
7571 tab_number = eap->line2;
7572
7573 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007574}
7575
7576/*
7577 * :tabs command: List tabs and their contents.
7578 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007579 static void
7580ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007581 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007582{
7583 tabpage_T *tp;
7584 win_T *wp;
7585 int tabcount = 1;
7586
7587 msg_start();
7588 msg_scroll = TRUE;
7589 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7590 {
7591 msg_putchar('\n');
7592 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7593 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7594 out_flush(); /* output one line at a time */
7595 ui_breakcheck();
7596
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007597 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007598 wp = firstwin;
7599 else
7600 wp = tp->tp_firstwin;
7601 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7602 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007603 msg_putchar('\n');
7604 msg_putchar(wp == curwin ? '>' : ' ');
7605 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007606 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7607 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007608 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007609 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007610 else
7611 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7612 IObuff, IOSIZE, TRUE);
7613 msg_outtrans(IObuff);
7614 out_flush(); /* output one line at a time */
7615 ui_breakcheck();
7616 }
7617 }
7618}
7619
7620#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
7622/*
7623 * ":mode": Set screen mode.
7624 * If no argument given, just get the screen size and redraw.
7625 */
7626 static void
7627ex_mode(eap)
7628 exarg_T *eap;
7629{
7630 if (*eap->arg == NUL)
7631 shell_resized();
7632 else
7633 mch_screenmode(eap->arg);
7634}
7635
7636#ifdef FEAT_WINDOWS
7637/*
7638 * ":resize".
7639 * set, increment or decrement current window height
7640 */
7641 static void
7642ex_resize(eap)
7643 exarg_T *eap;
7644{
7645 int n;
7646 win_T *wp = curwin;
7647
7648 if (eap->addr_count > 0)
7649 {
7650 n = eap->line2;
7651 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7652 ;
7653 }
7654
7655#ifdef FEAT_GUI
7656 need_mouse_correct = TRUE;
7657#endif
7658 n = atol((char *)eap->arg);
7659#ifdef FEAT_VERTSPLIT
7660 if (cmdmod.split & WSP_VERT)
7661 {
7662 if (*eap->arg == '-' || *eap->arg == '+')
7663 n += W_WIDTH(curwin);
7664 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7665 n = 9999;
7666 win_setwidth_win((int)n, wp);
7667 }
7668 else
7669#endif
7670 {
7671 if (*eap->arg == '-' || *eap->arg == '+')
7672 n += curwin->w_height;
7673 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7674 n = 9999;
7675 win_setheight_win((int)n, wp);
7676 }
7677}
7678#endif
7679
7680/*
7681 * ":find [+command] <file>" command.
7682 */
7683 static void
7684ex_find(eap)
7685 exarg_T *eap;
7686{
7687#ifdef FEAT_SEARCHPATH
7688 char_u *fname;
7689 int count;
7690
7691 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7692 TRUE, curbuf->b_ffname);
7693 if (eap->addr_count > 0)
7694 {
7695 /* Repeat finding the file "count" times. This matters when it
7696 * appears several times in the path. */
7697 count = eap->line2;
7698 while (fname != NULL && --count > 0)
7699 {
7700 vim_free(fname);
7701 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7702 FALSE, curbuf->b_ffname);
7703 }
7704 }
7705
7706 if (fname != NULL)
7707 {
7708 eap->arg = fname;
7709#endif
7710 do_exedit(eap, NULL);
7711#ifdef FEAT_SEARCHPATH
7712 vim_free(fname);
7713 }
7714#endif
7715}
7716
7717/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007718 * ":open" simulation: for now just work like ":visual".
7719 */
7720 static void
7721ex_open(eap)
7722 exarg_T *eap;
7723{
7724 regmatch_T regmatch;
7725 char_u *p;
7726
7727 curwin->w_cursor.lnum = eap->line2;
7728 beginline(BL_SOL | BL_FIX);
7729 if (*eap->arg == '/')
7730 {
7731 /* ":open /pattern/": put cursor in column found with pattern */
7732 ++eap->arg;
7733 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7734 *p = NUL;
7735 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7736 if (regmatch.regprog != NULL)
7737 {
7738 regmatch.rm_ic = p_ic;
7739 p = ml_get_curline();
7740 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007741 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007742 else
7743 EMSG(_(e_nomatch));
7744 vim_free(regmatch.regprog);
7745 }
7746 /* Move to the NUL, ignore any other arguments. */
7747 eap->arg += STRLEN(eap->arg);
7748 }
7749 check_cursor();
7750
7751 eap->cmdidx = CMD_visual;
7752 do_exedit(eap, NULL);
7753}
7754
7755/*
7756 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 */
7758 static void
7759ex_edit(eap)
7760 exarg_T *eap;
7761{
7762 do_exedit(eap, NULL);
7763}
7764
7765/*
7766 * ":edit <file>" command and alikes.
7767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 void
7769do_exedit(eap, old_curwin)
7770 exarg_T *eap;
7771 win_T *old_curwin; /* curwin before doing a split or NULL */
7772{
7773 int n;
7774#ifdef FEAT_WINDOWS
7775 int need_hide;
7776#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007777 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
7779 /*
7780 * ":vi" command ends Ex mode.
7781 */
7782 if (exmode_active && (eap->cmdidx == CMD_visual
7783 || eap->cmdidx == CMD_view))
7784 {
7785 exmode_active = FALSE;
7786 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007787 {
7788 /* Special case: ":global/pat/visual\NLvi-commands" */
7789 if (global_busy)
7790 {
7791 int rd = RedrawingDisabled;
7792 int nwr = no_wait_return;
7793 int ms = msg_scroll;
7794#ifdef FEAT_GUI
7795 int he = hold_gui_events;
7796#endif
7797
7798 if (eap->nextcmd != NULL)
7799 {
7800 stuffReadbuff(eap->nextcmd);
7801 eap->nextcmd = NULL;
7802 }
7803
7804 if (exmode_was != EXMODE_VIM)
7805 settmode(TMODE_RAW);
7806 RedrawingDisabled = 0;
7807 no_wait_return = 0;
7808 need_wait_return = FALSE;
7809 msg_scroll = 0;
7810#ifdef FEAT_GUI
7811 hold_gui_events = 0;
7812#endif
7813 must_redraw = CLEAR;
7814
7815 main_loop(FALSE, TRUE);
7816
7817 RedrawingDisabled = rd;
7818 no_wait_return = nwr;
7819 msg_scroll = ms;
7820#ifdef FEAT_GUI
7821 hold_gui_events = he;
7822#endif
7823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827
7828 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007829 || eap->cmdidx == CMD_tabnew
7830 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#ifdef FEAT_VERTSPLIT
7832 || eap->cmdidx == CMD_vnew
7833#endif
7834 ) && *eap->arg == NUL)
7835 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007836 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 setpcmark();
7838 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007839 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7840 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842 else if ((eap->cmdidx != CMD_split
7843#ifdef FEAT_VERTSPLIT
7844 && eap->cmdidx != CMD_vsplit
7845#endif
7846 )
7847 || *eap->arg != NUL
7848#ifdef FEAT_BROWSE
7849 || cmdmod.browse
7850#endif
7851 )
7852 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007853#ifdef FEAT_AUTOCMD
7854 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7855 * can bring us here, others are stopped earlier. */
7856 if (*eap->arg != NUL && curbuf_locked())
7857 return;
7858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 n = readonlymode;
7860 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7861 readonlymode = TRUE;
7862 else if (eap->cmdidx == CMD_enew)
7863 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7864 empty buffer */
7865 setpcmark();
7866 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7867 NULL, eap,
7868 /* ":edit" goes to first line if Vi compatible */
7869 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7870 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7871 ? ECMD_ONE : eap->do_ecmd_lnum,
7872 (P_HID(curbuf) ? ECMD_HIDE : 0)
7873 + (eap->forceit ? ECMD_FORCEIT : 0)
7874#ifdef FEAT_LISTCMDS
7875 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7876#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007877 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
7879 /* Editing the file failed. If the window was split, close it. */
7880#ifdef FEAT_WINDOWS
7881 if (old_curwin != NULL)
7882 {
7883 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7884 if (!need_hide || P_HID(curbuf))
7885 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007886# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7887 cleanup_T cs;
7888
7889 /* Reset the error/interrupt/exception state here so that
7890 * aborting() returns FALSE when closing a window. */
7891 enter_cleanup(&cs);
7892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893# ifdef FEAT_GUI
7894 need_mouse_correct = TRUE;
7895# endif
7896 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007897
7898# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7899 /* Restore the error/interrupt/exception state if not
7900 * discarded by a new aborting error, interrupt, or
7901 * uncaught exception. */
7902 leave_cleanup(&cs);
7903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905 }
7906#endif
7907 }
7908 else if (readonlymode && curbuf->b_nwindows == 1)
7909 {
7910 /* When editing an already visited buffer, 'readonly' won't be set
7911 * but the previous value is kept. With ":view" and ":sview" we
7912 * want the file to be readonly, except when another window is
7913 * editing the same buffer. */
7914 curbuf->b_p_ro = TRUE;
7915 }
7916 readonlymode = n;
7917 }
7918 else
7919 {
7920 if (eap->do_ecmd_cmd != NULL)
7921 do_cmdline_cmd(eap->do_ecmd_cmd);
7922#ifdef FEAT_TITLE
7923 n = curwin->w_arg_idx_invalid;
7924#endif
7925 check_arg_idx(curwin);
7926#ifdef FEAT_TITLE
7927 if (n != curwin->w_arg_idx_invalid)
7928 maketitle();
7929#endif
7930 }
7931
7932#ifdef FEAT_WINDOWS
7933 /*
7934 * if ":split file" worked, set alternate file name in old window to new
7935 * file
7936 */
7937 if (old_curwin != NULL
7938 && *eap->arg != NUL
7939 && curwin != old_curwin
7940 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007941 && old_curwin->w_buffer != curbuf
7942 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 old_curwin->w_alt_fnum = curbuf->b_fnum;
7944#endif
7945
7946 ex_no_reprint = TRUE;
7947}
7948
7949#ifndef FEAT_GUI
7950/*
7951 * ":gui" and ":gvim" when there is no GUI.
7952 */
7953 static void
7954ex_nogui(eap)
7955 exarg_T *eap;
7956{
7957 eap->errmsg = e_nogvim;
7958}
7959#endif
7960
7961#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7962 static void
7963ex_tearoff(eap)
7964 exarg_T *eap;
7965{
7966 gui_make_tearoff(eap->arg);
7967}
7968#endif
7969
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007970#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 static void
7972ex_popup(eap)
7973 exarg_T *eap;
7974{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007975 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976}
7977#endif
7978
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 static void
7980ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007981 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7984 MSG(_("No swap file"));
7985 else
7986 msg(curbuf->b_ml.ml_mfp->mf_fname);
7987}
7988
7989/*
7990 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7991 * offset.
7992 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 static void
7995ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007996 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998#ifdef FEAT_SCROLLBIND
7999 win_T *wp;
8000 long topline;
8001 long y;
8002 linenr_T old_linenr = curwin->w_cursor.lnum;
8003
8004 setpcmark();
8005
8006 /*
8007 * determine max topline
8008 */
8009 if (curwin->w_p_scb)
8010 {
8011 topline = curwin->w_topline;
8012 for (wp = firstwin; wp; wp = wp->w_next)
8013 {
8014 if (wp->w_p_scb && wp->w_buffer)
8015 {
8016 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8017 if (topline > y)
8018 topline = y;
8019 }
8020 }
8021 if (topline < 1)
8022 topline = 1;
8023 }
8024 else
8025 {
8026 topline = 1;
8027 }
8028
8029
8030 /*
8031 * set all scrollbind windows to the same topline
8032 */
8033 wp = curwin;
8034 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8035 {
8036 if (curwin->w_p_scb)
8037 {
8038 y = topline - curwin->w_topline;
8039 if (y > 0)
8040 scrollup(y, TRUE);
8041 else
8042 scrolldown(-y, TRUE);
8043 curwin->w_scbind_pos = topline;
8044 redraw_later(VALID);
8045 cursor_correct();
8046#ifdef FEAT_WINDOWS
8047 curwin->w_redr_status = TRUE;
8048#endif
8049 }
8050 }
8051 curwin = wp;
8052 if (curwin->w_p_scb)
8053 {
8054 did_syncbind = TRUE;
8055 checkpcmark();
8056 if (old_linenr != curwin->w_cursor.lnum)
8057 {
8058 char_u ctrl_o[2];
8059
8060 ctrl_o[0] = Ctrl_O;
8061 ctrl_o[1] = 0;
8062 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8063 }
8064 }
8065#endif
8066}
8067
8068
8069 static void
8070ex_read(eap)
8071 exarg_T *eap;
8072{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008073 int i;
8074 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8075 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 if (eap->usefilter) /* :r!cmd */
8078 do_bang(1, eap, FALSE, FALSE, TRUE);
8079 else
8080 {
8081 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8082 return;
8083
8084#ifdef FEAT_BROWSE
8085 if (cmdmod.browse)
8086 {
8087 char_u *browseFile;
8088
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008089 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 NULL, NULL, NULL, curbuf);
8091 if (browseFile != NULL)
8092 {
8093 i = readfile(browseFile, NULL,
8094 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8095 vim_free(browseFile);
8096 }
8097 else
8098 i = OK;
8099 }
8100 else
8101#endif
8102 if (*eap->arg == NUL)
8103 {
8104 if (check_fname() == FAIL) /* check for no file name */
8105 return;
8106 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8107 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8108 }
8109 else
8110 {
8111 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8112 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8113 i = readfile(eap->arg, NULL,
8114 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8115
8116 }
8117 if (i == FAIL)
8118 {
8119#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8120 if (!aborting())
8121#endif
8122 EMSG2(_(e_notopen), eap->arg);
8123 }
8124 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008125 {
8126 if (empty && exmode_active)
8127 {
8128 /* Delete the empty line that remains. Historically ex does
8129 * this but vi doesn't. */
8130 if (eap->line2 == 0)
8131 lnum = curbuf->b_ml.ml_line_count;
8132 else
8133 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008134 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008135 {
8136 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008137 if (curwin->w_cursor.lnum > 1
8138 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008139 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008140 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008141 }
8142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 }
8146}
8147
Bram Moolenaarea408852005-06-25 22:49:46 +00008148static char_u *prev_dir = NULL;
8149
8150#if defined(EXITFREE) || defined(PROTO)
8151 void
8152free_cd_dir()
8153{
8154 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008155 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008156
8157 vim_free(globaldir);
8158 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008159}
8160#endif
8161
8162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163/*
8164 * ":cd", ":lcd", ":chdir" and ":lchdir".
8165 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008166 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167ex_cd(eap)
8168 exarg_T *eap;
8169{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 char_u *new_dir;
8171 char_u *tofree;
8172
8173 new_dir = eap->arg;
8174#if !defined(UNIX) && !defined(VMS)
8175 /* for non-UNIX ":cd" means: print current directory */
8176 if (*new_dir == NUL)
8177 ex_pwd(NULL);
8178 else
8179#endif
8180 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008181#ifdef FEAT_AUTOCMD
8182 if (allbuf_locked())
8183 return;
8184#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008185 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8186 && !eap->forceit)
8187 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008188 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008189 return;
8190 }
8191
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 /* ":cd -": Change to previous directory */
8193 if (STRCMP(new_dir, "-") == 0)
8194 {
8195 if (prev_dir == NULL)
8196 {
8197 EMSG(_("E186: No previous directory"));
8198 return;
8199 }
8200 new_dir = prev_dir;
8201 }
8202
8203 /* Save current directory for next ":cd -" */
8204 tofree = prev_dir;
8205 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8206 prev_dir = vim_strsave(NameBuff);
8207 else
8208 prev_dir = NULL;
8209
8210#if defined(UNIX) || defined(VMS)
8211 /* for UNIX ":cd" means: go to home directory */
8212 if (*new_dir == NUL)
8213 {
8214 /* use NameBuff for home directory name */
8215# ifdef VMS
8216 char_u *p;
8217
8218 p = mch_getenv((char_u *)"SYS$LOGIN");
8219 if (p == NULL || *p == NUL) /* empty is the same as not set */
8220 NameBuff[0] = NUL;
8221 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008222 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223# else
8224 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8225# endif
8226 new_dir = NameBuff;
8227 }
8228#endif
8229 if (new_dir == NULL || vim_chdir(new_dir))
8230 EMSG(_(e_failed));
8231 else
8232 {
8233 vim_free(curwin->w_localdir);
8234 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8235 {
8236 /* If still in global directory, need to remember current
8237 * directory as global directory. */
8238 if (globaldir == NULL && prev_dir != NULL)
8239 globaldir = vim_strsave(prev_dir);
8240 /* Remember this local directory for the window. */
8241 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8242 curwin->w_localdir = vim_strsave(NameBuff);
8243 }
8244 else
8245 {
8246 /* We are now in the global directory, no need to remember its
8247 * name. */
8248 vim_free(globaldir);
8249 globaldir = NULL;
8250 curwin->w_localdir = NULL;
8251 }
8252
8253 shorten_fnames(TRUE);
8254
8255 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008256 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 ex_pwd(eap);
8258 }
8259 vim_free(tofree);
8260 }
8261}
8262
8263/*
8264 * ":pwd".
8265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 static void
8267ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008268 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
8270 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8271 {
8272#ifdef BACKSLASH_IN_FILENAME
8273 slash_adjust(NameBuff);
8274#endif
8275 msg(NameBuff);
8276 }
8277 else
8278 EMSG(_("E187: Unknown"));
8279}
8280
8281/*
8282 * ":=".
8283 */
8284 static void
8285ex_equal(eap)
8286 exarg_T *eap;
8287{
8288 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008289 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290}
8291
8292 static void
8293ex_sleep(eap)
8294 exarg_T *eap;
8295{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008296 int n;
8297 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298
8299 if (cursor_valid())
8300 {
8301 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8302 if (n >= 0)
8303 windgoto((int)n, curwin->w_wcol);
8304 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008305
8306 len = eap->line2;
8307 switch (*eap->arg)
8308 {
8309 case 'm': break;
8310 case NUL: len *= 1000L; break;
8311 default: EMSG2(_(e_invarg2), eap->arg); return;
8312 }
8313 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314}
8315
8316/*
8317 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8318 */
8319 void
8320do_sleep(msec)
8321 long msec;
8322{
8323 long done;
8324
8325 cursor_on();
8326 out_flush();
8327 for (done = 0; !got_int && done < msec; done += 1000L)
8328 {
8329 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8330 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008331#ifdef FEAT_NETBEANS_INTG
8332 /* Process the netbeans messages that may have been received in the
8333 * call to ui_breakcheck() when the GUI is in use. This may occur when
8334 * running a test case. */
8335 netbeans_parse_messages();
8336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
8338}
8339
8340 static void
8341do_exmap(eap, isabbrev)
8342 exarg_T *eap;
8343 int isabbrev;
8344{
8345 int mode;
8346 char_u *cmdp;
8347
8348 cmdp = eap->cmd;
8349 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8350
8351 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8352 eap->arg, mode, isabbrev))
8353 {
8354 case 1: EMSG(_(e_invarg));
8355 break;
8356 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8357 break;
8358 }
8359}
8360
8361/*
8362 * ":winsize" command (obsolete).
8363 */
8364 static void
8365ex_winsize(eap)
8366 exarg_T *eap;
8367{
8368 int w, h;
8369 char_u *arg = eap->arg;
8370 char_u *p;
8371
8372 w = getdigits(&arg);
8373 arg = skipwhite(arg);
8374 p = arg;
8375 h = getdigits(&arg);
8376 if (*p != NUL && *arg == NUL)
8377 set_shellsize(w, h, TRUE);
8378 else
8379 EMSG(_("E465: :winsize requires two number arguments"));
8380}
8381
8382#ifdef FEAT_WINDOWS
8383 static void
8384ex_wincmd(eap)
8385 exarg_T *eap;
8386{
8387 int xchar = NUL;
8388 char_u *p;
8389
8390 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8391 {
8392 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8393 if (eap->arg[1] == NUL)
8394 {
8395 EMSG(_(e_invarg));
8396 return;
8397 }
8398 xchar = eap->arg[1];
8399 p = eap->arg + 2;
8400 }
8401 else
8402 p = eap->arg + 1;
8403
8404 eap->nextcmd = check_nextcmd(p);
8405 p = skipwhite(p);
8406 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8407 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008408 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
8410 /* Pass flags on for ":vertical wincmd ]". */
8411 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008412 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8414 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008415 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 }
8417}
8418#endif
8419
Bram Moolenaar843ee412004-06-30 16:16:41 +00008420#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421/*
8422 * ":winpos".
8423 */
8424 static void
8425ex_winpos(eap)
8426 exarg_T *eap;
8427{
8428 int x, y;
8429 char_u *arg = eap->arg;
8430 char_u *p;
8431
8432 if (*arg == NUL)
8433 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008434# if defined(FEAT_GUI) || defined(MSWIN)
8435# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008437# else
8438 if (mch_get_winpos(&x, &y) != FAIL)
8439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
8441 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8442 msg(IObuff);
8443 }
8444 else
8445# endif
8446 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8447 }
8448 else
8449 {
8450 x = getdigits(&arg);
8451 arg = skipwhite(arg);
8452 p = arg;
8453 y = getdigits(&arg);
8454 if (*p == NUL || *arg != NUL)
8455 {
8456 EMSG(_("E466: :winpos requires two number arguments"));
8457 return;
8458 }
8459# ifdef FEAT_GUI
8460 if (gui.in_use)
8461 gui_mch_set_winpos(x, y);
8462 else if (gui.starting)
8463 {
8464 /* Remember the coordinates for when the window is opened. */
8465 gui_win_x = x;
8466 gui_win_y = y;
8467 }
8468# ifdef HAVE_TGETENT
8469 else
8470# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008471# else
8472# ifdef MSWIN
8473 mch_set_winpos(x, y);
8474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475# endif
8476# ifdef HAVE_TGETENT
8477 if (*T_CWP)
8478 term_set_winpos(x, y);
8479# endif
8480 }
8481}
8482#endif
8483
8484/*
8485 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8486 */
8487 static void
8488ex_operators(eap)
8489 exarg_T *eap;
8490{
8491 oparg_T oa;
8492
8493 clear_oparg(&oa);
8494 oa.regname = eap->regname;
8495 oa.start.lnum = eap->line1;
8496 oa.end.lnum = eap->line2;
8497 oa.line_count = eap->line2 - eap->line1 + 1;
8498 oa.motion_type = MLINE;
8499#ifdef FEAT_VIRTUALEDIT
8500 virtual_op = FALSE;
8501#endif
8502 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8503 {
8504 setpcmark();
8505 curwin->w_cursor.lnum = eap->line1;
8506 beginline(BL_SOL | BL_FIX);
8507 }
8508
8509 switch (eap->cmdidx)
8510 {
8511 case CMD_delete:
8512 oa.op_type = OP_DELETE;
8513 op_delete(&oa);
8514 break;
8515
8516 case CMD_yank:
8517 oa.op_type = OP_YANK;
8518 (void)op_yank(&oa, FALSE, TRUE);
8519 break;
8520
8521 default: /* CMD_rshift or CMD_lshift */
8522 if ((eap->cmdidx == CMD_rshift)
8523#ifdef FEAT_RIGHTLEFT
8524 ^ curwin->w_p_rl
8525#endif
8526 )
8527 oa.op_type = OP_RSHIFT;
8528 else
8529 oa.op_type = OP_LSHIFT;
8530 op_shift(&oa, FALSE, eap->amount);
8531 break;
8532 }
8533#ifdef FEAT_VIRTUALEDIT
8534 virtual_op = MAYBE;
8535#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008536 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537}
8538
8539/*
8540 * ":put".
8541 */
8542 static void
8543ex_put(eap)
8544 exarg_T *eap;
8545{
8546 /* ":0put" works like ":1put!". */
8547 if (eap->line2 == 0)
8548 {
8549 eap->line2 = 1;
8550 eap->forceit = TRUE;
8551 }
8552 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008553 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8554 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555}
8556
8557/*
8558 * Handle ":copy" and ":move".
8559 */
8560 static void
8561ex_copymove(eap)
8562 exarg_T *eap;
8563{
8564 long n;
8565
8566 n = get_address(&eap->arg, FALSE, FALSE);
8567 if (eap->arg == NULL) /* error detected */
8568 {
8569 eap->nextcmd = NULL;
8570 return;
8571 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008572 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574 /*
8575 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8576 */
8577 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8578 {
8579 EMSG(_(e_invaddr));
8580 return;
8581 }
8582
8583 if (eap->cmdidx == CMD_move)
8584 {
8585 if (do_move(eap->line1, eap->line2, n) == FAIL)
8586 return;
8587 }
8588 else
8589 ex_copy(eap->line1, eap->line2, n);
8590 u_clearline();
8591 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008592 ex_may_print(eap);
8593}
8594
8595/*
8596 * Print the current line if flags were given to the Ex command.
8597 */
8598 static void
8599ex_may_print(eap)
8600 exarg_T *eap;
8601{
8602 if (eap->flags != 0)
8603 {
8604 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8605 (eap->flags & EXFLAG_LIST));
8606 ex_no_reprint = TRUE;
8607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608}
8609
8610/*
8611 * ":smagic" and ":snomagic".
8612 */
8613 static void
8614ex_submagic(eap)
8615 exarg_T *eap;
8616{
8617 int magic_save = p_magic;
8618
8619 p_magic = (eap->cmdidx == CMD_smagic);
8620 do_sub(eap);
8621 p_magic = magic_save;
8622}
8623
8624/*
8625 * ":join".
8626 */
8627 static void
8628ex_join(eap)
8629 exarg_T *eap;
8630{
8631 curwin->w_cursor.lnum = eap->line1;
8632 if (eap->line1 == eap->line2)
8633 {
8634 if (eap->addr_count >= 2) /* :2,2join does nothing */
8635 return;
8636 if (eap->line2 == curbuf->b_ml.ml_line_count)
8637 {
8638 beep_flush();
8639 return;
8640 }
8641 ++eap->line2;
8642 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008643 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008645 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646}
8647
8648/*
8649 * ":[addr]@r" or ":[addr]*r": execute register
8650 */
8651 static void
8652ex_at(eap)
8653 exarg_T *eap;
8654{
8655 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008656 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
8658 curwin->w_cursor.lnum = eap->line2;
8659
8660#ifdef USE_ON_FLY_SCROLL
8661 dont_scroll = TRUE; /* disallow scrolling here */
8662#endif
8663
8664 /* get the register name. No name means to use the previous one */
8665 c = *eap->arg;
8666 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8667 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008668 /* Put the register in the typeahead buffer with the "silent" flag. */
8669 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8670 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 else
8675 {
8676 int save_efr = exec_from_reg;
8677
8678 exec_from_reg = TRUE;
8679
8680 /*
8681 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008682 * Continue until the stuff buffer is empty and all added characters
8683 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008685 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8687
8688 exec_from_reg = save_efr;
8689 }
8690}
8691
8692/*
8693 * ":!".
8694 */
8695 static void
8696ex_bang(eap)
8697 exarg_T *eap;
8698{
8699 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8700}
8701
8702/*
8703 * ":undo".
8704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 static void
8706ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008707 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008709 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008710 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008711 else
8712 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713}
8714
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008715#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008716 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008717ex_wundo(eap)
8718 exarg_T *eap;
8719{
8720 char_u hash[UNDO_HASH_SIZE];
8721
8722 u_compute_hash(hash);
8723 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8724}
8725
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008726 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008727ex_rundo(eap)
8728 exarg_T *eap;
8729{
8730 char_u hash[UNDO_HASH_SIZE];
8731
8732 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008733 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008734}
8735#endif
8736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737/*
8738 * ":redo".
8739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 static void
8741ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008742 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743{
8744 u_redo(1);
8745}
8746
8747/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008748 * ":earlier" and ":later".
8749 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008750 static void
8751ex_later(eap)
8752 exarg_T *eap;
8753{
8754 long count = 0;
8755 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008756 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008757 char_u *p = eap->arg;
8758
8759 if (*p == NUL)
8760 count = 1;
8761 else if (isdigit(*p))
8762 {
8763 count = getdigits(&p);
8764 switch (*p)
8765 {
8766 case 's': ++p; sec = TRUE; break;
8767 case 'm': ++p; sec = TRUE; count *= 60; break;
8768 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008769 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8770 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008771 }
8772 }
8773
8774 if (*p != NUL)
8775 EMSG2(_(e_invarg2), eap->arg);
8776 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008777 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8778 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008779}
8780
8781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 * ":redir": start/stop redirection.
8783 */
8784 static void
8785ex_redir(eap)
8786 exarg_T *eap;
8787{
8788 char *mode;
8789 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008790 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
8792 if (STRICMP(eap->arg, "END") == 0)
8793 close_redir();
8794 else
8795 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008796 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008798 ++arg;
8799 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008801 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 mode = "a";
8803 }
8804 else
8805 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008806 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808 close_redir();
8809
8810 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008811 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (fname == NULL)
8813 return;
8814#ifdef FEAT_BROWSE
8815 if (cmdmod.browse)
8816 {
8817 char_u *browseFile;
8818
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008819 browseFile = do_browse(BROWSE_SAVE,
8820 (char_u *)_("Save Redirection"),
8821 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (browseFile == NULL)
8823 return; /* operation cancelled */
8824 vim_free(fname);
8825 fname = browseFile;
8826 eap->forceit = TRUE; /* since dialog already asked */
8827 }
8828#endif
8829
8830 redir_fd = open_exfile(fname, eap->forceit, mode);
8831 vim_free(fname);
8832 }
8833#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008834 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 {
8836 /* redirect to a register a-z (resp. A-Z for appending) */
8837 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008838 ++arg;
8839 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008841 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008842 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008844 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008846 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008847 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008848 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008851 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008852 if (*arg == '>')
8853 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008854 /* Make register empty when not using @A-@Z and the
8855 * command is valid. */
8856 if (*arg == NUL && !isupper(redir_reg))
8857 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008859 }
8860 if (*arg != NUL)
8861 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008862 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008863 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008864 }
8865 }
8866 else if (*arg == '=' && arg[1] == '>')
8867 {
8868 int append;
8869
8870 /* redirect to a variable */
8871 close_redir();
8872 arg += 2;
8873
8874 if (*arg == '>')
8875 {
8876 ++arg;
8877 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008880 append = FALSE;
8881
8882 if (var_redir_start(skipwhite(arg), append) == OK)
8883 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885#endif
8886
8887 /* TODO: redirect to a buffer */
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008890 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008892
8893 /* Make sure redirection is not off. Can happen for cmdline completion
8894 * that indirectly invokes a command to catch its output. */
8895 if (redir_fd != NULL
8896#ifdef FEAT_EVAL
8897 || redir_reg || redir_vname
8898#endif
8899 )
8900 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901}
8902
8903/*
8904 * ":redraw": force redraw
8905 */
8906 static void
8907ex_redraw(eap)
8908 exarg_T *eap;
8909{
8910 int r = RedrawingDisabled;
8911 int p = p_lz;
8912
8913 RedrawingDisabled = 0;
8914 p_lz = FALSE;
8915 update_topline();
8916 update_screen(eap->forceit ? CLEAR :
8917#ifdef FEAT_VISUAL
8918 VIsual_active ? INVERTED :
8919#endif
8920 0);
8921#ifdef FEAT_TITLE
8922 if (need_maketitle)
8923 maketitle();
8924#endif
8925 RedrawingDisabled = r;
8926 p_lz = p;
8927
8928 /* Reset msg_didout, so that a message that's there is overwritten. */
8929 msg_didout = FALSE;
8930 msg_col = 0;
8931
8932 out_flush();
8933}
8934
8935/*
8936 * ":redrawstatus": force redraw of status line(s)
8937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 static void
8939ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008940 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
8942#if defined(FEAT_WINDOWS)
8943 int r = RedrawingDisabled;
8944 int p = p_lz;
8945
8946 RedrawingDisabled = 0;
8947 p_lz = FALSE;
8948 if (eap->forceit)
8949 status_redraw_all();
8950 else
8951 status_redraw_curbuf();
8952 update_screen(
8953# ifdef FEAT_VISUAL
8954 VIsual_active ? INVERTED :
8955# endif
8956 0);
8957 RedrawingDisabled = r;
8958 p_lz = p;
8959 out_flush();
8960#endif
8961}
8962
8963 static void
8964close_redir()
8965{
8966 if (redir_fd != NULL)
8967 {
8968 fclose(redir_fd);
8969 redir_fd = NULL;
8970 }
8971#ifdef FEAT_EVAL
8972 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008973 if (redir_vname)
8974 {
8975 var_redir_stop();
8976 redir_vname = 0;
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978#endif
8979}
8980
8981#if defined(FEAT_SESSION) && defined(USE_CRNL)
8982# define MKSESSION_NL
8983static int mksession_nl = FALSE; /* use NL only in put_eol() */
8984#endif
8985
8986/*
8987 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8988 */
8989 static void
8990ex_mkrc(eap)
8991 exarg_T *eap;
8992{
8993 FILE *fd;
8994 int failed = FALSE;
8995 char_u *fname;
8996#ifdef FEAT_BROWSE
8997 char_u *browseFile = NULL;
8998#endif
8999#ifdef FEAT_SESSION
9000 int view_session = FALSE;
9001 int using_vdir = FALSE; /* using 'viewdir'? */
9002 char_u *viewFile = NULL;
9003 unsigned *flagp;
9004#endif
9005
9006 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9007 {
9008#ifdef FEAT_SESSION
9009 view_session = TRUE;
9010#else
9011 ex_ni(eap);
9012 return;
9013#endif
9014 }
9015
9016#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009017 /* Use the short file name until ":lcd" is used. We also don't use the
9018 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009019 did_lcd = FALSE;
9020
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9022 if (eap->cmdidx == CMD_mkview
9023 && (*eap->arg == NUL
9024 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9025 {
9026 eap->forceit = TRUE;
9027 fname = get_view_file(*eap->arg);
9028 if (fname == NULL)
9029 return;
9030 viewFile = fname;
9031 using_vdir = TRUE;
9032 }
9033 else
9034#endif
9035 if (*eap->arg != NUL)
9036 fname = eap->arg;
9037 else if (eap->cmdidx == CMD_mkvimrc)
9038 fname = (char_u *)VIMRC_FILE;
9039#ifdef FEAT_SESSION
9040 else if (eap->cmdidx == CMD_mksession)
9041 fname = (char_u *)SESSION_FILE;
9042#endif
9043 else
9044 fname = (char_u *)EXRC_FILE;
9045
9046#ifdef FEAT_BROWSE
9047 if (cmdmod.browse)
9048 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009049 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050# ifdef FEAT_SESSION
9051 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9052 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9053# endif
9054 (char_u *)_("Save Setup"),
9055 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9056 if (browseFile == NULL)
9057 goto theend;
9058 fname = browseFile;
9059 eap->forceit = TRUE; /* since dialog already asked */
9060 }
9061#endif
9062
9063#if defined(FEAT_SESSION) && defined(vim_mkdir)
9064 /* When using 'viewdir' may have to create the directory. */
9065 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009066 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067#endif
9068
9069 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9070 if (fd != NULL)
9071 {
9072#ifdef FEAT_SESSION
9073 if (eap->cmdidx == CMD_mkview)
9074 flagp = &vop_flags;
9075 else
9076 flagp = &ssop_flags;
9077#endif
9078
9079#ifdef MKSESSION_NL
9080 /* "unix" in 'sessionoptions': use NL line separator */
9081 if (view_session && (*flagp & SSOP_UNIX))
9082 mksession_nl = TRUE;
9083#endif
9084
9085 /* Write the version command for :mkvimrc */
9086 if (eap->cmdidx == CMD_mkvimrc)
9087 (void)put_line(fd, "version 6.0");
9088
9089#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009090 if (eap->cmdidx == CMD_mksession)
9091 {
9092 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9093 failed = TRUE;
9094 }
9095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 if (eap->cmdidx != CMD_mkview)
9097#endif
9098 {
9099 /* Write setting 'compatible' first, because it has side effects.
9100 * For that same reason only do it when needed. */
9101 if (p_cp)
9102 (void)put_line(fd, "if !&cp | set cp | endif");
9103 else
9104 (void)put_line(fd, "if &cp | set nocp | endif");
9105 }
9106
9107#ifdef FEAT_SESSION
9108 if (!view_session
9109 || (eap->cmdidx == CMD_mksession
9110 && (*flagp & SSOP_OPTIONS)))
9111#endif
9112 failed |= (makemap(fd, NULL) == FAIL
9113 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9114
9115#ifdef FEAT_SESSION
9116 if (!failed && view_session)
9117 {
9118 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9119 failed = TRUE;
9120 if (eap->cmdidx == CMD_mksession)
9121 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009122 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123
Bram Moolenaard9462e32011-04-11 21:35:11 +02009124 dirnow = alloc(MAXPATHL);
9125 if (dirnow == NULL)
9126 failed = TRUE;
9127 else
9128 {
9129 /*
9130 * Change to session file's dir.
9131 */
9132 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009134 *dirnow = NUL;
9135 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9136 {
9137 if (vim_chdirfile(fname) == OK)
9138 shorten_fnames(TRUE);
9139 }
9140 else if (*dirnow != NUL
9141 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9142 {
9143 if (mch_chdir((char *)globaldir) == 0)
9144 shorten_fnames(TRUE);
9145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146
Bram Moolenaard9462e32011-04-11 21:35:11 +02009147 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148
Bram Moolenaard9462e32011-04-11 21:35:11 +02009149 /* restore original dir */
9150 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009152 {
9153 if (mch_chdir((char *)dirnow) != 0)
9154 EMSG(_(e_prev_dir));
9155 shorten_fnames(TRUE);
9156 }
9157 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 }
9159 }
9160 else
9161 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009162 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9163 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
9165 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9166 == FAIL)
9167 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009168 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9169 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009170 if (eap->cmdidx == CMD_mksession)
9171 {
9172 if (put_line(fd, "unlet SessionLoad") == FAIL)
9173 failed = TRUE;
9174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
9176#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009177 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9178 failed = TRUE;
9179
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 failed |= fclose(fd);
9181
9182 if (failed)
9183 EMSG(_(e_write));
9184#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9185 else if (eap->cmdidx == CMD_mksession)
9186 {
9187 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009188 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189
Bram Moolenaard9462e32011-04-11 21:35:11 +02009190 tbuf = alloc(MAXPATHL);
9191 if (tbuf != NULL)
9192 {
9193 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9194 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9195 vim_free(tbuf);
9196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
9198#endif
9199#ifdef MKSESSION_NL
9200 mksession_nl = FALSE;
9201#endif
9202 }
9203
9204#ifdef FEAT_BROWSE
9205theend:
9206 vim_free(browseFile);
9207#endif
9208#ifdef FEAT_SESSION
9209 vim_free(viewFile);
9210#endif
9211}
9212
Bram Moolenaardf177f62005-02-22 08:39:57 +00009213#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9214 || defined(PROTO)
9215 int
9216vim_mkdir_emsg(name, prot)
9217 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009218 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009219{
9220 if (vim_mkdir(name, prot) != 0)
9221 {
9222 EMSG2(_("E739: Cannot create directory: %s"), name);
9223 return FAIL;
9224 }
9225 return OK;
9226}
9227#endif
9228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229/*
9230 * Open a file for writing for an Ex command, with some checks.
9231 * Return file descriptor, or NULL on failure.
9232 */
9233 FILE *
9234open_exfile(fname, forceit, mode)
9235 char_u *fname;
9236 int forceit;
9237 char *mode; /* "w" for create new file or "a" for append */
9238{
9239 FILE *fd;
9240
9241#ifdef UNIX
9242 /* with Unix it is possible to open a directory */
9243 if (mch_isdir(fname))
9244 {
9245 EMSG2(_(e_isadir2), fname);
9246 return NULL;
9247 }
9248#endif
9249 if (!forceit && *mode != 'a' && vim_fexists(fname))
9250 {
9251 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9252 return NULL;
9253 }
9254
9255 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9256 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9257
9258 return fd;
9259}
9260
9261/*
9262 * ":mark" and ":k".
9263 */
9264 static void
9265ex_mark(eap)
9266 exarg_T *eap;
9267{
9268 pos_T pos;
9269
9270 if (*eap->arg == NUL) /* No argument? */
9271 EMSG(_(e_argreq));
9272 else if (eap->arg[1] != NUL) /* more than one character? */
9273 EMSG(_(e_trailing));
9274 else
9275 {
9276 pos = curwin->w_cursor; /* save curwin->w_cursor */
9277 curwin->w_cursor.lnum = eap->line2;
9278 beginline(BL_WHITE | BL_FIX);
9279 if (setmark(*eap->arg) == FAIL) /* set mark */
9280 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9281 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9282 }
9283}
9284
9285/*
9286 * Update w_topline, w_leftcol and the cursor position.
9287 */
9288 void
9289update_topline_cursor()
9290{
9291 check_cursor(); /* put cursor on valid line */
9292 update_topline();
9293 if (!curwin->w_p_wrap)
9294 validate_cursor();
9295 update_curswant();
9296}
9297
9298#ifdef FEAT_EX_EXTRA
9299/*
9300 * ":normal[!] {commands}": Execute normal mode commands.
9301 */
9302 static void
9303ex_normal(eap)
9304 exarg_T *eap;
9305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 int save_msg_scroll = msg_scroll;
9307 int save_restart_edit = restart_edit;
9308 int save_msg_didout = msg_didout;
9309 int save_State = State;
9310 tasave_T tabuf;
9311 int save_insertmode = p_im;
9312 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009313 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314#ifdef FEAT_MBYTE
9315 char_u *arg = NULL;
9316 int l;
9317 char_u *p;
9318#endif
9319
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009320 if (ex_normal_lock > 0)
9321 {
9322 EMSG(_(e_secure));
9323 return;
9324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 if (ex_normal_busy >= p_mmd)
9326 {
9327 EMSG(_("E192: Recursive use of :normal too deep"));
9328 return;
9329 }
9330 ++ex_normal_busy;
9331
9332 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9333 restart_edit = 0; /* don't go to Insert mode */
9334 p_im = FALSE; /* don't use 'insertmode' */
9335
9336#ifdef FEAT_MBYTE
9337 /*
9338 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9339 * this for the K_SPECIAL leading byte, otherwise special keys will not
9340 * work.
9341 */
9342 if (has_mbyte)
9343 {
9344 int len = 0;
9345
9346 /* Count the number of characters to be escaped. */
9347 for (p = eap->arg; *p != NUL; ++p)
9348 {
9349# ifdef FEAT_GUI
9350 if (*p == CSI) /* leadbyte CSI */
9351 len += 2;
9352# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009353 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9355# ifdef FEAT_GUI
9356 || *p == CSI
9357# endif
9358 )
9359 len += 2;
9360 }
9361 if (len > 0)
9362 {
9363 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9364 if (arg != NULL)
9365 {
9366 len = 0;
9367 for (p = eap->arg; *p != NUL; ++p)
9368 {
9369 arg[len++] = *p;
9370# ifdef FEAT_GUI
9371 if (*p == CSI)
9372 {
9373 arg[len++] = KS_EXTRA;
9374 arg[len++] = (int)KE_CSI;
9375 }
9376# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009377 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 {
9379 arg[len++] = *++p;
9380 if (*p == K_SPECIAL)
9381 {
9382 arg[len++] = KS_SPECIAL;
9383 arg[len++] = KE_FILLER;
9384 }
9385# ifdef FEAT_GUI
9386 else if (*p == CSI)
9387 {
9388 arg[len++] = KS_EXTRA;
9389 arg[len++] = (int)KE_CSI;
9390 }
9391# endif
9392 }
9393 arg[len] = NUL;
9394 }
9395 }
9396 }
9397 }
9398#endif
9399
9400 /*
9401 * Save the current typeahead. This is required to allow using ":normal"
9402 * from an event handler and makes sure we don't hang when the argument
9403 * ends with half a command.
9404 */
9405 save_typeahead(&tabuf);
9406 if (tabuf.typebuf_valid)
9407 {
9408 /*
9409 * Repeat the :normal command for each line in the range. When no
9410 * range given, execute it just once, without positioning the cursor
9411 * first.
9412 */
9413 do
9414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 if (eap->addr_count != 0)
9416 {
9417 curwin->w_cursor.lnum = eap->line1++;
9418 curwin->w_cursor.col = 0;
9419 }
9420
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009421 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#ifdef FEAT_MBYTE
9423 arg != NULL ? arg :
9424#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009425 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9428 }
9429
9430 /* Might not return to the main loop when in an event handler. */
9431 update_topline_cursor();
9432
9433 /* Restore the previous typeahead. */
9434 restore_typeahead(&tabuf);
9435
9436 --ex_normal_busy;
9437 msg_scroll = save_msg_scroll;
9438 restart_edit = save_restart_edit;
9439 p_im = save_insertmode;
9440 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009441 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9443
9444 /* Restore the state (needed when called from a function executed for
9445 * 'indentexpr'). */
9446 State = save_State;
9447#ifdef FEAT_MBYTE
9448 vim_free(arg);
9449#endif
9450}
9451
9452/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009453 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 */
9455 static void
9456ex_startinsert(eap)
9457 exarg_T *eap;
9458{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009459 if (eap->forceit)
9460 {
9461 coladvance((colnr_T)MAXCOL);
9462 curwin->w_curswant = MAXCOL;
9463 curwin->w_set_curswant = FALSE;
9464 }
9465
Bram Moolenaara40c5002005-01-09 21:16:21 +00009466 /* Ignore the command when already in Insert mode. Inserting an
9467 * expression register that invokes a function can do this. */
9468 if (State & INSERT)
9469 return;
9470
Bram Moolenaar64969662005-12-14 21:59:55 +00009471 if (eap->cmdidx == CMD_startinsert)
9472 restart_edit = 'a';
9473 else if (eap->cmdidx == CMD_startreplace)
9474 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009476 restart_edit = 'V';
9477
9478 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009480 if (eap->cmdidx == CMD_startinsert)
9481 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 curwin->w_curswant = 0; /* avoid MAXCOL */
9483 }
9484}
9485
9486/*
9487 * ":stopinsert"
9488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 static void
9490ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009491 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493 restart_edit = 0;
9494 stop_insert_mode = TRUE;
9495}
9496#endif
9497
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009498#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9499/*
9500 * Execute normal mode command "cmd".
9501 * "remap" can be REMAP_NONE or REMAP_YES.
9502 */
9503 void
9504exec_normal_cmd(cmd, remap, silent)
9505 char_u *cmd;
9506 int remap;
9507 int silent;
9508{
9509 oparg_T oa;
9510
9511 /*
9512 * Stuff the argument into the typeahead buffer.
9513 * Execute normal_cmd() until there is no typeahead left.
9514 */
9515 clear_oparg(&oa);
9516 finish_op = FALSE;
9517 ins_typebuf(cmd, remap, 0, TRUE, silent);
9518 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9519 && !got_int)
9520 {
9521 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009522 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009523 }
9524}
9525#endif
9526
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527#ifdef FEAT_FIND_ID
9528 static void
9529ex_checkpath(eap)
9530 exarg_T *eap;
9531{
9532 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9533 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9534 (linenr_T)1, (linenr_T)MAXLNUM);
9535}
9536
9537#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9538/*
9539 * ":psearch"
9540 */
9541 static void
9542ex_psearch(eap)
9543 exarg_T *eap;
9544{
9545 g_do_tagpreview = p_pvh;
9546 ex_findpat(eap);
9547 g_do_tagpreview = 0;
9548}
9549#endif
9550
9551 static void
9552ex_findpat(eap)
9553 exarg_T *eap;
9554{
9555 int whole = TRUE;
9556 long n;
9557 char_u *p;
9558 int action;
9559
9560 switch (cmdnames[eap->cmdidx].cmd_name[2])
9561 {
9562 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9563 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9564 action = ACTION_GOTO;
9565 else
9566 action = ACTION_SHOW;
9567 break;
9568 case 'i': /* ":ilist" and ":dlist" */
9569 action = ACTION_SHOW_ALL;
9570 break;
9571 case 'u': /* ":ijump" and ":djump" */
9572 action = ACTION_GOTO;
9573 break;
9574 default: /* ":isplit" and ":dsplit" */
9575 action = ACTION_SPLIT;
9576 break;
9577 }
9578
9579 n = 1;
9580 if (vim_isdigit(*eap->arg)) /* get count */
9581 {
9582 n = getdigits(&eap->arg);
9583 eap->arg = skipwhite(eap->arg);
9584 }
9585 if (*eap->arg == '/') /* Match regexp, not just whole words */
9586 {
9587 whole = FALSE;
9588 ++eap->arg;
9589 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9590 if (*p)
9591 {
9592 *p++ = NUL;
9593 p = skipwhite(p);
9594
9595 /* Check for trailing illegal characters */
9596 if (!ends_excmd(*p))
9597 eap->errmsg = e_trailing;
9598 else
9599 eap->nextcmd = check_nextcmd(p);
9600 }
9601 }
9602 if (!eap->skip)
9603 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9604 whole, !eap->forceit,
9605 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9606 n, action, eap->line1, eap->line2);
9607}
9608#endif
9609
9610#ifdef FEAT_WINDOWS
9611
9612# ifdef FEAT_QUICKFIX
9613/*
9614 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9615 */
9616 static void
9617ex_ptag(eap)
9618 exarg_T *eap;
9619{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009620 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9622}
9623
9624/*
9625 * ":pedit"
9626 */
9627 static void
9628ex_pedit(eap)
9629 exarg_T *eap;
9630{
9631 win_T *curwin_save = curwin;
9632
9633 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009634 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 keep_help_flag = curwin_save->w_buffer->b_help;
9636 do_exedit(eap, NULL);
9637 keep_help_flag = FALSE;
9638 if (curwin != curwin_save && win_valid(curwin_save))
9639 {
9640 /* Return cursor to where we were */
9641 validate_cursor();
9642 redraw_later(VALID);
9643 win_enter(curwin_save, TRUE);
9644 }
9645 g_do_tagpreview = 0;
9646}
9647# endif
9648
9649/*
9650 * ":stag", ":stselect" and ":stjump".
9651 */
9652 static void
9653ex_stag(eap)
9654 exarg_T *eap;
9655{
9656 postponed_split = -1;
9657 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009658 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9660 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009661 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663#endif
9664
9665/*
9666 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9667 */
9668 static void
9669ex_tag(eap)
9670 exarg_T *eap;
9671{
9672 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9673}
9674
9675 static void
9676ex_tag_cmd(eap, name)
9677 exarg_T *eap;
9678 char_u *name;
9679{
9680 int cmd;
9681
9682 switch (name[1])
9683 {
9684 case 'j': cmd = DT_JUMP; /* ":tjump" */
9685 break;
9686 case 's': cmd = DT_SELECT; /* ":tselect" */
9687 break;
9688 case 'p': cmd = DT_PREV; /* ":tprevious" */
9689 break;
9690 case 'N': cmd = DT_PREV; /* ":tNext" */
9691 break;
9692 case 'n': cmd = DT_NEXT; /* ":tnext" */
9693 break;
9694 case 'o': cmd = DT_POP; /* ":pop" */
9695 break;
9696 case 'f': /* ":tfirst" */
9697 case 'r': cmd = DT_FIRST; /* ":trewind" */
9698 break;
9699 case 'l': cmd = DT_LAST; /* ":tlast" */
9700 break;
9701 default: /* ":tag" */
9702#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009703 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 {
9705 do_cstag(eap);
9706 return;
9707 }
9708#endif
9709 cmd = DT_TAG;
9710 break;
9711 }
9712
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009713 if (name[0] == 'l')
9714 {
9715#ifndef FEAT_QUICKFIX
9716 ex_ni(eap);
9717 return;
9718#else
9719 cmd = DT_LTAG;
9720#endif
9721 }
9722
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9724 eap->forceit, TRUE);
9725}
9726
9727/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009728 * Check "str" for starting with a special cmdline variable.
9729 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9730 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9731 */
9732 int
9733find_cmdline_var(src, usedlen)
9734 char_u *src;
9735 int *usedlen;
9736{
9737 int len;
9738 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009739 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009740 "%",
9741#define SPEC_PERC 0
9742 "#",
9743#define SPEC_HASH 1
9744 "<cword>", /* cursor word */
9745#define SPEC_CWORD 2
9746 "<cWORD>", /* cursor WORD */
9747#define SPEC_CCWORD 3
9748 "<cfile>", /* cursor path name */
9749#define SPEC_CFILE 4
9750 "<sfile>", /* ":so" file name */
9751#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009752 "<slnum>", /* ":so" file line number */
9753#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009754#ifdef FEAT_AUTOCMD
9755 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009756# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009757 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009758# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009759 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009760# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009761#endif
9762#ifdef FEAT_CLIENTSERVER
9763 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009764# ifdef FEAT_AUTOCMD
9765# define SPEC_CLIENT 10
9766# else
9767# define SPEC_CLIENT 7
9768# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009769#endif
9770 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009771
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009772 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009773 {
9774 len = (int)STRLEN(spec_str[i]);
9775 if (STRNCMP(src, spec_str[i], len) == 0)
9776 {
9777 *usedlen = len;
9778 return i;
9779 }
9780 }
9781 return -1;
9782}
9783
9784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 * Evaluate cmdline variables.
9786 *
9787 * change '%' to curbuf->b_ffname
9788 * '#' to curwin->w_altfile
9789 * '<cword>' to word under the cursor
9790 * '<cWORD>' to WORD under the cursor
9791 * '<cfile>' to path name under the cursor
9792 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009793 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 * '<afile>' to file name for autocommand
9795 * '<abuf>' to buffer number for autocommand
9796 * '<amatch>' to matching name for autocommand
9797 *
9798 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9799 * "" for error without a message) and NULL is returned.
9800 * Returns an allocated string if a valid match was found.
9801 * Returns NULL if no match was found. "usedlen" then still contains the
9802 * number of characters to skip.
9803 */
9804 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009805eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009807 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 int *usedlen; /* characters after src that are used */
9809 linenr_T *lnump; /* line number for :e command, or NULL */
9810 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009811 int *escaped; /* return value has escaped white space (can
9812 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813{
9814 int i;
9815 char_u *s;
9816 char_u *result;
9817 char_u *resultbuf = NULL;
9818 int resultlen;
9819 buf_T *buf;
9820 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9821 int spec_idx;
9822#ifdef FEAT_MODIFY_FNAME
9823 int skip_mod = FALSE;
9824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826
9827 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009828 if (escaped != NULL)
9829 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830
9831 /*
9832 * Check if there is something to do.
9833 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009834 spec_idx = find_cmdline_var(src, usedlen);
9835 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 {
9837 *usedlen = 1;
9838 return NULL;
9839 }
9840
9841 /*
9842 * Skip when preceded with a backslash "\%" and "\#".
9843 * Note: In "\\%" the % is also not recognized!
9844 */
9845 if (src > srcstart && src[-1] == '\\')
9846 {
9847 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009848 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 return NULL;
9850 }
9851
9852 /*
9853 * word or WORD under cursor
9854 */
9855 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9856 {
9857 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9858 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9859 if (resultlen == 0)
9860 {
9861 *errormsg = (char_u *)"";
9862 return NULL;
9863 }
9864 }
9865
9866 /*
9867 * '#': Alternate file name
9868 * '%': Current file name
9869 * File name under the cursor
9870 * File name for autocommand
9871 * and following modifiers
9872 */
9873 else
9874 {
9875 switch (spec_idx)
9876 {
9877 case SPEC_PERC: /* '%': current file */
9878 if (curbuf->b_fname == NULL)
9879 {
9880 result = (char_u *)"";
9881 valid = 0; /* Must have ":p:h" to be valid */
9882 }
9883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 break;
9886
9887 case SPEC_HASH: /* '#' or "#99": alternate file */
9888 if (src[1] == '#') /* "##": the argument list */
9889 {
9890 result = arg_all();
9891 resultbuf = result;
9892 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009893 if (escaped != NULL)
9894 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895#ifdef FEAT_MODIFY_FNAME
9896 skip_mod = TRUE;
9897#endif
9898 break;
9899 }
9900 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009901 if (*s == '<') /* "#<99" uses v:oldfiles */
9902 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 i = (int)getdigits(&s);
9904 *usedlen = (int)(s - src); /* length of what we expand */
9905
Bram Moolenaard812df62008-11-09 12:46:09 +00009906 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009908 if (*usedlen < 2)
9909 {
9910 /* Should we give an error message for #<text? */
9911 *usedlen = 1;
9912 return NULL;
9913 }
9914#ifdef FEAT_EVAL
9915 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9916 (long)i);
9917 if (result == NULL)
9918 {
9919 *errormsg = (char_u *)"";
9920 return NULL;
9921 }
9922#else
9923 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 }
9927 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009928 {
9929 buf = buflist_findnr(i);
9930 if (buf == NULL)
9931 {
9932 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9933 return NULL;
9934 }
9935 if (lnump != NULL)
9936 *lnump = ECMD_LAST;
9937 if (buf->b_fname == NULL)
9938 {
9939 result = (char_u *)"";
9940 valid = 0; /* Must have ":p:h" to be valid */
9941 }
9942 else
9943 result = buf->b_fname;
9944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 break;
9946
9947#ifdef FEAT_SEARCHPATH
9948 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009949 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (result == NULL)
9951 {
9952 *errormsg = (char_u *)"";
9953 return NULL;
9954 }
9955 resultbuf = result; /* remember allocated string */
9956 break;
9957#endif
9958
9959#ifdef FEAT_AUTOCMD
9960 case SPEC_AFILE: /* file name for autocommand */
9961 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009962 if (result != NULL && !autocmd_fname_full)
9963 {
9964 /* Still need to turn the fname into a full path. It is
9965 * postponed to avoid a delay when <afile> is not used. */
9966 autocmd_fname_full = TRUE;
9967 result = FullName_save(autocmd_fname, FALSE);
9968 vim_free(autocmd_fname);
9969 autocmd_fname = result;
9970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (result == NULL)
9972 {
9973 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9974 return NULL;
9975 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009976 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 break;
9978
9979 case SPEC_ABUF: /* buffer number for autocommand */
9980 if (autocmd_bufnr <= 0)
9981 {
9982 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9983 return NULL;
9984 }
9985 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9986 result = strbuf;
9987 break;
9988
9989 case SPEC_AMATCH: /* match name for autocommand */
9990 result = autocmd_match;
9991 if (result == NULL)
9992 {
9993 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9994 return NULL;
9995 }
9996 break;
9997
9998#endif
9999 case SPEC_SFILE: /* file name for ":so" command */
10000 result = sourcing_name;
10001 if (result == NULL)
10002 {
10003 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10004 return NULL;
10005 }
10006 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010007 case SPEC_SLNUM: /* line in file for ":so" command */
10008 if (sourcing_name == NULL || sourcing_lnum == 0)
10009 {
10010 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10011 return NULL;
10012 }
10013 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10014 result = strbuf;
10015 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016#if defined(FEAT_CLIENTSERVER)
10017 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010018 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10019 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 result = strbuf;
10021 break;
10022#endif
10023 }
10024
10025 resultlen = (int)STRLEN(result); /* length of new string */
10026 if (src[*usedlen] == '<') /* remove the file name extension */
10027 {
10028 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 resultlen = (int)(s - result);
10031 }
10032#ifdef FEAT_MODIFY_FNAME
10033 else if (!skip_mod)
10034 {
10035 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10036 &resultlen);
10037 if (result == NULL)
10038 {
10039 *errormsg = (char_u *)"";
10040 return NULL;
10041 }
10042 }
10043#endif
10044 }
10045
10046 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10047 {
10048 if (valid != VALID_HEAD + VALID_PATH)
10049 /* xgettext:no-c-format */
10050 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10051 else
10052 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10053 result = NULL;
10054 }
10055 else
10056 result = vim_strnsave(result, resultlen);
10057 vim_free(resultbuf);
10058 return result;
10059}
10060
10061/*
10062 * Concatenate all files in the argument list, separated by spaces, and return
10063 * it in one allocated string.
10064 * Spaces and backslashes in the file names are escaped with a backslash.
10065 * Returns NULL when out of memory.
10066 */
10067 static char_u *
10068arg_all()
10069{
10070 int len;
10071 int idx;
10072 char_u *retval = NULL;
10073 char_u *p;
10074
10075 /*
10076 * Do this loop two times:
10077 * first time: compute the total length
10078 * second time: concatenate the names
10079 */
10080 for (;;)
10081 {
10082 len = 0;
10083 for (idx = 0; idx < ARGCOUNT; ++idx)
10084 {
10085 p = alist_name(&ARGLIST[idx]);
10086 if (p != NULL)
10087 {
10088 if (len > 0)
10089 {
10090 /* insert a space in between names */
10091 if (retval != NULL)
10092 retval[len] = ' ';
10093 ++len;
10094 }
10095 for ( ; *p != NUL; ++p)
10096 {
10097 if (*p == ' ' || *p == '\\')
10098 {
10099 /* insert a backslash */
10100 if (retval != NULL)
10101 retval[len] = '\\';
10102 ++len;
10103 }
10104 if (retval != NULL)
10105 retval[len] = *p;
10106 ++len;
10107 }
10108 }
10109 }
10110
10111 /* second time: break here */
10112 if (retval != NULL)
10113 {
10114 retval[len] = NUL;
10115 break;
10116 }
10117
10118 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010119 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 if (retval == NULL)
10121 break;
10122 }
10123
10124 return retval;
10125}
10126
10127#if defined(FEAT_AUTOCMD) || defined(PROTO)
10128/*
10129 * Expand the <sfile> string in "arg".
10130 *
10131 * Returns an allocated string, or NULL for any error.
10132 */
10133 char_u *
10134expand_sfile(arg)
10135 char_u *arg;
10136{
10137 char_u *errormsg;
10138 int len;
10139 char_u *result;
10140 char_u *newres;
10141 char_u *repl;
10142 int srclen;
10143 char_u *p;
10144
10145 result = vim_strsave(arg);
10146 if (result == NULL)
10147 return NULL;
10148
10149 for (p = result; *p; )
10150 {
10151 if (STRNCMP(p, "<sfile>", 7) != 0)
10152 ++p;
10153 else
10154 {
10155 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010156 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 if (errormsg != NULL)
10158 {
10159 if (*errormsg)
10160 emsg(errormsg);
10161 vim_free(result);
10162 return NULL;
10163 }
10164 if (repl == NULL) /* no match (cannot happen) */
10165 {
10166 p += srclen;
10167 continue;
10168 }
10169 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10170 newres = alloc(len);
10171 if (newres == NULL)
10172 {
10173 vim_free(repl);
10174 vim_free(result);
10175 return NULL;
10176 }
10177 mch_memmove(newres, result, (size_t)(p - result));
10178 STRCPY(newres + (p - result), repl);
10179 len = (int)STRLEN(newres);
10180 STRCAT(newres, p + srclen);
10181 vim_free(repl);
10182 vim_free(result);
10183 result = newres;
10184 p = newres + len; /* continue after the match */
10185 }
10186 }
10187
10188 return result;
10189}
10190#endif
10191
10192#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010193static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10194 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10196static frame_T *ses_skipframe __ARGS((frame_T *fr));
10197static int ses_do_frame __ARGS((frame_T *fr));
10198static int ses_do_win __ARGS((win_T *wp));
10199static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10200static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10201static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10202
10203/*
10204 * Write openfile commands for the current buffers to an .exrc file.
10205 * Return FAIL on error, OK otherwise.
10206 */
10207 static int
10208makeopens(fd, dirnow)
10209 FILE *fd;
10210 char_u *dirnow; /* Current directory name */
10211{
10212 buf_T *buf;
10213 int only_save_windows = TRUE;
10214 int nr;
10215 int cnr = 1;
10216 int restore_size = TRUE;
10217 win_T *wp;
10218 char_u *sname;
10219 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010220 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010221 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010222 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010223 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010224 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
10226 if (ssop_flags & SSOP_BUFFERS)
10227 only_save_windows = FALSE; /* Save ALL buffers */
10228
10229 /*
10230 * Begin by setting the this_session variable, and then other
10231 * sessionable variables.
10232 */
10233#ifdef FEAT_EVAL
10234 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10235 return FAIL;
10236 if (ssop_flags & SSOP_GLOBALS)
10237 if (store_session_globals(fd) == FAIL)
10238 return FAIL;
10239#endif
10240
10241 /*
10242 * Close all windows but one.
10243 */
10244 if (put_line(fd, "silent only") == FAIL)
10245 return FAIL;
10246
10247 /*
10248 * Now a :cd command to the session directory or the current directory
10249 */
10250 if (ssop_flags & SSOP_SESDIR)
10251 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010252 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10253 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 return FAIL;
10255 }
10256 else if (ssop_flags & SSOP_CURDIR)
10257 {
10258 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10259 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010260 || fputs("cd ", fd) < 0
10261 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10262 || put_eol(fd) == FAIL)
10263 {
10264 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 vim_free(sname);
10268 }
10269
10270 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010271 * If there is an empty, unnamed buffer we will wipe it out later.
10272 * Remember the buffer number.
10273 */
10274 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10275 return FAIL;
10276 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10277 return FAIL;
10278 if (put_line(fd, "endif") == FAIL)
10279 return FAIL;
10280
10281 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 * Now save the current files, current buffer first.
10283 */
10284 if (put_line(fd, "set shortmess=aoO") == FAIL)
10285 return FAIL;
10286
10287 /* Now put the other buffers into the buffer list */
10288 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10289 {
10290 if (!(only_save_windows && buf->b_nwindows == 0)
10291 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10292 && buf->b_fname != NULL
10293 && buf->b_p_bl)
10294 {
10295 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10296 : buf->b_wininfo->wi_fpos.lnum) < 0
10297 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10298 return FAIL;
10299 }
10300 }
10301
10302 /* the global argument list */
10303 if (ses_arglist(fd, "args", &global_alist.al_ga,
10304 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10305 return FAIL;
10306
10307 if (ssop_flags & SSOP_RESIZE)
10308 {
10309 /* Note: after the restore we still check it worked!*/
10310 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10311 || put_eol(fd) == FAIL)
10312 return FAIL;
10313 }
10314
10315#ifdef FEAT_GUI
10316 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10317 {
10318 int x, y;
10319
10320 if (gui_mch_get_winpos(&x, &y) == OK)
10321 {
10322 /* Note: after the restore we still check it worked!*/
10323 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10324 return FAIL;
10325 }
10326 }
10327#endif
10328
10329 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010330 * May repeat putting Windows for each tab, when "tabpages" is in
10331 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010332 * Don't use goto_tabpage(), it may change directory and trigger
10333 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010335 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010336 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010337 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010339 int need_tabnew = FALSE;
10340
Bram Moolenaar18144c82006-04-12 21:52:12 +000010341 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010343 tabpage_T *tp = find_tabpage(tabnr);
10344
10345 if (tp == NULL)
10346 break; /* done all tab pages */
10347 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010348 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010349 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010350 tab_topframe = topframe;
10351 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010352 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010353 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010354 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010355 tab_topframe = tp->tp_topframe;
10356 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010357 if (tabnr > 1)
10358 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360
Bram Moolenaar18144c82006-04-12 21:52:12 +000010361 /*
10362 * Before creating the window layout, try loading one file. If this
10363 * is aborted we don't end up with a number of useless windows.
10364 * This may have side effects! (e.g., compressed or network file).
10365 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010366 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010367 {
10368 if (ses_do_win(wp)
10369 && wp->w_buffer->b_ffname != NULL
10370 && !wp->w_buffer->b_help
10371#ifdef FEAT_QUICKFIX
10372 && !bt_nofile(wp->w_buffer)
10373#endif
10374 )
10375 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010376 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010377 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10378 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010379 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010380 if (!wp->w_arg_idx_invalid)
10381 edited_win = wp;
10382 break;
10383 }
10384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010386 /* If no file got edited create an empty tab page. */
10387 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10388 return FAIL;
10389
Bram Moolenaar18144c82006-04-12 21:52:12 +000010390 /*
10391 * Save current window layout.
10392 */
10393 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010395 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010397 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10398 return FAIL;
10399 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401
Bram Moolenaar18144c82006-04-12 21:52:12 +000010402 /*
10403 * Check if window sizes can be restored (no windows omitted).
10404 * Remember the window number of the current window after restoring.
10405 */
10406 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010407 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010408 {
10409 if (ses_do_win(wp))
10410 ++nr;
10411 else
10412 restore_size = FALSE;
10413 if (curwin == wp)
10414 cnr = nr;
10415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416
Bram Moolenaar18144c82006-04-12 21:52:12 +000010417 /* Go to the first window. */
10418 if (put_line(fd, "wincmd t") == FAIL)
10419 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010420
Bram Moolenaar18144c82006-04-12 21:52:12 +000010421 /*
10422 * If more than one window, see if sizes can be restored.
10423 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10424 * resized when moving between windows.
10425 * Do this before restoring the view, so that the topline and the
10426 * cursor can be set. This is done again below.
10427 */
10428 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10429 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010430 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010431 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432
Bram Moolenaar18144c82006-04-12 21:52:12 +000010433 /*
10434 * Restore the view of the window (options, file, cursor, etc.).
10435 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010436 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010437 {
10438 if (!ses_do_win(wp))
10439 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010440 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10441 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010442 return FAIL;
10443 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10444 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010445 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010446 }
10447
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010448 /* The argument index in the first tab page is zero, need to set it in
10449 * each window. For further tab pages it's the window where we do
10450 * "tabedit". */
10451 cur_arg_idx = next_arg_idx;
10452
Bram Moolenaar18144c82006-04-12 21:52:12 +000010453 /*
10454 * Restore cursor to the current window if it's not the first one.
10455 */
10456 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10457 || put_eol(fd) == FAIL))
10458 return FAIL;
10459
10460 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010461 * Restore window sizes again after jumping around in windows, because
10462 * the current window has a minimum size while others may not.
10463 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010464 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010465 return FAIL;
10466
Bram Moolenaar18144c82006-04-12 21:52:12 +000010467 /* Don't continue in another tab page when doing only the current one
10468 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010469 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010470 break;
10471 }
10472
10473 if (ssop_flags & SSOP_TABPAGES)
10474 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010475 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10476 || put_eol(fd) == FAIL)
10477 return FAIL;
10478 }
10479
Bram Moolenaar9c102382006-05-03 21:26:49 +000010480 /*
10481 * Wipe out an empty unnamed buffer we started in.
10482 */
10483 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10484 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010485 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010486 return FAIL;
10487 if (put_line(fd, "endif") == FAIL)
10488 return FAIL;
10489 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10490 return FAIL;
10491
10492 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10493 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10494 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10495 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
10497 /*
10498 * Lastly, execute the x.vim file if it exists.
10499 */
10500 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10501 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010502 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 || put_line(fd, "endif") == FAIL)
10504 return FAIL;
10505
10506 return OK;
10507}
10508
10509 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010510ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 FILE *fd;
10512 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010513 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 int n = 0;
10516 win_T *wp;
10517
10518 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10519 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010520 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 {
10522 if (!ses_do_win(wp))
10523 continue;
10524 ++n;
10525
10526 /* restore height when not full height */
10527 if (wp->w_height + wp->w_status_height < topframe->fr_height
10528 && (fprintf(fd,
10529 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10530 n, (long)wp->w_height, Rows / 2, Rows) < 0
10531 || put_eol(fd) == FAIL))
10532 return FAIL;
10533
10534 /* restore width when not full width */
10535 if (wp->w_width < Columns && (fprintf(fd,
10536 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10537 n, (long)wp->w_width, Columns / 2, Columns) < 0
10538 || put_eol(fd) == FAIL))
10539 return FAIL;
10540 }
10541 }
10542 else
10543 {
10544 /* Just equalise window sizes */
10545 if (put_line(fd, "wincmd =") == FAIL)
10546 return FAIL;
10547 }
10548 return OK;
10549}
10550
10551/*
10552 * Write commands to "fd" to recursively create windows for frame "fr",
10553 * horizontally and vertically split.
10554 * After the commands the last window in the frame is the current window.
10555 * Returns FAIL when writing the commands to "fd" fails.
10556 */
10557 static int
10558ses_win_rec(fd, fr)
10559 FILE *fd;
10560 frame_T *fr;
10561{
10562 frame_T *frc;
10563 int count = 0;
10564
10565 if (fr->fr_layout != FR_LEAF)
10566 {
10567 /* Find first frame that's not skipped and then create a window for
10568 * each following one (first frame is already there). */
10569 frc = ses_skipframe(fr->fr_child);
10570 if (frc != NULL)
10571 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10572 {
10573 /* Make window as big as possible so that we have lots of room
10574 * to split. */
10575 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10576 || put_line(fd, fr->fr_layout == FR_COL
10577 ? "split" : "vsplit") == FAIL)
10578 return FAIL;
10579 ++count;
10580 }
10581
10582 /* Go back to the first window. */
10583 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10584 ? "%dwincmd k" : "%dwincmd h", count) < 0
10585 || put_eol(fd) == FAIL))
10586 return FAIL;
10587
10588 /* Recursively create frames/windows in each window of this column or
10589 * row. */
10590 frc = ses_skipframe(fr->fr_child);
10591 while (frc != NULL)
10592 {
10593 ses_win_rec(fd, frc);
10594 frc = ses_skipframe(frc->fr_next);
10595 /* Go to next window. */
10596 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10597 return FAIL;
10598 }
10599 }
10600 return OK;
10601}
10602
10603/*
10604 * Skip frames that don't contain windows we want to save in the Session.
10605 * Returns NULL when there none.
10606 */
10607 static frame_T *
10608ses_skipframe(fr)
10609 frame_T *fr;
10610{
10611 frame_T *frc;
10612
10613 for (frc = fr; frc != NULL; frc = frc->fr_next)
10614 if (ses_do_frame(frc))
10615 break;
10616 return frc;
10617}
10618
10619/*
10620 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10621 * the Session.
10622 */
10623 static int
10624ses_do_frame(fr)
10625 frame_T *fr;
10626{
10627 frame_T *frc;
10628
10629 if (fr->fr_layout == FR_LEAF)
10630 return ses_do_win(fr->fr_win);
10631 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10632 if (ses_do_frame(frc))
10633 return TRUE;
10634 return FALSE;
10635}
10636
10637/*
10638 * Return non-zero if window "wp" is to be stored in the Session.
10639 */
10640 static int
10641ses_do_win(wp)
10642 win_T *wp;
10643{
10644 if (wp->w_buffer->b_fname == NULL
10645#ifdef FEAT_QUICKFIX
10646 /* When 'buftype' is "nofile" can't restore the window contents. */
10647 || bt_nofile(wp->w_buffer)
10648#endif
10649 )
10650 return (ssop_flags & SSOP_BLANK);
10651 if (wp->w_buffer->b_help)
10652 return (ssop_flags & SSOP_HELP);
10653 return TRUE;
10654}
10655
10656/*
10657 * Write commands to "fd" to restore the view of a window.
10658 * Caller must make sure 'scrolloff' is zero.
10659 */
10660 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010661put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 FILE *fd;
10663 win_T *wp;
10664 int add_edit; /* add ":edit" command to view */
10665 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010666 int current_arg_idx; /* current argument index of the window, use
10667 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
10669 win_T *save_curwin;
10670 int f;
10671 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010672 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673
10674 /* Always restore cursor position for ":mksession". For ":mkview" only
10675 * when 'viewoptions' contains "cursor". */
10676 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10677
10678 /*
10679 * Local argument list.
10680 */
10681 if (wp->w_alist == &global_alist)
10682 {
10683 if (put_line(fd, "argglobal") == FAIL)
10684 return FAIL;
10685 }
10686 else
10687 {
10688 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10689 flagp == &vop_flags
10690 || !(*flagp & SSOP_CURDIR)
10691 || wp->w_localdir != NULL, flagp) == FAIL)
10692 return FAIL;
10693 }
10694
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010695 /* Only when part of a session: restore the argument index. Some
10696 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010697 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010698 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010700 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 || put_eol(fd) == FAIL)
10702 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010703 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 }
10705
10706 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010707 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 {
10709 /*
10710 * Load the file.
10711 */
10712 if (wp->w_buffer->b_ffname != NULL
10713#ifdef FEAT_QUICKFIX
10714 && !bt_nofile(wp->w_buffer)
10715#endif
10716 )
10717 {
10718 /*
10719 * Editing a file in this buffer: use ":edit file".
10720 * This may have side effects! (e.g., compressed or network file).
10721 */
10722 if (fputs("edit ", fd) < 0
10723 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10724 return FAIL;
10725 }
10726 else
10727 {
10728 /* No file in this buffer, just make it empty. */
10729 if (put_line(fd, "enew") == FAIL)
10730 return FAIL;
10731#ifdef FEAT_QUICKFIX
10732 if (wp->w_buffer->b_ffname != NULL)
10733 {
10734 /* The buffer does have a name, but it's not a file name. */
10735 if (fputs("file ", fd) < 0
10736 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10737 return FAIL;
10738 }
10739#endif
10740 do_cursor = FALSE;
10741 }
10742 }
10743
10744 /*
10745 * Local mappings and abbreviations.
10746 */
10747 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10748 && makemap(fd, wp->w_buffer) == FAIL)
10749 return FAIL;
10750
10751 /*
10752 * Local options. Need to go to the window temporarily.
10753 * Store only local values when using ":mkview" and when ":mksession" is
10754 * used and 'sessionoptions' doesn't include "options".
10755 * Some folding options are always stored when "folds" is included,
10756 * otherwise the folds would not be restored correctly.
10757 */
10758 save_curwin = curwin;
10759 curwin = wp;
10760 curbuf = curwin->w_buffer;
10761 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10762 f = makeset(fd, OPT_LOCAL,
10763 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10764#ifdef FEAT_FOLDING
10765 else if (*flagp & SSOP_FOLDS)
10766 f = makefoldset(fd);
10767#endif
10768 else
10769 f = OK;
10770 curwin = save_curwin;
10771 curbuf = curwin->w_buffer;
10772 if (f == FAIL)
10773 return FAIL;
10774
10775#ifdef FEAT_FOLDING
10776 /*
10777 * Save Folds when 'buftype' is empty and for help files.
10778 */
10779 if ((*flagp & SSOP_FOLDS)
10780 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010781# ifdef FEAT_QUICKFIX
10782 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10783# endif
10784 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 {
10786 if (put_folds(fd, wp) == FAIL)
10787 return FAIL;
10788 }
10789#endif
10790
10791 /*
10792 * Set the cursor after creating folds, since that moves the cursor.
10793 */
10794 if (do_cursor)
10795 {
10796
10797 /* Restore the cursor line in the file and relatively in the
10798 * window. Don't use "G", it changes the jumplist. */
10799 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10800 (long)wp->w_cursor.lnum,
10801 (long)(wp->w_cursor.lnum - wp->w_topline),
10802 (long)wp->w_height / 2, (long)wp->w_height) < 0
10803 || put_eol(fd) == FAIL
10804 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10805 || put_line(fd, "exe s:l") == FAIL
10806 || put_line(fd, "normal! zt") == FAIL
10807 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10808 || put_eol(fd) == FAIL)
10809 return FAIL;
10810 /* Restore the cursor column and left offset when not wrapping. */
10811 if (wp->w_cursor.col == 0)
10812 {
10813 if (put_line(fd, "normal! 0") == FAIL)
10814 return FAIL;
10815 }
10816 else
10817 {
10818 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10819 {
10820 if (fprintf(fd,
10821 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10822 (long)wp->w_cursor.col,
10823 (long)(wp->w_cursor.col - wp->w_leftcol),
10824 (long)wp->w_width / 2, (long)wp->w_width) < 0
10825 || put_eol(fd) == FAIL
10826 || put_line(fd, "if s:c > 0") == FAIL
10827 || fprintf(fd,
10828 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10829 (long)wp->w_cursor.col) < 0
10830 || put_eol(fd) == FAIL
10831 || put_line(fd, "else") == FAIL
10832 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10833 || put_eol(fd) == FAIL
10834 || put_line(fd, "endif") == FAIL)
10835 return FAIL;
10836 }
10837 else
10838 {
10839 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10840 || put_eol(fd) == FAIL)
10841 return FAIL;
10842 }
10843 }
10844 }
10845
10846 /*
10847 * Local directory.
10848 */
10849 if (wp->w_localdir != NULL)
10850 {
10851 if (fputs("lcd ", fd) < 0
10852 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10853 || put_eol(fd) == FAIL)
10854 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010855 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 }
10857
10858 return OK;
10859}
10860
10861/*
10862 * Write an argument list to the session file.
10863 * Returns FAIL if writing fails.
10864 */
10865 static int
10866ses_arglist(fd, cmd, gap, fullname, flagp)
10867 FILE *fd;
10868 char *cmd;
10869 garray_T *gap;
10870 int fullname; /* TRUE: use full path name */
10871 unsigned *flagp;
10872{
10873 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010874 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 char_u *s;
10876
10877 if (gap->ga_len == 0)
10878 return put_line(fd, "silent! argdel *");
10879 if (fputs(cmd, fd) < 0)
10880 return FAIL;
10881 for (i = 0; i < gap->ga_len; ++i)
10882 {
10883 /* NULL file names are skipped (only happens when out of memory). */
10884 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10885 if (s != NULL)
10886 {
10887 if (fullname)
10888 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010889 buf = alloc(MAXPATHL);
10890 if (buf != NULL)
10891 {
10892 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10893 s = buf;
10894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 }
10896 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010897 {
10898 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010900 }
10901 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 }
10903 }
10904 return put_eol(fd);
10905}
10906
10907/*
10908 * Write a buffer name to the session file.
10909 * Also ends the line.
10910 * Returns FAIL if writing fails.
10911 */
10912 static int
10913ses_fname(fd, buf, flagp)
10914 FILE *fd;
10915 buf_T *buf;
10916 unsigned *flagp;
10917{
10918 char_u *name;
10919
10920 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010921 * the session file will be sourced.
10922 * Don't do this for ":mkview", we don't know the current directory.
10923 * Don't do this after ":lcd", we don't keep track of what the current
10924 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 if (buf->b_sfname != NULL
10926 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010927 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010928#ifdef FEAT_AUTOCHDIR
10929 && !p_acd
10930#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010931 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 name = buf->b_sfname;
10933 else
10934 name = buf->b_ffname;
10935 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10936 return FAIL;
10937 return OK;
10938}
10939
10940/*
10941 * Write a file name to the session file.
10942 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10943 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010944 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 */
10946 static int
10947ses_put_fname(fd, name, flagp)
10948 FILE *fd;
10949 char_u *name;
10950 unsigned *flagp;
10951{
10952 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010953 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955
10956 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010957 if (sname == NULL)
10958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010960 if (*flagp & SSOP_SLASH)
10961 {
10962 /* change all backslashes to forward slashes */
10963 for (p = sname; *p != NUL; mb_ptr_adv(p))
10964 if (*p == '\\')
10965 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010967
10968 /* escapse special characters */
10969 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010971 if (p == NULL)
10972 return FAIL;
10973
10974 /* write the result */
10975 if (fputs((char *)p, fd) < 0)
10976 retval = FAIL;
10977
10978 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 return retval;
10980}
10981
10982/*
10983 * ":loadview [nr]"
10984 */
10985 static void
10986ex_loadview(eap)
10987 exarg_T *eap;
10988{
10989 char_u *fname;
10990
10991 fname = get_view_file(*eap->arg);
10992 if (fname != NULL)
10993 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010994 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 vim_free(fname);
10996 }
10997}
10998
10999/*
11000 * Get the name of the view file for the current buffer.
11001 */
11002 static char_u *
11003get_view_file(c)
11004 int c;
11005{
11006 int len = 0;
11007 char_u *p, *s;
11008 char_u *retval;
11009 char_u *sname;
11010
11011 if (curbuf->b_ffname == NULL)
11012 {
11013 EMSG(_(e_noname));
11014 return NULL;
11015 }
11016 sname = home_replace_save(NULL, curbuf->b_ffname);
11017 if (sname == NULL)
11018 return NULL;
11019
11020 /*
11021 * We want a file name without separators, because we're not going to make
11022 * a directory.
11023 * "normal" path separator -> "=+"
11024 * "=" -> "=="
11025 * ":" path separator -> "=-"
11026 */
11027 for (p = sname; *p; ++p)
11028 if (*p == '=' || vim_ispathsep(*p))
11029 ++len;
11030 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11031 if (retval != NULL)
11032 {
11033 STRCPY(retval, p_vdir);
11034 add_pathsep(retval);
11035 s = retval + STRLEN(retval);
11036 for (p = sname; *p; ++p)
11037 {
11038 if (*p == '=')
11039 {
11040 *s++ = '=';
11041 *s++ = '=';
11042 }
11043 else if (vim_ispathsep(*p))
11044 {
11045 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011046#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 if (*p == ':')
11048 *s++ = '-';
11049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011051 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 }
11053 else
11054 *s++ = *p;
11055 }
11056 *s++ = '=';
11057 *s++ = c;
11058 STRCPY(s, ".vim");
11059 }
11060
11061 vim_free(sname);
11062 return retval;
11063}
11064
11065#endif /* FEAT_SESSION */
11066
11067/*
11068 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11069 * Return FAIL for a write error.
11070 */
11071 int
11072put_eol(fd)
11073 FILE *fd;
11074{
11075 if (
11076#ifdef USE_CRNL
11077 (
11078# ifdef MKSESSION_NL
11079 !mksession_nl &&
11080# endif
11081 (putc('\r', fd) < 0)) ||
11082#endif
11083 (putc('\n', fd) < 0))
11084 return FAIL;
11085 return OK;
11086}
11087
11088/*
11089 * Write a line to "fd".
11090 * Return FAIL for a write error.
11091 */
11092 int
11093put_line(fd, s)
11094 FILE *fd;
11095 char *s;
11096{
11097 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11098 return FAIL;
11099 return OK;
11100}
11101
11102#ifdef FEAT_VIMINFO
11103/*
11104 * ":rviminfo" and ":wviminfo".
11105 */
11106 static void
11107ex_viminfo(eap)
11108 exarg_T *eap;
11109{
11110 char_u *save_viminfo;
11111
11112 save_viminfo = p_viminfo;
11113 if (*p_viminfo == NUL)
11114 p_viminfo = (char_u *)"'100";
11115 if (eap->cmdidx == CMD_rviminfo)
11116 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011117 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11118 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 EMSG(_("E195: Cannot open viminfo file for reading"));
11120 }
11121 else
11122 write_viminfo(eap->arg, eap->forceit);
11123 p_viminfo = save_viminfo;
11124}
11125#endif
11126
11127#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011128/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011129 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011130 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 void
11133dialog_msg(buff, format, fname)
11134 char_u *buff;
11135 char *format;
11136 char_u *fname;
11137{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 if (fname == NULL)
11139 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011140 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141}
11142#endif
11143
11144/*
11145 * ":behave {mswin,xterm}"
11146 */
11147 static void
11148ex_behave(eap)
11149 exarg_T *eap;
11150{
11151 if (STRCMP(eap->arg, "mswin") == 0)
11152 {
11153 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11154 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11155 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11156 set_option_value((char_u *)"keymodel", 0L,
11157 (char_u *)"startsel,stopsel", 0);
11158 }
11159 else if (STRCMP(eap->arg, "xterm") == 0)
11160 {
11161 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11162 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11163 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11164 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11165 }
11166 else
11167 EMSG2(_(e_invarg2), eap->arg);
11168}
11169
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11171/*
11172 * Function given to ExpandGeneric() to obtain the possible arguments of the
11173 * ":behave {mswin,xterm}" command.
11174 */
11175 char_u *
11176get_behave_arg(xp, idx)
11177 expand_T *xp UNUSED;
11178 int idx;
11179{
11180 if (idx == 0)
11181 return (char_u *)"mswin";
11182 if (idx == 1)
11183 return (char_u *)"xterm";
11184 return NULL;
11185}
11186#endif
11187
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188#ifdef FEAT_AUTOCMD
11189static int filetype_detect = FALSE;
11190static int filetype_plugin = FALSE;
11191static int filetype_indent = FALSE;
11192
11193/*
11194 * ":filetype [plugin] [indent] {on,off,detect}"
11195 * on: Load the filetype.vim file to install autocommands for file types.
11196 * off: Load the ftoff.vim file to remove all autocommands for file types.
11197 * plugin on: load filetype.vim and ftplugin.vim
11198 * plugin off: load ftplugof.vim
11199 * indent on: load filetype.vim and indent.vim
11200 * indent off: load indoff.vim
11201 */
11202 static void
11203ex_filetype(eap)
11204 exarg_T *eap;
11205{
11206 char_u *arg = eap->arg;
11207 int plugin = FALSE;
11208 int indent = FALSE;
11209
11210 if (*eap->arg == NUL)
11211 {
11212 /* Print current status. */
11213 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11214 filetype_detect ? "ON" : "OFF",
11215 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11216 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11217 return;
11218 }
11219
11220 /* Accept "plugin" and "indent" in any order. */
11221 for (;;)
11222 {
11223 if (STRNCMP(arg, "plugin", 6) == 0)
11224 {
11225 plugin = TRUE;
11226 arg = skipwhite(arg + 6);
11227 continue;
11228 }
11229 if (STRNCMP(arg, "indent", 6) == 0)
11230 {
11231 indent = TRUE;
11232 arg = skipwhite(arg + 6);
11233 continue;
11234 }
11235 break;
11236 }
11237 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11238 {
11239 if (*arg == 'o' || !filetype_detect)
11240 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011241 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 filetype_detect = TRUE;
11243 if (plugin)
11244 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011245 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 filetype_plugin = TRUE;
11247 }
11248 if (indent)
11249 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011250 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 filetype_indent = TRUE;
11252 }
11253 }
11254 if (*arg == 'd')
11255 {
11256 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011257 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 }
11259 }
11260 else if (STRCMP(arg, "off") == 0)
11261 {
11262 if (plugin || indent)
11263 {
11264 if (plugin)
11265 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011266 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 filetype_plugin = FALSE;
11268 }
11269 if (indent)
11270 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011271 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 filetype_indent = FALSE;
11273 }
11274 }
11275 else
11276 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011277 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 filetype_detect = FALSE;
11279 }
11280 }
11281 else
11282 EMSG2(_(e_invarg2), arg);
11283}
11284
11285/*
11286 * ":setfiletype {name}"
11287 */
11288 static void
11289ex_setfiletype(eap)
11290 exarg_T *eap;
11291{
11292 if (!did_filetype)
11293 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11294}
11295#endif
11296
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 static void
11298ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011299 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300{
11301#ifdef FEAT_DIGRAPHS
11302 if (*eap->arg != NUL)
11303 putdigraph(eap->arg);
11304 else
11305 listdigraphs();
11306#else
11307 EMSG(_("E196: No digraphs in this version"));
11308#endif
11309}
11310
11311 static void
11312ex_set(eap)
11313 exarg_T *eap;
11314{
11315 int flags = 0;
11316
11317 if (eap->cmdidx == CMD_setlocal)
11318 flags = OPT_LOCAL;
11319 else if (eap->cmdidx == CMD_setglobal)
11320 flags = OPT_GLOBAL;
11321#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11322 if (cmdmod.browse && flags == 0)
11323 ex_options(eap);
11324 else
11325#endif
11326 (void)do_set(eap->arg, flags);
11327}
11328
11329#ifdef FEAT_SEARCH_EXTRA
11330/*
11331 * ":nohlsearch"
11332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 static void
11334ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011335 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
11337 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011338 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339}
11340
11341/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011342 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 * Sets nextcmd to the start of the next command, if any. Also called when
11344 * skipping commands to find the next command.
11345 */
11346 static void
11347ex_match(eap)
11348 exarg_T *eap;
11349{
11350 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011351 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 char_u *end;
11353 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011354 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011355
11356 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011357 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011358 else
11359 {
11360 EMSG(e_invcmd);
11361 return;
11362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363
11364 /* First clear any old pattern. */
11365 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011366 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
11368 if (ends_excmd(*eap->arg))
11369 end = eap->arg;
11370 else if ((STRNICMP(eap->arg, "none", 4) == 0
11371 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11372 end = eap->arg + 4;
11373 else
11374 {
11375 p = skiptowhite(eap->arg);
11376 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011377 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 p = skipwhite(p);
11379 if (*p == NUL)
11380 {
11381 /* There must be two arguments. */
11382 EMSG2(_(e_invarg2), eap->arg);
11383 return;
11384 }
11385 end = skip_regexp(p + 1, *p, TRUE, NULL);
11386 if (!eap->skip)
11387 {
11388 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11389 {
11390 eap->errmsg = e_trailing;
11391 return;
11392 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011393 if (*end != *p)
11394 {
11395 EMSG2(_(e_invarg2), p);
11396 return;
11397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398
11399 c = *end;
11400 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011401 match_add(curwin, g, p + 1, 10, id);
11402 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011403 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 }
11405 }
11406 eap->nextcmd = find_nextcmd(end);
11407}
11408#endif
11409
11410#ifdef FEAT_CRYPT
11411/*
11412 * ":X": Get crypt key
11413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 static void
11415ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011416 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011418 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011419 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420}
11421#endif
11422
11423#ifdef FEAT_FOLDING
11424 static void
11425ex_fold(eap)
11426 exarg_T *eap;
11427{
11428 if (foldManualAllowed(TRUE))
11429 foldCreate(eap->line1, eap->line2);
11430}
11431
11432 static void
11433ex_foldopen(eap)
11434 exarg_T *eap;
11435{
11436 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11437 eap->forceit, FALSE);
11438}
11439
11440 static void
11441ex_folddo(eap)
11442 exarg_T *eap;
11443{
11444 linenr_T lnum;
11445
11446 /* First set the marks for all lines closed/open. */
11447 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11448 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11449 ml_setmarked(lnum);
11450
11451 /* Execute the command on the marked lines. */
11452 global_exe(eap->arg);
11453 ml_clearmarked(); /* clear rest of the marks */
11454}
11455#endif