blob: cc80c14b78faefb006ccbee8b476e4a2fa3f6275 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
287#ifndef FEAT_SNIFF
288# define ex_sniff ex_ni
289#endif
290#ifndef FEAT_KEYMAP
291# define ex_loadkeymap ex_ni
292#endif
293static void ex_swapname __ARGS((exarg_T *eap));
294static void ex_syncbind __ARGS((exarg_T *eap));
295static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_pwd __ARGS((exarg_T *eap));
297static void ex_equal __ARGS((exarg_T *eap));
298static void ex_sleep __ARGS((exarg_T *eap));
299static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
300static void ex_winsize __ARGS((exarg_T *eap));
301#ifdef FEAT_WINDOWS
302static void ex_wincmd __ARGS((exarg_T *eap));
303#else
304# define ex_wincmd ex_ni
305#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_winpos __ARGS((exarg_T *eap));
308#else
309# define ex_winpos ex_ni
310#endif
311static void ex_operators __ARGS((exarg_T *eap));
312static void ex_put __ARGS((exarg_T *eap));
313static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000314static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_submagic __ARGS((exarg_T *eap));
316static void ex_join __ARGS((exarg_T *eap));
317static void ex_at __ARGS((exarg_T *eap));
318static void ex_bang __ARGS((exarg_T *eap));
319static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#ifdef FEAT_PERSISTENT_UNDO
321static void ex_wundo __ARGS((exarg_T *eap));
322static void ex_rundo __ARGS((exarg_T *eap));
323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000325static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redir __ARGS((exarg_T *eap));
327static void ex_redraw __ARGS((exarg_T *eap));
328static void ex_redrawstatus __ARGS((exarg_T *eap));
329static void close_redir __ARGS((void));
330static void ex_mkrc __ARGS((exarg_T *eap));
331static void ex_mark __ARGS((exarg_T *eap));
332#ifdef FEAT_USR_CMDS
333static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000334static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336#ifdef FEAT_EX_EXTRA
337static void ex_normal __ARGS((exarg_T *eap));
338static void ex_startinsert __ARGS((exarg_T *eap));
339static void ex_stopinsert __ARGS((exarg_T *eap));
340#else
341# define ex_normal ex_ni
342# define ex_align ex_ni
343# define ex_retab ex_ni
344# define ex_startinsert ex_ni
345# define ex_stopinsert ex_ni
346# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000347# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349#ifdef FEAT_FIND_ID
350static void ex_checkpath __ARGS((exarg_T *eap));
351static void ex_findpat __ARGS((exarg_T *eap));
352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357static void ex_psearch __ARGS((exarg_T *eap));
358#else
359# define ex_psearch ex_ni
360#endif
361static void ex_tag __ARGS((exarg_T *eap));
362static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392static char_u *arg_all __ARGS((void));
393#ifdef FEAT_SESSION
394static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000395static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static void ex_loadview __ARGS((exarg_T *eap));
397static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
406static void ex_viminfo __ARGS((exarg_T *eap));
407#else
408# define ex_viminfo ex_ni
409#endif
410static void ex_behave __ARGS((exarg_T *eap));
411#ifdef FEAT_AUTOCMD
412static void ex_filetype __ARGS((exarg_T *eap));
413static void ex_setfiletype __ARGS((exarg_T *eap));
414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
426static void ex_digraphs __ARGS((exarg_T *eap));
427static void ex_set __ARGS((exarg_T *eap));
428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
432static void ex_nohlsearch __ARGS((exarg_T *eap));
433static void ex_match __ARGS((exarg_T *eap));
434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
439static void ex_X __ARGS((exarg_T *eap));
440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
444static void ex_fold __ARGS((exarg_T *eap));
445static void ex_foldopen __ARGS((exarg_T *eap));
446static void ex_folddo __ARGS((exarg_T *eap));
447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
550 char_u *(*getline) __ARGS((int, void *, int));
551 void *cookie;
552};
553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
555static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
574static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
575static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576
577 static void
578save_dbg_stuff(dsp)
579 struct dbg_stuff *dsp;
580{
581 dsp->trylevel = trylevel; trylevel = 0;
582 dsp->force_abort = force_abort; force_abort = FALSE;
583 dsp->caught_stack = caught_stack; caught_stack = NULL;
584 dsp->vv_exception = v_exception(NULL);
585 dsp->vv_throwpoint = v_throwpoint(NULL);
586
587 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
588 dsp->did_emsg = did_emsg; did_emsg = FALSE;
589 dsp->got_int = got_int; got_int = FALSE;
590 dsp->did_throw = did_throw; did_throw = FALSE;
591 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
592 dsp->check_cstack = check_cstack; check_cstack = FALSE;
593 dsp->current_exception = current_exception; current_exception = NULL;
594}
595
596 static void
597restore_dbg_stuff(dsp)
598 struct dbg_stuff *dsp;
599{
600 suppress_errthrow = FALSE;
601 trylevel = dsp->trylevel;
602 force_abort = dsp->force_abort;
603 caught_stack = dsp->caught_stack;
604 (void)v_exception(dsp->vv_exception);
605 (void)v_throwpoint(dsp->vv_throwpoint);
606 did_emsg = dsp->did_emsg;
607 got_int = dsp->got_int;
608 did_throw = dsp->did_throw;
609 need_rethrow = dsp->need_rethrow;
610 check_cstack = dsp->check_cstack;
611 current_exception = dsp->current_exception;
612}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614
615
616/*
617 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
618 * command is given.
619 */
620 void
621do_exmode(improved)
622 int improved; /* TRUE for "improved Ex" mode */
623{
624 int save_msg_scroll;
625 int prev_msg_row;
626 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 int changedtick;
628
629 if (improved)
630 exmode_active = EXMODE_VIM;
631 else
632 exmode_active = EXMODE_NORMAL;
633 State = NORMAL;
634
635 /* When using ":global /pat/ visual" and then "Q" we return to continue
636 * the :global command. */
637 if (global_busy)
638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 save_msg_scroll = msg_scroll;
641 ++RedrawingDisabled; /* don't redisplay the window */
642 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_GUI
644 /* Ignore scrollbar and mouse events in Ex mode */
645 ++hold_gui_events;
646#endif
647#ifdef FEAT_SNIFF
648 want_sniff_request = 0; /* No K_SNIFF wanted */
649#endif
650
651 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
652 while (exmode_active)
653 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000654#ifdef FEAT_EX_EXTRA
655 /* Check for a ":normal" command and no more characters left. */
656 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
657 {
658 exmode_active = FALSE;
659 break;
660 }
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 msg_scroll = TRUE;
663 need_wait_return = FALSE;
664 ex_pressedreturn = FALSE;
665 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000666 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 prev_msg_row = msg_row;
668 prev_line = curwin->w_cursor.lnum;
669#ifdef FEAT_SNIFF
670 ProcessSniffRequests();
671#endif
672 if (improved)
673 {
674 cmdline_row = msg_row;
675 do_cmdline(NULL, getexline, NULL, 0);
676 }
677 else
678 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
679 lines_left = Rows - 1;
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if ((prev_line != curwin->w_cursor.lnum
682 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (curbuf->b_ml.ml_flags & ML_EMPTY)
685 EMSG(_(e_emptybuf));
686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (ex_pressedreturn)
689 {
690 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000691 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 msg_row = prev_msg_row;
693 if (prev_msg_row == Rows - 1)
694 msg_row--;
695 }
696 msg_col = 0;
697 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
698 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
702 {
703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
704 EMSG(_(e_emptybuf));
705 else
706 EMSG(_("E501: At end-of-file"));
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710#ifdef FEAT_GUI
711 --hold_gui_events;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 --RedrawingDisabled;
714 --no_wait_return;
715 update_screen(CLEAR);
716 need_wait_return = FALSE;
717 msg_scroll = save_msg_scroll;
718}
719
720/*
721 * Execute a simple command line. Used for translated commands like "*".
722 */
723 int
724do_cmdline_cmd(cmd)
725 char_u *cmd;
726{
727 return do_cmdline(cmd, NULL, NULL,
728 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
729}
730
731/*
732 * do_cmdline(): execute one Ex command line
733 *
734 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * 2. Split up in parts separated with '|'.
737 *
738 * This function can be called recursively!
739 *
740 * flags:
741 * DOCMD_VERBOSE - The command will be included in the error message.
742 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * DOCMD_KEYTYPED - Don't reset KeyTyped.
745 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
746 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
747 *
748 * return FAIL if cmdline could not be executed, OK otherwise
749 */
750 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 char_u *(*fgetline) __ARGS((int, void *, int));
754 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int flags;
756{
757 char_u *next_cmdline; /* next cmd to execute */
758 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 static int recursive = 0; /* recursive depth */
761 int msg_didout_before_start = 0;
762 int count = 0; /* line number count */
763 int did_inc = FALSE; /* incremented RedrawingDisabled */
764 int retval = OK;
765#ifdef FEAT_EVAL
766 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int current_line = 0; /* active line in lines_ga */
769 char_u *fname = NULL; /* function or script name */
770 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
771 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000772 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int initial_trylevel;
774 struct msglist **saved_msg_list = NULL;
775 struct msglist *private_msg_list;
776
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 char_u *(*cmd_getline) __ARGS((int, void *, int));
779 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# define cmd_cookie cookie
786#endif
787 static int call_depth = 0; /* recursiveness */
788
789#ifdef FEAT_EVAL
790 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
791 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000792 * This ensures that the do_errthrow() call in do_one_cmd() does not
793 * combine the messages stored by an earlier invocation of do_one_cmd()
794 * with the command name of the later one. This would happen when
795 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 saved_msg_list = msg_list;
797 msg_list = &private_msg_list;
798 private_msg_list = NULL;
799#endif
800
801 /* It's possible to create an endless loop with ":execute", catch that
802 * here. The value of 200 allows nested function calls, ":source", etc. */
803 if (call_depth == 200)
804 {
805 EMSG(_("E169: Command too recursive"));
806#ifdef FEAT_EVAL
807 /* When converting to an exception, we do not include the command name
808 * since this is not an error of the specific command. */
809 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
810 msg_list = saved_msg_list;
811#endif
812 return FAIL;
813 }
814 ++call_depth;
815
816#ifdef FEAT_EVAL
817 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 cstack.cs_trylevel = 0;
820 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000821 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
823
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++ex_nesting_level;
830
831 /* Get the function or script name and the address where the next breakpoint
832 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = func_name(real_cookie);
836 breakpoint = func_breakpoint(real_cookie);
837 dbg_tick = func_dbg_tick(real_cookie);
838 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 fname = sourcing_name;
842 breakpoint = source_breakpoint(real_cookie);
843 dbg_tick = source_dbg_tick(real_cookie);
844 }
845
846 /*
847 * Initialize "force_abort" and "suppress_errthrow" at the top level.
848 */
849 if (!recursive)
850 {
851 force_abort = FALSE;
852 suppress_errthrow = FALSE;
853 }
854
855 /*
856 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 * exception handling (used when debugging). Otherwise clear it to avoid
858 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000860 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000861 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200863 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 initial_trylevel = trylevel;
866
867 /*
868 * "did_throw" will be set to TRUE when an exception is being thrown.
869 */
870 did_throw = FALSE;
871#endif
872 /*
873 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * If force_abort is set, we cancel everything.
876 */
877 did_emsg = FALSE;
878
879 /*
880 * KeyTyped is only set when calling vgetc(). Reset it here when not
881 * calling vgetc() (sourced command lines).
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (!(flags & DOCMD_KEYTYPED)
884 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100972 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cmd_cookie = cookie;
1006 }
1007#endif
1008
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001145 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001166 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
1298 && !(did_emsg && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001299 && (getline_equal(fgetline, cookie, getexmodeline)
1300 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 && (next_cmdline != NULL
1302#ifdef FEAT_EVAL
1303 || cstack.cs_idx >= 0
1304#endif
1305 || (flags & DOCMD_REPEAT)));
1306
1307 vim_free(cmdline_copy);
1308#ifdef FEAT_EVAL
1309 free_cmdlines(&lines_ga);
1310 ga_clear(&lines_ga);
1311
1312 if (cstack.cs_idx >= 0)
1313 {
1314 /*
1315 * If a sourced file or executed function ran to its end, report the
1316 * unclosed conditional.
1317 */
1318 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001319 && ((getline_equal(fgetline, cookie, getsourceline)
1320 && !source_finished(fgetline, cookie))
1321 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 && !func_has_ended(real_cookie))))
1323 {
1324 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1325 EMSG(_(e_endtry));
1326 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1327 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001328 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1329 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
1331 EMSG(_(e_endif));
1332 }
1333
1334 /*
1335 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1336 * ":endtry" in a sourced file or executed function. If the try
1337 * conditional is in its finally clause, ignore anything pending.
1338 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001339 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 */
1341 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001342 {
1343 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1344
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001345 if (idx >= 0)
1346 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1348 &cstack.cs_looplevel);
1349 }
1350 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 trylevel = initial_trylevel;
1352 }
1353
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001354 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1355 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001357 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 ? (char_u *)"endfunction" : (char_u *)NULL);
1359
1360 if (trylevel == 0)
1361 {
1362 /*
1363 * When an exception is being thrown out of the outermost try
1364 * conditional, discard the uncaught exception, disable the conversion
1365 * of interrupts or errors to exceptions, and ensure that no more
1366 * commands are executed.
1367 */
1368 if (did_throw)
1369 {
1370 void *p = NULL;
1371 char_u *saved_sourcing_name;
1372 int saved_sourcing_lnum;
1373 struct msglist *messages = NULL, *next;
1374
1375 /*
1376 * If the uncaught exception is a user exception, report it as an
1377 * error. If it is an error exception, display the saved error
1378 * message now. For an interrupt exception, do nothing; the
1379 * interrupt message is given elsewhere.
1380 */
1381 switch (current_exception->type)
1382 {
1383 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001384 vim_snprintf((char *)IObuff, IOSIZE,
1385 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 current_exception->value);
1387 p = vim_strsave(IObuff);
1388 break;
1389 case ET_ERROR:
1390 messages = current_exception->messages;
1391 current_exception->messages = NULL;
1392 break;
1393 case ET_INTERRUPT:
1394 break;
1395 default:
1396 p = vim_strsave((char_u *)_(e_internal));
1397 }
1398
1399 saved_sourcing_name = sourcing_name;
1400 saved_sourcing_lnum = sourcing_lnum;
1401 sourcing_name = current_exception->throw_name;
1402 sourcing_lnum = current_exception->throw_lnum;
1403 current_exception->throw_name = NULL;
1404
1405 discard_current_exception(); /* uses IObuff if 'verbose' */
1406 suppress_errthrow = TRUE;
1407 force_abort = TRUE;
1408
1409 if (messages != NULL)
1410 {
1411 do
1412 {
1413 next = messages->next;
1414 emsg(messages->msg);
1415 vim_free(messages->msg);
1416 vim_free(messages);
1417 messages = next;
1418 }
1419 while (messages != NULL);
1420 }
1421 else if (p != NULL)
1422 {
1423 emsg(p);
1424 vim_free(p);
1425 }
1426 vim_free(sourcing_name);
1427 sourcing_name = saved_sourcing_name;
1428 sourcing_lnum = saved_sourcing_lnum;
1429 }
1430
1431 /*
1432 * On an interrupt or an aborting error not converted to an exception,
1433 * disable the conversion of errors to exceptions. (Interrupts are not
1434 * converted any more, here.) This enables also the interrupt message
1435 * when force_abort is set and did_emsg unset in case of an interrupt
1436 * from a finally clause after an error.
1437 */
1438 else if (got_int || (did_emsg && force_abort))
1439 suppress_errthrow = TRUE;
1440 }
1441
1442 /*
1443 * The current cstack will be freed when do_cmdline() returns. An uncaught
1444 * exception will have to be rethrown in the previous cstack. If a function
1445 * has just returned or a script file was just finished and the previous
1446 * cstack belongs to the same function or, respectively, script file, it
1447 * will have to be checked for finally clauses to be executed due to the
1448 * ":return" or ":finish". This is done in do_one_cmd().
1449 */
1450 if (did_throw)
1451 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001452 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001454 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 && ex_nesting_level > func_level(real_cookie) + 1))
1456 {
1457 if (!did_throw)
1458 check_cstack = TRUE;
1459 }
1460 else
1461 {
1462 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 --ex_nesting_level;
1465 /*
1466 * Go to debug mode when returning from a function in which we are
1467 * single-stepping.
1468 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001469 if ((getline_equal(fgetline, cookie, getsourceline)
1470 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001472 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ? (char_u *)_("End of sourced file")
1474 : (char_u *)_("End of function"));
1475 }
1476
1477 /*
1478 * Restore the exception environment (done after returning from the
1479 * debugger).
1480 */
1481 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001482 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 msg_list = saved_msg_list;
1485#endif /* FEAT_EVAL */
1486
1487 /*
1488 * If there was too much output to fit on the command line, ask the user to
1489 * hit return before redrawing the screen. With the ":global" command we do
1490 * this only once after the command is finished.
1491 */
1492 if (did_inc)
1493 {
1494 --RedrawingDisabled;
1495 --no_wait_return;
1496 msg_scroll = FALSE;
1497
1498 /*
1499 * When just finished an ":if"-":else" which was typed, no need to
1500 * wait for hit-return. Also for an error situation.
1501 */
1502 if (retval == FAIL
1503#ifdef FEAT_EVAL
1504 || (did_endif && KeyTyped && !did_emsg)
1505#endif
1506 )
1507 {
1508 need_wait_return = FALSE;
1509 msg_didany = FALSE; /* don't wait when restarting edit */
1510 }
1511 else if (need_wait_return)
1512 {
1513 /*
1514 * The msg_start() above clears msg_didout. The wait_return we do
1515 * here should not overwrite the command that may be shown before
1516 * doing that.
1517 */
1518 msg_didout |= msg_didout_before_start;
1519 wait_return(FALSE);
1520 }
1521 }
1522
1523#ifndef FEAT_EVAL
1524 /*
1525 * Reset if_level, in case a sourced script file contains more ":if" than
1526 * ":endif" (could be ":if x | foo | endif").
1527 */
1528 if_level = 0;
1529#endif
1530
1531 --call_depth;
1532 return retval;
1533}
1534
1535#ifdef FEAT_EVAL
1536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 */
1539 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int c;
1542 void *cookie;
1543 int indent;
1544{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wcmd_T *wp;
1547 char_u *line;
1548
1549 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1550 {
1551 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (cp->getline == NULL)
1556 line = getcmdline(c, 0L, indent);
1557 else
1558 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001559 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 ++cp->current_line;
1561
1562 return line;
1563 }
1564
1565 KeyTyped = FALSE;
1566 ++cp->current_line;
1567 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1568 sourcing_lnum = wp->lnum;
1569 return vim_strsave(wp->line);
1570}
1571
1572/*
1573 * Store a line in "gap" so that a ":while" loop can execute it again.
1574 */
1575 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 garray_T *gap;
1578 char_u *line;
1579{
1580 if (ga_grow(gap, 1) == FAIL)
1581 return FAIL;
1582 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1584 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return OK;
1586}
1587
1588/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 static void
1592free_cmdlines(gap)
1593 garray_T *gap;
1594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608getline_equal(fgetline, cookie, func)
1609 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001610 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 char_u *(*func) __ARGS((int, void *, int));
1612{
1613#ifdef FEAT_EVAL
1614 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001640 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
1644 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
1664/*
1665 * Execute one Ex command.
1666 *
1667 * If 'sourcing' is TRUE, the command will be included in the error message.
1668 *
1669 * 1. skip comment lines and leading space
1670 * 2. handle command modifiers
1671 * 3. parse range
1672 * 4. parse command
1673 * 5. parse arguments
1674 * 6. switch on command name
1675 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001676 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 *
1678 * This function may be called recursively!
1679 */
1680#if (_MSC_VER == 1200)
1681/*
Bram Moolenaared203462004-06-16 11:19:22 +00001682 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001684 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 static char_u *
1687do_one_cmd(cmdlinep, sourcing,
1688#ifdef FEAT_EVAL
1689 cstack,
1690#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 char_u **cmdlinep;
1693 int sourcing;
1694#ifdef FEAT_EVAL
1695 struct condstack *cstack;
1696#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001697 char_u *(*fgetline) __ARGS((int, void *, int));
1698 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char_u *errormsg = NULL; /* error message */
1704 exarg_T ea; /* Ex command arguments */
1705 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001706 int save_msg_scroll = msg_scroll;
1707 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001709#ifdef HAVE_SANDBOX
1710 int did_sandbox = FALSE;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 cmdmod_T save_cmdmod;
1713 int ni; /* set when Not Implemented */
1714
1715 vim_memset(&ea, 0, sizeof(ea));
1716 ea.line1 = 1;
1717 ea.line2 = 1;
1718#ifdef FEAT_EVAL
1719 ++ex_nesting_level;
1720#endif
1721
1722 /* when not editing the last file :q has to be typed twice */
1723 if (quitmore
1724#ifdef FEAT_EVAL
1725 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001726 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 )
1729 --quitmore;
1730
1731 /*
1732 * Reset browse, confirm, etc.. They are restored when returning, for
1733 * recursive calls.
1734 */
1735 save_cmdmod = cmdmod;
1736 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1737
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001738 /* "#!anything" is handled like a comment. */
1739 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1740 goto doend;
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * Repeat until no more command modifiers are found.
1744 */
1745 ea.cmd = *cmdlinep;
1746 for (;;)
1747 {
1748/*
1749 * 1. skip comment lines and leading white space and colons
1750 */
1751 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1752 ++ea.cmd;
1753
1754 /* in ex mode, an empty line works like :+ */
1755 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && (getline_equal(fgetline, cookie, getexmodeline)
1757 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 ea.cmd = (char_u *)"+";
1761 ex_pressedreturn = TRUE;
1762 }
1763
1764 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001765 if (*ea.cmd == '"')
1766 goto doend;
1767 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001768 {
1769 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773/*
1774 * 2. handle command modifiers.
1775 */
1776 p = ea.cmd;
1777 if (VIM_ISDIGIT(*ea.cmd))
1778 p = skipwhite(skipdigits(ea.cmd));
1779 switch (*p)
1780 {
1781 /* When adding an entry, also modify cmd_exists(). */
1782 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1783 break;
1784#ifdef FEAT_WINDOWS
1785 cmdmod.split |= WSP_ABOVE;
1786#endif
1787 continue;
1788
1789 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1790 {
1791#ifdef FEAT_WINDOWS
1792 cmdmod.split |= WSP_BELOW;
1793#endif
1794 continue;
1795 }
1796 if (checkforcmd(&ea.cmd, "browse", 3))
1797 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001798#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 cmdmod.browse = TRUE;
1800#endif
1801 continue;
1802 }
1803 if (!checkforcmd(&ea.cmd, "botright", 2))
1804 break;
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BOT;
1807#endif
1808 continue;
1809
1810 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1811 break;
1812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1813 cmdmod.confirm = TRUE;
1814#endif
1815 continue;
1816
1817 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1818 {
1819 cmdmod.keepmarks = TRUE;
1820 continue;
1821 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001822 if (checkforcmd(&ea.cmd, "keepalt", 5))
1823 {
1824 cmdmod.keepalt = TRUE;
1825 continue;
1826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1828 break;
1829 cmdmod.keepjumps = TRUE;
1830 continue;
1831
1832 /* ":hide" and ":hide | cmd" are not modifiers */
1833 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1834 || *p == NUL || ends_excmd(*p))
1835 break;
1836 ea.cmd = p;
1837 cmdmod.hide = TRUE;
1838 continue;
1839
1840 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1841 {
1842 cmdmod.lockmarks = TRUE;
1843 continue;
1844 }
1845
1846 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1847 break;
1848#ifdef FEAT_WINDOWS
1849 cmdmod.split |= WSP_ABOVE;
1850#endif
1851 continue;
1852
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001853 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1854 break;
1855#ifdef FEAT_AUTOCMD
1856 if (cmdmod.save_ei == NULL)
1857 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001858 /* Set 'eventignore' to "all". Restore the
1859 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001860 cmdmod.save_ei = vim_strsave(p_ei);
1861 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001862 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001863 }
1864#endif
1865 continue;
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1868 break;
1869#ifdef FEAT_WINDOWS
1870 cmdmod.split |= WSP_BELOW;
1871#endif
1872 continue;
1873
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001874 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1875 {
1876#ifdef HAVE_SANDBOX
1877 if (!did_sandbox)
1878 ++sandbox;
1879 did_sandbox = TRUE;
1880#endif
1881 continue;
1882 }
1883 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001885 if (save_msg_silent == -1)
1886 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1889 {
1890 /* ":silent!", but not "silent !cmd" */
1891 ea.cmd = skipwhite(ea.cmd + 1);
1892 ++emsg_silent;
1893 ++did_esilent;
1894 }
1895 continue;
1896
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001897 case 't': if (checkforcmd(&p, "tab", 3))
1898 {
1899#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001900 if (vim_isdigit(*ea.cmd))
1901 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1902 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001903 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001904 ea.cmd = p;
1905#endif
1906 continue;
1907 }
1908 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 break;
1910#ifdef FEAT_WINDOWS
1911 cmdmod.split |= WSP_TOP;
1912#endif
1913 continue;
1914
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001915 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1916 break;
1917 if (save_msg_silent == -1)
1918 save_msg_silent = msg_silent;
1919 msg_silent = 0;
1920 continue;
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 cmdmod.split |= WSP_VERT;
1926#endif
1927 continue;
1928 }
1929 if (!checkforcmd(&p, "verbose", 4))
1930 break;
1931 if (verbose_save < 0)
1932 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001933 if (vim_isdigit(*ea.cmd))
1934 p_verbose = atoi((char *)ea.cmd);
1935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 p_verbose = 1;
1937 ea.cmd = p;
1938 continue;
1939 }
1940 break;
1941 }
1942
1943#ifdef FEAT_EVAL
1944 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1945 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1946#else
1947 ea.skip = (if_level > 0);
1948#endif
1949
1950#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001951# ifdef FEAT_PROFILE
1952 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001953 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001955 if (getline_equal(fgetline, cookie, get_func_line))
1956 func_line_exec(getline_cookie(fgetline, cookie));
1957 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001958 script_line_exec();
1959 }
1960#endif
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 /* May go to debug mode. If this happens and the ">quit" debug command is
1963 * used, throw an interrupt exception and skip the next command. */
1964 dbg_check_breakpoint(&ea);
1965 if (!ea.skip && got_int)
1966 {
1967 ea.skip = TRUE;
1968 (void)do_intthrow(cstack);
1969 }
1970#endif
1971
1972/*
1973 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1974 *
1975 * where 'addr' is:
1976 *
1977 * % (entire file)
1978 * $ [+-NUM]
1979 * 'x [+-NUM] (where x denotes a currently defined mark)
1980 * . [+-NUM]
1981 * [+-NUM]..
1982 * NUM
1983 *
1984 * The ea.cmd pointer is updated to point to the first character following the
1985 * range spec. If an initial address is found, but no second, the upper bound
1986 * is equal to the lower.
1987 */
1988
1989 /* repeat for all ',' or ';' separated addresses */
1990 for (;;)
1991 {
1992 ea.line1 = ea.line2;
1993 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1994 ea.cmd = skipwhite(ea.cmd);
1995 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1996 if (ea.cmd == NULL) /* error detected */
1997 goto doend;
1998 if (lnum == MAXLNUM)
1999 {
2000 if (*ea.cmd == '%') /* '%' - all lines */
2001 {
2002 ++ea.cmd;
2003 ea.line1 = 1;
2004 ea.line2 = curbuf->b_ml.ml_line_count;
2005 ++ea.addr_count;
2006 }
2007 /* '*' - visual area */
2008 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2009 {
2010 pos_T *fp;
2011
2012 ++ea.cmd;
2013 if (!ea.skip)
2014 {
2015 fp = getmark('<', FALSE);
2016 if (check_mark(fp) == FAIL)
2017 goto doend;
2018 ea.line1 = fp->lnum;
2019 fp = getmark('>', FALSE);
2020 if (check_mark(fp) == FAIL)
2021 goto doend;
2022 ea.line2 = fp->lnum;
2023 ++ea.addr_count;
2024 }
2025 }
2026 }
2027 else
2028 ea.line2 = lnum;
2029 ea.addr_count++;
2030
2031 if (*ea.cmd == ';')
2032 {
2033 if (!ea.skip)
2034 curwin->w_cursor.lnum = ea.line2;
2035 }
2036 else if (*ea.cmd != ',')
2037 break;
2038 ++ea.cmd;
2039 }
2040
2041 /* One address given: set start and end lines */
2042 if (ea.addr_count == 1)
2043 {
2044 ea.line1 = ea.line2;
2045 /* ... but only implicit: really no address given */
2046 if (lnum == MAXLNUM)
2047 ea.addr_count = 0;
2048 }
2049
2050 /* Don't leave the cursor on an illegal line (caused by ';') */
2051 check_cursor_lnum();
2052
2053/*
2054 * 4. parse command
2055 */
2056
2057 /*
2058 * Skip ':' and any white space
2059 */
2060 ea.cmd = skipwhite(ea.cmd);
2061 while (*ea.cmd == ':')
2062 ea.cmd = skipwhite(ea.cmd + 1);
2063
2064 /*
2065 * If we got a line, but no command, then go to the line.
2066 * If we find a '|' or '\n' we set ea.nextcmd.
2067 */
2068 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2069 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2070 {
2071 /*
2072 * strange vi behaviour:
2073 * ":3" jumps to line 3
2074 * ":3|..." prints line 3
2075 * ":|" prints current line
2076 */
2077 if (ea.skip) /* skip this if inside :if */
2078 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002079 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 ea.cmdidx = CMD_print;
2082 ea.argt = RANGE+COUNT+TRLBAR;
2083 if ((errormsg = invalid_range(&ea)) == NULL)
2084 {
2085 correct_range(&ea);
2086 ex_print(&ea);
2087 }
2088 }
2089 else if (ea.addr_count != 0)
2090 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002091 if (ea.line2 > curbuf->b_ml.ml_line_count)
2092 {
2093 /* With '-' in 'cpoptions' a line number past the file is an
2094 * error, otherwise put it at the end of the file. */
2095 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2096 ea.line2 = -1;
2097 else
2098 ea.line2 = curbuf->b_ml.ml_line_count;
2099 }
2100
2101 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002102 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 {
2105 if (ea.line2 == 0)
2106 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 curwin->w_cursor.lnum = ea.line2;
2109 beginline(BL_SOL | BL_FIX);
2110 }
2111 }
2112 goto doend;
2113 }
2114
2115 /* Find the command and let "p" point to after it. */
2116 p = find_command(&ea, NULL);
2117
2118#ifdef FEAT_USR_CMDS
2119 if (p == NULL)
2120 {
2121 if (!ea.skip)
2122 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2123 goto doend;
2124 }
2125 /* Check for wrong commands. */
2126 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2127 {
2128 errormsg = uc_fun_cmd();
2129 goto doend;
2130 }
2131#endif
2132 if (ea.cmdidx == CMD_SIZE)
2133 {
2134 if (!ea.skip)
2135 {
2136 STRCPY(IObuff, _("E492: Not an editor command"));
2137 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002138 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 errormsg = IObuff;
2140 }
2141 goto doend;
2142 }
2143
2144 ni = (
2145#ifdef FEAT_USR_CMDS
2146 !USER_CMDIDX(ea.cmdidx) &&
2147#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002148 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002149#ifdef HAVE_EX_SCRIPT_NI
2150 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2151#endif
2152 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154#ifndef FEAT_EVAL
2155 /*
2156 * When the expression evaluation is disabled, recognize the ":if" and
2157 * ":endif" commands and ignore everything in between it.
2158 */
2159 if (ea.cmdidx == CMD_if)
2160 ++if_level;
2161 if (if_level)
2162 {
2163 if (ea.cmdidx == CMD_endif)
2164 --if_level;
2165 goto doend;
2166 }
2167
2168#endif
2169
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002170 /* forced commands */
2171 if (*p == '!' && ea.cmdidx != CMD_substitute
2172 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174 ++p;
2175 ea.forceit = TRUE;
2176 }
2177 else
2178 ea.forceit = FALSE;
2179
2180/*
2181 * 5. parse arguments
2182 */
2183#ifdef FEAT_USR_CMDS
2184 if (!USER_CMDIDX(ea.cmdidx))
2185#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 if (!ea.skip)
2189 {
2190#ifdef HAVE_SANDBOX
2191 if (sandbox != 0 && !(ea.argt & SBOXOK))
2192 {
2193 /* Command not allowed in sandbox. */
2194 errormsg = (char_u *)_(e_sandbox);
2195 goto doend;
2196 }
2197#endif
2198 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2199 {
2200 /* Command not allowed in non-'modifiable' buffer */
2201 errormsg = (char_u *)_(e_modifiable);
2202 goto doend;
2203 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002204
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002205 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206# ifdef FEAT_USR_CMDS
2207 && !USER_CMDIDX(ea.cmdidx)
2208# endif
2209 )
2210 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002211 /* Command not allowed when editing the command line. */
2212#ifdef FEAT_CMDWIN
2213 if (cmdwin_type != 0)
2214 errormsg = (char_u *)_(e_cmdwin);
2215 else
2216#endif
2217 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 goto doend;
2219 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002221 /* Disallow editing another buffer when "curbuf_lock" is set.
2222 * Do allow ":edit" (check for argument later).
2223 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002225 && ea.cmdidx != CMD_edit
2226 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002227# ifdef FEAT_USR_CMDS
2228 && !USER_CMDIDX(ea.cmdidx)
2229# endif
2230 && curbuf_locked())
2231 goto doend;
2232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2235 {
2236 /* no range allowed */
2237 errormsg = (char_u *)_(e_norange);
2238 goto doend;
2239 }
2240 }
2241
2242 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2243 {
2244 errormsg = (char_u *)_(e_nobang);
2245 goto doend;
2246 }
2247
2248 /*
2249 * Don't complain about the range if it is not used
2250 * (could happen if line_count is accidentally set to 0).
2251 */
2252 if (!ea.skip && !ni)
2253 {
2254 /*
2255 * If the range is backwards, ask for confirmation and, if given, swap
2256 * ea.line1 & ea.line2 so it's forwards again.
2257 * When global command is busy, don't ask, will fail below.
2258 */
2259 if (!global_busy && ea.line1 > ea.line2)
2260 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002261 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002263 if (sourcing || exmode_active)
2264 {
2265 errormsg = (char_u *)_("E493: Backwards range given");
2266 goto doend;
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (ask_yesno((char_u *)
2269 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002270 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 lnum = ea.line1;
2273 ea.line1 = ea.line2;
2274 ea.line2 = lnum;
2275 }
2276 if ((errormsg = invalid_range(&ea)) != NULL)
2277 goto doend;
2278 }
2279
2280 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2281 ea.line2 = 1;
2282
2283 correct_range(&ea);
2284
2285#ifdef FEAT_FOLDING
2286 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2287 {
2288 /* Put the first line at the start of a closed fold, put the last line
2289 * at the end of a closed fold. */
2290 (void)hasFolding(ea.line1, &ea.line1, NULL);
2291 (void)hasFolding(ea.line2, NULL, &ea.line2);
2292 }
2293#endif
2294
2295#ifdef FEAT_QUICKFIX
2296 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002297 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * option here, so things like % get expanded.
2299 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002300 p = replace_makeprg(&ea, p, cmdlinep);
2301 if (p == NULL)
2302 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304
2305 /*
2306 * Skip to start of argument.
2307 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2308 */
2309 if (ea.cmdidx == CMD_bang)
2310 ea.arg = p;
2311 else
2312 ea.arg = skipwhite(p);
2313
2314 /*
2315 * Check for "++opt=val" argument.
2316 * Must be first, allow ":w ++enc=utf8 !cmd"
2317 */
2318 if (ea.argt & ARGOPT)
2319 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2320 if (getargopt(&ea) == FAIL && !ni)
2321 {
2322 errormsg = (char_u *)_(e_invarg);
2323 goto doend;
2324 }
2325
2326 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2327 {
2328 if (*ea.arg == '>') /* append */
2329 {
2330 if (*++ea.arg != '>') /* typed wrong */
2331 {
2332 errormsg = (char_u *)_("E494: Use w or w>>");
2333 goto doend;
2334 }
2335 ea.arg = skipwhite(ea.arg + 1);
2336 ea.append = TRUE;
2337 }
2338 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2339 {
2340 ++ea.arg;
2341 ea.usefilter = TRUE;
2342 }
2343 }
2344
2345 if (ea.cmdidx == CMD_read)
2346 {
2347 if (ea.forceit)
2348 {
2349 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2350 ea.forceit = FALSE;
2351 }
2352 else if (*ea.arg == '!') /* :r !filter */
2353 {
2354 ++ea.arg;
2355 ea.usefilter = TRUE;
2356 }
2357 }
2358
2359 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2360 {
2361 ea.amount = 1;
2362 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2363 {
2364 ++ea.arg;
2365 ++ea.amount;
2366 }
2367 ea.arg = skipwhite(ea.arg);
2368 }
2369
2370 /*
2371 * Check for "+command" argument, before checking for next command.
2372 * Don't do this for ":read !cmd" and ":write !cmd".
2373 */
2374 if ((ea.argt & EDITCMD) && !ea.usefilter)
2375 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2376
2377 /*
2378 * Check for '|' to separate commands and '"' to start comments.
2379 * Don't do this for ":read !cmd" and ":write !cmd".
2380 */
2381 if ((ea.argt & TRLBAR) && !ea.usefilter)
2382 separate_nextcmd(&ea);
2383
2384 /*
2385 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002386 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2387 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389 else if (ea.cmdidx == CMD_bang
2390 || ea.cmdidx == CMD_global
2391 || ea.cmdidx == CMD_vglobal
2392 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 for (p = ea.arg; *p; ++p)
2395 {
2396 /* Remove one backslash before a newline, so that it's possible to
2397 * pass a newline to the shell and also a newline that is preceded
2398 * with a backslash. This makes it impossible to end a shell
2399 * command in a backslash, but that doesn't appear useful.
2400 * Halving the number of backslashes is incompatible with previous
2401 * versions. */
2402 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002403 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 else if (*p == '\n')
2405 {
2406 ea.nextcmd = p + 1;
2407 *p = NUL;
2408 break;
2409 }
2410 }
2411 }
2412
2413 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2414 {
2415 ea.line1 = 1;
2416 ea.line2 = curbuf->b_ml.ml_line_count;
2417 }
2418
2419 /* accept numbered register only when no count allowed (:put) */
2420 if ( (ea.argt & REGSTR)
2421 && *ea.arg != NUL
2422#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 /* Do not allow register = for user commands */
2424 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#endif
2426 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2427 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002428#ifndef FEAT_CLIPBOARD
2429 /* check these explicitly for a more specific error message */
2430 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002432 errormsg = (char_u *)_(e_invalidreg);
2433 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002436 if (
2437#ifdef FEAT_USR_CMDS
2438 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2439 && USER_CMDIDX(ea.cmdidx)))
2440#else
2441 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2442#endif
2443 )
2444 {
2445 ea.regname = *ea.arg++;
2446#ifdef FEAT_EVAL
2447 /* for '=' register: accept the rest of the line as an expression */
2448 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2449 {
2450 set_expr_line(vim_strsave(ea.arg));
2451 ea.arg += STRLEN(ea.arg);
2452 }
2453#endif
2454 ea.arg = skipwhite(ea.arg);
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457
2458 /*
2459 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2460 * count, it's a buffer name.
2461 */
2462 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2463 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2464 || vim_iswhite(*p)))
2465 {
2466 n = getdigits(&ea.arg);
2467 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002468 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 errormsg = (char_u *)_(e_zerocount);
2471 goto doend;
2472 }
2473 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2474 {
2475 ea.line2 = n;
2476 if (ea.addr_count == 0)
2477 ea.addr_count = 1;
2478 }
2479 else
2480 {
2481 ea.line1 = ea.line2;
2482 ea.line2 += n - 1;
2483 ++ea.addr_count;
2484 /*
2485 * Be vi compatible: no error message for out of range.
2486 */
2487 if (ea.line2 > curbuf->b_ml.ml_line_count)
2488 ea.line2 = curbuf->b_ml.ml_line_count;
2489 }
2490 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002491
2492 /*
2493 * Check for flags: 'l', 'p' and '#'.
2494 */
2495 if (ea.argt & EXFLAGS)
2496 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002498 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002499 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 errormsg = (char_u *)_(e_trailing);
2502 goto doend;
2503 }
2504
2505 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2506 {
2507 errormsg = (char_u *)_(e_argreq);
2508 goto doend;
2509 }
2510
2511#ifdef FEAT_EVAL
2512 /*
2513 * Skip the command when it's not going to be executed.
2514 * The commands like :if, :endif, etc. always need to be executed.
2515 * Also make an exception for commands that handle a trailing command
2516 * themselves.
2517 */
2518 if (ea.skip)
2519 {
2520 switch (ea.cmdidx)
2521 {
2522 /* commands that need evaluation */
2523 case CMD_while:
2524 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002525 case CMD_for:
2526 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 case CMD_if:
2528 case CMD_elseif:
2529 case CMD_else:
2530 case CMD_endif:
2531 case CMD_try:
2532 case CMD_catch:
2533 case CMD_finally:
2534 case CMD_endtry:
2535 case CMD_function:
2536 break;
2537
2538 /* Commands that handle '|' themselves. Check: A command should
2539 * either have the TRLBAR flag, appear in this list or appear in
2540 * the list at ":help :bar". */
2541 case CMD_aboveleft:
2542 case CMD_and:
2543 case CMD_belowright:
2544 case CMD_botright:
2545 case CMD_browse:
2546 case CMD_call:
2547 case CMD_confirm:
2548 case CMD_delfunction:
2549 case CMD_djump:
2550 case CMD_dlist:
2551 case CMD_dsearch:
2552 case CMD_dsplit:
2553 case CMD_echo:
2554 case CMD_echoerr:
2555 case CMD_echomsg:
2556 case CMD_echon:
2557 case CMD_execute:
2558 case CMD_help:
2559 case CMD_hide:
2560 case CMD_ijump:
2561 case CMD_ilist:
2562 case CMD_isearch:
2563 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002564 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 case CMD_keepjumps:
2566 case CMD_keepmarks:
2567 case CMD_leftabove:
2568 case CMD_let:
2569 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002570 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 case CMD_perl:
2574 case CMD_psearch:
2575 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002576 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002577 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 case CMD_return:
2579 case CMD_rightbelow:
2580 case CMD_ruby:
2581 case CMD_silent:
2582 case CMD_smagic:
2583 case CMD_snomagic:
2584 case CMD_substitute:
2585 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002586 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 case CMD_tcl:
2588 case CMD_throw:
2589 case CMD_tilde:
2590 case CMD_topleft:
2591 case CMD_unlet:
2592 case CMD_verbose:
2593 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002594 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
2596
2597 default: goto doend;
2598 }
2599 }
2600#endif
2601
2602 if (ea.argt & XFILE)
2603 {
2604 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2605 goto doend;
2606 }
2607
2608#ifdef FEAT_LISTCMDS
2609 /*
2610 * Accept buffer name. Cannot be used at the same time with a buffer
2611 * number. Don't do this for a user command.
2612 */
2613 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2614# ifdef FEAT_USR_CMDS
2615 && !USER_CMDIDX(ea.cmdidx)
2616# endif
2617 )
2618 {
2619 /*
2620 * :bdelete, :bwipeout and :bunload take several arguments, separated
2621 * by spaces: find next space (skipping over escaped characters).
2622 * The others take one argument: ignore trailing spaces.
2623 */
2624 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2625 || ea.cmdidx == CMD_bunload)
2626 p = skiptowhite_esc(ea.arg);
2627 else
2628 {
2629 p = ea.arg + STRLEN(ea.arg);
2630 while (p > ea.arg && vim_iswhite(p[-1]))
2631 --p;
2632 }
2633 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2634 if (ea.line2 < 0) /* failed */
2635 goto doend;
2636 ea.addr_count = 1;
2637 ea.arg = skipwhite(p);
2638 }
2639#endif
2640
2641/*
2642 * 6. switch on command name
2643 *
2644 * The "ea" structure holds the arguments that can be used.
2645 */
2646 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002647 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 ea.cookie = cookie;
2649#ifdef FEAT_EVAL
2650 ea.cstack = cstack;
2651#endif
2652
2653#ifdef FEAT_USR_CMDS
2654 if (USER_CMDIDX(ea.cmdidx))
2655 {
2656 /*
2657 * Execute a user-defined command.
2658 */
2659 do_ucmd(&ea);
2660 }
2661 else
2662#endif
2663 {
2664 /*
2665 * Call the function to execute the command.
2666 */
2667 ea.errmsg = NULL;
2668 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2669 if (ea.errmsg != NULL)
2670 errormsg = (char_u *)_(ea.errmsg);
2671 }
2672
2673#ifdef FEAT_EVAL
2674 /*
2675 * If the command just executed called do_cmdline(), any throw or ":return"
2676 * or ":finish" encountered there must also check the cstack of the still
2677 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2678 * exception, or reanimate a returned function or finished script file and
2679 * return or finish it again.
2680 */
2681 if (need_rethrow)
2682 do_throw(cstack);
2683 else if (check_cstack)
2684 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002685 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 && current_func_returned())
2689 do_return(&ea, TRUE, FALSE, NULL);
2690 }
2691 need_rethrow = check_cstack = FALSE;
2692#endif
2693
2694doend:
2695 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2696 curwin->w_cursor.lnum = 1;
2697
2698 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2699 {
2700 if (sourcing)
2701 {
2702 if (errormsg != IObuff)
2703 {
2704 STRCPY(IObuff, errormsg);
2705 errormsg = IObuff;
2706 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002707 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 emsg(errormsg);
2710 }
2711#ifdef FEAT_EVAL
2712 do_errthrow(cstack,
2713 (ea.cmdidx != CMD_SIZE
2714# ifdef FEAT_USR_CMDS
2715 && !USER_CMDIDX(ea.cmdidx)
2716# endif
2717 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2718#endif
2719
2720 if (verbose_save >= 0)
2721 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002722#ifdef FEAT_AUTOCMD
2723 if (cmdmod.save_ei != NULL)
2724 {
2725 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002726 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2727 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002728 free_string_option(cmdmod.save_ei);
2729 }
2730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 cmdmod = save_cmdmod;
2733
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002734 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
2736 /* messages could be enabled for a serious error, need to check if the
2737 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002738 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002739 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 emsg_silent -= did_esilent;
2741 if (emsg_silent < 0)
2742 emsg_silent = 0;
2743 /* Restore msg_scroll, it's set by file I/O commands, even when no
2744 * message is actually displayed. */
2745 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002746
2747 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2748 * somewhere in the line. Put it back in the first column. */
2749 if (redirecting())
2750 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002753#ifdef HAVE_SANDBOX
2754 if (did_sandbox)
2755 --sandbox;
2756#endif
2757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2759 ea.nextcmd = NULL;
2760
2761#ifdef FEAT_EVAL
2762 --ex_nesting_level;
2763#endif
2764
2765 return ea.nextcmd;
2766}
2767#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002768 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#endif
2770
2771/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002772 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 * If there is a match advance "pp" to the argument and return TRUE.
2774 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002775 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002777 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 char *cmd; /* name of command */
2779 int len; /* required length */
2780{
2781 int i;
2782
2783 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002784 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 break;
2786 if (i >= len && !isalpha((*pp)[i]))
2787 {
2788 *pp = skipwhite(*pp + i);
2789 return TRUE;
2790 }
2791 return FALSE;
2792}
2793
2794/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002795 * Append "cmd" to the error message in IObuff.
2796 * Takes care of limiting the length and handling 0xa0, which would be
2797 * invisible otherwise.
2798 */
2799 static void
2800append_command(cmd)
2801 char_u *cmd;
2802{
2803 char_u *s = cmd;
2804 char_u *d;
2805
2806 STRCAT(IObuff, ": ");
2807 d = IObuff + STRLEN(IObuff);
2808 while (*s != NUL && d - IObuff < IOSIZE - 7)
2809 {
2810 if (
2811#ifdef FEAT_MBYTE
2812 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2813#endif
2814 *s == 0xa0)
2815 {
2816 s +=
2817#ifdef FEAT_MBYTE
2818 enc_utf8 ? 2 :
2819#endif
2820 1;
2821 STRCPY(d, "<a0>");
2822 d += 4;
2823 }
2824 else
2825 MB_COPY_CHAR(s, d);
2826 }
2827 *d = NUL;
2828}
2829
2830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002832 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002834 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 * Returns NULL for an ambiguous user command.
2836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 static char_u *
2838find_command(eap, full)
2839 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002840 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
2842 int len;
2843 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002844 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
2846 /*
2847 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002848 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * - the 'k' command can directly be followed by any character.
2850 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2851 * but :sre[wind] is another command, as are :scrip[tnames],
2852 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002853 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 */
2855 p = eap->cmd;
2856 if (*p == 'k')
2857 {
2858 eap->cmdidx = CMD_k;
2859 ++p;
2860 }
2861 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002862 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2863 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 || p[1] == 'g'
2865 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2866 || p[1] == 'I'
2867 || (p[1] == 'r' && p[2] != 'e')))
2868 {
2869 eap->cmdidx = CMD_substitute;
2870 ++p;
2871 }
2872 else
2873 {
2874 while (ASCII_ISALPHA(*p))
2875 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002876 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002877 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002878 while (ASCII_ISALNUM(*p))
2879 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 /* check for non-alpha command */
2882 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2883 ++p;
2884 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002885 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2886 {
2887 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2888 * :delete with the 'l' flag. Same for 'p'. */
2889 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002890 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002891 break;
2892 if (i == len - 1)
2893 {
2894 --len;
2895 if (p[-1] == 'l')
2896 eap->flags |= EXFLAG_LIST;
2897 else
2898 eap->flags |= EXFLAG_PRINT;
2899 }
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
2902 if (ASCII_ISLOWER(*eap->cmd))
2903 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2904 else
2905 eap->cmdidx = cmdidxs[26];
2906
2907 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2908 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2909 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2910 (size_t)len) == 0)
2911 {
2912#ifdef FEAT_EVAL
2913 if (full != NULL
2914 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2915 *full = TRUE;
2916#endif
2917 break;
2918 }
2919
2920#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002921 /* Look for a user defined command as a last resort. Let ":Print" be
2922 * overruled by a user defined command. */
2923 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2924 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002926 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 while (ASCII_ISALNUM(*p))
2928 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002929 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002932 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 eap->cmdidx = CMD_SIZE;
2934 }
2935
2936 return p;
2937}
2938
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002939#ifdef FEAT_USR_CMDS
2940/*
2941 * Search for a user command that matches "eap->cmd".
2942 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2943 * Return a pointer to just after the command.
2944 * Return NULL if there is no matching command.
2945 */
2946 static char_u *
2947find_ucmd(eap, p, full, xp, compl)
2948 exarg_T *eap;
2949 char_u *p; /* end of the command (possibly including count) */
2950 int *full; /* set to TRUE for a full match */
2951 expand_T *xp; /* used for completion, NULL otherwise */
2952 int *compl; /* completion flags or NULL */
2953{
2954 int len = (int)(p - eap->cmd);
2955 int j, k, matchlen = 0;
2956 ucmd_T *uc;
2957 int found = FALSE;
2958 int possible = FALSE;
2959 char_u *cp, *np; /* Point into typed cmd and test name */
2960 garray_T *gap;
2961 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2962 only full match global is accepted. */
2963
2964 /*
2965 * Look for buffer-local user commands first, then global ones.
2966 */
2967 gap = &curbuf->b_ucmds;
2968 for (;;)
2969 {
2970 for (j = 0; j < gap->ga_len; ++j)
2971 {
2972 uc = USER_CMD_GA(gap, j);
2973 cp = eap->cmd;
2974 np = uc->uc_name;
2975 k = 0;
2976 while (k < len && *np != NUL && *cp++ == *np++)
2977 k++;
2978 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2979 {
2980 /* If finding a second match, the command is ambiguous. But
2981 * not if a buffer-local command wasn't a full match and a
2982 * global command is a full match. */
2983 if (k == len && found && *np != NUL)
2984 {
2985 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002986 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002987 amb_local = TRUE;
2988 }
2989
2990 if (!found || (k == len && *np == NUL))
2991 {
2992 /* If we matched up to a digit, then there could
2993 * be another command including the digit that we
2994 * should use instead.
2995 */
2996 if (k == len)
2997 found = TRUE;
2998 else
2999 possible = TRUE;
3000
3001 if (gap == &ucmds)
3002 eap->cmdidx = CMD_USER;
3003 else
3004 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003005 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003006 eap->useridx = j;
3007
3008# ifdef FEAT_CMDL_COMPL
3009 if (compl != NULL)
3010 *compl = uc->uc_compl;
3011# ifdef FEAT_EVAL
3012 if (xp != NULL)
3013 {
3014 xp->xp_arg = uc->uc_compl_arg;
3015 xp->xp_scriptID = uc->uc_scriptID;
3016 }
3017# endif
3018# endif
3019 /* Do not search for further abbreviations
3020 * if this is an exact match. */
3021 matchlen = k;
3022 if (k == len && *np == NUL)
3023 {
3024 if (full != NULL)
3025 *full = TRUE;
3026 amb_local = FALSE;
3027 break;
3028 }
3029 }
3030 }
3031 }
3032
3033 /* Stop if we found a full match or searched all. */
3034 if (j < gap->ga_len || gap == &ucmds)
3035 break;
3036 gap = &ucmds;
3037 }
3038
3039 /* Only found ambiguous matches. */
3040 if (amb_local)
3041 {
3042 if (xp != NULL)
3043 xp->xp_context = EXPAND_UNSUCCESSFUL;
3044 return NULL;
3045 }
3046
3047 /* The match we found may be followed immediately by a number. Move "p"
3048 * back to point to it. */
3049 if (found || possible)
3050 return p + (matchlen - len);
3051 return p;
3052}
3053#endif
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003056static struct cmdmod
3057{
3058 char *name;
3059 int minlen;
3060 int has_count; /* :123verbose :3tab */
3061} cmdmods[] = {
3062 {"aboveleft", 3, FALSE},
3063 {"belowright", 3, FALSE},
3064 {"botright", 2, FALSE},
3065 {"browse", 3, FALSE},
3066 {"confirm", 4, FALSE},
3067 {"hide", 3, FALSE},
3068 {"keepalt", 5, FALSE},
3069 {"keepjumps", 5, FALSE},
3070 {"keepmarks", 3, FALSE},
3071 {"leftabove", 5, FALSE},
3072 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003073 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003074 {"rightbelow", 6, FALSE},
3075 {"sandbox", 3, FALSE},
3076 {"silent", 3, FALSE},
3077 {"tab", 3, TRUE},
3078 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003079 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003080 {"verbose", 4, TRUE},
3081 {"vertical", 4, FALSE},
3082};
3083
3084/*
3085 * Return length of a command modifier (including optional count).
3086 * Return zero when it's not a modifier.
3087 */
3088 int
3089modifier_len(cmd)
3090 char_u *cmd;
3091{
3092 int i, j;
3093 char_u *p = cmd;
3094
3095 if (VIM_ISDIGIT(*cmd))
3096 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003097 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003098 {
3099 for (j = 0; p[j] != NUL; ++j)
3100 if (p[j] != cmdmods[i].name[j])
3101 break;
3102 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3103 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003104 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003105 }
3106 return 0;
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * Return > 0 if an Ex command "name" exists.
3111 * Return 2 if there is an exact match.
3112 * Return 3 if there is an ambiguous match.
3113 */
3114 int
3115cmd_exists(name)
3116 char_u *name;
3117{
3118 exarg_T ea;
3119 int full = FALSE;
3120 int i;
3121 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003122 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003125 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
3127 for (j = 0; name[j] != NUL; ++j)
3128 if (name[j] != cmdmods[i].name[j])
3129 break;
3130 if (name[j] == NUL && j >= cmdmods[i].minlen)
3131 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3132 }
3133
Bram Moolenaara9587612006-05-04 21:47:50 +00003134 /* Check built-in commands and user defined commands.
3135 * For ":2match" and ":3match" we need to skip the number. */
3136 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003138 p = find_command(&ea, &full);
3139 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003141 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3142 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003143 if (*skipwhite(p) != NUL)
3144 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3146}
3147#endif
3148
3149/*
3150 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3151 * we don't need/want deleted. Maybe this could be done better if we didn't
3152 * repeat all this stuff. The only problem is that they may not stay
3153 * perfectly compatible with each other, but then the command line syntax
3154 * probably won't change that much -- webb.
3155 */
3156 char_u *
3157set_one_cmd_context(xp, buff)
3158 expand_T *xp;
3159 char_u *buff; /* buffer for command string */
3160{
3161 char_u *p;
3162 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003163 int len = 0;
3164 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3166 int compl = EXPAND_NOTHING;
3167#endif
3168#ifdef FEAT_CMDL_COMPL
3169 int delim;
3170#endif
3171 int forceit = FALSE;
3172 int usefilter = FALSE; /* filter instead of file name */
3173
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003174 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 xp->xp_pattern = buff;
3176 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179/*
3180 * 2. skip comment lines and leading space, colons or bars
3181 */
3182 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3183 ;
3184 xp->xp_pattern = cmd;
3185
3186 if (*cmd == NUL)
3187 return NULL;
3188 if (*cmd == '"') /* ignore comment lines */
3189 {
3190 xp->xp_context = EXPAND_NOTHING;
3191 return NULL;
3192 }
3193
3194/*
3195 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3196 */
3197 cmd = skip_range(cmd, &xp->xp_context);
3198
3199/*
3200 * 4. parse command
3201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 xp->xp_pattern = cmd;
3203 if (*cmd == NUL)
3204 return NULL;
3205 if (*cmd == '"')
3206 {
3207 xp->xp_context = EXPAND_NOTHING;
3208 return NULL;
3209 }
3210
3211 if (*cmd == '|' || *cmd == '\n')
3212 return cmd + 1; /* There's another command */
3213
3214 /*
3215 * Isolate the command and search for it in the command table.
3216 * Exceptions:
3217 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003218 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3220 */
3221 if (*cmd == 'k' && cmd[1] != 'e')
3222 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 p = cmd + 1;
3225 }
3226 else
3227 {
3228 p = cmd;
3229 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3230 ++p;
3231 /* check for non-alpha command */
3232 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3233 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 xp->xp_context = EXPAND_UNSUCCESSFUL;
3239 return NULL;
3240 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003242 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3243 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3244 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 break;
3246
3247#ifdef FEAT_USR_CMDS
3248 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3250 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251#endif
3252 }
3253
3254 /*
3255 * If the cursor is touching the command, and it ends in an alpha-numeric
3256 * character, complete the command name.
3257 */
3258 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3259 return NULL;
3260
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 {
3263 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3264 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 p = cmd + 1;
3267 }
3268#ifdef FEAT_USR_CMDS
3269 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3270 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 ea.cmd = cmd;
3272 p = find_ucmd(&ea, p, NULL, xp,
3273# if defined(FEAT_CMDL_COMPL)
3274 &compl
3275# else
3276 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003279 if (p == NULL)
3280 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282#endif
3283 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 /* Not still touching the command and it was an illegal one */
3287 xp->xp_context = EXPAND_UNSUCCESSFUL;
3288 return NULL;
3289 }
3290
3291 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3292
3293 if (*p == '!') /* forced commands */
3294 {
3295 forceit = TRUE;
3296 ++p;
3297 }
3298
3299/*
3300 * 5. parse arguments
3301 */
3302#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003305 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 arg = skipwhite(p);
3308
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 if (*arg == '>') /* append */
3312 {
3313 if (*++arg == '>')
3314 ++arg;
3315 arg = skipwhite(arg);
3316 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
3319 ++arg;
3320 usefilter = TRUE;
3321 }
3322 }
3323
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
3326 usefilter = forceit; /* :r! filter if forced */
3327 if (*arg == '!') /* :r !filter */
3328 {
3329 ++arg;
3330 usefilter = TRUE;
3331 }
3332 }
3333
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 while (*arg == *cmd) /* allow any number of '>' or '<' */
3337 ++arg;
3338 arg = skipwhite(arg);
3339 }
3340
3341 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 /* Check if we're in the +command */
3345 p = arg + 1;
3346 arg = skip_cmd_arg(arg, FALSE);
3347
3348 /* Still touching the command after '+'? */
3349 if (*arg == NUL)
3350 return p;
3351
3352 /* Skip space(s) after +command to get to the real argument */
3353 arg = skipwhite(arg);
3354 }
3355
3356 /*
3357 * Check for '|' to separate commands and '"' to start comments.
3358 * Don't do this for ":read !cmd" and ":write !cmd".
3359 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003360 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 p = arg;
3363 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003364 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 p += 2;
3366 while (*p)
3367 {
3368 if (*p == Ctrl_V)
3369 {
3370 if (p[1] != NUL)
3371 ++p;
3372 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003373 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 || *p == '|' || *p == '\n')
3375 {
3376 if (*(p - 1) != '\\')
3377 {
3378 if (*p == '|' || *p == '\n')
3379 return p + 1;
3380 return NULL; /* It's a comment */
3381 }
3382 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003383 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 }
3386
3387 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003388 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 vim_strchr((char_u *)"|\"", *arg) == NULL)
3390 return NULL;
3391
3392 /* Find start of last argument (argument just before cursor): */
3393 p = buff + STRLEN(buff);
3394 while (p != arg && *p != ' ' && *p != TAB)
3395 p--;
3396 if (*p == ' ' || *p == TAB)
3397 p++;
3398 xp->xp_pattern = p;
3399
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003400 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003402 int c;
3403 int in_quote = FALSE;
3404 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 /*
3407 * Allow spaces within back-quotes to count as part of the argument
3408 * being expanded.
3409 */
3410 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003411 p = xp->xp_pattern;
3412 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003414#ifdef FEAT_MBYTE
3415 if (has_mbyte)
3416 c = mb_ptr2char(p);
3417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003419 c = *p;
3420 if (c == '\\' && p[1] != NUL)
3421 ++p;
3422 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 if (!in_quote)
3425 {
3426 xp->xp_pattern = p;
3427 bow = p + 1;
3428 }
3429 in_quote = !in_quote;
3430 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003431 /* An argument can contain just about everything, except
3432 * characters that end the command and white space. */
3433 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003434#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003435 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003436#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003437 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003438 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003439 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003440 while (*p != NUL)
3441 {
3442#ifdef FEAT_MBYTE
3443 if (has_mbyte)
3444 c = mb_ptr2char(p);
3445 else
3446#endif
3447 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003448 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003449 break;
3450#ifdef FEAT_MBYTE
3451 if (has_mbyte)
3452 len = (*mb_ptr2len)(p);
3453 else
3454#endif
3455 len = 1;
3456 mb_ptr_adv(p);
3457 }
3458 if (in_quote)
3459 bow = p;
3460 else
3461 xp->xp_pattern = p;
3462 p -= len;
3463 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003464 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466
3467 /*
3468 * If we are still inside the quotes, and we passed a space, just
3469 * expand from there.
3470 */
3471 if (bow != NULL && in_quote)
3472 xp->xp_pattern = bow;
3473 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003474
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003475 /* For a shell command more chars need to be escaped. */
3476 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003477 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003478#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003479 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003480#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003481 /* When still after the command name expand executables. */
3482 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003483 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 /* Check for environment variable */
3487 if (*xp->xp_pattern == '$'
3488#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3489 || *xp->xp_pattern == '%'
3490#endif
3491 )
3492 {
3493 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3494 if (!vim_isIDc(*p))
3495 break;
3496 if (*p == NUL)
3497 {
3498 xp->xp_context = EXPAND_ENV_VARS;
3499 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003500#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3501 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003502 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003503 compl = EXPAND_ENV_VARS;
3504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 }
3507 }
3508
3509/*
3510 * 6. switch on command name
3511 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003512 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003514 case CMD_find:
3515 case CMD_sfind:
3516 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003517 if (xp->xp_context == EXPAND_FILES)
3518 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 case CMD_cd:
3521 case CMD_chdir:
3522 case CMD_lcd:
3523 case CMD_lchdir:
3524 if (xp->xp_context == EXPAND_FILES)
3525 xp->xp_context = EXPAND_DIRECTORIES;
3526 break;
3527 case CMD_help:
3528 xp->xp_context = EXPAND_HELP;
3529 xp->xp_pattern = arg;
3530 break;
3531
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003532 /* Command modifiers: return the argument.
3533 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003535 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 case CMD_belowright:
3537 case CMD_botright:
3538 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003539 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003541 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 case CMD_folddoclosed:
3543 case CMD_folddoopen:
3544 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003545 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 case CMD_keepjumps:
3547 case CMD_keepmarks:
3548 case CMD_leftabove:
3549 case CMD_lockmarks:
3550 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003551 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003553 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 case CMD_topleft:
3555 case CMD_verbose:
3556 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003557 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 return arg;
3559
Bram Moolenaar4f688582007-07-24 12:34:30 +00003560#ifdef FEAT_CMDL_COMPL
3561# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 case CMD_match:
3563 if (*arg == NUL || !ends_excmd(*arg))
3564 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003565 /* also complete "None" */
3566 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 arg = skipwhite(skiptowhite(arg));
3568 if (*arg != NUL)
3569 {
3570 xp->xp_context = EXPAND_NOTHING;
3571 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3572 }
3573 }
3574 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577/*
3578 * All completion for the +cmdline_compl feature goes here.
3579 */
3580
3581# ifdef FEAT_USR_CMDS
3582 case CMD_command:
3583 /* Check for attributes */
3584 while (*arg == '-')
3585 {
3586 arg++; /* Skip "-" */
3587 p = skiptowhite(arg);
3588 if (*p == NUL)
3589 {
3590 /* Cursor is still in the attribute */
3591 p = vim_strchr(arg, '=');
3592 if (p == NULL)
3593 {
3594 /* No "=", so complete attribute names */
3595 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3596 xp->xp_pattern = arg;
3597 return NULL;
3598 }
3599
3600 /* For the -complete and -nargs attributes, we complete
3601 * their arguments as well.
3602 */
3603 if (STRNICMP(arg, "complete", p - arg) == 0)
3604 {
3605 xp->xp_context = EXPAND_USER_COMPLETE;
3606 xp->xp_pattern = p + 1;
3607 return NULL;
3608 }
3609 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3610 {
3611 xp->xp_context = EXPAND_USER_NARGS;
3612 xp->xp_pattern = p + 1;
3613 return NULL;
3614 }
3615 return NULL;
3616 }
3617 arg = skipwhite(p);
3618 }
3619
3620 /* After the attributes comes the new command name */
3621 p = skiptowhite(arg);
3622 if (*p == NUL)
3623 {
3624 xp->xp_context = EXPAND_USER_COMMANDS;
3625 xp->xp_pattern = arg;
3626 break;
3627 }
3628
3629 /* And finally comes a normal command */
3630 return skipwhite(p);
3631
3632 case CMD_delcommand:
3633 xp->xp_context = EXPAND_USER_COMMANDS;
3634 xp->xp_pattern = arg;
3635 break;
3636# endif
3637
3638 case CMD_global:
3639 case CMD_vglobal:
3640 delim = *arg; /* get the delimiter */
3641 if (delim)
3642 ++arg; /* skip delimiter if there is one */
3643
3644 while (arg[0] != NUL && arg[0] != delim)
3645 {
3646 if (arg[0] == '\\' && arg[1] != NUL)
3647 ++arg;
3648 ++arg;
3649 }
3650 if (arg[0] != NUL)
3651 return arg + 1;
3652 break;
3653 case CMD_and:
3654 case CMD_substitute:
3655 delim = *arg;
3656 if (delim)
3657 {
3658 /* skip "from" part */
3659 ++arg;
3660 arg = skip_regexp(arg, delim, p_magic, NULL);
3661 }
3662 /* skip "to" part */
3663 while (arg[0] != NUL && arg[0] != delim)
3664 {
3665 if (arg[0] == '\\' && arg[1] != NUL)
3666 ++arg;
3667 ++arg;
3668 }
3669 if (arg[0] != NUL) /* skip delimiter */
3670 ++arg;
3671 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3672 ++arg;
3673 if (arg[0] != NUL)
3674 return arg;
3675 break;
3676 case CMD_isearch:
3677 case CMD_dsearch:
3678 case CMD_ilist:
3679 case CMD_dlist:
3680 case CMD_ijump:
3681 case CMD_psearch:
3682 case CMD_djump:
3683 case CMD_isplit:
3684 case CMD_dsplit:
3685 arg = skipwhite(skipdigits(arg)); /* skip count */
3686 if (*arg == '/') /* Match regexp, not just whole words */
3687 {
3688 for (++arg; *arg && *arg != '/'; arg++)
3689 if (*arg == '\\' && arg[1] != NUL)
3690 arg++;
3691 if (*arg)
3692 {
3693 arg = skipwhite(arg + 1);
3694
3695 /* Check for trailing illegal characters */
3696 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3697 xp->xp_context = EXPAND_NOTHING;
3698 else
3699 return arg;
3700 }
3701 }
3702 break;
3703#ifdef FEAT_AUTOCMD
3704 case CMD_autocmd:
3705 return set_context_in_autocmd(xp, arg, FALSE);
3706
3707 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003708 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return set_context_in_autocmd(xp, arg, TRUE);
3710#endif
3711 case CMD_set:
3712 set_context_in_set_cmd(xp, arg, 0);
3713 break;
3714 case CMD_setglobal:
3715 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3716 break;
3717 case CMD_setlocal:
3718 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3719 break;
3720 case CMD_tag:
3721 case CMD_stag:
3722 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003723 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 case CMD_tselect:
3725 case CMD_stselect:
3726 case CMD_ptselect:
3727 case CMD_tjump:
3728 case CMD_stjump:
3729 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003730 if (*p_wop != NUL)
3731 xp->xp_context = EXPAND_TAGS_LISTFILES;
3732 else
3733 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 xp->xp_pattern = arg;
3735 break;
3736 case CMD_augroup:
3737 xp->xp_context = EXPAND_AUGROUP;
3738 xp->xp_pattern = arg;
3739 break;
3740#ifdef FEAT_SYN_HL
3741 case CMD_syntax:
3742 set_context_in_syntax_cmd(xp, arg);
3743 break;
3744#endif
3745#ifdef FEAT_EVAL
3746 case CMD_let:
3747 case CMD_if:
3748 case CMD_elseif:
3749 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003750 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_echo:
3752 case CMD_echon:
3753 case CMD_execute:
3754 case CMD_echomsg:
3755 case CMD_echoerr:
3756 case CMD_call:
3757 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003758 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 break;
3760
3761 case CMD_unlet:
3762 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3763 arg = xp->xp_pattern + 1;
3764 xp->xp_context = EXPAND_USER_VARS;
3765 xp->xp_pattern = arg;
3766 break;
3767
3768 case CMD_function:
3769 case CMD_delfunction:
3770 xp->xp_context = EXPAND_USER_FUNC;
3771 xp->xp_pattern = arg;
3772 break;
3773
3774 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003775 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 break;
3777#endif
3778 case CMD_highlight:
3779 set_context_in_highlight_cmd(xp, arg);
3780 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003781#ifdef FEAT_CSCOPE
3782 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003783 case CMD_lcscope:
3784 case CMD_scscope:
3785 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003786 break;
3787#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003788#ifdef FEAT_SIGNS
3789 case CMD_sign:
3790 set_context_in_sign_cmd(xp, arg);
3791 break;
3792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#ifdef FEAT_LISTCMDS
3794 case CMD_bdelete:
3795 case CMD_bwipeout:
3796 case CMD_bunload:
3797 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3798 arg = xp->xp_pattern + 1;
3799 /*FALLTHROUGH*/
3800 case CMD_buffer:
3801 case CMD_sbuffer:
3802 case CMD_checktime:
3803 xp->xp_context = EXPAND_BUFFERS;
3804 xp->xp_pattern = arg;
3805 break;
3806#endif
3807#ifdef FEAT_USR_CMDS
3808 case CMD_USER:
3809 case CMD_USER_BUF:
3810 if (compl != EXPAND_NOTHING)
3811 {
3812 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003813 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815# ifdef FEAT_MENU
3816 if (compl == EXPAND_MENUS)
3817 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3818# endif
3819 if (compl == EXPAND_COMMANDS)
3820 return arg;
3821 if (compl == EXPAND_MAPPINGS)
3822 return set_context_in_map_cmd(xp, (char_u *)"map",
3823 arg, forceit, FALSE, FALSE, CMD_map);
3824 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3825 arg = xp->xp_pattern + 1;
3826 xp->xp_pattern = arg;
3827 }
3828 xp->xp_context = compl;
3829 }
3830 break;
3831#endif
3832 case CMD_map: case CMD_noremap:
3833 case CMD_nmap: case CMD_nnoremap:
3834 case CMD_vmap: case CMD_vnoremap:
3835 case CMD_omap: case CMD_onoremap:
3836 case CMD_imap: case CMD_inoremap:
3837 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003838 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003840 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_unmap:
3842 case CMD_nunmap:
3843 case CMD_vunmap:
3844 case CMD_ounmap:
3845 case CMD_iunmap:
3846 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003847 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003849 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 case CMD_abbreviate: case CMD_noreabbrev:
3851 case CMD_cabbrev: case CMD_cnoreabbrev:
3852 case CMD_iabbrev: case CMD_inoreabbrev:
3853 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003854 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 case CMD_unabbreviate:
3856 case CMD_cunabbrev:
3857 case CMD_iunabbrev:
3858 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003859 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860#ifdef FEAT_MENU
3861 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3862 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3863 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3864 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3865 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3866 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3867 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3868 case CMD_tmenu: case CMD_tunmenu:
3869 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3870 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3871#endif
3872
3873 case CMD_colorscheme:
3874 xp->xp_context = EXPAND_COLORS;
3875 xp->xp_pattern = arg;
3876 break;
3877
3878 case CMD_compiler:
3879 xp->xp_context = EXPAND_COMPILER;
3880 xp->xp_pattern = arg;
3881 break;
3882
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003883 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003884 xp->xp_context = EXPAND_OWNSYNTAX;
3885 xp->xp_pattern = arg;
3886 break;
3887
3888 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003889 xp->xp_context = EXPAND_FILETYPE;
3890 xp->xp_pattern = arg;
3891 break;
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3894 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3895 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003896 p = skiptowhite(arg);
3897 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
3899 xp->xp_context = EXPAND_LANGUAGE;
3900 xp->xp_pattern = arg;
3901 }
3902 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003903 {
3904 if ( STRNCMP(arg, "messages", p - arg) == 0
3905 || STRNCMP(arg, "ctype", p - arg) == 0
3906 || STRNCMP(arg, "time", p - arg) == 0)
3907 {
3908 xp->xp_context = EXPAND_LOCALES;
3909 xp->xp_pattern = skipwhite(p);
3910 }
3911 else
3912 xp->xp_context = EXPAND_NOTHING;
3913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003916#if defined(FEAT_PROFILE)
3917 case CMD_profile:
3918 set_context_in_profile_cmd(xp, arg);
3919 break;
3920#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003921 case CMD_behave:
3922 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003923 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003926#if defined(FEAT_CMDHIST)
3927 case CMD_history:
3928 xp->xp_context = EXPAND_HISTORY;
3929 xp->xp_pattern = arg;
3930 break;
3931#endif
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#endif /* FEAT_CMDL_COMPL */
3934
3935 default:
3936 break;
3937 }
3938 return NULL;
3939}
3940
3941/*
3942 * skip a range specifier of the form: addr [,addr] [;addr] ..
3943 *
3944 * Backslashed delimiters after / or ? will be skipped, and commands will
3945 * not be expanded between /'s and ?'s or after "'".
3946 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003947 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 * Returns the "cmd" pointer advanced to beyond the range.
3949 */
3950 char_u *
3951skip_range(cmd, ctx)
3952 char_u *cmd;
3953 int *ctx; /* pointer to xp_context or NULL */
3954{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003955 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
Bram Moolenaardf177f62005-02-22 08:39:57 +00003957 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 if (*cmd == '\'')
3960 {
3961 if (*++cmd == NUL && ctx != NULL)
3962 *ctx = EXPAND_NOTHING;
3963 }
3964 else if (*cmd == '/' || *cmd == '?')
3965 {
3966 delim = *cmd++;
3967 while (*cmd != NUL && *cmd != delim)
3968 if (*cmd++ == '\\' && *cmd != NUL)
3969 ++cmd;
3970 if (*cmd == NUL && ctx != NULL)
3971 *ctx = EXPAND_NOTHING;
3972 }
3973 if (*cmd != NUL)
3974 ++cmd;
3975 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003976
3977 /* Skip ":" and white space. */
3978 while (*cmd == ':')
3979 cmd = skipwhite(cmd + 1);
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return cmd;
3982}
3983
3984/*
3985 * get a single EX address
3986 *
3987 * Set ptr to the next character after the part that was interpreted.
3988 * Set ptr to NULL when an error is encountered.
3989 *
3990 * Return MAXLNUM when no Ex address was found.
3991 */
3992 static linenr_T
3993get_address(ptr, skip, to_other_file)
3994 char_u **ptr;
3995 int skip; /* only skip the address, don't use it */
3996 int to_other_file; /* flag: may jump to other file */
3997{
3998 int c;
3999 int i;
4000 long n;
4001 char_u *cmd;
4002 pos_T pos;
4003 pos_T *fp;
4004 linenr_T lnum;
4005
4006 cmd = skipwhite(*ptr);
4007 lnum = MAXLNUM;
4008 do
4009 {
4010 switch (*cmd)
4011 {
4012 case '.': /* '.' - Cursor position */
4013 ++cmd;
4014 lnum = curwin->w_cursor.lnum;
4015 break;
4016
4017 case '$': /* '$' - last line */
4018 ++cmd;
4019 lnum = curbuf->b_ml.ml_line_count;
4020 break;
4021
4022 case '\'': /* ''' - mark */
4023 if (*++cmd == NUL)
4024 {
4025 cmd = NULL;
4026 goto error;
4027 }
4028 if (skip)
4029 ++cmd;
4030 else
4031 {
4032 /* Only accept a mark in another file when it is
4033 * used by itself: ":'M". */
4034 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4035 ++cmd;
4036 if (fp == (pos_T *)-1)
4037 /* Jumped to another file. */
4038 lnum = curwin->w_cursor.lnum;
4039 else
4040 {
4041 if (check_mark(fp) == FAIL)
4042 {
4043 cmd = NULL;
4044 goto error;
4045 }
4046 lnum = fp->lnum;
4047 }
4048 }
4049 break;
4050
4051 case '/':
4052 case '?': /* '/' or '?' - search */
4053 c = *cmd++;
4054 if (skip) /* skip "/pat/" */
4055 {
4056 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4057 if (*cmd == c)
4058 ++cmd;
4059 }
4060 else
4061 {
4062 pos = curwin->w_cursor; /* save curwin->w_cursor */
4063 /*
4064 * When '/' or '?' follows another address, start
4065 * from there.
4066 */
4067 if (lnum != MAXLNUM)
4068 curwin->w_cursor.lnum = lnum;
4069 /*
4070 * Start a forward search at the end of the line.
4071 * Start a backward search at the start of the line.
4072 * This makes sure we never match in the current
4073 * line, and can match anywhere in the
4074 * next/previous line.
4075 */
4076 if (c == '/')
4077 curwin->w_cursor.col = MAXCOL;
4078 else
4079 curwin->w_cursor.col = 0;
4080 searchcmdlen = 0;
4081 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004082 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
4084 curwin->w_cursor = pos;
4085 cmd = NULL;
4086 goto error;
4087 }
4088 lnum = curwin->w_cursor.lnum;
4089 curwin->w_cursor = pos;
4090 /* adjust command string pointer */
4091 cmd += searchcmdlen;
4092 }
4093 break;
4094
4095 case '\\': /* "\?", "\/" or "\&", repeat search */
4096 ++cmd;
4097 if (*cmd == '&')
4098 i = RE_SUBST;
4099 else if (*cmd == '?' || *cmd == '/')
4100 i = RE_SEARCH;
4101 else
4102 {
4103 EMSG(_(e_backslash));
4104 cmd = NULL;
4105 goto error;
4106 }
4107
4108 if (!skip)
4109 {
4110 /*
4111 * When search follows another address, start from
4112 * there.
4113 */
4114 if (lnum != MAXLNUM)
4115 pos.lnum = lnum;
4116 else
4117 pos.lnum = curwin->w_cursor.lnum;
4118
4119 /*
4120 * Start the search just like for the above
4121 * do_search().
4122 */
4123 if (*cmd != '?')
4124 pos.col = MAXCOL;
4125 else
4126 pos.col = 0;
4127 if (searchit(curwin, curbuf, &pos,
4128 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004129 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004130 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 lnum = pos.lnum;
4132 else
4133 {
4134 cmd = NULL;
4135 goto error;
4136 }
4137 }
4138 ++cmd;
4139 break;
4140
4141 default:
4142 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4143 lnum = getdigits(&cmd);
4144 }
4145
4146 for (;;)
4147 {
4148 cmd = skipwhite(cmd);
4149 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4150 break;
4151
4152 if (lnum == MAXLNUM)
4153 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4154 if (VIM_ISDIGIT(*cmd))
4155 i = '+'; /* "number" is same as "+number" */
4156 else
4157 i = *cmd++;
4158 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4159 n = 1;
4160 else
4161 n = getdigits(&cmd);
4162 if (i == '-')
4163 lnum -= n;
4164 else
4165 lnum += n;
4166 }
4167 } while (*cmd == '/' || *cmd == '?');
4168
4169error:
4170 *ptr = cmd;
4171 return lnum;
4172}
4173
4174/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004175 * Get flags from an Ex command argument.
4176 */
4177 static void
4178get_flags(eap)
4179 exarg_T *eap;
4180{
4181 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4182 {
4183 if (*eap->arg == 'l')
4184 eap->flags |= EXFLAG_LIST;
4185 else if (*eap->arg == 'p')
4186 eap->flags |= EXFLAG_PRINT;
4187 else
4188 eap->flags |= EXFLAG_NR;
4189 eap->arg = skipwhite(eap->arg + 1);
4190 }
4191}
4192
4193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 * Function called for command which is Not Implemented. NI!
4195 */
4196 void
4197ex_ni(eap)
4198 exarg_T *eap;
4199{
4200 if (!eap->skip)
4201 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4202}
4203
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004204#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205/*
4206 * Function called for script command which is Not Implemented. NI!
4207 * Skips over ":perl <<EOF" constructs.
4208 */
4209 static void
4210ex_script_ni(eap)
4211 exarg_T *eap;
4212{
4213 if (!eap->skip)
4214 ex_ni(eap);
4215 else
4216 vim_free(script_get(eap, eap->arg));
4217}
4218#endif
4219
4220/*
4221 * Check range in Ex command for validity.
4222 * Return NULL when valid, error message when invalid.
4223 */
4224 static char_u *
4225invalid_range(eap)
4226 exarg_T *eap;
4227{
4228 if ( eap->line1 < 0
4229 || eap->line2 < 0
4230 || eap->line1 > eap->line2
4231 || ((eap->argt & RANGE)
4232 && !(eap->argt & NOTADR)
4233 && eap->line2 > curbuf->b_ml.ml_line_count
4234#ifdef FEAT_DIFF
4235 + (eap->cmdidx == CMD_diffget)
4236#endif
4237 ))
4238 return (char_u *)_(e_invrange);
4239 return NULL;
4240}
4241
4242/*
4243 * Correct the range for zero line number, if required.
4244 */
4245 static void
4246correct_range(eap)
4247 exarg_T *eap;
4248{
4249 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4250 {
4251 if (eap->line1 == 0)
4252 eap->line1 = 1;
4253 if (eap->line2 == 0)
4254 eap->line2 = 1;
4255 }
4256}
4257
Bram Moolenaar748bf032005-02-02 23:04:36 +00004258#ifdef FEAT_QUICKFIX
4259static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4260
4261/*
4262 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4263 * pattern. Otherwise return eap->arg.
4264 */
4265 static char_u *
4266skip_grep_pat(eap)
4267 exarg_T *eap;
4268{
4269 char_u *p = eap->arg;
4270
Bram Moolenaara37420f2006-02-04 22:37:47 +00004271 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4272 || eap->cmdidx == CMD_vimgrepadd
4273 || eap->cmdidx == CMD_lvimgrepadd
4274 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004275 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004276 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004277 if (p == NULL)
4278 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004279 }
4280 return p;
4281}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004282
4283/*
4284 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4285 * in the command line, so that things like % get expanded.
4286 */
4287 static char_u *
4288replace_makeprg(eap, p, cmdlinep)
4289 exarg_T *eap;
4290 char_u *p;
4291 char_u **cmdlinep;
4292{
4293 char_u *new_cmdline;
4294 char_u *program;
4295 char_u *pos;
4296 char_u *ptr;
4297 int len;
4298 int i;
4299
4300 /*
4301 * Don't do it when ":vimgrep" is used for ":grep".
4302 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004303 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4304 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4305 || eap->cmdidx == CMD_grepadd
4306 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004307 && !grep_internal(eap->cmdidx))
4308 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004309 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4310 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004311 {
4312 if (*curbuf->b_p_gp == NUL)
4313 program = p_gp;
4314 else
4315 program = curbuf->b_p_gp;
4316 }
4317 else
4318 {
4319 if (*curbuf->b_p_mp == NUL)
4320 program = p_mp;
4321 else
4322 program = curbuf->b_p_mp;
4323 }
4324
4325 p = skipwhite(p);
4326
4327 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4328 {
4329 /* replace $* by given arguments */
4330 i = 1;
4331 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4332 ++i;
4333 len = (int)STRLEN(p);
4334 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4335 if (new_cmdline == NULL)
4336 return NULL; /* out of memory */
4337 ptr = new_cmdline;
4338 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4339 {
4340 i = (int)(pos - program);
4341 STRNCPY(ptr, program, i);
4342 STRCPY(ptr += i, p);
4343 ptr += len;
4344 program = pos + 2;
4345 }
4346 STRCPY(ptr, program);
4347 }
4348 else
4349 {
4350 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4351 if (new_cmdline == NULL)
4352 return NULL; /* out of memory */
4353 STRCPY(new_cmdline, program);
4354 STRCAT(new_cmdline, " ");
4355 STRCAT(new_cmdline, p);
4356 }
4357 msg_make(p);
4358
4359 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4360 vim_free(*cmdlinep);
4361 *cmdlinep = new_cmdline;
4362 p = new_cmdline;
4363 }
4364 return p;
4365}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004366#endif
4367
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368/*
4369 * Expand file name in Ex command argument.
4370 * Return FAIL for failure, OK otherwise.
4371 */
4372 int
4373expand_filename(eap, cmdlinep, errormsgp)
4374 exarg_T *eap;
4375 char_u **cmdlinep;
4376 char_u **errormsgp;
4377{
4378 int has_wildcards; /* need to expand wildcards */
4379 char_u *repl;
4380 int srclen;
4381 char_u *p;
4382 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004383 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaar748bf032005-02-02 23:04:36 +00004385#ifdef FEAT_QUICKFIX
4386 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4387 p = skip_grep_pat(eap);
4388#else
4389 p = eap->arg;
4390#endif
4391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 /*
4393 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4394 * the file name contains a wildcard it should not cause expanding.
4395 * (it will be expanded anyway if there is a wildcard before replacing).
4396 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004397 has_wildcards = mch_has_wildcard(p);
4398 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004400#ifdef FEAT_EVAL
4401 /* Skip over `=expr`, wildcards in it are not expanded. */
4402 if (p[0] == '`' && p[1] == '=')
4403 {
4404 p += 2;
4405 (void)skip_expr(&p);
4406 if (*p == '`')
4407 ++p;
4408 continue;
4409 }
4410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 /*
4412 * Quick check if this cannot be the start of a special string.
4413 * Also removes backslash before '%', '#' and '<'.
4414 */
4415 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4416 {
4417 ++p;
4418 continue;
4419 }
4420
4421 /*
4422 * Try to find a match at this position.
4423 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004424 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4425 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (*errormsgp != NULL) /* error detected */
4427 return FAIL;
4428 if (repl == NULL) /* no match found */
4429 {
4430 p += srclen;
4431 continue;
4432 }
4433
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004434 /* Wildcards won't be expanded below, the replacement is taken
4435 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4436 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4437 {
4438 char_u *l = repl;
4439
4440 repl = expand_env_save(repl);
4441 vim_free(l);
4442 }
4443
Bram Moolenaar63b92542007-03-27 14:57:09 +00004444 /* Need to escape white space et al. with a backslash.
4445 * Don't do this for:
4446 * - replacement that already has been escaped: "##"
4447 * - shell commands (may have to use quotes instead).
4448 * - non-unix systems when there is a single argument (spaces don't
4449 * separate arguments then).
4450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004452 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 && eap->cmdidx != CMD_bang
4454 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004455 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004457 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004459 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460#ifndef UNIX
4461 && !(eap->argt & NOSPC)
4462#endif
4463 )
4464 {
4465 char_u *l;
4466#ifdef BACKSLASH_IN_FILENAME
4467 /* Don't escape a backslash here, because rem_backslash() doesn't
4468 * remove it later. */
4469 static char_u *nobslash = (char_u *)" \t\"|";
4470# define ESCAPE_CHARS nobslash
4471#else
4472# define ESCAPE_CHARS escape_chars
4473#endif
4474
4475 for (l = repl; *l; ++l)
4476 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4477 {
4478 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4479 if (l != NULL)
4480 {
4481 vim_free(repl);
4482 repl = l;
4483 }
4484 break;
4485 }
4486 }
4487
4488 /* For a shell command a '!' must be escaped. */
4489 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004490 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 char_u *l;
4493
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004494 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (l != NULL)
4496 {
4497 vim_free(repl);
4498 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004499 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (strstr((char *)p_sh, "sh") != NULL)
4501 {
4502 l = vim_strsave_escaped(repl, (char_u *)"!");
4503 if (l != NULL)
4504 {
4505 vim_free(repl);
4506 repl = l;
4507 }
4508 }
4509 }
4510 }
4511
4512 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4513 vim_free(repl);
4514 if (p == NULL)
4515 return FAIL;
4516 }
4517
4518 /*
4519 * One file argument: Expand wildcards.
4520 * Don't do this with ":r !command" or ":w !command".
4521 */
4522 if ((eap->argt & NOSPC) && !eap->usefilter)
4523 {
4524 /*
4525 * May do this twice:
4526 * 1. Replace environment variables.
4527 * 2. Replace any other wildcards, remove backslashes.
4528 */
4529 for (n = 1; n <= 2; ++n)
4530 {
4531 if (n == 2)
4532 {
4533#ifdef UNIX
4534 /*
4535 * Only for Unix we check for more than one file name.
4536 * For other systems spaces are considered to be part
4537 * of the file name.
4538 * Only check here if there is no wildcard, otherwise
4539 * ExpandOne() will check for errors. This allows
4540 * ":e `ls ve*.c`" on Unix.
4541 */
4542 if (!has_wildcards)
4543 for (p = eap->arg; *p; ++p)
4544 {
4545 /* skip escaped characters */
4546 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4547 ++p;
4548 else if (vim_iswhite(*p))
4549 {
4550 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4551 return FAIL;
4552 }
4553 }
4554#endif
4555
4556 /*
4557 * Halve the number of backslashes (this is Vi compatible).
4558 * For Unix and OS/2, when wildcards are expanded, this is
4559 * done by ExpandOne() below.
4560 */
4561#if defined(UNIX) || defined(OS2)
4562 if (!has_wildcards)
4563#endif
4564 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566
4567 if (has_wildcards)
4568 {
4569 if (n == 1)
4570 {
4571 /*
4572 * First loop: May expand environment variables. This
4573 * can be done much faster with expand_env() than with
4574 * something else (e.g., calling a shell).
4575 * After expanding environment variables, check again
4576 * if there are still wildcards present.
4577 */
4578 if (vim_strchr(eap->arg, '$') != NULL
4579 || vim_strchr(eap->arg, '~') != NULL)
4580 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004581 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004582 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 has_wildcards = mch_has_wildcard(NameBuff);
4584 p = NameBuff;
4585 }
4586 else
4587 p = NULL;
4588 }
4589 else /* n == 2 */
4590 {
4591 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004592 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 ExpandInit(&xpc);
4595 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004596 if (p_wic)
4597 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004599 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (p == NULL)
4601 return FAIL;
4602 }
4603 if (p != NULL)
4604 {
4605 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4606 p, cmdlinep);
4607 if (n == 2) /* p came from ExpandOne() */
4608 vim_free(p);
4609 }
4610 }
4611 }
4612 }
4613 return OK;
4614}
4615
4616/*
4617 * Replace part of the command line, keeping eap->cmd, eap->arg and
4618 * eap->nextcmd correct.
4619 * "src" points to the part that is to be replaced, of length "srclen".
4620 * "repl" is the replacement string.
4621 * Returns a pointer to the character after the replaced string.
4622 * Returns NULL for failure.
4623 */
4624 static char_u *
4625repl_cmdline(eap, src, srclen, repl, cmdlinep)
4626 exarg_T *eap;
4627 char_u *src;
4628 int srclen;
4629 char_u *repl;
4630 char_u **cmdlinep;
4631{
4632 int len;
4633 int i;
4634 char_u *new_cmdline;
4635
4636 /*
4637 * The new command line is build in new_cmdline[].
4638 * First allocate it.
4639 * Careful: a "+cmd" argument may have been NUL terminated.
4640 */
4641 len = (int)STRLEN(repl);
4642 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004643 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4645 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4646 return NULL; /* out of memory! */
4647
4648 /*
4649 * Copy the stuff before the expanded part.
4650 * Copy the expanded stuff.
4651 * Copy what came after the expanded part.
4652 * Copy the next commands, if there are any.
4653 */
4654 i = (int)(src - *cmdlinep); /* length of part before match */
4655 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 mch_memmove(new_cmdline + i, repl, (size_t)len);
4658 i += len; /* remember the end of the string */
4659 STRCPY(new_cmdline + i, src + srclen);
4660 src = new_cmdline + i; /* remember where to continue */
4661
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004662 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
4664 i = (int)STRLEN(new_cmdline) + 1;
4665 STRCPY(new_cmdline + i, eap->nextcmd);
4666 eap->nextcmd = new_cmdline + i;
4667 }
4668 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4669 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4670 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4671 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4672 vim_free(*cmdlinep);
4673 *cmdlinep = new_cmdline;
4674
4675 return src;
4676}
4677
4678/*
4679 * Check for '|' to separate commands and '"' to start comments.
4680 */
4681 void
4682separate_nextcmd(eap)
4683 exarg_T *eap;
4684{
4685 char_u *p;
4686
Bram Moolenaar86b68352004-12-27 21:59:20 +00004687#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004688 p = skip_grep_pat(eap);
4689#else
4690 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004691#endif
4692
4693 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 if (*p == Ctrl_V)
4696 {
4697 if (eap->argt & (USECTRLV | XFILE))
4698 ++p; /* skip CTRL-V and next char */
4699 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004700 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004701 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 if (*p == NUL) /* stop at NUL after CTRL-V */
4703 break;
4704 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004705
4706#ifdef FEAT_EVAL
4707 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004708 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004709 {
4710 p += 2;
4711 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004712 }
4713#endif
4714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 /* Check for '"': start of comment or '|': next command */
4716 /* :@" and :*" do not start a comment!
4717 * :redir @" doesn't either. */
4718 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4719 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4720 || p != eap->arg)
4721 && (eap->cmdidx != CMD_redir
4722 || p != eap->arg + 1 || p[-1] != '@'))
4723 || *p == '|' || *p == '\n')
4724 {
4725 /*
4726 * We remove the '\' before the '|', unless USECTRLV is used
4727 * AND 'b' is present in 'cpoptions'.
4728 */
4729 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4730 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4731 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004732 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 --p;
4734 }
4735 else
4736 {
4737 eap->nextcmd = check_nextcmd(p);
4738 *p = NUL;
4739 break;
4740 }
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4745 del_trailing_spaces(eap->arg);
4746}
4747
4748/*
4749 * get + command from ex argument
4750 */
4751 static char_u *
4752getargcmd(argp)
4753 char_u **argp;
4754{
4755 char_u *arg = *argp;
4756 char_u *command = NULL;
4757
4758 if (*arg == '+') /* +[command] */
4759 {
4760 ++arg;
4761 if (vim_isspace(*arg))
4762 command = dollar_command;
4763 else
4764 {
4765 command = arg;
4766 arg = skip_cmd_arg(command, TRUE);
4767 if (*arg != NUL)
4768 *arg++ = NUL; /* terminate command with NUL */
4769 }
4770
4771 arg = skipwhite(arg); /* skip over spaces */
4772 *argp = arg;
4773 }
4774 return command;
4775}
4776
4777/*
4778 * Find end of "+command" argument. Skip over "\ " and "\\".
4779 */
4780 static char_u *
4781skip_cmd_arg(p, rembs)
4782 char_u *p;
4783 int rembs; /* TRUE to halve the number of backslashes */
4784{
4785 while (*p && !vim_isspace(*p))
4786 {
4787 if (*p == '\\' && p[1] != NUL)
4788 {
4789 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004790 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
4792 ++p;
4793 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004794 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 return p;
4797}
4798
4799/*
4800 * Get "++opt=arg" argument.
4801 * Return FAIL or OK.
4802 */
4803 static int
4804getargopt(eap)
4805 exarg_T *eap;
4806{
4807 char_u *arg = eap->arg + 2;
4808 int *pp = NULL;
4809#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004810 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 char_u *p;
4812#endif
4813
4814 /* ":edit ++[no]bin[ary] file" */
4815 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4816 {
4817 if (*arg == 'n')
4818 {
4819 arg += 2;
4820 eap->force_bin = FORCE_NOBIN;
4821 }
4822 else
4823 eap->force_bin = FORCE_BIN;
4824 if (!checkforcmd(&arg, "binary", 3))
4825 return FAIL;
4826 eap->arg = skipwhite(arg);
4827 return OK;
4828 }
4829
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004830 /* ":read ++edit file" */
4831 if (STRNCMP(arg, "edit", 4) == 0)
4832 {
4833 eap->read_edit = TRUE;
4834 eap->arg = skipwhite(arg + 4);
4835 return OK;
4836 }
4837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (STRNCMP(arg, "ff", 2) == 0)
4839 {
4840 arg += 2;
4841 pp = &eap->force_ff;
4842 }
4843 else if (STRNCMP(arg, "fileformat", 10) == 0)
4844 {
4845 arg += 10;
4846 pp = &eap->force_ff;
4847 }
4848#ifdef FEAT_MBYTE
4849 else if (STRNCMP(arg, "enc", 3) == 0)
4850 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004851 if (STRNCMP(arg, "encoding", 8) == 0)
4852 arg += 8;
4853 else
4854 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 pp = &eap->force_enc;
4856 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004857 else if (STRNCMP(arg, "bad", 3) == 0)
4858 {
4859 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004860 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#endif
4863
4864 if (pp == NULL || *arg != '=')
4865 return FAIL;
4866
4867 ++arg;
4868 *pp = (int)(arg - eap->cmd);
4869 arg = skip_cmd_arg(arg, FALSE);
4870 eap->arg = skipwhite(arg);
4871 *arg = NUL;
4872
4873#ifdef FEAT_MBYTE
4874 if (pp == &eap->force_ff)
4875 {
4876#endif
4877 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4878 return FAIL;
4879#ifdef FEAT_MBYTE
4880 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004881 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 /* Make 'fileencoding' lower case. */
4884 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4885 *p = TOLOWER_ASC(*p);
4886 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004887 else
4888 {
4889 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4890 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004891 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004892 if (STRICMP(p, "keep") == 0)
4893 eap->bad_char = BAD_KEEP;
4894 else if (STRICMP(p, "drop") == 0)
4895 eap->bad_char = BAD_DROP;
4896 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4897 eap->bad_char = *p;
4898 else
4899 return FAIL;
4900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
4902
4903 return OK;
4904}
4905
4906/*
4907 * ":abbreviate" and friends.
4908 */
4909 static void
4910ex_abbreviate(eap)
4911 exarg_T *eap;
4912{
4913 do_exmap(eap, TRUE); /* almost the same as mapping */
4914}
4915
4916/*
4917 * ":map" and friends.
4918 */
4919 static void
4920ex_map(eap)
4921 exarg_T *eap;
4922{
4923 /*
4924 * If we are sourcing .exrc or .vimrc in current directory we
4925 * print the mappings for security reasons.
4926 */
4927 if (secure)
4928 {
4929 secure = 2;
4930 msg_outtrans(eap->cmd);
4931 msg_putchar('\n');
4932 }
4933 do_exmap(eap, FALSE);
4934}
4935
4936/*
4937 * ":unmap" and friends.
4938 */
4939 static void
4940ex_unmap(eap)
4941 exarg_T *eap;
4942{
4943 do_exmap(eap, FALSE);
4944}
4945
4946/*
4947 * ":mapclear" and friends.
4948 */
4949 static void
4950ex_mapclear(eap)
4951 exarg_T *eap;
4952{
4953 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4954}
4955
4956/*
4957 * ":abclear" and friends.
4958 */
4959 static void
4960ex_abclear(eap)
4961 exarg_T *eap;
4962{
4963 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4964}
4965
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004966#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 static void
4968ex_autocmd(eap)
4969 exarg_T *eap;
4970{
4971 /*
4972 * Disallow auto commands from .exrc and .vimrc in current
4973 * directory for security reasons.
4974 */
4975 if (secure)
4976 {
4977 secure = 2;
4978 eap->errmsg = e_curdir;
4979 }
4980 else if (eap->cmdidx == CMD_autocmd)
4981 do_autocmd(eap->arg, eap->forceit);
4982 else
4983 do_augroup(eap->arg, eap->forceit);
4984}
4985
4986/*
4987 * ":doautocmd": Apply the automatic commands to the current buffer.
4988 */
4989 static void
4990ex_doautocmd(eap)
4991 exarg_T *eap;
4992{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004993 char_u *arg = eap->arg;
4994 int call_do_modelines = check_nomodeline(&arg);
4995
4996 (void)do_doautocmd(arg, TRUE);
4997 if (call_do_modelines) /* Only when there is no <nomodeline>. */
4998 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999}
5000#endif
5001
5002#ifdef FEAT_LISTCMDS
5003/*
5004 * :[N]bunload[!] [N] [bufname] unload buffer
5005 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5006 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5007 */
5008 static void
5009ex_bunload(eap)
5010 exarg_T *eap;
5011{
5012 eap->errmsg = do_bufdel(
5013 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5014 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5015 : DOBUF_UNLOAD, eap->arg,
5016 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5017}
5018
5019/*
5020 * :[N]buffer [N] to buffer N
5021 * :[N]sbuffer [N] to buffer N
5022 */
5023 static void
5024ex_buffer(eap)
5025 exarg_T *eap;
5026{
5027 if (*eap->arg)
5028 eap->errmsg = e_trailing;
5029 else
5030 {
5031 if (eap->addr_count == 0) /* default is current buffer */
5032 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5033 else
5034 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5035 }
5036}
5037
5038/*
5039 * :[N]bmodified [N] to next mod. buffer
5040 * :[N]sbmodified [N] to next mod. buffer
5041 */
5042 static void
5043ex_bmodified(eap)
5044 exarg_T *eap;
5045{
5046 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5047}
5048
5049/*
5050 * :[N]bnext [N] to next buffer
5051 * :[N]sbnext [N] split and to next buffer
5052 */
5053 static void
5054ex_bnext(eap)
5055 exarg_T *eap;
5056{
5057 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5058}
5059
5060/*
5061 * :[N]bNext [N] to previous buffer
5062 * :[N]bprevious [N] to previous buffer
5063 * :[N]sbNext [N] split and to previous buffer
5064 * :[N]sbprevious [N] split and to previous buffer
5065 */
5066 static void
5067ex_bprevious(eap)
5068 exarg_T *eap;
5069{
5070 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5071}
5072
5073/*
5074 * :brewind to first buffer
5075 * :bfirst to first buffer
5076 * :sbrewind split and to first buffer
5077 * :sbfirst split and to first buffer
5078 */
5079 static void
5080ex_brewind(eap)
5081 exarg_T *eap;
5082{
5083 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5084}
5085
5086/*
5087 * :blast to last buffer
5088 * :sblast split and to last buffer
5089 */
5090 static void
5091ex_blast(eap)
5092 exarg_T *eap;
5093{
5094 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5095}
5096#endif
5097
5098 int
5099ends_excmd(c)
5100 int c;
5101{
5102 return (c == NUL || c == '|' || c == '"' || c == '\n');
5103}
5104
5105#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5106 || defined(PROTO)
5107/*
5108 * Return the next command, after the first '|' or '\n'.
5109 * Return NULL if not found.
5110 */
5111 char_u *
5112find_nextcmd(p)
5113 char_u *p;
5114{
5115 while (*p != '|' && *p != '\n')
5116 {
5117 if (*p == NUL)
5118 return NULL;
5119 ++p;
5120 }
5121 return (p + 1);
5122}
5123#endif
5124
5125/*
5126 * Check if *p is a separator between Ex commands.
5127 * Return NULL if it isn't, (p + 1) if it is.
5128 */
5129 char_u *
5130check_nextcmd(p)
5131 char_u *p;
5132{
5133 p = skipwhite(p);
5134 if (*p == '|' || *p == '\n')
5135 return (p + 1);
5136 else
5137 return NULL;
5138}
5139
5140/*
5141 * - if there are more files to edit
5142 * - and this is the last window
5143 * - and forceit not used
5144 * - and not repeated twice on a row
5145 * return FAIL and give error message if 'message' TRUE
5146 * return OK otherwise
5147 */
5148 static int
5149check_more(message, forceit)
5150 int message; /* when FALSE check only, no messages */
5151 int forceit;
5152{
5153 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5154
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005155 if (!forceit && only_one_window()
5156 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
5158 if (message)
5159 {
5160#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5161 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5162 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005163 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005166 vim_strncpy(buff,
5167 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005168 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005170 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 _("%d more files to edit. Quit anyway?"), n);
5172 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5173 return OK;
5174 return FAIL;
5175 }
5176#endif
5177 if (n == 1)
5178 EMSG(_("E173: 1 more file to edit"));
5179 else
5180 EMSGN(_("E173: %ld more files to edit"), n);
5181 quitmore = 2; /* next try to quit is allowed */
5182 }
5183 return FAIL;
5184 }
5185 return OK;
5186}
5187
5188#ifdef FEAT_CMDL_COMPL
5189/*
5190 * Function given to ExpandGeneric() to obtain the list of command names.
5191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 char_u *
5193get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005194 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 int idx;
5196{
5197 if (idx >= (int)CMD_SIZE)
5198# ifdef FEAT_USR_CMDS
5199 return get_user_command_name(idx);
5200# else
5201 return NULL;
5202# endif
5203 return cmdnames[idx].cmd_name;
5204}
5205#endif
5206
5207#if defined(FEAT_USR_CMDS) || defined(PROTO)
5208static 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));
5209static void uc_list __ARGS((char_u *name, size_t name_len));
5210static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5211static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5212static 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));
5213
5214 static int
5215uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5216 char_u *name;
5217 size_t name_len;
5218 char_u *rep;
5219 long argt;
5220 long def;
5221 int flags;
5222 int compl;
5223 char_u *compl_arg;
5224 int force;
5225{
5226 ucmd_T *cmd = NULL;
5227 char_u *p;
5228 int i;
5229 int cmp = 1;
5230 char_u *rep_buf = NULL;
5231 garray_T *gap;
5232
Bram Moolenaar9c102382006-05-03 21:26:49 +00005233 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if (rep_buf == NULL)
5235 {
5236 /* Can't replace termcodes - try using the string as is */
5237 rep_buf = vim_strsave(rep);
5238
5239 /* Give up if out of memory */
5240 if (rep_buf == NULL)
5241 return FAIL;
5242 }
5243
5244 /* get address of growarray: global or in curbuf */
5245 if (flags & UC_BUFFER)
5246 {
5247 gap = &curbuf->b_ucmds;
5248 if (gap->ga_itemsize == 0)
5249 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5250 }
5251 else
5252 gap = &ucmds;
5253
5254 /* Search for the command in the already defined commands. */
5255 for (i = 0; i < gap->ga_len; ++i)
5256 {
5257 size_t len;
5258
5259 cmd = USER_CMD_GA(gap, i);
5260 len = STRLEN(cmd->uc_name);
5261 cmp = STRNCMP(name, cmd->uc_name, name_len);
5262 if (cmp == 0)
5263 {
5264 if (name_len < len)
5265 cmp = -1;
5266 else if (name_len > len)
5267 cmp = 1;
5268 }
5269
5270 if (cmp == 0)
5271 {
5272 if (!force)
5273 {
5274 EMSG(_("E174: Command already exists: add ! to replace it"));
5275 goto fail;
5276 }
5277
5278 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005279 cmd->uc_rep = NULL;
5280#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5281 vim_free(cmd->uc_compl_arg);
5282 cmd->uc_compl_arg = NULL;
5283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 break;
5285 }
5286
5287 /* Stop as soon as we pass the name to add */
5288 if (cmp < 0)
5289 break;
5290 }
5291
5292 /* Extend the array unless we're replacing an existing command */
5293 if (cmp != 0)
5294 {
5295 if (ga_grow(gap, 1) != OK)
5296 goto fail;
5297 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5298 goto fail;
5299
5300 cmd = USER_CMD_GA(gap, i);
5301 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5302
5303 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305 cmd->uc_name = p;
5306 }
5307
5308 cmd->uc_rep = rep_buf;
5309 cmd->uc_argt = argt;
5310 cmd->uc_def = def;
5311 cmd->uc_compl = compl;
5312#ifdef FEAT_EVAL
5313 cmd->uc_scriptID = current_SID;
5314# ifdef FEAT_CMDL_COMPL
5315 cmd->uc_compl_arg = compl_arg;
5316# endif
5317#endif
5318
5319 return OK;
5320
5321fail:
5322 vim_free(rep_buf);
5323#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5324 vim_free(compl_arg);
5325#endif
5326 return FAIL;
5327}
5328
5329/*
5330 * List of names for completion for ":command" with the EXPAND_ flag.
5331 * Must be alphabetical for completion.
5332 */
5333static struct
5334{
5335 int expand;
5336 char *name;
5337} command_complete[] =
5338{
5339 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005340 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005342 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005344 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005345#if defined(FEAT_CSCOPE)
5346 {EXPAND_CSCOPE, "cscope"},
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5349 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005350 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#endif
5352 {EXPAND_DIRECTORIES, "dir"},
5353 {EXPAND_ENV_VARS, "environment"},
5354 {EXPAND_EVENTS, "event"},
5355 {EXPAND_EXPRESSION, "expression"},
5356 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005357 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005358 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {EXPAND_FUNCTIONS, "function"},
5360 {EXPAND_HELP, "help"},
5361 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005362#if defined(FEAT_CMDHIST)
5363 {EXPAND_HISTORY, "history"},
5364#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005365#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005366 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005367 {EXPAND_LOCALES, "locale"},
5368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {EXPAND_MAPPINGS, "mapping"},
5370 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005371 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005373 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005374#if defined(FEAT_SIGNS)
5375 {EXPAND_SIGN, "sign"},
5376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {EXPAND_TAGS, "tag"},
5378 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5379 {EXPAND_USER_VARS, "var"},
5380 {0, NULL}
5381};
5382
5383 static void
5384uc_list(name, name_len)
5385 char_u *name;
5386 size_t name_len;
5387{
5388 int i, j;
5389 int found = FALSE;
5390 ucmd_T *cmd;
5391 int len;
5392 long a;
5393 garray_T *gap;
5394
5395 gap = &curbuf->b_ucmds;
5396 for (;;)
5397 {
5398 for (i = 0; i < gap->ga_len; ++i)
5399 {
5400 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005401 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 /* Skip commands which don't match the requested prefix */
5404 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5405 continue;
5406
5407 /* Put out the title first time */
5408 if (!found)
5409 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5410 found = TRUE;
5411 msg_putchar('\n');
5412 if (got_int)
5413 break;
5414
5415 /* Special cases */
5416 msg_putchar(a & BANG ? '!' : ' ');
5417 msg_putchar(a & REGSTR ? '"' : ' ');
5418 msg_putchar(gap != &ucmds ? 'b' : ' ');
5419 msg_putchar(' ');
5420
5421 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5422 len = (int)STRLEN(cmd->uc_name) + 4;
5423
5424 do {
5425 msg_putchar(' ');
5426 ++len;
5427 } while (len < 16);
5428
5429 len = 0;
5430
5431 /* Arguments */
5432 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5433 {
5434 case 0: IObuff[len++] = '0'; break;
5435 case (EXTRA): IObuff[len++] = '*'; break;
5436 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5437 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5438 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5439 }
5440
5441 do {
5442 IObuff[len++] = ' ';
5443 } while (len < 5);
5444
5445 /* Range */
5446 if (a & (RANGE|COUNT))
5447 {
5448 if (a & COUNT)
5449 {
5450 /* -count=N */
5451 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5452 len += (int)STRLEN(IObuff + len);
5453 }
5454 else if (a & DFLALL)
5455 IObuff[len++] = '%';
5456 else if (cmd->uc_def >= 0)
5457 {
5458 /* -range=N */
5459 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5460 len += (int)STRLEN(IObuff + len);
5461 }
5462 else
5463 IObuff[len++] = '.';
5464 }
5465
5466 do {
5467 IObuff[len++] = ' ';
5468 } while (len < 11);
5469
5470 /* Completion */
5471 for (j = 0; command_complete[j].expand != 0; ++j)
5472 if (command_complete[j].expand == cmd->uc_compl)
5473 {
5474 STRCPY(IObuff + len, command_complete[j].name);
5475 len += (int)STRLEN(IObuff + len);
5476 break;
5477 }
5478
5479 do {
5480 IObuff[len++] = ' ';
5481 } while (len < 21);
5482
5483 IObuff[len] = '\0';
5484 msg_outtrans(IObuff);
5485
5486 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005487#ifdef FEAT_EVAL
5488 if (p_verbose > 0)
5489 last_set_msg(cmd->uc_scriptID);
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 out_flush();
5492 ui_breakcheck();
5493 if (got_int)
5494 break;
5495 }
5496 if (gap == &ucmds || i < gap->ga_len)
5497 break;
5498 gap = &ucmds;
5499 }
5500
5501 if (!found)
5502 MSG(_("No user-defined commands found"));
5503}
5504
5505 static char_u *
5506uc_fun_cmd()
5507{
5508 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5509 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5510 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5511 0xb9, 0x7f, 0};
5512 int i;
5513
5514 for (i = 0; fcmd[i]; ++i)
5515 IObuff[i] = fcmd[i] - 0x40;
5516 IObuff[i] = 0;
5517 return IObuff;
5518}
5519
5520 static int
5521uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5522 char_u *attr;
5523 size_t len;
5524 long *argt;
5525 long *def;
5526 int *flags;
5527 int *compl;
5528 char_u **compl_arg;
5529{
5530 char_u *p;
5531
5532 if (len == 0)
5533 {
5534 EMSG(_("E175: No attribute specified"));
5535 return FAIL;
5536 }
5537
5538 /* First, try the simple attributes (no arguments) */
5539 if (STRNICMP(attr, "bang", len) == 0)
5540 *argt |= BANG;
5541 else if (STRNICMP(attr, "buffer", len) == 0)
5542 *flags |= UC_BUFFER;
5543 else if (STRNICMP(attr, "register", len) == 0)
5544 *argt |= REGSTR;
5545 else if (STRNICMP(attr, "bar", len) == 0)
5546 *argt |= TRLBAR;
5547 else
5548 {
5549 int i;
5550 char_u *val = NULL;
5551 size_t vallen = 0;
5552 size_t attrlen = len;
5553
5554 /* Look for the attribute name - which is the part before any '=' */
5555 for (i = 0; i < (int)len; ++i)
5556 {
5557 if (attr[i] == '=')
5558 {
5559 val = &attr[i + 1];
5560 vallen = len - i - 1;
5561 attrlen = i;
5562 break;
5563 }
5564 }
5565
5566 if (STRNICMP(attr, "nargs", attrlen) == 0)
5567 {
5568 if (vallen == 1)
5569 {
5570 if (*val == '0')
5571 /* Do nothing - this is the default */;
5572 else if (*val == '1')
5573 *argt |= (EXTRA | NOSPC | NEEDARG);
5574 else if (*val == '*')
5575 *argt |= EXTRA;
5576 else if (*val == '?')
5577 *argt |= (EXTRA | NOSPC);
5578 else if (*val == '+')
5579 *argt |= (EXTRA | NEEDARG);
5580 else
5581 goto wrong_nargs;
5582 }
5583 else
5584 {
5585wrong_nargs:
5586 EMSG(_("E176: Invalid number of arguments"));
5587 return FAIL;
5588 }
5589 }
5590 else if (STRNICMP(attr, "range", attrlen) == 0)
5591 {
5592 *argt |= RANGE;
5593 if (vallen == 1 && *val == '%')
5594 *argt |= DFLALL;
5595 else if (val != NULL)
5596 {
5597 p = val;
5598 if (*def >= 0)
5599 {
5600two_count:
5601 EMSG(_("E177: Count cannot be specified twice"));
5602 return FAIL;
5603 }
5604
5605 *def = getdigits(&p);
5606 *argt |= (ZEROR | NOTADR);
5607
5608 if (p != val + vallen || vallen == 0)
5609 {
5610invalid_count:
5611 EMSG(_("E178: Invalid default value for count"));
5612 return FAIL;
5613 }
5614 }
5615 }
5616 else if (STRNICMP(attr, "count", attrlen) == 0)
5617 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005618 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 if (val != NULL)
5621 {
5622 p = val;
5623 if (*def >= 0)
5624 goto two_count;
5625
5626 *def = getdigits(&p);
5627
5628 if (p != val + vallen)
5629 goto invalid_count;
5630 }
5631
5632 if (*def < 0)
5633 *def = 0;
5634 }
5635 else if (STRNICMP(attr, "complete", attrlen) == 0)
5636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (val == NULL)
5638 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005639 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 return FAIL;
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005643 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5644 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 else
5648 {
5649 char_u ch = attr[len];
5650 attr[len] = '\0';
5651 EMSG2(_("E181: Invalid attribute: %s"), attr);
5652 attr[len] = ch;
5653 return FAIL;
5654 }
5655 }
5656
5657 return OK;
5658}
5659
Bram Moolenaara850a712009-01-28 14:42:59 +00005660/*
5661 * ":command ..."
5662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 static void
5664ex_command(eap)
5665 exarg_T *eap;
5666{
5667 char_u *name;
5668 char_u *end;
5669 char_u *p;
5670 long argt = 0;
5671 long def = -1;
5672 int flags = 0;
5673 int compl = EXPAND_NOTHING;
5674 char_u *compl_arg = NULL;
5675 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005676 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 p = eap->arg;
5679
5680 /* Check for attributes */
5681 while (*p == '-')
5682 {
5683 ++p;
5684 end = skiptowhite(p);
5685 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5686 == FAIL)
5687 return;
5688 p = skipwhite(end);
5689 }
5690
5691 /* Get the name (if any) and skip to the following argument */
5692 name = p;
5693 if (ASCII_ISALPHA(*p))
5694 while (ASCII_ISALNUM(*p))
5695 ++p;
5696 if (!ends_excmd(*p) && !vim_iswhite(*p))
5697 {
5698 EMSG(_("E182: Invalid command name"));
5699 return;
5700 }
5701 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005702 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704 /* If there is nothing after the name, and no attributes were specified,
5705 * we are listing commands
5706 */
5707 p = skipwhite(end);
5708 if (!has_attr && ends_excmd(*p))
5709 {
5710 uc_list(name, end - name);
5711 }
5712 else if (!ASCII_ISUPPER(*name))
5713 {
5714 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5715 return;
5716 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005717 else if ((name_len == 1 && *name == 'X')
5718 || (name_len <= 4
5719 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5720 {
5721 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5722 return;
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 else
5725 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5726 eap->forceit);
5727}
5728
5729/*
5730 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 * Clear all user commands, global and for current buffer.
5732 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005733 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005735 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 uc_clear(&ucmds);
5738 uc_clear(&curbuf->b_ucmds);
5739}
5740
5741/*
5742 * Clear all user commands for "gap".
5743 */
5744 void
5745uc_clear(gap)
5746 garray_T *gap;
5747{
5748 int i;
5749 ucmd_T *cmd;
5750
5751 for (i = 0; i < gap->ga_len; ++i)
5752 {
5753 cmd = USER_CMD_GA(gap, i);
5754 vim_free(cmd->uc_name);
5755 vim_free(cmd->uc_rep);
5756# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5757 vim_free(cmd->uc_compl_arg);
5758# endif
5759 }
5760 ga_clear(gap);
5761}
5762
5763 static void
5764ex_delcommand(eap)
5765 exarg_T *eap;
5766{
5767 int i = 0;
5768 ucmd_T *cmd = NULL;
5769 int cmp = -1;
5770 garray_T *gap;
5771
5772 gap = &curbuf->b_ucmds;
5773 for (;;)
5774 {
5775 for (i = 0; i < gap->ga_len; ++i)
5776 {
5777 cmd = USER_CMD_GA(gap, i);
5778 cmp = STRCMP(eap->arg, cmd->uc_name);
5779 if (cmp <= 0)
5780 break;
5781 }
5782 if (gap == &ucmds || cmp == 0)
5783 break;
5784 gap = &ucmds;
5785 }
5786
5787 if (cmp != 0)
5788 {
5789 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5790 return;
5791 }
5792
5793 vim_free(cmd->uc_name);
5794 vim_free(cmd->uc_rep);
5795# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5796 vim_free(cmd->uc_compl_arg);
5797# endif
5798
5799 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
5801 if (i < gap->ga_len)
5802 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5803}
5804
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005805/*
5806 * split and quote args for <f-args>
5807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 static char_u *
5809uc_split_args(arg, lenp)
5810 char_u *arg;
5811 size_t *lenp;
5812{
5813 char_u *buf;
5814 char_u *p;
5815 char_u *q;
5816 int len;
5817
5818 /* Precalculate length */
5819 p = arg;
5820 len = 2; /* Initial and final quotes */
5821
5822 while (*p)
5823 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005824 if (p[0] == '\\' && p[1] == '\\')
5825 {
5826 len += 2;
5827 p += 2;
5828 }
5829 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
5831 len += 1;
5832 p += 2;
5833 }
5834 else if (*p == '\\' || *p == '"')
5835 {
5836 len += 2;
5837 p += 1;
5838 }
5839 else if (vim_iswhite(*p))
5840 {
5841 p = skipwhite(p);
5842 if (*p == NUL)
5843 break;
5844 len += 3; /* "," */
5845 }
5846 else
5847 {
5848 ++len;
5849 ++p;
5850 }
5851 }
5852
5853 buf = alloc(len + 1);
5854 if (buf == NULL)
5855 {
5856 *lenp = 0;
5857 return buf;
5858 }
5859
5860 p = arg;
5861 q = buf;
5862 *q++ = '"';
5863 while (*p)
5864 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005865 if (p[0] == '\\' && p[1] == '\\')
5866 {
5867 *q++ = '\\';
5868 *q++ = '\\';
5869 p += 2;
5870 }
5871 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
5873 *q++ = p[1];
5874 p += 2;
5875 }
5876 else if (*p == '\\' || *p == '"')
5877 {
5878 *q++ = '\\';
5879 *q++ = *p++;
5880 }
5881 else if (vim_iswhite(*p))
5882 {
5883 p = skipwhite(p);
5884 if (*p == NUL)
5885 break;
5886 *q++ = '"';
5887 *q++ = ',';
5888 *q++ = '"';
5889 }
5890 else
5891 {
5892 *q++ = *p++;
5893 }
5894 }
5895 *q++ = '"';
5896 *q = 0;
5897
5898 *lenp = len;
5899 return buf;
5900}
5901
5902/*
5903 * Check for a <> code in a user command.
5904 * "code" points to the '<'. "len" the length of the <> (inclusive).
5905 * "buf" is where the result is to be added.
5906 * "split_buf" points to a buffer used for splitting, caller should free it.
5907 * "split_len" is the length of what "split_buf" contains.
5908 * Returns the length of the replacement, which has been added to "buf".
5909 * Returns -1 if there was no match, and only the "<" has been copied.
5910 */
5911 static size_t
5912uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5913 char_u *code;
5914 size_t len;
5915 char_u *buf;
5916 ucmd_T *cmd; /* the user command we're expanding */
5917 exarg_T *eap; /* ex arguments */
5918 char_u **split_buf;
5919 size_t *split_len;
5920{
5921 size_t result = 0;
5922 char_u *p = code + 1;
5923 size_t l = len - 2;
5924 int quote = 0;
5925 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5926 ct_LT, ct_NONE } type = ct_NONE;
5927
5928 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5929 {
5930 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5931 p += 2;
5932 l -= 2;
5933 }
5934
Bram Moolenaar371d5402006-03-20 21:47:49 +00005935 ++l;
5936 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005938 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005940 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005942 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005944 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005946 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005948 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005950 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 type = ct_REGISTER;
5952
5953 switch (type)
5954 {
5955 case ct_ARGS:
5956 /* Simple case first */
5957 if (*eap->arg == NUL)
5958 {
5959 if (quote == 1)
5960 {
5961 result = 2;
5962 if (buf != NULL)
5963 STRCPY(buf, "''");
5964 }
5965 else
5966 result = 0;
5967 break;
5968 }
5969
5970 /* When specified there is a single argument don't split it.
5971 * Works for ":Cmd %" when % is "a b c". */
5972 if ((eap->argt & NOSPC) && quote == 2)
5973 quote = 1;
5974
5975 switch (quote)
5976 {
5977 case 0: /* No quoting, no splitting */
5978 result = STRLEN(eap->arg);
5979 if (buf != NULL)
5980 STRCPY(buf, eap->arg);
5981 break;
5982 case 1: /* Quote, but don't split */
5983 result = STRLEN(eap->arg) + 2;
5984 for (p = eap->arg; *p; ++p)
5985 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01005986#ifdef FEAT_MBYTE
5987 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
5988 /* DBCS can contain \ in a trail byte, skip the
5989 * double-byte character. */
5990 ++p;
5991 else
5992#endif
5993 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 ++result;
5995 }
5996
5997 if (buf != NULL)
5998 {
5999 *buf++ = '"';
6000 for (p = eap->arg; *p; ++p)
6001 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006002#ifdef FEAT_MBYTE
6003 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6004 /* DBCS can contain \ in a trail byte, copy the
6005 * double-byte character to avoid escaping. */
6006 *buf++ = *p++;
6007 else
6008#endif
6009 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 *buf++ = '\\';
6011 *buf++ = *p;
6012 }
6013 *buf = '"';
6014 }
6015
6016 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006017 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 /* This is hard, so only do it once, and cache the result */
6019 if (*split_buf == NULL)
6020 *split_buf = uc_split_args(eap->arg, split_len);
6021
6022 result = *split_len;
6023 if (buf != NULL && result != 0)
6024 STRCPY(buf, *split_buf);
6025
6026 break;
6027 }
6028 break;
6029
6030 case ct_BANG:
6031 result = eap->forceit ? 1 : 0;
6032 if (quote)
6033 result += 2;
6034 if (buf != NULL)
6035 {
6036 if (quote)
6037 *buf++ = '"';
6038 if (eap->forceit)
6039 *buf++ = '!';
6040 if (quote)
6041 *buf = '"';
6042 }
6043 break;
6044
6045 case ct_LINE1:
6046 case ct_LINE2:
6047 case ct_COUNT:
6048 {
6049 char num_buf[20];
6050 long num = (type == ct_LINE1) ? eap->line1 :
6051 (type == ct_LINE2) ? eap->line2 :
6052 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6053 size_t num_len;
6054
6055 sprintf(num_buf, "%ld", num);
6056 num_len = STRLEN(num_buf);
6057 result = num_len;
6058
6059 if (quote)
6060 result += 2;
6061
6062 if (buf != NULL)
6063 {
6064 if (quote)
6065 *buf++ = '"';
6066 STRCPY(buf, num_buf);
6067 buf += num_len;
6068 if (quote)
6069 *buf = '"';
6070 }
6071
6072 break;
6073 }
6074
6075 case ct_REGISTER:
6076 result = eap->regname ? 1 : 0;
6077 if (quote)
6078 result += 2;
6079 if (buf != NULL)
6080 {
6081 if (quote)
6082 *buf++ = '\'';
6083 if (eap->regname)
6084 *buf++ = eap->regname;
6085 if (quote)
6086 *buf = '\'';
6087 }
6088 break;
6089
6090 case ct_LT:
6091 result = 1;
6092 if (buf != NULL)
6093 *buf = '<';
6094 break;
6095
6096 default:
6097 /* Not recognized: just copy the '<' and return -1. */
6098 result = (size_t)-1;
6099 if (buf != NULL)
6100 *buf = '<';
6101 break;
6102 }
6103
6104 return result;
6105}
6106
6107 static void
6108do_ucmd(eap)
6109 exarg_T *eap;
6110{
6111 char_u *buf;
6112 char_u *p;
6113 char_u *q;
6114
6115 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006116 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006117 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 size_t len, totlen;
6119
6120 size_t split_len = 0;
6121 char_u *split_buf = NULL;
6122 ucmd_T *cmd;
6123#ifdef FEAT_EVAL
6124 scid_T save_current_SID = current_SID;
6125#endif
6126
6127 if (eap->cmdidx == CMD_USER)
6128 cmd = USER_CMD(eap->useridx);
6129 else
6130 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6131
6132 /*
6133 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006134 * First round: "buf" is NULL, compute length, allocate "buf".
6135 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 */
6137 buf = NULL;
6138 for (;;)
6139 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006140 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006141 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006143
6144 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006146 start = vim_strchr(p, '<');
6147 if (start != NULL)
6148 end = vim_strchr(start + 1, '>');
6149 if (buf != NULL)
6150 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006151 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6152 ;
6153 if (*ksp == K_SPECIAL
6154 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006155 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6156# ifdef FEAT_GUI
6157 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6158# endif
6159 ))
6160 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006161 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006162 * KS_SPECIAL KE_FILLER, like for mappings, but
6163 * do_cmdline() doesn't handle that, so convert it back.
6164 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6165 len = ksp - p;
6166 if (len > 0)
6167 {
6168 mch_memmove(q, p, len);
6169 q += len;
6170 }
6171 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6172 p = ksp + 3;
6173 continue;
6174 }
6175 }
6176
6177 /* break if there no <item> is found */
6178 if (start == NULL || end == NULL)
6179 break;
6180
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 /* Include the '>' */
6182 ++end;
6183
6184 /* Take everything up to the '<' */
6185 len = start - p;
6186 if (buf == NULL)
6187 totlen += len;
6188 else
6189 {
6190 mch_memmove(q, p, len);
6191 q += len;
6192 }
6193
6194 len = uc_check_code(start, end - start, q, cmd, eap,
6195 &split_buf, &split_len);
6196 if (len == (size_t)-1)
6197 {
6198 /* no match, continue after '<' */
6199 p = start + 1;
6200 len = 1;
6201 }
6202 else
6203 p = end;
6204 if (buf == NULL)
6205 totlen += len;
6206 else
6207 q += len;
6208 }
6209 if (buf != NULL) /* second time here, finished */
6210 {
6211 STRCPY(q, p);
6212 break;
6213 }
6214
6215 totlen += STRLEN(p); /* Add on the trailing characters */
6216 buf = alloc((unsigned)(totlen + 1));
6217 if (buf == NULL)
6218 {
6219 vim_free(split_buf);
6220 return;
6221 }
6222 }
6223
6224#ifdef FEAT_EVAL
6225 current_SID = cmd->uc_scriptID;
6226#endif
6227 (void)do_cmdline(buf, eap->getline, eap->cookie,
6228 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6229#ifdef FEAT_EVAL
6230 current_SID = save_current_SID;
6231#endif
6232 vim_free(buf);
6233 vim_free(split_buf);
6234}
6235
6236# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6237 static char_u *
6238get_user_command_name(idx)
6239 int idx;
6240{
6241 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6242}
6243
6244/*
6245 * Function given to ExpandGeneric() to obtain the list of user command names.
6246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 char_u *
6248get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006249 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 int idx;
6251{
6252 if (idx < curbuf->b_ucmds.ga_len)
6253 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6254 idx -= curbuf->b_ucmds.ga_len;
6255 if (idx < ucmds.ga_len)
6256 return USER_CMD(idx)->uc_name;
6257 return NULL;
6258}
6259
6260/*
6261 * Function given to ExpandGeneric() to obtain the list of user command
6262 * attributes.
6263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 char_u *
6265get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006266 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 int idx;
6268{
6269 static char *user_cmd_flags[] =
6270 {"bang", "bar", "buffer", "complete", "count",
6271 "nargs", "range", "register"};
6272
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006273 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 return NULL;
6275 return (char_u *)user_cmd_flags[idx];
6276}
6277
6278/*
6279 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 char_u *
6282get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006283 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 int idx;
6285{
6286 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6287
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006288 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 return NULL;
6290 return (char_u *)user_cmd_nargs[idx];
6291}
6292
6293/*
6294 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 char_u *
6297get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006298 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 int idx;
6300{
6301 return (char_u *)command_complete[idx].name;
6302}
6303# endif /* FEAT_CMDL_COMPL */
6304
6305#endif /* FEAT_USR_CMDS */
6306
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006307#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6308/*
6309 * Parse a completion argument "value[vallen]".
6310 * The detected completion goes in "*complp", argument type in "*argt".
6311 * When there is an argument, for function and user defined completion, it's
6312 * copied to allocated memory and stored in "*compl_arg".
6313 * Returns FAIL if something is wrong.
6314 */
6315 int
6316parse_compl_arg(value, vallen, complp, argt, compl_arg)
6317 char_u *value;
6318 int vallen;
6319 int *complp;
6320 long *argt;
6321 char_u **compl_arg;
6322{
6323 char_u *arg = NULL;
6324 size_t arglen = 0;
6325 int i;
6326 int valend = vallen;
6327
6328 /* Look for any argument part - which is the part after any ',' */
6329 for (i = 0; i < vallen; ++i)
6330 {
6331 if (value[i] == ',')
6332 {
6333 arg = &value[i + 1];
6334 arglen = vallen - i - 1;
6335 valend = i;
6336 break;
6337 }
6338 }
6339
6340 for (i = 0; command_complete[i].expand != 0; ++i)
6341 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006342 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006343 && STRNCMP(value, command_complete[i].name, valend) == 0)
6344 {
6345 *complp = command_complete[i].expand;
6346 if (command_complete[i].expand == EXPAND_BUFFERS)
6347 *argt |= BUFNAME;
6348 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6349 || command_complete[i].expand == EXPAND_FILES)
6350 *argt |= XFILE;
6351 break;
6352 }
6353 }
6354
6355 if (command_complete[i].expand == 0)
6356 {
6357 EMSG2(_("E180: Invalid complete value: %s"), value);
6358 return FAIL;
6359 }
6360
6361# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6362 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6363 && arg != NULL)
6364# else
6365 if (arg != NULL)
6366# endif
6367 {
6368 EMSG(_("E468: Completion argument only allowed for custom completion"));
6369 return FAIL;
6370 }
6371
6372# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6373 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6374 && arg == NULL)
6375 {
6376 EMSG(_("E467: Custom completion requires a function argument"));
6377 return FAIL;
6378 }
6379
6380 if (arg != NULL)
6381 *compl_arg = vim_strnsave(arg, (int)arglen);
6382# endif
6383 return OK;
6384}
6385#endif
6386
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 static void
6388ex_colorscheme(eap)
6389 exarg_T *eap;
6390{
Bram Moolenaare6850792010-05-14 15:28:44 +02006391 if (*eap->arg == NUL)
6392 {
6393#ifdef FEAT_EVAL
6394 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6395 char_u *p = NULL;
6396
6397 if (expr != NULL)
6398 {
6399 ++emsg_off;
6400 p = eval_to_string(expr, NULL, FALSE);
6401 --emsg_off;
6402 vim_free(expr);
6403 }
6404 if (p != NULL)
6405 {
6406 MSG(p);
6407 vim_free(p);
6408 }
6409 else
6410 MSG("default");
6411#else
6412 MSG(_("unknown"));
6413#endif
6414 }
6415 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6417}
6418
6419 static void
6420ex_highlight(eap)
6421 exarg_T *eap;
6422{
6423 if (*eap->arg == NUL && eap->cmd[2] == '!')
6424 MSG(_("Greetings, Vim user!"));
6425 do_highlight(eap->arg, eap->forceit, FALSE);
6426}
6427
6428
6429/*
6430 * Call this function if we thought we were going to exit, but we won't
6431 * (because of an error). May need to restore the terminal mode.
6432 */
6433 void
6434not_exiting()
6435{
6436 exiting = FALSE;
6437 settmode(TMODE_RAW);
6438}
6439
6440/*
6441 * ":quit": quit current window, quit Vim if closed the last window.
6442 */
6443 static void
6444ex_quit(eap)
6445 exarg_T *eap;
6446{
6447#ifdef FEAT_CMDWIN
6448 if (cmdwin_type != 0)
6449 {
6450 cmdwin_result = Ctrl_C;
6451 return;
6452 }
6453#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006454 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006455 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006456 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006457 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006458 return;
6459 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006461 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02006462 /* Refuse to quick when locked or when the buffer in the last window is
6463 * being closed (can only happen in autocommands). */
6464 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006465 return;
6466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468#ifdef FEAT_NETBEANS_INTG
6469 netbeansForcedQuit = eap->forceit;
6470#endif
6471
6472 /*
6473 * If there are more files or windows we won't exit.
6474 */
6475 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6476 exiting = TRUE;
6477 if ((!P_HID(curbuf)
6478 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6479 || check_more(TRUE, eap->forceit) == FAIL
6480 || (only_one_window() && check_changed_any(eap->forceit)))
6481 {
6482 not_exiting();
6483 }
6484 else
6485 {
6486#ifdef FEAT_WINDOWS
6487 if (only_one_window()) /* quit last window */
6488#endif
6489 getout(0);
6490#ifdef FEAT_WINDOWS
6491# ifdef FEAT_GUI
6492 need_mouse_correct = TRUE;
6493# endif
6494 /* close window; may free buffer */
6495 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6496#endif
6497 }
6498}
6499
6500/*
6501 * ":cquit".
6502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 static void
6504ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006505 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 getout(1); /* this does not always pass on the exit code to the Manx
6508 compiler. why? */
6509}
6510
6511/*
6512 * ":qall": try to quit all windows
6513 */
6514 static void
6515ex_quit_all(eap)
6516 exarg_T *eap;
6517{
6518# ifdef FEAT_CMDWIN
6519 if (cmdwin_type != 0)
6520 {
6521 if (eap->forceit)
6522 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6523 else
6524 cmdwin_result = K_XF2;
6525 return;
6526 }
6527# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006528
6529 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006530 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006531 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006532 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006533 return;
6534 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006535#ifdef FEAT_AUTOCMD
6536 if (curbuf_locked())
6537 return;
6538#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 exiting = TRUE;
6541 if (eap->forceit || !check_changed_any(FALSE))
6542 getout(0);
6543 not_exiting();
6544}
6545
Bram Moolenaard9967712006-03-11 21:18:15 +00006546#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547/*
6548 * ":close": close current window, unless it is the last one
6549 */
6550 static void
6551ex_close(eap)
6552 exarg_T *eap;
6553{
6554# ifdef FEAT_CMDWIN
6555 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006556 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 else
6558# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006559 if (!text_locked()
6560#ifdef FEAT_AUTOCMD
6561 && !curbuf_locked()
6562#endif
6563 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006564 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
6566
Bram Moolenaard9967712006-03-11 21:18:15 +00006567# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006568/*
6569 * ":pclose": Close any preview window.
6570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006572ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574{
6575 win_T *win;
6576
6577 for (win = firstwin; win != NULL; win = win->w_next)
6578 if (win->w_p_pvw)
6579 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006580 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006581 break;
6582 }
6583}
Bram Moolenaard9967712006-03-11 21:18:15 +00006584# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585
Bram Moolenaarf740b292006-02-16 22:11:02 +00006586/*
6587 * Close window "win" and take care of handling closing the last window for a
6588 * modified buffer.
6589 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006590 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006591ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006594 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 int need_hide;
6597 buf_T *buf = win->w_buffer;
6598
6599 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006600 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006602# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if ((p_confirm || cmdmod.confirm) && p_write)
6604 {
6605 dialog_changed(buf, FALSE);
6606 if (buf_valid(buf) && bufIsChanged(buf))
6607 return;
6608 need_hide = FALSE;
6609 }
6610 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 {
6613 EMSG(_(e_nowrtmsg));
6614 return;
6615 }
6616 }
6617
Bram Moolenaard9967712006-03-11 21:18:15 +00006618# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006620# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006623 if (tp == NULL)
6624 win_close(win, !need_hide && !P_HID(buf));
6625 else
6626 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
6628
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006630 * ":tabclose": close current tab page, unless it is the last one.
6631 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
6633 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006634ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 exarg_T *eap;
6636{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006637 tabpage_T *tp;
6638
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006639# ifdef FEAT_CMDWIN
6640 if (cmdwin_type != 0)
6641 cmdwin_result = K_IGNORE;
6642 else
6643# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006644 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006645 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006648 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006650 tp = find_tabpage((int)eap->line2);
6651 if (tp == NULL)
6652 {
6653 beep_flush();
6654 return;
6655 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006656 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006657 {
6658 tabpage_close_other(tp, eap->forceit);
6659 return;
6660 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006662 if (!text_locked()
6663#ifdef FEAT_AUTOCMD
6664 && !curbuf_locked()
6665#endif
6666 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006667 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006669}
6670
6671/*
6672 * ":tabonly": close all tab pages except the current one
6673 */
6674 static void
6675ex_tabonly(eap)
6676 exarg_T *eap;
6677{
6678 tabpage_T *tp;
6679 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006680
6681# ifdef FEAT_CMDWIN
6682 if (cmdwin_type != 0)
6683 cmdwin_result = K_IGNORE;
6684 else
6685# endif
6686 if (first_tabpage->tp_next == NULL)
6687 MSG(_("Already only one tab page"));
6688 else
6689 {
6690 /* Repeat this up to a 1000 times, because autocommands may mess
6691 * up the lists. */
6692 for (done = 0; done < 1000; ++done)
6693 {
6694 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6695 if (tp->tp_topframe != topframe)
6696 {
6697 tabpage_close_other(tp, eap->forceit);
6698 /* if we failed to close it quit */
6699 if (valid_tabpage(tp))
6700 done = 1000;
6701 /* start over, "tp" is now invalid */
6702 break;
6703 }
6704 if (first_tabpage->tp_next == NULL)
6705 break;
6706 }
6707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006711 * Close the current tab page.
6712 */
6713 void
6714tabpage_close(forceit)
6715 int forceit;
6716{
6717 /* First close all the windows but the current one. If that worked then
6718 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006719 if (lastwin != firstwin)
6720 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006721 if (lastwin == firstwin)
6722 ex_win_close(forceit, curwin, NULL);
6723# ifdef FEAT_GUI
6724 need_mouse_correct = TRUE;
6725# endif
6726}
6727
6728/*
6729 * Close tab page "tp", which is not the current tab page.
6730 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006731 * Also takes care of the tab pages line disappearing when closing the
6732 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006733 */
6734 void
6735tabpage_close_other(tp, forceit)
6736 tabpage_T *tp;
6737 int forceit;
6738{
6739 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006740 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006741 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006742
6743 /* Limit to 1000 windows, autocommands may add a window while we close
6744 * one. OK, so I'm paranoid... */
6745 while (++done < 1000)
6746 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006747 wp = tp->tp_firstwin;
6748 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006749
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006750 /* Autocommands may delete the tab page under our fingers and we may
6751 * fail to close a window with a modified buffer. */
6752 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006753 break;
6754 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006755
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006756 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006757 if (h != tabline_height())
6758 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006759}
6760
6761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 * ":only".
6763 */
6764 static void
6765ex_only(eap)
6766 exarg_T *eap;
6767{
6768# ifdef FEAT_GUI
6769 need_mouse_correct = TRUE;
6770# endif
6771 close_others(TRUE, eap->forceit);
6772}
6773
6774/*
6775 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006776 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006778 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779ex_all(eap)
6780 exarg_T *eap;
6781{
6782 if (eap->addr_count == 0)
6783 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006784 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785}
6786#endif /* FEAT_WINDOWS */
6787
6788 static void
6789ex_hide(eap)
6790 exarg_T *eap;
6791{
6792 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6793 eap->errmsg = e_invarg;
6794 else
6795 {
6796 /* ":hide" or ":hide | cmd": hide current window */
6797 eap->nextcmd = check_nextcmd(eap->arg);
6798#ifdef FEAT_WINDOWS
6799 if (!eap->skip)
6800 {
6801# ifdef FEAT_GUI
6802 need_mouse_correct = TRUE;
6803# endif
6804 win_close(curwin, FALSE); /* don't free buffer */
6805 }
6806#endif
6807 }
6808}
6809
6810/*
6811 * ":stop" and ":suspend": Suspend Vim.
6812 */
6813 static void
6814ex_stop(eap)
6815 exarg_T *eap;
6816{
6817 /*
6818 * Disallow suspending for "rvim".
6819 */
6820 if (!check_restricted()
6821#ifdef WIN3264
6822 /*
6823 * Check if external commands are allowed now.
6824 */
6825 && can_end_termcap_mode(TRUE)
6826#endif
6827 )
6828 {
6829 if (!eap->forceit)
6830 autowrite_all();
6831 windgoto((int)Rows - 1, 0);
6832 out_char('\n');
6833 out_flush();
6834 stoptermcap();
6835 out_flush(); /* needed for SUN to restore xterm buffer */
6836#ifdef FEAT_TITLE
6837 mch_restore_title(3); /* restore window titles */
6838#endif
6839 ui_suspend(); /* call machine specific function */
6840#ifdef FEAT_TITLE
6841 maketitle();
6842 resettitle(); /* force updating the title */
6843#endif
6844 starttermcap();
6845 scroll_start(); /* scroll screen before redrawing */
6846 redraw_later_clear();
6847 shell_resized(); /* may have resized window */
6848 }
6849}
6850
6851/*
6852 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6853 */
6854 static void
6855ex_exit(eap)
6856 exarg_T *eap;
6857{
6858#ifdef FEAT_CMDWIN
6859 if (cmdwin_type != 0)
6860 {
6861 cmdwin_result = Ctrl_C;
6862 return;
6863 }
6864#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006865 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006866 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006867 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006868 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006869 return;
6870 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006871#ifdef FEAT_AUTOCMD
6872 if (curbuf_locked())
6873 return;
6874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
6876 /*
6877 * if more files or windows we won't exit
6878 */
6879 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6880 exiting = TRUE;
6881 if ( ((eap->cmdidx == CMD_wq
6882 || curbufIsChanged())
6883 && do_write(eap) == FAIL)
6884 || check_more(TRUE, eap->forceit) == FAIL
6885 || (only_one_window() && check_changed_any(eap->forceit)))
6886 {
6887 not_exiting();
6888 }
6889 else
6890 {
6891#ifdef FEAT_WINDOWS
6892 if (only_one_window()) /* quit last window, exit Vim */
6893#endif
6894 getout(0);
6895#ifdef FEAT_WINDOWS
6896# ifdef FEAT_GUI
6897 need_mouse_correct = TRUE;
6898# endif
6899 /* quit current window, may free buffer */
6900 win_close(curwin, !P_HID(curwin->w_buffer));
6901#endif
6902 }
6903}
6904
6905/*
6906 * ":print", ":list", ":number".
6907 */
6908 static void
6909ex_print(eap)
6910 exarg_T *eap;
6911{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006912 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6913 EMSG(_(e_emptybuf));
6914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006916 for ( ;!got_int; ui_breakcheck())
6917 {
6918 print_line(eap->line1,
6919 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6920 || (eap->flags & EXFLAG_NR)),
6921 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6922 if (++eap->line1 > eap->line2)
6923 break;
6924 out_flush(); /* show one line at a time */
6925 }
6926 setpcmark();
6927 /* put cursor at last line */
6928 curwin->w_cursor.lnum = eap->line2;
6929 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 }
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
6935#ifdef FEAT_BYTEOFF
6936 static void
6937ex_goto(eap)
6938 exarg_T *eap;
6939{
6940 goto_byte(eap->line2);
6941}
6942#endif
6943
6944/*
6945 * ":shell".
6946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 static void
6948ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006949 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
6951 do_shell(NULL, 0);
6952}
6953
6954#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6955 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006956 || defined(FEAT_GUI_MSWIN) \
6957 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 || defined(PROTO)
6959
6960/*
6961 * Handle a file drop. The code is here because a drop is *nearly* like an
6962 * :args command, but not quite (we have a list of exact filenames, so we
6963 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6964 * code is very similar to :args and hence needs access to a lot of the static
6965 * functions in this file.
6966 *
6967 * The list should be allocated using alloc(), as should each item in the
6968 * list. This function takes over responsibility for freeing the list.
6969 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006970 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6972 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6973 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6974 * file functionality is (currently) not in EMX this is not presently a
6975 * problem.
6976 */
6977 void
6978handle_drop(filec, filev, split)
6979 int filec; /* the number of files dropped */
6980 char_u **filev; /* the list of files dropped */
6981 int split; /* force splitting the window */
6982{
6983 exarg_T ea;
6984 int save_msg_scroll = msg_scroll;
6985
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006986 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006987 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006989#ifdef FEAT_AUTOCMD
6990 if (curbuf_locked())
6991 return;
6992#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006993 /* When the screen is being updated we should not change buffers and
6994 * windows structures, it may cause freed memory to be used. */
6995 if (updating_screen)
6996 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 /* Check whether the current buffer is changed. If so, we will need
6999 * to split the current window or data could be lost.
7000 * We don't need to check if the 'hidden' option is set, as in this
7001 * case the buffer won't be lost.
7002 */
7003 if (!P_HID(curbuf) && !split)
7004 {
7005 ++emsg_off;
7006 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7007 --emsg_off;
7008 }
7009 if (split)
7010 {
7011# ifdef FEAT_WINDOWS
7012 if (win_split(0, 0) == FAIL)
7013 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007014 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 /* When splitting the window, create a new alist. Otherwise the
7017 * existing one is overwritten. */
7018 alist_unlink(curwin->w_alist);
7019 alist_new();
7020# else
7021 return; /* can't split, always fail */
7022# endif
7023 }
7024
7025 /*
7026 * Set up the new argument list.
7027 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007028 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030 /*
7031 * Move to the first file.
7032 */
7033 /* Fake up a minimal "next" command for do_argfile() */
7034 vim_memset(&ea, 0, sizeof(ea));
7035 ea.cmd = (char_u *)"next";
7036 do_argfile(&ea, 0);
7037
7038 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7039 * mode that is not needed here. */
7040 need_start_insertmode = FALSE;
7041
7042 /* Restore msg_scroll, otherwise a following command may cause scrolling
7043 * unexpectedly. The screen will be redrawn by the caller, thus
7044 * msg_scroll being set by displaying a message is irrelevant. */
7045 msg_scroll = save_msg_scroll;
7046}
7047#endif
7048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049/*
7050 * Clear an argument list: free all file names and reset it to zero entries.
7051 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007052 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053alist_clear(al)
7054 alist_T *al;
7055{
7056 while (--al->al_ga.ga_len >= 0)
7057 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7058 ga_clear(&al->al_ga);
7059}
7060
7061/*
7062 * Init an argument list.
7063 */
7064 void
7065alist_init(al)
7066 alist_T *al;
7067{
7068 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7069}
7070
7071#if defined(FEAT_WINDOWS) || defined(PROTO)
7072
7073/*
7074 * Remove a reference from an argument list.
7075 * Ignored when the argument list is the global one.
7076 * If the argument list is no longer used by any window, free it.
7077 */
7078 void
7079alist_unlink(al)
7080 alist_T *al;
7081{
7082 if (al != &global_alist && --al->al_refcount <= 0)
7083 {
7084 alist_clear(al);
7085 vim_free(al);
7086 }
7087}
7088
7089# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7090/*
7091 * Create a new argument list and use it for the current window.
7092 */
7093 void
7094alist_new()
7095{
7096 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7097 if (curwin->w_alist == NULL)
7098 {
7099 curwin->w_alist = &global_alist;
7100 ++global_alist.al_refcount;
7101 }
7102 else
7103 {
7104 curwin->w_alist->al_refcount = 1;
7105 alist_init(curwin->w_alist);
7106 }
7107}
7108# endif
7109#endif
7110
7111#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7112/*
7113 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007114 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7115 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 */
7117 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007118alist_expand(fnum_list, fnum_len)
7119 int *fnum_list;
7120 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007123 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 char_u **new_arg_files;
7125 int new_arg_file_count;
7126 char_u *save_p_su = p_su;
7127 int i;
7128
7129 /* Don't use 'suffixes' here. This should work like the shell did the
7130 * expansion. Also, the vimrc file isn't read yet, thus the user
7131 * can't set the options. */
7132 p_su = empty_option;
7133 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7134 if (old_arg_files != NULL)
7135 {
7136 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007137 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7138 old_arg_count = GARGCOUNT;
7139 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007141 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 && new_arg_file_count > 0)
7143 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007144 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7145 TRUE, fnum_list, fnum_len);
7146 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
7149 p_su = save_p_su;
7150}
7151#endif
7152
7153/*
7154 * Set the argument list for the current window.
7155 * Takes over the allocated files[] and the allocated fnames in it.
7156 */
7157 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007158alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 alist_T *al;
7160 int count;
7161 char_u **files;
7162 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007163 int *fnum_list;
7164 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 int i;
7167
7168 alist_clear(al);
7169 if (ga_grow(&al->al_ga, count) == OK)
7170 {
7171 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007172 {
7173 if (got_int)
7174 {
7175 /* When adding many buffers this can take a long time. Allow
7176 * interrupting here. */
7177 while (i < count)
7178 vim_free(files[i++]);
7179 break;
7180 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007181
7182 /* May set buffer name of a buffer previously used for the
7183 * argument list, so that it's re-used by alist_add. */
7184 if (fnum_list != NULL && i < fnum_len)
7185 buf_set_name(fnum_list[i], files[i]);
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007188 ui_breakcheck();
7189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 vim_free(files);
7191 }
7192 else
7193 FreeWild(count, files);
7194#ifdef FEAT_WINDOWS
7195 if (al == &global_alist)
7196#endif
7197 arg_had_last = FALSE;
7198}
7199
7200/*
7201 * Add file "fname" to argument list "al".
7202 * "fname" must have been allocated and "al" must have been checked for room.
7203 */
7204 void
7205alist_add(al, fname, set_fnum)
7206 alist_T *al;
7207 char_u *fname;
7208 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7209{
7210 if (fname == NULL) /* don't add NULL file names */
7211 return;
7212#ifdef BACKSLASH_IN_FILENAME
7213 slash_adjust(fname);
7214#endif
7215 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7216 if (set_fnum > 0)
7217 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7218 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7219 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220}
7221
7222#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7223/*
7224 * Adjust slashes in file names. Called after 'shellslash' was set.
7225 */
7226 void
7227alist_slash_adjust()
7228{
7229 int i;
7230# ifdef FEAT_WINDOWS
7231 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007232 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233# endif
7234
7235 for (i = 0; i < GARGCOUNT; ++i)
7236 if (GARGLIST[i].ae_fname != NULL)
7237 slash_adjust(GARGLIST[i].ae_fname);
7238# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007239 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 if (wp->w_alist != &global_alist)
7241 for (i = 0; i < WARGCOUNT(wp); ++i)
7242 if (WARGLIST(wp)[i].ae_fname != NULL)
7243 slash_adjust(WARGLIST(wp)[i].ae_fname);
7244# endif
7245}
7246#endif
7247
7248/*
7249 * ":preserve".
7250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 static void
7252ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007253 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007255 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 ml_preserve(curbuf, TRUE);
7257}
7258
7259/*
7260 * ":recover".
7261 */
7262 static void
7263ex_recover(eap)
7264 exarg_T *eap;
7265{
7266 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7267 recoverymode = TRUE;
7268 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7269 && (*eap->arg == NUL
7270 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7271 ml_recover();
7272 recoverymode = FALSE;
7273}
7274
7275/*
7276 * Command modifier used in a wrong way.
7277 */
7278 static void
7279ex_wrongmodifier(eap)
7280 exarg_T *eap;
7281{
7282 eap->errmsg = e_invcmd;
7283}
7284
7285#ifdef FEAT_WINDOWS
7286/*
7287 * :sview [+command] file split window with new file, read-only
7288 * :split [[+command] file] split window with current or new file
7289 * :vsplit [[+command] file] split window vertically with current or new file
7290 * :new [[+command] file] split window with no or new file
7291 * :vnew [[+command] file] split vertically window with no or new file
7292 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007293 *
7294 * :tabedit open new Tab page with empty window
7295 * :tabedit [+command] file open new Tab page and edit "file"
7296 * :tabnew [[+command] file] just like :tabedit
7297 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 */
7299 void
7300ex_splitview(eap)
7301 exarg_T *eap;
7302{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007303 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007306# endif
7307# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7313 {
7314 ex_ni(eap);
7315 return;
7316 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007319# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007323# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007325 * quickfix windows. But it's OK when doing ":tab split". */
7326 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 if (eap->cmdidx == CMD_split)
7329 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007330# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (eap->cmdidx == CMD_vsplit)
7332 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007337# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007338 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
7340 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7341 FNAME_MESS, TRUE, curbuf->b_ffname);
7342 if (fname == NULL)
7343 goto theend;
7344 eap->arg = fname;
7345 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007346# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007352# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 && eap->cmdidx != CMD_new)
7356 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007357# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007358 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007359# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007360 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007361# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007362 au_has_group((char_u *)"FileExplorer"))
7363 {
7364 /* No browsing supported but we do have the file explorer:
7365 * Edit the directory. */
7366 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7367 eap->arg = (char_u *)".";
7368 }
7369 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007370# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007371 {
7372 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007374 if (fname == NULL)
7375 goto theend;
7376 eap->arg = fname;
7377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007382 /*
7383 * Either open new tab page or split the window.
7384 */
7385 if (eap->cmdidx == CMD_tabedit
7386 || eap->cmdidx == CMD_tabfind
7387 || eap->cmdidx == CMD_tabnew)
7388 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007389 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7390 : eap->addr_count == 0 ? 0
7391 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007392 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007393 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007394
7395 /* set the alternate buffer for the window we came from */
7396 if (curwin != old_curwin
7397 && win_valid(old_curwin)
7398 && old_curwin->w_buffer != curbuf
7399 && !cmdmod.keepalt)
7400 old_curwin->w_alt_fnum = curbuf->b_fnum;
7401 }
7402 }
7403 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7405 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 /* Reset 'scrollbind' when editing another file, but keep it when
7408 * doing ":split" without arguments. */
7409 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007410# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007414 {
7415 RESET_BINDING(curwin);
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 else
7418 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 do_exedit(eap, old_curwin);
7421 }
7422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007427# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428theend:
7429 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432
7433/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007434 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435 */
7436 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007437tabpage_new()
7438{
7439 exarg_T ea;
7440
7441 vim_memset(&ea, 0, sizeof(ea));
7442 ea.cmdidx = CMD_tabnew;
7443 ea.cmd = (char_u *)"tabn";
7444 ea.arg = (char_u *)"";
7445 ex_splitview(&ea);
7446}
7447
7448/*
7449 * :tabnext command
7450 */
7451 static void
7452ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007453 exarg_T *eap;
7454{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007455 switch (eap->cmdidx)
7456 {
7457 case CMD_tabfirst:
7458 case CMD_tabrewind:
7459 goto_tabpage(1);
7460 break;
7461 case CMD_tablast:
7462 goto_tabpage(9999);
7463 break;
7464 case CMD_tabprevious:
7465 case CMD_tabNext:
7466 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7467 break;
7468 default: /* CMD_tabnext */
7469 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7470 break;
7471 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007472}
7473
7474/*
7475 * :tabmove command
7476 */
7477 static void
7478ex_tabmove(eap)
7479 exarg_T *eap;
7480{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007481 int tab_number = 9999;
7482
7483 if (eap->arg && *eap->arg != NUL)
7484 {
7485 char_u *p = eap->arg;
7486 int relative = 0; /* argument +N/-N means: move N places to the
7487 * right/left relative to the current position. */
7488
7489 if (*eap->arg == '-')
7490 {
7491 relative = -1;
7492 p = eap->arg + 1;
7493 }
7494 else if (*eap->arg == '+')
7495 {
7496 relative = 1;
7497 p = eap->arg + 1;
7498 }
7499 else
7500 p = eap->arg;
7501
7502 if (p == skipdigits(p))
7503 {
7504 /* No numbers as argument. */
7505 eap->errmsg = e_invarg;
7506 return;
7507 }
7508
7509 tab_number = getdigits(&p);
7510 if (relative != 0)
7511 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7512 }
7513 else if (eap->addr_count != 0)
7514 tab_number = eap->line2;
7515
7516 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007517}
7518
7519/*
7520 * :tabs command: List tabs and their contents.
7521 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007522 static void
7523ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007524 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007525{
7526 tabpage_T *tp;
7527 win_T *wp;
7528 int tabcount = 1;
7529
7530 msg_start();
7531 msg_scroll = TRUE;
7532 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7533 {
7534 msg_putchar('\n');
7535 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7536 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7537 out_flush(); /* output one line at a time */
7538 ui_breakcheck();
7539
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007540 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007541 wp = firstwin;
7542 else
7543 wp = tp->tp_firstwin;
7544 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7545 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007546 msg_putchar('\n');
7547 msg_putchar(wp == curwin ? '>' : ' ');
7548 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007549 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7550 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007551 if (buf_spname(wp->w_buffer) != NULL)
7552 STRCPY(IObuff, buf_spname(wp->w_buffer));
7553 else
7554 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7555 IObuff, IOSIZE, TRUE);
7556 msg_outtrans(IObuff);
7557 out_flush(); /* output one line at a time */
7558 ui_breakcheck();
7559 }
7560 }
7561}
7562
7563#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
7565/*
7566 * ":mode": Set screen mode.
7567 * If no argument given, just get the screen size and redraw.
7568 */
7569 static void
7570ex_mode(eap)
7571 exarg_T *eap;
7572{
7573 if (*eap->arg == NUL)
7574 shell_resized();
7575 else
7576 mch_screenmode(eap->arg);
7577}
7578
7579#ifdef FEAT_WINDOWS
7580/*
7581 * ":resize".
7582 * set, increment or decrement current window height
7583 */
7584 static void
7585ex_resize(eap)
7586 exarg_T *eap;
7587{
7588 int n;
7589 win_T *wp = curwin;
7590
7591 if (eap->addr_count > 0)
7592 {
7593 n = eap->line2;
7594 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7595 ;
7596 }
7597
7598#ifdef FEAT_GUI
7599 need_mouse_correct = TRUE;
7600#endif
7601 n = atol((char *)eap->arg);
7602#ifdef FEAT_VERTSPLIT
7603 if (cmdmod.split & WSP_VERT)
7604 {
7605 if (*eap->arg == '-' || *eap->arg == '+')
7606 n += W_WIDTH(curwin);
7607 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7608 n = 9999;
7609 win_setwidth_win((int)n, wp);
7610 }
7611 else
7612#endif
7613 {
7614 if (*eap->arg == '-' || *eap->arg == '+')
7615 n += curwin->w_height;
7616 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7617 n = 9999;
7618 win_setheight_win((int)n, wp);
7619 }
7620}
7621#endif
7622
7623/*
7624 * ":find [+command] <file>" command.
7625 */
7626 static void
7627ex_find(eap)
7628 exarg_T *eap;
7629{
7630#ifdef FEAT_SEARCHPATH
7631 char_u *fname;
7632 int count;
7633
7634 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7635 TRUE, curbuf->b_ffname);
7636 if (eap->addr_count > 0)
7637 {
7638 /* Repeat finding the file "count" times. This matters when it
7639 * appears several times in the path. */
7640 count = eap->line2;
7641 while (fname != NULL && --count > 0)
7642 {
7643 vim_free(fname);
7644 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7645 FALSE, curbuf->b_ffname);
7646 }
7647 }
7648
7649 if (fname != NULL)
7650 {
7651 eap->arg = fname;
7652#endif
7653 do_exedit(eap, NULL);
7654#ifdef FEAT_SEARCHPATH
7655 vim_free(fname);
7656 }
7657#endif
7658}
7659
7660/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007661 * ":open" simulation: for now just work like ":visual".
7662 */
7663 static void
7664ex_open(eap)
7665 exarg_T *eap;
7666{
7667 regmatch_T regmatch;
7668 char_u *p;
7669
7670 curwin->w_cursor.lnum = eap->line2;
7671 beginline(BL_SOL | BL_FIX);
7672 if (*eap->arg == '/')
7673 {
7674 /* ":open /pattern/": put cursor in column found with pattern */
7675 ++eap->arg;
7676 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7677 *p = NUL;
7678 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7679 if (regmatch.regprog != NULL)
7680 {
7681 regmatch.rm_ic = p_ic;
7682 p = ml_get_curline();
7683 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007684 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007685 else
7686 EMSG(_(e_nomatch));
7687 vim_free(regmatch.regprog);
7688 }
7689 /* Move to the NUL, ignore any other arguments. */
7690 eap->arg += STRLEN(eap->arg);
7691 }
7692 check_cursor();
7693
7694 eap->cmdidx = CMD_visual;
7695 do_exedit(eap, NULL);
7696}
7697
7698/*
7699 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 */
7701 static void
7702ex_edit(eap)
7703 exarg_T *eap;
7704{
7705 do_exedit(eap, NULL);
7706}
7707
7708/*
7709 * ":edit <file>" command and alikes.
7710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 void
7712do_exedit(eap, old_curwin)
7713 exarg_T *eap;
7714 win_T *old_curwin; /* curwin before doing a split or NULL */
7715{
7716 int n;
7717#ifdef FEAT_WINDOWS
7718 int need_hide;
7719#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007720 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
7722 /*
7723 * ":vi" command ends Ex mode.
7724 */
7725 if (exmode_active && (eap->cmdidx == CMD_visual
7726 || eap->cmdidx == CMD_view))
7727 {
7728 exmode_active = FALSE;
7729 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007730 {
7731 /* Special case: ":global/pat/visual\NLvi-commands" */
7732 if (global_busy)
7733 {
7734 int rd = RedrawingDisabled;
7735 int nwr = no_wait_return;
7736 int ms = msg_scroll;
7737#ifdef FEAT_GUI
7738 int he = hold_gui_events;
7739#endif
7740
7741 if (eap->nextcmd != NULL)
7742 {
7743 stuffReadbuff(eap->nextcmd);
7744 eap->nextcmd = NULL;
7745 }
7746
7747 if (exmode_was != EXMODE_VIM)
7748 settmode(TMODE_RAW);
7749 RedrawingDisabled = 0;
7750 no_wait_return = 0;
7751 need_wait_return = FALSE;
7752 msg_scroll = 0;
7753#ifdef FEAT_GUI
7754 hold_gui_events = 0;
7755#endif
7756 must_redraw = CLEAR;
7757
7758 main_loop(FALSE, TRUE);
7759
7760 RedrawingDisabled = rd;
7761 no_wait_return = nwr;
7762 msg_scroll = ms;
7763#ifdef FEAT_GUI
7764 hold_gui_events = he;
7765#endif
7766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
7770
7771 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007772 || eap->cmdidx == CMD_tabnew
7773 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774#ifdef FEAT_VERTSPLIT
7775 || eap->cmdidx == CMD_vnew
7776#endif
7777 ) && *eap->arg == NUL)
7778 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007779 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 setpcmark();
7781 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007782 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7783 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785 else if ((eap->cmdidx != CMD_split
7786#ifdef FEAT_VERTSPLIT
7787 && eap->cmdidx != CMD_vsplit
7788#endif
7789 )
7790 || *eap->arg != NUL
7791#ifdef FEAT_BROWSE
7792 || cmdmod.browse
7793#endif
7794 )
7795 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007796#ifdef FEAT_AUTOCMD
7797 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7798 * can bring us here, others are stopped earlier. */
7799 if (*eap->arg != NUL && curbuf_locked())
7800 return;
7801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 n = readonlymode;
7803 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7804 readonlymode = TRUE;
7805 else if (eap->cmdidx == CMD_enew)
7806 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7807 empty buffer */
7808 setpcmark();
7809 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7810 NULL, eap,
7811 /* ":edit" goes to first line if Vi compatible */
7812 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7813 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7814 ? ECMD_ONE : eap->do_ecmd_lnum,
7815 (P_HID(curbuf) ? ECMD_HIDE : 0)
7816 + (eap->forceit ? ECMD_FORCEIT : 0)
7817#ifdef FEAT_LISTCMDS
7818 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7819#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007820 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
7822 /* Editing the file failed. If the window was split, close it. */
7823#ifdef FEAT_WINDOWS
7824 if (old_curwin != NULL)
7825 {
7826 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7827 if (!need_hide || P_HID(curbuf))
7828 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007829# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7830 cleanup_T cs;
7831
7832 /* Reset the error/interrupt/exception state here so that
7833 * aborting() returns FALSE when closing a window. */
7834 enter_cleanup(&cs);
7835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836# ifdef FEAT_GUI
7837 need_mouse_correct = TRUE;
7838# endif
7839 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007840
7841# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7842 /* Restore the error/interrupt/exception state if not
7843 * discarded by a new aborting error, interrupt, or
7844 * uncaught exception. */
7845 leave_cleanup(&cs);
7846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848 }
7849#endif
7850 }
7851 else if (readonlymode && curbuf->b_nwindows == 1)
7852 {
7853 /* When editing an already visited buffer, 'readonly' won't be set
7854 * but the previous value is kept. With ":view" and ":sview" we
7855 * want the file to be readonly, except when another window is
7856 * editing the same buffer. */
7857 curbuf->b_p_ro = TRUE;
7858 }
7859 readonlymode = n;
7860 }
7861 else
7862 {
7863 if (eap->do_ecmd_cmd != NULL)
7864 do_cmdline_cmd(eap->do_ecmd_cmd);
7865#ifdef FEAT_TITLE
7866 n = curwin->w_arg_idx_invalid;
7867#endif
7868 check_arg_idx(curwin);
7869#ifdef FEAT_TITLE
7870 if (n != curwin->w_arg_idx_invalid)
7871 maketitle();
7872#endif
7873 }
7874
7875#ifdef FEAT_WINDOWS
7876 /*
7877 * if ":split file" worked, set alternate file name in old window to new
7878 * file
7879 */
7880 if (old_curwin != NULL
7881 && *eap->arg != NUL
7882 && curwin != old_curwin
7883 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007884 && old_curwin->w_buffer != curbuf
7885 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 old_curwin->w_alt_fnum = curbuf->b_fnum;
7887#endif
7888
7889 ex_no_reprint = TRUE;
7890}
7891
7892#ifndef FEAT_GUI
7893/*
7894 * ":gui" and ":gvim" when there is no GUI.
7895 */
7896 static void
7897ex_nogui(eap)
7898 exarg_T *eap;
7899{
7900 eap->errmsg = e_nogvim;
7901}
7902#endif
7903
7904#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7905 static void
7906ex_tearoff(eap)
7907 exarg_T *eap;
7908{
7909 gui_make_tearoff(eap->arg);
7910}
7911#endif
7912
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007913#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
7915ex_popup(eap)
7916 exarg_T *eap;
7917{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007918 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919}
7920#endif
7921
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 static void
7923ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007924 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
7926 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7927 MSG(_("No swap file"));
7928 else
7929 msg(curbuf->b_ml.ml_mfp->mf_fname);
7930}
7931
7932/*
7933 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7934 * offset.
7935 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 static void
7938ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007939 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941#ifdef FEAT_SCROLLBIND
7942 win_T *wp;
7943 long topline;
7944 long y;
7945 linenr_T old_linenr = curwin->w_cursor.lnum;
7946
7947 setpcmark();
7948
7949 /*
7950 * determine max topline
7951 */
7952 if (curwin->w_p_scb)
7953 {
7954 topline = curwin->w_topline;
7955 for (wp = firstwin; wp; wp = wp->w_next)
7956 {
7957 if (wp->w_p_scb && wp->w_buffer)
7958 {
7959 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7960 if (topline > y)
7961 topline = y;
7962 }
7963 }
7964 if (topline < 1)
7965 topline = 1;
7966 }
7967 else
7968 {
7969 topline = 1;
7970 }
7971
7972
7973 /*
7974 * set all scrollbind windows to the same topline
7975 */
7976 wp = curwin;
7977 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7978 {
7979 if (curwin->w_p_scb)
7980 {
7981 y = topline - curwin->w_topline;
7982 if (y > 0)
7983 scrollup(y, TRUE);
7984 else
7985 scrolldown(-y, TRUE);
7986 curwin->w_scbind_pos = topline;
7987 redraw_later(VALID);
7988 cursor_correct();
7989#ifdef FEAT_WINDOWS
7990 curwin->w_redr_status = TRUE;
7991#endif
7992 }
7993 }
7994 curwin = wp;
7995 if (curwin->w_p_scb)
7996 {
7997 did_syncbind = TRUE;
7998 checkpcmark();
7999 if (old_linenr != curwin->w_cursor.lnum)
8000 {
8001 char_u ctrl_o[2];
8002
8003 ctrl_o[0] = Ctrl_O;
8004 ctrl_o[1] = 0;
8005 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8006 }
8007 }
8008#endif
8009}
8010
8011
8012 static void
8013ex_read(eap)
8014 exarg_T *eap;
8015{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008016 int i;
8017 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8018 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 if (eap->usefilter) /* :r!cmd */
8021 do_bang(1, eap, FALSE, FALSE, TRUE);
8022 else
8023 {
8024 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8025 return;
8026
8027#ifdef FEAT_BROWSE
8028 if (cmdmod.browse)
8029 {
8030 char_u *browseFile;
8031
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008032 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 NULL, NULL, NULL, curbuf);
8034 if (browseFile != NULL)
8035 {
8036 i = readfile(browseFile, NULL,
8037 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8038 vim_free(browseFile);
8039 }
8040 else
8041 i = OK;
8042 }
8043 else
8044#endif
8045 if (*eap->arg == NUL)
8046 {
8047 if (check_fname() == FAIL) /* check for no file name */
8048 return;
8049 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8050 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8051 }
8052 else
8053 {
8054 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8055 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8056 i = readfile(eap->arg, NULL,
8057 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8058
8059 }
8060 if (i == FAIL)
8061 {
8062#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8063 if (!aborting())
8064#endif
8065 EMSG2(_(e_notopen), eap->arg);
8066 }
8067 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008068 {
8069 if (empty && exmode_active)
8070 {
8071 /* Delete the empty line that remains. Historically ex does
8072 * this but vi doesn't. */
8073 if (eap->line2 == 0)
8074 lnum = curbuf->b_ml.ml_line_count;
8075 else
8076 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008077 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008078 {
8079 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008080 if (curwin->w_cursor.lnum > 1
8081 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008082 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008083 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008084 }
8085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089}
8090
Bram Moolenaarea408852005-06-25 22:49:46 +00008091static char_u *prev_dir = NULL;
8092
8093#if defined(EXITFREE) || defined(PROTO)
8094 void
8095free_cd_dir()
8096{
8097 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008098 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008099
8100 vim_free(globaldir);
8101 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008102}
8103#endif
8104
8105
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106/*
8107 * ":cd", ":lcd", ":chdir" and ":lchdir".
8108 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008109 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110ex_cd(eap)
8111 exarg_T *eap;
8112{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 char_u *new_dir;
8114 char_u *tofree;
8115
8116 new_dir = eap->arg;
8117#if !defined(UNIX) && !defined(VMS)
8118 /* for non-UNIX ":cd" means: print current directory */
8119 if (*new_dir == NUL)
8120 ex_pwd(NULL);
8121 else
8122#endif
8123 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008124#ifdef FEAT_AUTOCMD
8125 if (allbuf_locked())
8126 return;
8127#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008128 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8129 && !eap->forceit)
8130 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008131 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008132 return;
8133 }
8134
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 /* ":cd -": Change to previous directory */
8136 if (STRCMP(new_dir, "-") == 0)
8137 {
8138 if (prev_dir == NULL)
8139 {
8140 EMSG(_("E186: No previous directory"));
8141 return;
8142 }
8143 new_dir = prev_dir;
8144 }
8145
8146 /* Save current directory for next ":cd -" */
8147 tofree = prev_dir;
8148 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8149 prev_dir = vim_strsave(NameBuff);
8150 else
8151 prev_dir = NULL;
8152
8153#if defined(UNIX) || defined(VMS)
8154 /* for UNIX ":cd" means: go to home directory */
8155 if (*new_dir == NUL)
8156 {
8157 /* use NameBuff for home directory name */
8158# ifdef VMS
8159 char_u *p;
8160
8161 p = mch_getenv((char_u *)"SYS$LOGIN");
8162 if (p == NULL || *p == NUL) /* empty is the same as not set */
8163 NameBuff[0] = NUL;
8164 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008165 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166# else
8167 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8168# endif
8169 new_dir = NameBuff;
8170 }
8171#endif
8172 if (new_dir == NULL || vim_chdir(new_dir))
8173 EMSG(_(e_failed));
8174 else
8175 {
8176 vim_free(curwin->w_localdir);
8177 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8178 {
8179 /* If still in global directory, need to remember current
8180 * directory as global directory. */
8181 if (globaldir == NULL && prev_dir != NULL)
8182 globaldir = vim_strsave(prev_dir);
8183 /* Remember this local directory for the window. */
8184 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8185 curwin->w_localdir = vim_strsave(NameBuff);
8186 }
8187 else
8188 {
8189 /* We are now in the global directory, no need to remember its
8190 * name. */
8191 vim_free(globaldir);
8192 globaldir = NULL;
8193 curwin->w_localdir = NULL;
8194 }
8195
8196 shorten_fnames(TRUE);
8197
8198 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008199 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 ex_pwd(eap);
8201 }
8202 vim_free(tofree);
8203 }
8204}
8205
8206/*
8207 * ":pwd".
8208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 static void
8210ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008211 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
8213 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8214 {
8215#ifdef BACKSLASH_IN_FILENAME
8216 slash_adjust(NameBuff);
8217#endif
8218 msg(NameBuff);
8219 }
8220 else
8221 EMSG(_("E187: Unknown"));
8222}
8223
8224/*
8225 * ":=".
8226 */
8227 static void
8228ex_equal(eap)
8229 exarg_T *eap;
8230{
8231 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008232 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233}
8234
8235 static void
8236ex_sleep(eap)
8237 exarg_T *eap;
8238{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008239 int n;
8240 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241
8242 if (cursor_valid())
8243 {
8244 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8245 if (n >= 0)
8246 windgoto((int)n, curwin->w_wcol);
8247 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008248
8249 len = eap->line2;
8250 switch (*eap->arg)
8251 {
8252 case 'm': break;
8253 case NUL: len *= 1000L; break;
8254 default: EMSG2(_(e_invarg2), eap->arg); return;
8255 }
8256 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257}
8258
8259/*
8260 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8261 */
8262 void
8263do_sleep(msec)
8264 long msec;
8265{
8266 long done;
8267
8268 cursor_on();
8269 out_flush();
8270 for (done = 0; !got_int && done < msec; done += 1000L)
8271 {
8272 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8273 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008274#ifdef FEAT_NETBEANS_INTG
8275 /* Process the netbeans messages that may have been received in the
8276 * call to ui_breakcheck() when the GUI is in use. This may occur when
8277 * running a test case. */
8278 netbeans_parse_messages();
8279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 }
8281}
8282
8283 static void
8284do_exmap(eap, isabbrev)
8285 exarg_T *eap;
8286 int isabbrev;
8287{
8288 int mode;
8289 char_u *cmdp;
8290
8291 cmdp = eap->cmd;
8292 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8293
8294 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8295 eap->arg, mode, isabbrev))
8296 {
8297 case 1: EMSG(_(e_invarg));
8298 break;
8299 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8300 break;
8301 }
8302}
8303
8304/*
8305 * ":winsize" command (obsolete).
8306 */
8307 static void
8308ex_winsize(eap)
8309 exarg_T *eap;
8310{
8311 int w, h;
8312 char_u *arg = eap->arg;
8313 char_u *p;
8314
8315 w = getdigits(&arg);
8316 arg = skipwhite(arg);
8317 p = arg;
8318 h = getdigits(&arg);
8319 if (*p != NUL && *arg == NUL)
8320 set_shellsize(w, h, TRUE);
8321 else
8322 EMSG(_("E465: :winsize requires two number arguments"));
8323}
8324
8325#ifdef FEAT_WINDOWS
8326 static void
8327ex_wincmd(eap)
8328 exarg_T *eap;
8329{
8330 int xchar = NUL;
8331 char_u *p;
8332
8333 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8334 {
8335 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8336 if (eap->arg[1] == NUL)
8337 {
8338 EMSG(_(e_invarg));
8339 return;
8340 }
8341 xchar = eap->arg[1];
8342 p = eap->arg + 2;
8343 }
8344 else
8345 p = eap->arg + 1;
8346
8347 eap->nextcmd = check_nextcmd(p);
8348 p = skipwhite(p);
8349 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8350 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008351 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
8353 /* Pass flags on for ":vertical wincmd ]". */
8354 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008355 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8357 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008358 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
8360}
8361#endif
8362
Bram Moolenaar843ee412004-06-30 16:16:41 +00008363#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364/*
8365 * ":winpos".
8366 */
8367 static void
8368ex_winpos(eap)
8369 exarg_T *eap;
8370{
8371 int x, y;
8372 char_u *arg = eap->arg;
8373 char_u *p;
8374
8375 if (*arg == NUL)
8376 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008377# if defined(FEAT_GUI) || defined(MSWIN)
8378# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008380# else
8381 if (mch_get_winpos(&x, &y) != FAIL)
8382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {
8384 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8385 msg(IObuff);
8386 }
8387 else
8388# endif
8389 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8390 }
8391 else
8392 {
8393 x = getdigits(&arg);
8394 arg = skipwhite(arg);
8395 p = arg;
8396 y = getdigits(&arg);
8397 if (*p == NUL || *arg != NUL)
8398 {
8399 EMSG(_("E466: :winpos requires two number arguments"));
8400 return;
8401 }
8402# ifdef FEAT_GUI
8403 if (gui.in_use)
8404 gui_mch_set_winpos(x, y);
8405 else if (gui.starting)
8406 {
8407 /* Remember the coordinates for when the window is opened. */
8408 gui_win_x = x;
8409 gui_win_y = y;
8410 }
8411# ifdef HAVE_TGETENT
8412 else
8413# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008414# else
8415# ifdef MSWIN
8416 mch_set_winpos(x, y);
8417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418# endif
8419# ifdef HAVE_TGETENT
8420 if (*T_CWP)
8421 term_set_winpos(x, y);
8422# endif
8423 }
8424}
8425#endif
8426
8427/*
8428 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8429 */
8430 static void
8431ex_operators(eap)
8432 exarg_T *eap;
8433{
8434 oparg_T oa;
8435
8436 clear_oparg(&oa);
8437 oa.regname = eap->regname;
8438 oa.start.lnum = eap->line1;
8439 oa.end.lnum = eap->line2;
8440 oa.line_count = eap->line2 - eap->line1 + 1;
8441 oa.motion_type = MLINE;
8442#ifdef FEAT_VIRTUALEDIT
8443 virtual_op = FALSE;
8444#endif
8445 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8446 {
8447 setpcmark();
8448 curwin->w_cursor.lnum = eap->line1;
8449 beginline(BL_SOL | BL_FIX);
8450 }
8451
8452 switch (eap->cmdidx)
8453 {
8454 case CMD_delete:
8455 oa.op_type = OP_DELETE;
8456 op_delete(&oa);
8457 break;
8458
8459 case CMD_yank:
8460 oa.op_type = OP_YANK;
8461 (void)op_yank(&oa, FALSE, TRUE);
8462 break;
8463
8464 default: /* CMD_rshift or CMD_lshift */
8465 if ((eap->cmdidx == CMD_rshift)
8466#ifdef FEAT_RIGHTLEFT
8467 ^ curwin->w_p_rl
8468#endif
8469 )
8470 oa.op_type = OP_RSHIFT;
8471 else
8472 oa.op_type = OP_LSHIFT;
8473 op_shift(&oa, FALSE, eap->amount);
8474 break;
8475 }
8476#ifdef FEAT_VIRTUALEDIT
8477 virtual_op = MAYBE;
8478#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008479 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480}
8481
8482/*
8483 * ":put".
8484 */
8485 static void
8486ex_put(eap)
8487 exarg_T *eap;
8488{
8489 /* ":0put" works like ":1put!". */
8490 if (eap->line2 == 0)
8491 {
8492 eap->line2 = 1;
8493 eap->forceit = TRUE;
8494 }
8495 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008496 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8497 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498}
8499
8500/*
8501 * Handle ":copy" and ":move".
8502 */
8503 static void
8504ex_copymove(eap)
8505 exarg_T *eap;
8506{
8507 long n;
8508
8509 n = get_address(&eap->arg, FALSE, FALSE);
8510 if (eap->arg == NULL) /* error detected */
8511 {
8512 eap->nextcmd = NULL;
8513 return;
8514 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008515 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516
8517 /*
8518 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8519 */
8520 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8521 {
8522 EMSG(_(e_invaddr));
8523 return;
8524 }
8525
8526 if (eap->cmdidx == CMD_move)
8527 {
8528 if (do_move(eap->line1, eap->line2, n) == FAIL)
8529 return;
8530 }
8531 else
8532 ex_copy(eap->line1, eap->line2, n);
8533 u_clearline();
8534 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008535 ex_may_print(eap);
8536}
8537
8538/*
8539 * Print the current line if flags were given to the Ex command.
8540 */
8541 static void
8542ex_may_print(eap)
8543 exarg_T *eap;
8544{
8545 if (eap->flags != 0)
8546 {
8547 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8548 (eap->flags & EXFLAG_LIST));
8549 ex_no_reprint = TRUE;
8550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * ":smagic" and ":snomagic".
8555 */
8556 static void
8557ex_submagic(eap)
8558 exarg_T *eap;
8559{
8560 int magic_save = p_magic;
8561
8562 p_magic = (eap->cmdidx == CMD_smagic);
8563 do_sub(eap);
8564 p_magic = magic_save;
8565}
8566
8567/*
8568 * ":join".
8569 */
8570 static void
8571ex_join(eap)
8572 exarg_T *eap;
8573{
8574 curwin->w_cursor.lnum = eap->line1;
8575 if (eap->line1 == eap->line2)
8576 {
8577 if (eap->addr_count >= 2) /* :2,2join does nothing */
8578 return;
8579 if (eap->line2 == curbuf->b_ml.ml_line_count)
8580 {
8581 beep_flush();
8582 return;
8583 }
8584 ++eap->line2;
8585 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008586 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591/*
8592 * ":[addr]@r" or ":[addr]*r": execute register
8593 */
8594 static void
8595ex_at(eap)
8596 exarg_T *eap;
8597{
8598 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008599 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601 curwin->w_cursor.lnum = eap->line2;
8602
8603#ifdef USE_ON_FLY_SCROLL
8604 dont_scroll = TRUE; /* disallow scrolling here */
8605#endif
8606
8607 /* get the register name. No name means to use the previous one */
8608 c = *eap->arg;
8609 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8610 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008611 /* Put the register in the typeahead buffer with the "silent" flag. */
8612 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8613 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 else
8618 {
8619 int save_efr = exec_from_reg;
8620
8621 exec_from_reg = TRUE;
8622
8623 /*
8624 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008625 * Continue until the stuff buffer is empty and all added characters
8626 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008628 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8630
8631 exec_from_reg = save_efr;
8632 }
8633}
8634
8635/*
8636 * ":!".
8637 */
8638 static void
8639ex_bang(eap)
8640 exarg_T *eap;
8641{
8642 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8643}
8644
8645/*
8646 * ":undo".
8647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 static void
8649ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008650 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008652 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008653 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008654 else
8655 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008658#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008659 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008660ex_wundo(eap)
8661 exarg_T *eap;
8662{
8663 char_u hash[UNDO_HASH_SIZE];
8664
8665 u_compute_hash(hash);
8666 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8667}
8668
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008669 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008670ex_rundo(eap)
8671 exarg_T *eap;
8672{
8673 char_u hash[UNDO_HASH_SIZE];
8674
8675 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008676 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008677}
8678#endif
8679
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680/*
8681 * ":redo".
8682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 static void
8684ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008685 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
8687 u_redo(1);
8688}
8689
8690/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008691 * ":earlier" and ":later".
8692 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008693 static void
8694ex_later(eap)
8695 exarg_T *eap;
8696{
8697 long count = 0;
8698 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008699 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008700 char_u *p = eap->arg;
8701
8702 if (*p == NUL)
8703 count = 1;
8704 else if (isdigit(*p))
8705 {
8706 count = getdigits(&p);
8707 switch (*p)
8708 {
8709 case 's': ++p; sec = TRUE; break;
8710 case 'm': ++p; sec = TRUE; count *= 60; break;
8711 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008712 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8713 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008714 }
8715 }
8716
8717 if (*p != NUL)
8718 EMSG2(_(e_invarg2), eap->arg);
8719 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008720 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8721 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008722}
8723
8724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 * ":redir": start/stop redirection.
8726 */
8727 static void
8728ex_redir(eap)
8729 exarg_T *eap;
8730{
8731 char *mode;
8732 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008733 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
8735 if (STRICMP(eap->arg, "END") == 0)
8736 close_redir();
8737 else
8738 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008739 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008741 ++arg;
8742 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008744 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 mode = "a";
8746 }
8747 else
8748 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008749 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
8751 close_redir();
8752
8753 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008754 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 if (fname == NULL)
8756 return;
8757#ifdef FEAT_BROWSE
8758 if (cmdmod.browse)
8759 {
8760 char_u *browseFile;
8761
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008762 browseFile = do_browse(BROWSE_SAVE,
8763 (char_u *)_("Save Redirection"),
8764 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (browseFile == NULL)
8766 return; /* operation cancelled */
8767 vim_free(fname);
8768 fname = browseFile;
8769 eap->forceit = TRUE; /* since dialog already asked */
8770 }
8771#endif
8772
8773 redir_fd = open_exfile(fname, eap->forceit, mode);
8774 vim_free(fname);
8775 }
8776#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008777 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {
8779 /* redirect to a register a-z (resp. A-Z for appending) */
8780 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008781 ++arg;
8782 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008784 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008785 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008787 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008789 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008790 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008791 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008792 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008794 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008795 if (*arg == '>')
8796 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008797 /* Make register empty when not using @A-@Z and the
8798 * command is valid. */
8799 if (*arg == NUL && !isupper(redir_reg))
8800 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008802 }
8803 if (*arg != NUL)
8804 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008805 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008806 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008807 }
8808 }
8809 else if (*arg == '=' && arg[1] == '>')
8810 {
8811 int append;
8812
8813 /* redirect to a variable */
8814 close_redir();
8815 arg += 2;
8816
8817 if (*arg == '>')
8818 {
8819 ++arg;
8820 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008823 append = FALSE;
8824
8825 if (var_redir_start(skipwhite(arg), append) == OK)
8826 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 }
8828#endif
8829
8830 /* TODO: redirect to a buffer */
8831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008833 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008835
8836 /* Make sure redirection is not off. Can happen for cmdline completion
8837 * that indirectly invokes a command to catch its output. */
8838 if (redir_fd != NULL
8839#ifdef FEAT_EVAL
8840 || redir_reg || redir_vname
8841#endif
8842 )
8843 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
8846/*
8847 * ":redraw": force redraw
8848 */
8849 static void
8850ex_redraw(eap)
8851 exarg_T *eap;
8852{
8853 int r = RedrawingDisabled;
8854 int p = p_lz;
8855
8856 RedrawingDisabled = 0;
8857 p_lz = FALSE;
8858 update_topline();
8859 update_screen(eap->forceit ? CLEAR :
8860#ifdef FEAT_VISUAL
8861 VIsual_active ? INVERTED :
8862#endif
8863 0);
8864#ifdef FEAT_TITLE
8865 if (need_maketitle)
8866 maketitle();
8867#endif
8868 RedrawingDisabled = r;
8869 p_lz = p;
8870
8871 /* Reset msg_didout, so that a message that's there is overwritten. */
8872 msg_didout = FALSE;
8873 msg_col = 0;
8874
8875 out_flush();
8876}
8877
8878/*
8879 * ":redrawstatus": force redraw of status line(s)
8880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 static void
8882ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008883 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885#if defined(FEAT_WINDOWS)
8886 int r = RedrawingDisabled;
8887 int p = p_lz;
8888
8889 RedrawingDisabled = 0;
8890 p_lz = FALSE;
8891 if (eap->forceit)
8892 status_redraw_all();
8893 else
8894 status_redraw_curbuf();
8895 update_screen(
8896# ifdef FEAT_VISUAL
8897 VIsual_active ? INVERTED :
8898# endif
8899 0);
8900 RedrawingDisabled = r;
8901 p_lz = p;
8902 out_flush();
8903#endif
8904}
8905
8906 static void
8907close_redir()
8908{
8909 if (redir_fd != NULL)
8910 {
8911 fclose(redir_fd);
8912 redir_fd = NULL;
8913 }
8914#ifdef FEAT_EVAL
8915 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008916 if (redir_vname)
8917 {
8918 var_redir_stop();
8919 redir_vname = 0;
8920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#endif
8922}
8923
8924#if defined(FEAT_SESSION) && defined(USE_CRNL)
8925# define MKSESSION_NL
8926static int mksession_nl = FALSE; /* use NL only in put_eol() */
8927#endif
8928
8929/*
8930 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8931 */
8932 static void
8933ex_mkrc(eap)
8934 exarg_T *eap;
8935{
8936 FILE *fd;
8937 int failed = FALSE;
8938 char_u *fname;
8939#ifdef FEAT_BROWSE
8940 char_u *browseFile = NULL;
8941#endif
8942#ifdef FEAT_SESSION
8943 int view_session = FALSE;
8944 int using_vdir = FALSE; /* using 'viewdir'? */
8945 char_u *viewFile = NULL;
8946 unsigned *flagp;
8947#endif
8948
8949 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8950 {
8951#ifdef FEAT_SESSION
8952 view_session = TRUE;
8953#else
8954 ex_ni(eap);
8955 return;
8956#endif
8957 }
8958
8959#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008960 /* Use the short file name until ":lcd" is used. We also don't use the
8961 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008962 did_lcd = FALSE;
8963
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8965 if (eap->cmdidx == CMD_mkview
8966 && (*eap->arg == NUL
8967 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8968 {
8969 eap->forceit = TRUE;
8970 fname = get_view_file(*eap->arg);
8971 if (fname == NULL)
8972 return;
8973 viewFile = fname;
8974 using_vdir = TRUE;
8975 }
8976 else
8977#endif
8978 if (*eap->arg != NUL)
8979 fname = eap->arg;
8980 else if (eap->cmdidx == CMD_mkvimrc)
8981 fname = (char_u *)VIMRC_FILE;
8982#ifdef FEAT_SESSION
8983 else if (eap->cmdidx == CMD_mksession)
8984 fname = (char_u *)SESSION_FILE;
8985#endif
8986 else
8987 fname = (char_u *)EXRC_FILE;
8988
8989#ifdef FEAT_BROWSE
8990 if (cmdmod.browse)
8991 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008992 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993# ifdef FEAT_SESSION
8994 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8995 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8996# endif
8997 (char_u *)_("Save Setup"),
8998 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8999 if (browseFile == NULL)
9000 goto theend;
9001 fname = browseFile;
9002 eap->forceit = TRUE; /* since dialog already asked */
9003 }
9004#endif
9005
9006#if defined(FEAT_SESSION) && defined(vim_mkdir)
9007 /* When using 'viewdir' may have to create the directory. */
9008 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009009 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010#endif
9011
9012 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9013 if (fd != NULL)
9014 {
9015#ifdef FEAT_SESSION
9016 if (eap->cmdidx == CMD_mkview)
9017 flagp = &vop_flags;
9018 else
9019 flagp = &ssop_flags;
9020#endif
9021
9022#ifdef MKSESSION_NL
9023 /* "unix" in 'sessionoptions': use NL line separator */
9024 if (view_session && (*flagp & SSOP_UNIX))
9025 mksession_nl = TRUE;
9026#endif
9027
9028 /* Write the version command for :mkvimrc */
9029 if (eap->cmdidx == CMD_mkvimrc)
9030 (void)put_line(fd, "version 6.0");
9031
9032#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009033 if (eap->cmdidx == CMD_mksession)
9034 {
9035 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9036 failed = TRUE;
9037 }
9038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 if (eap->cmdidx != CMD_mkview)
9040#endif
9041 {
9042 /* Write setting 'compatible' first, because it has side effects.
9043 * For that same reason only do it when needed. */
9044 if (p_cp)
9045 (void)put_line(fd, "if !&cp | set cp | endif");
9046 else
9047 (void)put_line(fd, "if &cp | set nocp | endif");
9048 }
9049
9050#ifdef FEAT_SESSION
9051 if (!view_session
9052 || (eap->cmdidx == CMD_mksession
9053 && (*flagp & SSOP_OPTIONS)))
9054#endif
9055 failed |= (makemap(fd, NULL) == FAIL
9056 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9057
9058#ifdef FEAT_SESSION
9059 if (!failed && view_session)
9060 {
9061 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9062 failed = TRUE;
9063 if (eap->cmdidx == CMD_mksession)
9064 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009065 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
Bram Moolenaard9462e32011-04-11 21:35:11 +02009067 dirnow = alloc(MAXPATHL);
9068 if (dirnow == NULL)
9069 failed = TRUE;
9070 else
9071 {
9072 /*
9073 * Change to session file's dir.
9074 */
9075 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009077 *dirnow = NUL;
9078 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9079 {
9080 if (vim_chdirfile(fname) == OK)
9081 shorten_fnames(TRUE);
9082 }
9083 else if (*dirnow != NUL
9084 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9085 {
9086 if (mch_chdir((char *)globaldir) == 0)
9087 shorten_fnames(TRUE);
9088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
Bram Moolenaard9462e32011-04-11 21:35:11 +02009090 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaard9462e32011-04-11 21:35:11 +02009092 /* restore original dir */
9093 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009095 {
9096 if (mch_chdir((char *)dirnow) != 0)
9097 EMSG(_(e_prev_dir));
9098 shorten_fnames(TRUE);
9099 }
9100 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 }
9102 }
9103 else
9104 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009105 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9106 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 }
9108 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9109 == FAIL)
9110 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009111 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9112 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009113 if (eap->cmdidx == CMD_mksession)
9114 {
9115 if (put_line(fd, "unlet SessionLoad") == FAIL)
9116 failed = TRUE;
9117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 }
9119#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009120 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9121 failed = TRUE;
9122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 failed |= fclose(fd);
9124
9125 if (failed)
9126 EMSG(_(e_write));
9127#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9128 else if (eap->cmdidx == CMD_mksession)
9129 {
9130 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009131 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
Bram Moolenaard9462e32011-04-11 21:35:11 +02009133 tbuf = alloc(MAXPATHL);
9134 if (tbuf != NULL)
9135 {
9136 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9137 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9138 vim_free(tbuf);
9139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 }
9141#endif
9142#ifdef MKSESSION_NL
9143 mksession_nl = FALSE;
9144#endif
9145 }
9146
9147#ifdef FEAT_BROWSE
9148theend:
9149 vim_free(browseFile);
9150#endif
9151#ifdef FEAT_SESSION
9152 vim_free(viewFile);
9153#endif
9154}
9155
Bram Moolenaardf177f62005-02-22 08:39:57 +00009156#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9157 || defined(PROTO)
9158 int
9159vim_mkdir_emsg(name, prot)
9160 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009161 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009162{
9163 if (vim_mkdir(name, prot) != 0)
9164 {
9165 EMSG2(_("E739: Cannot create directory: %s"), name);
9166 return FAIL;
9167 }
9168 return OK;
9169}
9170#endif
9171
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172/*
9173 * Open a file for writing for an Ex command, with some checks.
9174 * Return file descriptor, or NULL on failure.
9175 */
9176 FILE *
9177open_exfile(fname, forceit, mode)
9178 char_u *fname;
9179 int forceit;
9180 char *mode; /* "w" for create new file or "a" for append */
9181{
9182 FILE *fd;
9183
9184#ifdef UNIX
9185 /* with Unix it is possible to open a directory */
9186 if (mch_isdir(fname))
9187 {
9188 EMSG2(_(e_isadir2), fname);
9189 return NULL;
9190 }
9191#endif
9192 if (!forceit && *mode != 'a' && vim_fexists(fname))
9193 {
9194 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9195 return NULL;
9196 }
9197
9198 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9199 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9200
9201 return fd;
9202}
9203
9204/*
9205 * ":mark" and ":k".
9206 */
9207 static void
9208ex_mark(eap)
9209 exarg_T *eap;
9210{
9211 pos_T pos;
9212
9213 if (*eap->arg == NUL) /* No argument? */
9214 EMSG(_(e_argreq));
9215 else if (eap->arg[1] != NUL) /* more than one character? */
9216 EMSG(_(e_trailing));
9217 else
9218 {
9219 pos = curwin->w_cursor; /* save curwin->w_cursor */
9220 curwin->w_cursor.lnum = eap->line2;
9221 beginline(BL_WHITE | BL_FIX);
9222 if (setmark(*eap->arg) == FAIL) /* set mark */
9223 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9224 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9225 }
9226}
9227
9228/*
9229 * Update w_topline, w_leftcol and the cursor position.
9230 */
9231 void
9232update_topline_cursor()
9233{
9234 check_cursor(); /* put cursor on valid line */
9235 update_topline();
9236 if (!curwin->w_p_wrap)
9237 validate_cursor();
9238 update_curswant();
9239}
9240
9241#ifdef FEAT_EX_EXTRA
9242/*
9243 * ":normal[!] {commands}": Execute normal mode commands.
9244 */
9245 static void
9246ex_normal(eap)
9247 exarg_T *eap;
9248{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 int save_msg_scroll = msg_scroll;
9250 int save_restart_edit = restart_edit;
9251 int save_msg_didout = msg_didout;
9252 int save_State = State;
9253 tasave_T tabuf;
9254 int save_insertmode = p_im;
9255 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009256 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257#ifdef FEAT_MBYTE
9258 char_u *arg = NULL;
9259 int l;
9260 char_u *p;
9261#endif
9262
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009263 if (ex_normal_lock > 0)
9264 {
9265 EMSG(_(e_secure));
9266 return;
9267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 if (ex_normal_busy >= p_mmd)
9269 {
9270 EMSG(_("E192: Recursive use of :normal too deep"));
9271 return;
9272 }
9273 ++ex_normal_busy;
9274
9275 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9276 restart_edit = 0; /* don't go to Insert mode */
9277 p_im = FALSE; /* don't use 'insertmode' */
9278
9279#ifdef FEAT_MBYTE
9280 /*
9281 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9282 * this for the K_SPECIAL leading byte, otherwise special keys will not
9283 * work.
9284 */
9285 if (has_mbyte)
9286 {
9287 int len = 0;
9288
9289 /* Count the number of characters to be escaped. */
9290 for (p = eap->arg; *p != NUL; ++p)
9291 {
9292# ifdef FEAT_GUI
9293 if (*p == CSI) /* leadbyte CSI */
9294 len += 2;
9295# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009296 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9298# ifdef FEAT_GUI
9299 || *p == CSI
9300# endif
9301 )
9302 len += 2;
9303 }
9304 if (len > 0)
9305 {
9306 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9307 if (arg != NULL)
9308 {
9309 len = 0;
9310 for (p = eap->arg; *p != NUL; ++p)
9311 {
9312 arg[len++] = *p;
9313# ifdef FEAT_GUI
9314 if (*p == CSI)
9315 {
9316 arg[len++] = KS_EXTRA;
9317 arg[len++] = (int)KE_CSI;
9318 }
9319# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009320 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 {
9322 arg[len++] = *++p;
9323 if (*p == K_SPECIAL)
9324 {
9325 arg[len++] = KS_SPECIAL;
9326 arg[len++] = KE_FILLER;
9327 }
9328# ifdef FEAT_GUI
9329 else if (*p == CSI)
9330 {
9331 arg[len++] = KS_EXTRA;
9332 arg[len++] = (int)KE_CSI;
9333 }
9334# endif
9335 }
9336 arg[len] = NUL;
9337 }
9338 }
9339 }
9340 }
9341#endif
9342
9343 /*
9344 * Save the current typeahead. This is required to allow using ":normal"
9345 * from an event handler and makes sure we don't hang when the argument
9346 * ends with half a command.
9347 */
9348 save_typeahead(&tabuf);
9349 if (tabuf.typebuf_valid)
9350 {
9351 /*
9352 * Repeat the :normal command for each line in the range. When no
9353 * range given, execute it just once, without positioning the cursor
9354 * first.
9355 */
9356 do
9357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 if (eap->addr_count != 0)
9359 {
9360 curwin->w_cursor.lnum = eap->line1++;
9361 curwin->w_cursor.col = 0;
9362 }
9363
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009364 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365#ifdef FEAT_MBYTE
9366 arg != NULL ? arg :
9367#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009368 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 }
9370 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9371 }
9372
9373 /* Might not return to the main loop when in an event handler. */
9374 update_topline_cursor();
9375
9376 /* Restore the previous typeahead. */
9377 restore_typeahead(&tabuf);
9378
9379 --ex_normal_busy;
9380 msg_scroll = save_msg_scroll;
9381 restart_edit = save_restart_edit;
9382 p_im = save_insertmode;
9383 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009384 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9386
9387 /* Restore the state (needed when called from a function executed for
9388 * 'indentexpr'). */
9389 State = save_State;
9390#ifdef FEAT_MBYTE
9391 vim_free(arg);
9392#endif
9393}
9394
9395/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009396 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 */
9398 static void
9399ex_startinsert(eap)
9400 exarg_T *eap;
9401{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009402 if (eap->forceit)
9403 {
9404 coladvance((colnr_T)MAXCOL);
9405 curwin->w_curswant = MAXCOL;
9406 curwin->w_set_curswant = FALSE;
9407 }
9408
Bram Moolenaara40c5002005-01-09 21:16:21 +00009409 /* Ignore the command when already in Insert mode. Inserting an
9410 * expression register that invokes a function can do this. */
9411 if (State & INSERT)
9412 return;
9413
Bram Moolenaar64969662005-12-14 21:59:55 +00009414 if (eap->cmdidx == CMD_startinsert)
9415 restart_edit = 'a';
9416 else if (eap->cmdidx == CMD_startreplace)
9417 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009419 restart_edit = 'V';
9420
9421 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009423 if (eap->cmdidx == CMD_startinsert)
9424 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 curwin->w_curswant = 0; /* avoid MAXCOL */
9426 }
9427}
9428
9429/*
9430 * ":stopinsert"
9431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 static void
9433ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009434 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436 restart_edit = 0;
9437 stop_insert_mode = TRUE;
9438}
9439#endif
9440
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009441#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9442/*
9443 * Execute normal mode command "cmd".
9444 * "remap" can be REMAP_NONE or REMAP_YES.
9445 */
9446 void
9447exec_normal_cmd(cmd, remap, silent)
9448 char_u *cmd;
9449 int remap;
9450 int silent;
9451{
9452 oparg_T oa;
9453
9454 /*
9455 * Stuff the argument into the typeahead buffer.
9456 * Execute normal_cmd() until there is no typeahead left.
9457 */
9458 clear_oparg(&oa);
9459 finish_op = FALSE;
9460 ins_typebuf(cmd, remap, 0, TRUE, silent);
9461 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9462 && !got_int)
9463 {
9464 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009465 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009466 }
9467}
9468#endif
9469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470#ifdef FEAT_FIND_ID
9471 static void
9472ex_checkpath(eap)
9473 exarg_T *eap;
9474{
9475 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9476 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9477 (linenr_T)1, (linenr_T)MAXLNUM);
9478}
9479
9480#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9481/*
9482 * ":psearch"
9483 */
9484 static void
9485ex_psearch(eap)
9486 exarg_T *eap;
9487{
9488 g_do_tagpreview = p_pvh;
9489 ex_findpat(eap);
9490 g_do_tagpreview = 0;
9491}
9492#endif
9493
9494 static void
9495ex_findpat(eap)
9496 exarg_T *eap;
9497{
9498 int whole = TRUE;
9499 long n;
9500 char_u *p;
9501 int action;
9502
9503 switch (cmdnames[eap->cmdidx].cmd_name[2])
9504 {
9505 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9506 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9507 action = ACTION_GOTO;
9508 else
9509 action = ACTION_SHOW;
9510 break;
9511 case 'i': /* ":ilist" and ":dlist" */
9512 action = ACTION_SHOW_ALL;
9513 break;
9514 case 'u': /* ":ijump" and ":djump" */
9515 action = ACTION_GOTO;
9516 break;
9517 default: /* ":isplit" and ":dsplit" */
9518 action = ACTION_SPLIT;
9519 break;
9520 }
9521
9522 n = 1;
9523 if (vim_isdigit(*eap->arg)) /* get count */
9524 {
9525 n = getdigits(&eap->arg);
9526 eap->arg = skipwhite(eap->arg);
9527 }
9528 if (*eap->arg == '/') /* Match regexp, not just whole words */
9529 {
9530 whole = FALSE;
9531 ++eap->arg;
9532 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9533 if (*p)
9534 {
9535 *p++ = NUL;
9536 p = skipwhite(p);
9537
9538 /* Check for trailing illegal characters */
9539 if (!ends_excmd(*p))
9540 eap->errmsg = e_trailing;
9541 else
9542 eap->nextcmd = check_nextcmd(p);
9543 }
9544 }
9545 if (!eap->skip)
9546 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9547 whole, !eap->forceit,
9548 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9549 n, action, eap->line1, eap->line2);
9550}
9551#endif
9552
9553#ifdef FEAT_WINDOWS
9554
9555# ifdef FEAT_QUICKFIX
9556/*
9557 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9558 */
9559 static void
9560ex_ptag(eap)
9561 exarg_T *eap;
9562{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009563 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9565}
9566
9567/*
9568 * ":pedit"
9569 */
9570 static void
9571ex_pedit(eap)
9572 exarg_T *eap;
9573{
9574 win_T *curwin_save = curwin;
9575
9576 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009577 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 keep_help_flag = curwin_save->w_buffer->b_help;
9579 do_exedit(eap, NULL);
9580 keep_help_flag = FALSE;
9581 if (curwin != curwin_save && win_valid(curwin_save))
9582 {
9583 /* Return cursor to where we were */
9584 validate_cursor();
9585 redraw_later(VALID);
9586 win_enter(curwin_save, TRUE);
9587 }
9588 g_do_tagpreview = 0;
9589}
9590# endif
9591
9592/*
9593 * ":stag", ":stselect" and ":stjump".
9594 */
9595 static void
9596ex_stag(eap)
9597 exarg_T *eap;
9598{
9599 postponed_split = -1;
9600 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009601 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9603 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009604 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606#endif
9607
9608/*
9609 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9610 */
9611 static void
9612ex_tag(eap)
9613 exarg_T *eap;
9614{
9615 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9616}
9617
9618 static void
9619ex_tag_cmd(eap, name)
9620 exarg_T *eap;
9621 char_u *name;
9622{
9623 int cmd;
9624
9625 switch (name[1])
9626 {
9627 case 'j': cmd = DT_JUMP; /* ":tjump" */
9628 break;
9629 case 's': cmd = DT_SELECT; /* ":tselect" */
9630 break;
9631 case 'p': cmd = DT_PREV; /* ":tprevious" */
9632 break;
9633 case 'N': cmd = DT_PREV; /* ":tNext" */
9634 break;
9635 case 'n': cmd = DT_NEXT; /* ":tnext" */
9636 break;
9637 case 'o': cmd = DT_POP; /* ":pop" */
9638 break;
9639 case 'f': /* ":tfirst" */
9640 case 'r': cmd = DT_FIRST; /* ":trewind" */
9641 break;
9642 case 'l': cmd = DT_LAST; /* ":tlast" */
9643 break;
9644 default: /* ":tag" */
9645#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009646 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 {
9648 do_cstag(eap);
9649 return;
9650 }
9651#endif
9652 cmd = DT_TAG;
9653 break;
9654 }
9655
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009656 if (name[0] == 'l')
9657 {
9658#ifndef FEAT_QUICKFIX
9659 ex_ni(eap);
9660 return;
9661#else
9662 cmd = DT_LTAG;
9663#endif
9664 }
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9667 eap->forceit, TRUE);
9668}
9669
9670/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009671 * Check "str" for starting with a special cmdline variable.
9672 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9673 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9674 */
9675 int
9676find_cmdline_var(src, usedlen)
9677 char_u *src;
9678 int *usedlen;
9679{
9680 int len;
9681 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009682 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009683 "%",
9684#define SPEC_PERC 0
9685 "#",
9686#define SPEC_HASH 1
9687 "<cword>", /* cursor word */
9688#define SPEC_CWORD 2
9689 "<cWORD>", /* cursor WORD */
9690#define SPEC_CCWORD 3
9691 "<cfile>", /* cursor path name */
9692#define SPEC_CFILE 4
9693 "<sfile>", /* ":so" file name */
9694#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009695 "<slnum>", /* ":so" file line number */
9696#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009697#ifdef FEAT_AUTOCMD
9698 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009699# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009700 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009701# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009702 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009703# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009704#endif
9705#ifdef FEAT_CLIENTSERVER
9706 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009707# ifdef FEAT_AUTOCMD
9708# define SPEC_CLIENT 10
9709# else
9710# define SPEC_CLIENT 7
9711# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009712#endif
9713 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009714
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009715 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009716 {
9717 len = (int)STRLEN(spec_str[i]);
9718 if (STRNCMP(src, spec_str[i], len) == 0)
9719 {
9720 *usedlen = len;
9721 return i;
9722 }
9723 }
9724 return -1;
9725}
9726
9727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 * Evaluate cmdline variables.
9729 *
9730 * change '%' to curbuf->b_ffname
9731 * '#' to curwin->w_altfile
9732 * '<cword>' to word under the cursor
9733 * '<cWORD>' to WORD under the cursor
9734 * '<cfile>' to path name under the cursor
9735 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009736 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 * '<afile>' to file name for autocommand
9738 * '<abuf>' to buffer number for autocommand
9739 * '<amatch>' to matching name for autocommand
9740 *
9741 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9742 * "" for error without a message) and NULL is returned.
9743 * Returns an allocated string if a valid match was found.
9744 * Returns NULL if no match was found. "usedlen" then still contains the
9745 * number of characters to skip.
9746 */
9747 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009748eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009750 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 int *usedlen; /* characters after src that are used */
9752 linenr_T *lnump; /* line number for :e command, or NULL */
9753 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009754 int *escaped; /* return value has escaped white space (can
9755 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757 int i;
9758 char_u *s;
9759 char_u *result;
9760 char_u *resultbuf = NULL;
9761 int resultlen;
9762 buf_T *buf;
9763 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9764 int spec_idx;
9765#ifdef FEAT_MODIFY_FNAME
9766 int skip_mod = FALSE;
9767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
9770 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009771 if (escaped != NULL)
9772 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773
9774 /*
9775 * Check if there is something to do.
9776 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009777 spec_idx = find_cmdline_var(src, usedlen);
9778 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
9780 *usedlen = 1;
9781 return NULL;
9782 }
9783
9784 /*
9785 * Skip when preceded with a backslash "\%" and "\#".
9786 * Note: In "\\%" the % is also not recognized!
9787 */
9788 if (src > srcstart && src[-1] == '\\')
9789 {
9790 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009791 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 return NULL;
9793 }
9794
9795 /*
9796 * word or WORD under cursor
9797 */
9798 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9799 {
9800 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9801 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9802 if (resultlen == 0)
9803 {
9804 *errormsg = (char_u *)"";
9805 return NULL;
9806 }
9807 }
9808
9809 /*
9810 * '#': Alternate file name
9811 * '%': Current file name
9812 * File name under the cursor
9813 * File name for autocommand
9814 * and following modifiers
9815 */
9816 else
9817 {
9818 switch (spec_idx)
9819 {
9820 case SPEC_PERC: /* '%': current file */
9821 if (curbuf->b_fname == NULL)
9822 {
9823 result = (char_u *)"";
9824 valid = 0; /* Must have ":p:h" to be valid */
9825 }
9826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 break;
9829
9830 case SPEC_HASH: /* '#' or "#99": alternate file */
9831 if (src[1] == '#') /* "##": the argument list */
9832 {
9833 result = arg_all();
9834 resultbuf = result;
9835 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009836 if (escaped != NULL)
9837 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838#ifdef FEAT_MODIFY_FNAME
9839 skip_mod = TRUE;
9840#endif
9841 break;
9842 }
9843 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009844 if (*s == '<') /* "#<99" uses v:oldfiles */
9845 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 i = (int)getdigits(&s);
9847 *usedlen = (int)(s - src); /* length of what we expand */
9848
Bram Moolenaard812df62008-11-09 12:46:09 +00009849 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009851 if (*usedlen < 2)
9852 {
9853 /* Should we give an error message for #<text? */
9854 *usedlen = 1;
9855 return NULL;
9856 }
9857#ifdef FEAT_EVAL
9858 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9859 (long)i);
9860 if (result == NULL)
9861 {
9862 *errormsg = (char_u *)"";
9863 return NULL;
9864 }
9865#else
9866 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 }
9870 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009871 {
9872 buf = buflist_findnr(i);
9873 if (buf == NULL)
9874 {
9875 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9876 return NULL;
9877 }
9878 if (lnump != NULL)
9879 *lnump = ECMD_LAST;
9880 if (buf->b_fname == NULL)
9881 {
9882 result = (char_u *)"";
9883 valid = 0; /* Must have ":p:h" to be valid */
9884 }
9885 else
9886 result = buf->b_fname;
9887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 break;
9889
9890#ifdef FEAT_SEARCHPATH
9891 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009892 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 if (result == NULL)
9894 {
9895 *errormsg = (char_u *)"";
9896 return NULL;
9897 }
9898 resultbuf = result; /* remember allocated string */
9899 break;
9900#endif
9901
9902#ifdef FEAT_AUTOCMD
9903 case SPEC_AFILE: /* file name for autocommand */
9904 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009905 if (result != NULL && !autocmd_fname_full)
9906 {
9907 /* Still need to turn the fname into a full path. It is
9908 * postponed to avoid a delay when <afile> is not used. */
9909 autocmd_fname_full = TRUE;
9910 result = FullName_save(autocmd_fname, FALSE);
9911 vim_free(autocmd_fname);
9912 autocmd_fname = result;
9913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (result == NULL)
9915 {
9916 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9917 return NULL;
9918 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009919 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 break;
9921
9922 case SPEC_ABUF: /* buffer number for autocommand */
9923 if (autocmd_bufnr <= 0)
9924 {
9925 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9926 return NULL;
9927 }
9928 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9929 result = strbuf;
9930 break;
9931
9932 case SPEC_AMATCH: /* match name for autocommand */
9933 result = autocmd_match;
9934 if (result == NULL)
9935 {
9936 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9937 return NULL;
9938 }
9939 break;
9940
9941#endif
9942 case SPEC_SFILE: /* file name for ":so" command */
9943 result = sourcing_name;
9944 if (result == NULL)
9945 {
9946 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9947 return NULL;
9948 }
9949 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009950 case SPEC_SLNUM: /* line in file for ":so" command */
9951 if (sourcing_name == NULL || sourcing_lnum == 0)
9952 {
9953 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
9954 return NULL;
9955 }
9956 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9957 result = strbuf;
9958 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959#if defined(FEAT_CLIENTSERVER)
9960 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009961 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9962 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 result = strbuf;
9964 break;
9965#endif
9966 }
9967
9968 resultlen = (int)STRLEN(result); /* length of new string */
9969 if (src[*usedlen] == '<') /* remove the file name extension */
9970 {
9971 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 resultlen = (int)(s - result);
9974 }
9975#ifdef FEAT_MODIFY_FNAME
9976 else if (!skip_mod)
9977 {
9978 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9979 &resultlen);
9980 if (result == NULL)
9981 {
9982 *errormsg = (char_u *)"";
9983 return NULL;
9984 }
9985 }
9986#endif
9987 }
9988
9989 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9990 {
9991 if (valid != VALID_HEAD + VALID_PATH)
9992 /* xgettext:no-c-format */
9993 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9994 else
9995 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9996 result = NULL;
9997 }
9998 else
9999 result = vim_strnsave(result, resultlen);
10000 vim_free(resultbuf);
10001 return result;
10002}
10003
10004/*
10005 * Concatenate all files in the argument list, separated by spaces, and return
10006 * it in one allocated string.
10007 * Spaces and backslashes in the file names are escaped with a backslash.
10008 * Returns NULL when out of memory.
10009 */
10010 static char_u *
10011arg_all()
10012{
10013 int len;
10014 int idx;
10015 char_u *retval = NULL;
10016 char_u *p;
10017
10018 /*
10019 * Do this loop two times:
10020 * first time: compute the total length
10021 * second time: concatenate the names
10022 */
10023 for (;;)
10024 {
10025 len = 0;
10026 for (idx = 0; idx < ARGCOUNT; ++idx)
10027 {
10028 p = alist_name(&ARGLIST[idx]);
10029 if (p != NULL)
10030 {
10031 if (len > 0)
10032 {
10033 /* insert a space in between names */
10034 if (retval != NULL)
10035 retval[len] = ' ';
10036 ++len;
10037 }
10038 for ( ; *p != NUL; ++p)
10039 {
10040 if (*p == ' ' || *p == '\\')
10041 {
10042 /* insert a backslash */
10043 if (retval != NULL)
10044 retval[len] = '\\';
10045 ++len;
10046 }
10047 if (retval != NULL)
10048 retval[len] = *p;
10049 ++len;
10050 }
10051 }
10052 }
10053
10054 /* second time: break here */
10055 if (retval != NULL)
10056 {
10057 retval[len] = NUL;
10058 break;
10059 }
10060
10061 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010062 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (retval == NULL)
10064 break;
10065 }
10066
10067 return retval;
10068}
10069
10070#if defined(FEAT_AUTOCMD) || defined(PROTO)
10071/*
10072 * Expand the <sfile> string in "arg".
10073 *
10074 * Returns an allocated string, or NULL for any error.
10075 */
10076 char_u *
10077expand_sfile(arg)
10078 char_u *arg;
10079{
10080 char_u *errormsg;
10081 int len;
10082 char_u *result;
10083 char_u *newres;
10084 char_u *repl;
10085 int srclen;
10086 char_u *p;
10087
10088 result = vim_strsave(arg);
10089 if (result == NULL)
10090 return NULL;
10091
10092 for (p = result; *p; )
10093 {
10094 if (STRNCMP(p, "<sfile>", 7) != 0)
10095 ++p;
10096 else
10097 {
10098 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010099 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (errormsg != NULL)
10101 {
10102 if (*errormsg)
10103 emsg(errormsg);
10104 vim_free(result);
10105 return NULL;
10106 }
10107 if (repl == NULL) /* no match (cannot happen) */
10108 {
10109 p += srclen;
10110 continue;
10111 }
10112 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10113 newres = alloc(len);
10114 if (newres == NULL)
10115 {
10116 vim_free(repl);
10117 vim_free(result);
10118 return NULL;
10119 }
10120 mch_memmove(newres, result, (size_t)(p - result));
10121 STRCPY(newres + (p - result), repl);
10122 len = (int)STRLEN(newres);
10123 STRCAT(newres, p + srclen);
10124 vim_free(repl);
10125 vim_free(result);
10126 result = newres;
10127 p = newres + len; /* continue after the match */
10128 }
10129 }
10130
10131 return result;
10132}
10133#endif
10134
10135#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010136static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10137 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10139static frame_T *ses_skipframe __ARGS((frame_T *fr));
10140static int ses_do_frame __ARGS((frame_T *fr));
10141static int ses_do_win __ARGS((win_T *wp));
10142static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10143static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10144static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10145
10146/*
10147 * Write openfile commands for the current buffers to an .exrc file.
10148 * Return FAIL on error, OK otherwise.
10149 */
10150 static int
10151makeopens(fd, dirnow)
10152 FILE *fd;
10153 char_u *dirnow; /* Current directory name */
10154{
10155 buf_T *buf;
10156 int only_save_windows = TRUE;
10157 int nr;
10158 int cnr = 1;
10159 int restore_size = TRUE;
10160 win_T *wp;
10161 char_u *sname;
10162 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010163 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010164 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010165 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010166 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010167 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168
10169 if (ssop_flags & SSOP_BUFFERS)
10170 only_save_windows = FALSE; /* Save ALL buffers */
10171
10172 /*
10173 * Begin by setting the this_session variable, and then other
10174 * sessionable variables.
10175 */
10176#ifdef FEAT_EVAL
10177 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10178 return FAIL;
10179 if (ssop_flags & SSOP_GLOBALS)
10180 if (store_session_globals(fd) == FAIL)
10181 return FAIL;
10182#endif
10183
10184 /*
10185 * Close all windows but one.
10186 */
10187 if (put_line(fd, "silent only") == FAIL)
10188 return FAIL;
10189
10190 /*
10191 * Now a :cd command to the session directory or the current directory
10192 */
10193 if (ssop_flags & SSOP_SESDIR)
10194 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010195 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10196 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 return FAIL;
10198 }
10199 else if (ssop_flags & SSOP_CURDIR)
10200 {
10201 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10202 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010203 || fputs("cd ", fd) < 0
10204 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10205 || put_eol(fd) == FAIL)
10206 {
10207 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 vim_free(sname);
10211 }
10212
10213 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010214 * If there is an empty, unnamed buffer we will wipe it out later.
10215 * Remember the buffer number.
10216 */
10217 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10218 return FAIL;
10219 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10220 return FAIL;
10221 if (put_line(fd, "endif") == FAIL)
10222 return FAIL;
10223
10224 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 * Now save the current files, current buffer first.
10226 */
10227 if (put_line(fd, "set shortmess=aoO") == FAIL)
10228 return FAIL;
10229
10230 /* Now put the other buffers into the buffer list */
10231 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10232 {
10233 if (!(only_save_windows && buf->b_nwindows == 0)
10234 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10235 && buf->b_fname != NULL
10236 && buf->b_p_bl)
10237 {
10238 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10239 : buf->b_wininfo->wi_fpos.lnum) < 0
10240 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10241 return FAIL;
10242 }
10243 }
10244
10245 /* the global argument list */
10246 if (ses_arglist(fd, "args", &global_alist.al_ga,
10247 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10248 return FAIL;
10249
10250 if (ssop_flags & SSOP_RESIZE)
10251 {
10252 /* Note: after the restore we still check it worked!*/
10253 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10254 || put_eol(fd) == FAIL)
10255 return FAIL;
10256 }
10257
10258#ifdef FEAT_GUI
10259 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10260 {
10261 int x, y;
10262
10263 if (gui_mch_get_winpos(&x, &y) == OK)
10264 {
10265 /* Note: after the restore we still check it worked!*/
10266 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10267 return FAIL;
10268 }
10269 }
10270#endif
10271
10272 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010273 * May repeat putting Windows for each tab, when "tabpages" is in
10274 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010275 * Don't use goto_tabpage(), it may change directory and trigger
10276 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010278 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010279 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010280 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010282 int need_tabnew = FALSE;
10283
Bram Moolenaar18144c82006-04-12 21:52:12 +000010284 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010286 tabpage_T *tp = find_tabpage(tabnr);
10287
10288 if (tp == NULL)
10289 break; /* done all tab pages */
10290 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010291 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010292 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010293 tab_topframe = topframe;
10294 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010295 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010296 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010297 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010298 tab_topframe = tp->tp_topframe;
10299 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010300 if (tabnr > 1)
10301 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303
Bram Moolenaar18144c82006-04-12 21:52:12 +000010304 /*
10305 * Before creating the window layout, try loading one file. If this
10306 * is aborted we don't end up with a number of useless windows.
10307 * This may have side effects! (e.g., compressed or network file).
10308 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010309 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010310 {
10311 if (ses_do_win(wp)
10312 && wp->w_buffer->b_ffname != NULL
10313 && !wp->w_buffer->b_help
10314#ifdef FEAT_QUICKFIX
10315 && !bt_nofile(wp->w_buffer)
10316#endif
10317 )
10318 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010319 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010320 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10321 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010322 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010323 if (!wp->w_arg_idx_invalid)
10324 edited_win = wp;
10325 break;
10326 }
10327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010329 /* If no file got edited create an empty tab page. */
10330 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10331 return FAIL;
10332
Bram Moolenaar18144c82006-04-12 21:52:12 +000010333 /*
10334 * Save current window layout.
10335 */
10336 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010338 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010340 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10341 return FAIL;
10342 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344
Bram Moolenaar18144c82006-04-12 21:52:12 +000010345 /*
10346 * Check if window sizes can be restored (no windows omitted).
10347 * Remember the window number of the current window after restoring.
10348 */
10349 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010350 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010351 {
10352 if (ses_do_win(wp))
10353 ++nr;
10354 else
10355 restore_size = FALSE;
10356 if (curwin == wp)
10357 cnr = nr;
10358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359
Bram Moolenaar18144c82006-04-12 21:52:12 +000010360 /* Go to the first window. */
10361 if (put_line(fd, "wincmd t") == FAIL)
10362 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010363
Bram Moolenaar18144c82006-04-12 21:52:12 +000010364 /*
10365 * If more than one window, see if sizes can be restored.
10366 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10367 * resized when moving between windows.
10368 * Do this before restoring the view, so that the topline and the
10369 * cursor can be set. This is done again below.
10370 */
10371 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10372 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010373 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375
Bram Moolenaar18144c82006-04-12 21:52:12 +000010376 /*
10377 * Restore the view of the window (options, file, cursor, etc.).
10378 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010379 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010380 {
10381 if (!ses_do_win(wp))
10382 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010383 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10384 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010385 return FAIL;
10386 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10387 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010388 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010389 }
10390
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010391 /* The argument index in the first tab page is zero, need to set it in
10392 * each window. For further tab pages it's the window where we do
10393 * "tabedit". */
10394 cur_arg_idx = next_arg_idx;
10395
Bram Moolenaar18144c82006-04-12 21:52:12 +000010396 /*
10397 * Restore cursor to the current window if it's not the first one.
10398 */
10399 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10400 || put_eol(fd) == FAIL))
10401 return FAIL;
10402
10403 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010404 * Restore window sizes again after jumping around in windows, because
10405 * the current window has a minimum size while others may not.
10406 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010407 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010408 return FAIL;
10409
Bram Moolenaar18144c82006-04-12 21:52:12 +000010410 /* Don't continue in another tab page when doing only the current one
10411 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010412 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010413 break;
10414 }
10415
10416 if (ssop_flags & SSOP_TABPAGES)
10417 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010418 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10419 || put_eol(fd) == FAIL)
10420 return FAIL;
10421 }
10422
Bram Moolenaar9c102382006-05-03 21:26:49 +000010423 /*
10424 * Wipe out an empty unnamed buffer we started in.
10425 */
10426 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10427 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010428 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010429 return FAIL;
10430 if (put_line(fd, "endif") == FAIL)
10431 return FAIL;
10432 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10433 return FAIL;
10434
10435 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10436 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10437 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10438 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439
10440 /*
10441 * Lastly, execute the x.vim file if it exists.
10442 */
10443 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10444 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010445 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 || put_line(fd, "endif") == FAIL)
10447 return FAIL;
10448
10449 return OK;
10450}
10451
10452 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010453ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 FILE *fd;
10455 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010456 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457{
10458 int n = 0;
10459 win_T *wp;
10460
10461 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10462 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010463 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
10465 if (!ses_do_win(wp))
10466 continue;
10467 ++n;
10468
10469 /* restore height when not full height */
10470 if (wp->w_height + wp->w_status_height < topframe->fr_height
10471 && (fprintf(fd,
10472 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10473 n, (long)wp->w_height, Rows / 2, Rows) < 0
10474 || put_eol(fd) == FAIL))
10475 return FAIL;
10476
10477 /* restore width when not full width */
10478 if (wp->w_width < Columns && (fprintf(fd,
10479 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10480 n, (long)wp->w_width, Columns / 2, Columns) < 0
10481 || put_eol(fd) == FAIL))
10482 return FAIL;
10483 }
10484 }
10485 else
10486 {
10487 /* Just equalise window sizes */
10488 if (put_line(fd, "wincmd =") == FAIL)
10489 return FAIL;
10490 }
10491 return OK;
10492}
10493
10494/*
10495 * Write commands to "fd" to recursively create windows for frame "fr",
10496 * horizontally and vertically split.
10497 * After the commands the last window in the frame is the current window.
10498 * Returns FAIL when writing the commands to "fd" fails.
10499 */
10500 static int
10501ses_win_rec(fd, fr)
10502 FILE *fd;
10503 frame_T *fr;
10504{
10505 frame_T *frc;
10506 int count = 0;
10507
10508 if (fr->fr_layout != FR_LEAF)
10509 {
10510 /* Find first frame that's not skipped and then create a window for
10511 * each following one (first frame is already there). */
10512 frc = ses_skipframe(fr->fr_child);
10513 if (frc != NULL)
10514 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10515 {
10516 /* Make window as big as possible so that we have lots of room
10517 * to split. */
10518 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10519 || put_line(fd, fr->fr_layout == FR_COL
10520 ? "split" : "vsplit") == FAIL)
10521 return FAIL;
10522 ++count;
10523 }
10524
10525 /* Go back to the first window. */
10526 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10527 ? "%dwincmd k" : "%dwincmd h", count) < 0
10528 || put_eol(fd) == FAIL))
10529 return FAIL;
10530
10531 /* Recursively create frames/windows in each window of this column or
10532 * row. */
10533 frc = ses_skipframe(fr->fr_child);
10534 while (frc != NULL)
10535 {
10536 ses_win_rec(fd, frc);
10537 frc = ses_skipframe(frc->fr_next);
10538 /* Go to next window. */
10539 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10540 return FAIL;
10541 }
10542 }
10543 return OK;
10544}
10545
10546/*
10547 * Skip frames that don't contain windows we want to save in the Session.
10548 * Returns NULL when there none.
10549 */
10550 static frame_T *
10551ses_skipframe(fr)
10552 frame_T *fr;
10553{
10554 frame_T *frc;
10555
10556 for (frc = fr; frc != NULL; frc = frc->fr_next)
10557 if (ses_do_frame(frc))
10558 break;
10559 return frc;
10560}
10561
10562/*
10563 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10564 * the Session.
10565 */
10566 static int
10567ses_do_frame(fr)
10568 frame_T *fr;
10569{
10570 frame_T *frc;
10571
10572 if (fr->fr_layout == FR_LEAF)
10573 return ses_do_win(fr->fr_win);
10574 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10575 if (ses_do_frame(frc))
10576 return TRUE;
10577 return FALSE;
10578}
10579
10580/*
10581 * Return non-zero if window "wp" is to be stored in the Session.
10582 */
10583 static int
10584ses_do_win(wp)
10585 win_T *wp;
10586{
10587 if (wp->w_buffer->b_fname == NULL
10588#ifdef FEAT_QUICKFIX
10589 /* When 'buftype' is "nofile" can't restore the window contents. */
10590 || bt_nofile(wp->w_buffer)
10591#endif
10592 )
10593 return (ssop_flags & SSOP_BLANK);
10594 if (wp->w_buffer->b_help)
10595 return (ssop_flags & SSOP_HELP);
10596 return TRUE;
10597}
10598
10599/*
10600 * Write commands to "fd" to restore the view of a window.
10601 * Caller must make sure 'scrolloff' is zero.
10602 */
10603 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010604put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 FILE *fd;
10606 win_T *wp;
10607 int add_edit; /* add ":edit" command to view */
10608 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010609 int current_arg_idx; /* current argument index of the window, use
10610 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611{
10612 win_T *save_curwin;
10613 int f;
10614 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010615 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616
10617 /* Always restore cursor position for ":mksession". For ":mkview" only
10618 * when 'viewoptions' contains "cursor". */
10619 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10620
10621 /*
10622 * Local argument list.
10623 */
10624 if (wp->w_alist == &global_alist)
10625 {
10626 if (put_line(fd, "argglobal") == FAIL)
10627 return FAIL;
10628 }
10629 else
10630 {
10631 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10632 flagp == &vop_flags
10633 || !(*flagp & SSOP_CURDIR)
10634 || wp->w_localdir != NULL, flagp) == FAIL)
10635 return FAIL;
10636 }
10637
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010638 /* Only when part of a session: restore the argument index. Some
10639 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010640 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010641 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010643 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 || put_eol(fd) == FAIL)
10645 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010646 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 }
10648
10649 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010650 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 {
10652 /*
10653 * Load the file.
10654 */
10655 if (wp->w_buffer->b_ffname != NULL
10656#ifdef FEAT_QUICKFIX
10657 && !bt_nofile(wp->w_buffer)
10658#endif
10659 )
10660 {
10661 /*
10662 * Editing a file in this buffer: use ":edit file".
10663 * This may have side effects! (e.g., compressed or network file).
10664 */
10665 if (fputs("edit ", fd) < 0
10666 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10667 return FAIL;
10668 }
10669 else
10670 {
10671 /* No file in this buffer, just make it empty. */
10672 if (put_line(fd, "enew") == FAIL)
10673 return FAIL;
10674#ifdef FEAT_QUICKFIX
10675 if (wp->w_buffer->b_ffname != NULL)
10676 {
10677 /* The buffer does have a name, but it's not a file name. */
10678 if (fputs("file ", fd) < 0
10679 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10680 return FAIL;
10681 }
10682#endif
10683 do_cursor = FALSE;
10684 }
10685 }
10686
10687 /*
10688 * Local mappings and abbreviations.
10689 */
10690 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10691 && makemap(fd, wp->w_buffer) == FAIL)
10692 return FAIL;
10693
10694 /*
10695 * Local options. Need to go to the window temporarily.
10696 * Store only local values when using ":mkview" and when ":mksession" is
10697 * used and 'sessionoptions' doesn't include "options".
10698 * Some folding options are always stored when "folds" is included,
10699 * otherwise the folds would not be restored correctly.
10700 */
10701 save_curwin = curwin;
10702 curwin = wp;
10703 curbuf = curwin->w_buffer;
10704 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10705 f = makeset(fd, OPT_LOCAL,
10706 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10707#ifdef FEAT_FOLDING
10708 else if (*flagp & SSOP_FOLDS)
10709 f = makefoldset(fd);
10710#endif
10711 else
10712 f = OK;
10713 curwin = save_curwin;
10714 curbuf = curwin->w_buffer;
10715 if (f == FAIL)
10716 return FAIL;
10717
10718#ifdef FEAT_FOLDING
10719 /*
10720 * Save Folds when 'buftype' is empty and for help files.
10721 */
10722 if ((*flagp & SSOP_FOLDS)
10723 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010724# ifdef FEAT_QUICKFIX
10725 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10726# endif
10727 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 {
10729 if (put_folds(fd, wp) == FAIL)
10730 return FAIL;
10731 }
10732#endif
10733
10734 /*
10735 * Set the cursor after creating folds, since that moves the cursor.
10736 */
10737 if (do_cursor)
10738 {
10739
10740 /* Restore the cursor line in the file and relatively in the
10741 * window. Don't use "G", it changes the jumplist. */
10742 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10743 (long)wp->w_cursor.lnum,
10744 (long)(wp->w_cursor.lnum - wp->w_topline),
10745 (long)wp->w_height / 2, (long)wp->w_height) < 0
10746 || put_eol(fd) == FAIL
10747 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10748 || put_line(fd, "exe s:l") == FAIL
10749 || put_line(fd, "normal! zt") == FAIL
10750 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10751 || put_eol(fd) == FAIL)
10752 return FAIL;
10753 /* Restore the cursor column and left offset when not wrapping. */
10754 if (wp->w_cursor.col == 0)
10755 {
10756 if (put_line(fd, "normal! 0") == FAIL)
10757 return FAIL;
10758 }
10759 else
10760 {
10761 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10762 {
10763 if (fprintf(fd,
10764 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10765 (long)wp->w_cursor.col,
10766 (long)(wp->w_cursor.col - wp->w_leftcol),
10767 (long)wp->w_width / 2, (long)wp->w_width) < 0
10768 || put_eol(fd) == FAIL
10769 || put_line(fd, "if s:c > 0") == FAIL
10770 || fprintf(fd,
10771 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10772 (long)wp->w_cursor.col) < 0
10773 || put_eol(fd) == FAIL
10774 || put_line(fd, "else") == FAIL
10775 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10776 || put_eol(fd) == FAIL
10777 || put_line(fd, "endif") == FAIL)
10778 return FAIL;
10779 }
10780 else
10781 {
10782 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10783 || put_eol(fd) == FAIL)
10784 return FAIL;
10785 }
10786 }
10787 }
10788
10789 /*
10790 * Local directory.
10791 */
10792 if (wp->w_localdir != NULL)
10793 {
10794 if (fputs("lcd ", fd) < 0
10795 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10796 || put_eol(fd) == FAIL)
10797 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010798 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 }
10800
10801 return OK;
10802}
10803
10804/*
10805 * Write an argument list to the session file.
10806 * Returns FAIL if writing fails.
10807 */
10808 static int
10809ses_arglist(fd, cmd, gap, fullname, flagp)
10810 FILE *fd;
10811 char *cmd;
10812 garray_T *gap;
10813 int fullname; /* TRUE: use full path name */
10814 unsigned *flagp;
10815{
10816 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010817 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 char_u *s;
10819
10820 if (gap->ga_len == 0)
10821 return put_line(fd, "silent! argdel *");
10822 if (fputs(cmd, fd) < 0)
10823 return FAIL;
10824 for (i = 0; i < gap->ga_len; ++i)
10825 {
10826 /* NULL file names are skipped (only happens when out of memory). */
10827 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10828 if (s != NULL)
10829 {
10830 if (fullname)
10831 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010832 buf = alloc(MAXPATHL);
10833 if (buf != NULL)
10834 {
10835 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10836 s = buf;
10837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 }
10839 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010840 {
10841 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010843 }
10844 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 }
10846 }
10847 return put_eol(fd);
10848}
10849
10850/*
10851 * Write a buffer name to the session file.
10852 * Also ends the line.
10853 * Returns FAIL if writing fails.
10854 */
10855 static int
10856ses_fname(fd, buf, flagp)
10857 FILE *fd;
10858 buf_T *buf;
10859 unsigned *flagp;
10860{
10861 char_u *name;
10862
10863 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010864 * the session file will be sourced.
10865 * Don't do this for ":mkview", we don't know the current directory.
10866 * Don't do this after ":lcd", we don't keep track of what the current
10867 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 if (buf->b_sfname != NULL
10869 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010870 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010871#ifdef FEAT_AUTOCHDIR
10872 && !p_acd
10873#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010874 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 name = buf->b_sfname;
10876 else
10877 name = buf->b_ffname;
10878 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10879 return FAIL;
10880 return OK;
10881}
10882
10883/*
10884 * Write a file name to the session file.
10885 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10886 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010887 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 */
10889 static int
10890ses_put_fname(fd, name, flagp)
10891 FILE *fd;
10892 char_u *name;
10893 unsigned *flagp;
10894{
10895 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010896 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898
10899 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010900 if (sname == NULL)
10901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010903 if (*flagp & SSOP_SLASH)
10904 {
10905 /* change all backslashes to forward slashes */
10906 for (p = sname; *p != NUL; mb_ptr_adv(p))
10907 if (*p == '\\')
10908 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010910
10911 /* escapse special characters */
10912 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010914 if (p == NULL)
10915 return FAIL;
10916
10917 /* write the result */
10918 if (fputs((char *)p, fd) < 0)
10919 retval = FAIL;
10920
10921 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 return retval;
10923}
10924
10925/*
10926 * ":loadview [nr]"
10927 */
10928 static void
10929ex_loadview(eap)
10930 exarg_T *eap;
10931{
10932 char_u *fname;
10933
10934 fname = get_view_file(*eap->arg);
10935 if (fname != NULL)
10936 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010937 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 vim_free(fname);
10939 }
10940}
10941
10942/*
10943 * Get the name of the view file for the current buffer.
10944 */
10945 static char_u *
10946get_view_file(c)
10947 int c;
10948{
10949 int len = 0;
10950 char_u *p, *s;
10951 char_u *retval;
10952 char_u *sname;
10953
10954 if (curbuf->b_ffname == NULL)
10955 {
10956 EMSG(_(e_noname));
10957 return NULL;
10958 }
10959 sname = home_replace_save(NULL, curbuf->b_ffname);
10960 if (sname == NULL)
10961 return NULL;
10962
10963 /*
10964 * We want a file name without separators, because we're not going to make
10965 * a directory.
10966 * "normal" path separator -> "=+"
10967 * "=" -> "=="
10968 * ":" path separator -> "=-"
10969 */
10970 for (p = sname; *p; ++p)
10971 if (*p == '=' || vim_ispathsep(*p))
10972 ++len;
10973 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10974 if (retval != NULL)
10975 {
10976 STRCPY(retval, p_vdir);
10977 add_pathsep(retval);
10978 s = retval + STRLEN(retval);
10979 for (p = sname; *p; ++p)
10980 {
10981 if (*p == '=')
10982 {
10983 *s++ = '=';
10984 *s++ = '=';
10985 }
10986 else if (vim_ispathsep(*p))
10987 {
10988 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010989#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 if (*p == ':')
10991 *s++ = '-';
10992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010994 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 }
10996 else
10997 *s++ = *p;
10998 }
10999 *s++ = '=';
11000 *s++ = c;
11001 STRCPY(s, ".vim");
11002 }
11003
11004 vim_free(sname);
11005 return retval;
11006}
11007
11008#endif /* FEAT_SESSION */
11009
11010/*
11011 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11012 * Return FAIL for a write error.
11013 */
11014 int
11015put_eol(fd)
11016 FILE *fd;
11017{
11018 if (
11019#ifdef USE_CRNL
11020 (
11021# ifdef MKSESSION_NL
11022 !mksession_nl &&
11023# endif
11024 (putc('\r', fd) < 0)) ||
11025#endif
11026 (putc('\n', fd) < 0))
11027 return FAIL;
11028 return OK;
11029}
11030
11031/*
11032 * Write a line to "fd".
11033 * Return FAIL for a write error.
11034 */
11035 int
11036put_line(fd, s)
11037 FILE *fd;
11038 char *s;
11039{
11040 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11041 return FAIL;
11042 return OK;
11043}
11044
11045#ifdef FEAT_VIMINFO
11046/*
11047 * ":rviminfo" and ":wviminfo".
11048 */
11049 static void
11050ex_viminfo(eap)
11051 exarg_T *eap;
11052{
11053 char_u *save_viminfo;
11054
11055 save_viminfo = p_viminfo;
11056 if (*p_viminfo == NUL)
11057 p_viminfo = (char_u *)"'100";
11058 if (eap->cmdidx == CMD_rviminfo)
11059 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011060 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11061 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 EMSG(_("E195: Cannot open viminfo file for reading"));
11063 }
11064 else
11065 write_viminfo(eap->arg, eap->forceit);
11066 p_viminfo = save_viminfo;
11067}
11068#endif
11069
11070#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011071/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011072 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011073 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 void
11076dialog_msg(buff, format, fname)
11077 char_u *buff;
11078 char *format;
11079 char_u *fname;
11080{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 if (fname == NULL)
11082 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011083 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084}
11085#endif
11086
11087/*
11088 * ":behave {mswin,xterm}"
11089 */
11090 static void
11091ex_behave(eap)
11092 exarg_T *eap;
11093{
11094 if (STRCMP(eap->arg, "mswin") == 0)
11095 {
11096 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11097 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11098 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11099 set_option_value((char_u *)"keymodel", 0L,
11100 (char_u *)"startsel,stopsel", 0);
11101 }
11102 else if (STRCMP(eap->arg, "xterm") == 0)
11103 {
11104 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11105 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11106 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11107 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11108 }
11109 else
11110 EMSG2(_(e_invarg2), eap->arg);
11111}
11112
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011113#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11114/*
11115 * Function given to ExpandGeneric() to obtain the possible arguments of the
11116 * ":behave {mswin,xterm}" command.
11117 */
11118 char_u *
11119get_behave_arg(xp, idx)
11120 expand_T *xp UNUSED;
11121 int idx;
11122{
11123 if (idx == 0)
11124 return (char_u *)"mswin";
11125 if (idx == 1)
11126 return (char_u *)"xterm";
11127 return NULL;
11128}
11129#endif
11130
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131#ifdef FEAT_AUTOCMD
11132static int filetype_detect = FALSE;
11133static int filetype_plugin = FALSE;
11134static int filetype_indent = FALSE;
11135
11136/*
11137 * ":filetype [plugin] [indent] {on,off,detect}"
11138 * on: Load the filetype.vim file to install autocommands for file types.
11139 * off: Load the ftoff.vim file to remove all autocommands for file types.
11140 * plugin on: load filetype.vim and ftplugin.vim
11141 * plugin off: load ftplugof.vim
11142 * indent on: load filetype.vim and indent.vim
11143 * indent off: load indoff.vim
11144 */
11145 static void
11146ex_filetype(eap)
11147 exarg_T *eap;
11148{
11149 char_u *arg = eap->arg;
11150 int plugin = FALSE;
11151 int indent = FALSE;
11152
11153 if (*eap->arg == NUL)
11154 {
11155 /* Print current status. */
11156 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11157 filetype_detect ? "ON" : "OFF",
11158 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11159 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11160 return;
11161 }
11162
11163 /* Accept "plugin" and "indent" in any order. */
11164 for (;;)
11165 {
11166 if (STRNCMP(arg, "plugin", 6) == 0)
11167 {
11168 plugin = TRUE;
11169 arg = skipwhite(arg + 6);
11170 continue;
11171 }
11172 if (STRNCMP(arg, "indent", 6) == 0)
11173 {
11174 indent = TRUE;
11175 arg = skipwhite(arg + 6);
11176 continue;
11177 }
11178 break;
11179 }
11180 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11181 {
11182 if (*arg == 'o' || !filetype_detect)
11183 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011184 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 filetype_detect = TRUE;
11186 if (plugin)
11187 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011188 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 filetype_plugin = TRUE;
11190 }
11191 if (indent)
11192 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011193 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 filetype_indent = TRUE;
11195 }
11196 }
11197 if (*arg == 'd')
11198 {
11199 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011200 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 }
11202 }
11203 else if (STRCMP(arg, "off") == 0)
11204 {
11205 if (plugin || indent)
11206 {
11207 if (plugin)
11208 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011209 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 filetype_plugin = FALSE;
11211 }
11212 if (indent)
11213 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011214 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 filetype_indent = FALSE;
11216 }
11217 }
11218 else
11219 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011220 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 filetype_detect = FALSE;
11222 }
11223 }
11224 else
11225 EMSG2(_(e_invarg2), arg);
11226}
11227
11228/*
11229 * ":setfiletype {name}"
11230 */
11231 static void
11232ex_setfiletype(eap)
11233 exarg_T *eap;
11234{
11235 if (!did_filetype)
11236 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11237}
11238#endif
11239
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 static void
11241ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011242 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243{
11244#ifdef FEAT_DIGRAPHS
11245 if (*eap->arg != NUL)
11246 putdigraph(eap->arg);
11247 else
11248 listdigraphs();
11249#else
11250 EMSG(_("E196: No digraphs in this version"));
11251#endif
11252}
11253
11254 static void
11255ex_set(eap)
11256 exarg_T *eap;
11257{
11258 int flags = 0;
11259
11260 if (eap->cmdidx == CMD_setlocal)
11261 flags = OPT_LOCAL;
11262 else if (eap->cmdidx == CMD_setglobal)
11263 flags = OPT_GLOBAL;
11264#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11265 if (cmdmod.browse && flags == 0)
11266 ex_options(eap);
11267 else
11268#endif
11269 (void)do_set(eap->arg, flags);
11270}
11271
11272#ifdef FEAT_SEARCH_EXTRA
11273/*
11274 * ":nohlsearch"
11275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 static void
11277ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011278 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279{
11280 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011281 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282}
11283
11284/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011285 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 * Sets nextcmd to the start of the next command, if any. Also called when
11287 * skipping commands to find the next command.
11288 */
11289 static void
11290ex_match(eap)
11291 exarg_T *eap;
11292{
11293 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011294 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 char_u *end;
11296 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011297 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011298
11299 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011300 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011301 else
11302 {
11303 EMSG(e_invcmd);
11304 return;
11305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
11307 /* First clear any old pattern. */
11308 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011309 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310
11311 if (ends_excmd(*eap->arg))
11312 end = eap->arg;
11313 else if ((STRNICMP(eap->arg, "none", 4) == 0
11314 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11315 end = eap->arg + 4;
11316 else
11317 {
11318 p = skiptowhite(eap->arg);
11319 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011320 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 p = skipwhite(p);
11322 if (*p == NUL)
11323 {
11324 /* There must be two arguments. */
11325 EMSG2(_(e_invarg2), eap->arg);
11326 return;
11327 }
11328 end = skip_regexp(p + 1, *p, TRUE, NULL);
11329 if (!eap->skip)
11330 {
11331 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11332 {
11333 eap->errmsg = e_trailing;
11334 return;
11335 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011336 if (*end != *p)
11337 {
11338 EMSG2(_(e_invarg2), p);
11339 return;
11340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
11342 c = *end;
11343 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011344 match_add(curwin, g, p + 1, 10, id);
11345 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011346 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 }
11348 }
11349 eap->nextcmd = find_nextcmd(end);
11350}
11351#endif
11352
11353#ifdef FEAT_CRYPT
11354/*
11355 * ":X": Get crypt key
11356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 static void
11358ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011359 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011361 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011362 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363}
11364#endif
11365
11366#ifdef FEAT_FOLDING
11367 static void
11368ex_fold(eap)
11369 exarg_T *eap;
11370{
11371 if (foldManualAllowed(TRUE))
11372 foldCreate(eap->line1, eap->line2);
11373}
11374
11375 static void
11376ex_foldopen(eap)
11377 exarg_T *eap;
11378{
11379 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11380 eap->forceit, FALSE);
11381}
11382
11383 static void
11384ex_folddo(eap)
11385 exarg_T *eap;
11386{
11387 linenr_T lnum;
11388
11389 /* First set the marks for all lines closed/open. */
11390 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11391 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11392 ml_setmarked(lnum);
11393
11394 /* Execute the command on the marked lines. */
11395 global_exe(eap->arg);
11396 ml_clearmarked(); /* clear rest of the marks */
11397}
11398#endif