blob: 97b836e005964cc8f9ee7fe1b8dddf062ca1b99d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
287#ifndef FEAT_SNIFF
288# define ex_sniff ex_ni
289#endif
290#ifndef FEAT_KEYMAP
291# define ex_loadkeymap ex_ni
292#endif
293static void ex_swapname __ARGS((exarg_T *eap));
294static void ex_syncbind __ARGS((exarg_T *eap));
295static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_pwd __ARGS((exarg_T *eap));
297static void ex_equal __ARGS((exarg_T *eap));
298static void ex_sleep __ARGS((exarg_T *eap));
299static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
300static void ex_winsize __ARGS((exarg_T *eap));
301#ifdef FEAT_WINDOWS
302static void ex_wincmd __ARGS((exarg_T *eap));
303#else
304# define ex_wincmd ex_ni
305#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_winpos __ARGS((exarg_T *eap));
308#else
309# define ex_winpos ex_ni
310#endif
311static void ex_operators __ARGS((exarg_T *eap));
312static void ex_put __ARGS((exarg_T *eap));
313static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000314static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_submagic __ARGS((exarg_T *eap));
316static void ex_join __ARGS((exarg_T *eap));
317static void ex_at __ARGS((exarg_T *eap));
318static void ex_bang __ARGS((exarg_T *eap));
319static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#ifdef FEAT_PERSISTENT_UNDO
321static void ex_wundo __ARGS((exarg_T *eap));
322static void ex_rundo __ARGS((exarg_T *eap));
323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000325static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redir __ARGS((exarg_T *eap));
327static void ex_redraw __ARGS((exarg_T *eap));
328static void ex_redrawstatus __ARGS((exarg_T *eap));
329static void close_redir __ARGS((void));
330static void ex_mkrc __ARGS((exarg_T *eap));
331static void ex_mark __ARGS((exarg_T *eap));
332#ifdef FEAT_USR_CMDS
333static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000334static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336#ifdef FEAT_EX_EXTRA
337static void ex_normal __ARGS((exarg_T *eap));
338static void ex_startinsert __ARGS((exarg_T *eap));
339static void ex_stopinsert __ARGS((exarg_T *eap));
340#else
341# define ex_normal ex_ni
342# define ex_align ex_ni
343# define ex_retab ex_ni
344# define ex_startinsert ex_ni
345# define ex_stopinsert ex_ni
346# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000347# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349#ifdef FEAT_FIND_ID
350static void ex_checkpath __ARGS((exarg_T *eap));
351static void ex_findpat __ARGS((exarg_T *eap));
352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357static void ex_psearch __ARGS((exarg_T *eap));
358#else
359# define ex_psearch ex_ni
360#endif
361static void ex_tag __ARGS((exarg_T *eap));
362static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392static char_u *arg_all __ARGS((void));
393#ifdef FEAT_SESSION
394static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000395static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static void ex_loadview __ARGS((exarg_T *eap));
397static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
406static void ex_viminfo __ARGS((exarg_T *eap));
407#else
408# define ex_viminfo ex_ni
409#endif
410static void ex_behave __ARGS((exarg_T *eap));
411#ifdef FEAT_AUTOCMD
412static void ex_filetype __ARGS((exarg_T *eap));
413static void ex_setfiletype __ARGS((exarg_T *eap));
414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
426static void ex_digraphs __ARGS((exarg_T *eap));
427static void ex_set __ARGS((exarg_T *eap));
428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
432static void ex_nohlsearch __ARGS((exarg_T *eap));
433static void ex_match __ARGS((exarg_T *eap));
434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
439static void ex_X __ARGS((exarg_T *eap));
440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
444static void ex_fold __ARGS((exarg_T *eap));
445static void ex_foldopen __ARGS((exarg_T *eap));
446static void ex_folddo __ARGS((exarg_T *eap));
447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
550 char_u *(*getline) __ARGS((int, void *, int));
551 void *cookie;
552};
553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
555static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
574static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
575static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576
577 static void
578save_dbg_stuff(dsp)
579 struct dbg_stuff *dsp;
580{
581 dsp->trylevel = trylevel; trylevel = 0;
582 dsp->force_abort = force_abort; force_abort = FALSE;
583 dsp->caught_stack = caught_stack; caught_stack = NULL;
584 dsp->vv_exception = v_exception(NULL);
585 dsp->vv_throwpoint = v_throwpoint(NULL);
586
587 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
588 dsp->did_emsg = did_emsg; did_emsg = FALSE;
589 dsp->got_int = got_int; got_int = FALSE;
590 dsp->did_throw = did_throw; did_throw = FALSE;
591 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
592 dsp->check_cstack = check_cstack; check_cstack = FALSE;
593 dsp->current_exception = current_exception; current_exception = NULL;
594}
595
596 static void
597restore_dbg_stuff(dsp)
598 struct dbg_stuff *dsp;
599{
600 suppress_errthrow = FALSE;
601 trylevel = dsp->trylevel;
602 force_abort = dsp->force_abort;
603 caught_stack = dsp->caught_stack;
604 (void)v_exception(dsp->vv_exception);
605 (void)v_throwpoint(dsp->vv_throwpoint);
606 did_emsg = dsp->did_emsg;
607 got_int = dsp->got_int;
608 did_throw = dsp->did_throw;
609 need_rethrow = dsp->need_rethrow;
610 check_cstack = dsp->check_cstack;
611 current_exception = dsp->current_exception;
612}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614
615
616/*
617 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
618 * command is given.
619 */
620 void
621do_exmode(improved)
622 int improved; /* TRUE for "improved Ex" mode */
623{
624 int save_msg_scroll;
625 int prev_msg_row;
626 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 int changedtick;
628
629 if (improved)
630 exmode_active = EXMODE_VIM;
631 else
632 exmode_active = EXMODE_NORMAL;
633 State = NORMAL;
634
635 /* When using ":global /pat/ visual" and then "Q" we return to continue
636 * the :global command. */
637 if (global_busy)
638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 save_msg_scroll = msg_scroll;
641 ++RedrawingDisabled; /* don't redisplay the window */
642 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_GUI
644 /* Ignore scrollbar and mouse events in Ex mode */
645 ++hold_gui_events;
646#endif
647#ifdef FEAT_SNIFF
648 want_sniff_request = 0; /* No K_SNIFF wanted */
649#endif
650
651 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
652 while (exmode_active)
653 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000654#ifdef FEAT_EX_EXTRA
655 /* Check for a ":normal" command and no more characters left. */
656 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
657 {
658 exmode_active = FALSE;
659 break;
660 }
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 msg_scroll = TRUE;
663 need_wait_return = FALSE;
664 ex_pressedreturn = FALSE;
665 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000666 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 prev_msg_row = msg_row;
668 prev_line = curwin->w_cursor.lnum;
669#ifdef FEAT_SNIFF
670 ProcessSniffRequests();
671#endif
672 if (improved)
673 {
674 cmdline_row = msg_row;
675 do_cmdline(NULL, getexline, NULL, 0);
676 }
677 else
678 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
679 lines_left = Rows - 1;
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if ((prev_line != curwin->w_cursor.lnum
682 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (curbuf->b_ml.ml_flags & ML_EMPTY)
685 EMSG(_(e_emptybuf));
686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (ex_pressedreturn)
689 {
690 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000691 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 msg_row = prev_msg_row;
693 if (prev_msg_row == Rows - 1)
694 msg_row--;
695 }
696 msg_col = 0;
697 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
698 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
702 {
703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
704 EMSG(_(e_emptybuf));
705 else
706 EMSG(_("E501: At end-of-file"));
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710#ifdef FEAT_GUI
711 --hold_gui_events;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 --RedrawingDisabled;
714 --no_wait_return;
715 update_screen(CLEAR);
716 need_wait_return = FALSE;
717 msg_scroll = save_msg_scroll;
718}
719
720/*
721 * Execute a simple command line. Used for translated commands like "*".
722 */
723 int
724do_cmdline_cmd(cmd)
725 char_u *cmd;
726{
727 return do_cmdline(cmd, NULL, NULL,
728 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
729}
730
731/*
732 * do_cmdline(): execute one Ex command line
733 *
734 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * 2. Split up in parts separated with '|'.
737 *
738 * This function can be called recursively!
739 *
740 * flags:
741 * DOCMD_VERBOSE - The command will be included in the error message.
742 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * DOCMD_KEYTYPED - Don't reset KeyTyped.
745 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
746 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
747 *
748 * return FAIL if cmdline could not be executed, OK otherwise
749 */
750 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 char_u *(*fgetline) __ARGS((int, void *, int));
754 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int flags;
756{
757 char_u *next_cmdline; /* next cmd to execute */
758 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 static int recursive = 0; /* recursive depth */
761 int msg_didout_before_start = 0;
762 int count = 0; /* line number count */
763 int did_inc = FALSE; /* incremented RedrawingDisabled */
764 int retval = OK;
765#ifdef FEAT_EVAL
766 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int current_line = 0; /* active line in lines_ga */
769 char_u *fname = NULL; /* function or script name */
770 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
771 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000772 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int initial_trylevel;
774 struct msglist **saved_msg_list = NULL;
775 struct msglist *private_msg_list;
776
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 char_u *(*cmd_getline) __ARGS((int, void *, int));
779 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# define cmd_cookie cookie
786#endif
787 static int call_depth = 0; /* recursiveness */
788
789#ifdef FEAT_EVAL
790 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
791 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000792 * This ensures that the do_errthrow() call in do_one_cmd() does not
793 * combine the messages stored by an earlier invocation of do_one_cmd()
794 * with the command name of the later one. This would happen when
795 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 saved_msg_list = msg_list;
797 msg_list = &private_msg_list;
798 private_msg_list = NULL;
799#endif
800
801 /* It's possible to create an endless loop with ":execute", catch that
802 * here. The value of 200 allows nested function calls, ":source", etc. */
803 if (call_depth == 200)
804 {
805 EMSG(_("E169: Command too recursive"));
806#ifdef FEAT_EVAL
807 /* When converting to an exception, we do not include the command name
808 * since this is not an error of the specific command. */
809 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
810 msg_list = saved_msg_list;
811#endif
812 return FAIL;
813 }
814 ++call_depth;
815
816#ifdef FEAT_EVAL
817 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 cstack.cs_trylevel = 0;
820 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000821 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
823
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++ex_nesting_level;
830
831 /* Get the function or script name and the address where the next breakpoint
832 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = func_name(real_cookie);
836 breakpoint = func_breakpoint(real_cookie);
837 dbg_tick = func_dbg_tick(real_cookie);
838 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 fname = sourcing_name;
842 breakpoint = source_breakpoint(real_cookie);
843 dbg_tick = source_dbg_tick(real_cookie);
844 }
845
846 /*
847 * Initialize "force_abort" and "suppress_errthrow" at the top level.
848 */
849 if (!recursive)
850 {
851 force_abort = FALSE;
852 suppress_errthrow = FALSE;
853 }
854
855 /*
856 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 * exception handling (used when debugging). Otherwise clear it to avoid
858 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000860 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000861 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200863 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 initial_trylevel = trylevel;
866
867 /*
868 * "did_throw" will be set to TRUE when an exception is being thrown.
869 */
870 did_throw = FALSE;
871#endif
872 /*
873 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * If force_abort is set, we cancel everything.
876 */
877 did_emsg = FALSE;
878
879 /*
880 * KeyTyped is only set when calling vgetc(). Reset it here when not
881 * calling vgetc() (sourced command lines).
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (!(flags & DOCMD_KEYTYPED)
884 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100972 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cmd_cookie = cookie;
1006 }
1007#endif
1008
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001145 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001166 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001298 && !(did_emsg
1299#ifdef FEAT_EVAL
1300 /* Keep going when inside try/catch, so that the error can be
1301 * dealth with, except when it is a syntax error, it may cause
1302 * the :endtry to be missed. */
1303 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1304#endif
1305 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 && (getline_equal(fgetline, cookie, getexmodeline)
1307 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (next_cmdline != NULL
1309#ifdef FEAT_EVAL
1310 || cstack.cs_idx >= 0
1311#endif
1312 || (flags & DOCMD_REPEAT)));
1313
1314 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001315 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef FEAT_EVAL
1317 free_cmdlines(&lines_ga);
1318 ga_clear(&lines_ga);
1319
1320 if (cstack.cs_idx >= 0)
1321 {
1322 /*
1323 * If a sourced file or executed function ran to its end, report the
1324 * unclosed conditional.
1325 */
1326 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 && ((getline_equal(fgetline, cookie, getsourceline)
1328 && !source_finished(fgetline, cookie))
1329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && !func_has_ended(real_cookie))))
1331 {
1332 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1333 EMSG(_(e_endtry));
1334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1335 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001336 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1337 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 EMSG(_(e_endif));
1340 }
1341
1342 /*
1343 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1344 * ":endtry" in a sourced file or executed function. If the try
1345 * conditional is in its finally clause, ignore anything pending.
1346 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001350 {
1351 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1352
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001353 if (idx >= 0)
1354 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1356 &cstack.cs_looplevel);
1357 }
1358 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 trylevel = initial_trylevel;
1360 }
1361
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001362 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1363 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 ? (char_u *)"endfunction" : (char_u *)NULL);
1367
1368 if (trylevel == 0)
1369 {
1370 /*
1371 * When an exception is being thrown out of the outermost try
1372 * conditional, discard the uncaught exception, disable the conversion
1373 * of interrupts or errors to exceptions, and ensure that no more
1374 * commands are executed.
1375 */
1376 if (did_throw)
1377 {
1378 void *p = NULL;
1379 char_u *saved_sourcing_name;
1380 int saved_sourcing_lnum;
1381 struct msglist *messages = NULL, *next;
1382
1383 /*
1384 * If the uncaught exception is a user exception, report it as an
1385 * error. If it is an error exception, display the saved error
1386 * message now. For an interrupt exception, do nothing; the
1387 * interrupt message is given elsewhere.
1388 */
1389 switch (current_exception->type)
1390 {
1391 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001392 vim_snprintf((char *)IObuff, IOSIZE,
1393 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 current_exception->value);
1395 p = vim_strsave(IObuff);
1396 break;
1397 case ET_ERROR:
1398 messages = current_exception->messages;
1399 current_exception->messages = NULL;
1400 break;
1401 case ET_INTERRUPT:
1402 break;
1403 default:
1404 p = vim_strsave((char_u *)_(e_internal));
1405 }
1406
1407 saved_sourcing_name = sourcing_name;
1408 saved_sourcing_lnum = sourcing_lnum;
1409 sourcing_name = current_exception->throw_name;
1410 sourcing_lnum = current_exception->throw_lnum;
1411 current_exception->throw_name = NULL;
1412
1413 discard_current_exception(); /* uses IObuff if 'verbose' */
1414 suppress_errthrow = TRUE;
1415 force_abort = TRUE;
1416
1417 if (messages != NULL)
1418 {
1419 do
1420 {
1421 next = messages->next;
1422 emsg(messages->msg);
1423 vim_free(messages->msg);
1424 vim_free(messages);
1425 messages = next;
1426 }
1427 while (messages != NULL);
1428 }
1429 else if (p != NULL)
1430 {
1431 emsg(p);
1432 vim_free(p);
1433 }
1434 vim_free(sourcing_name);
1435 sourcing_name = saved_sourcing_name;
1436 sourcing_lnum = saved_sourcing_lnum;
1437 }
1438
1439 /*
1440 * On an interrupt or an aborting error not converted to an exception,
1441 * disable the conversion of errors to exceptions. (Interrupts are not
1442 * converted any more, here.) This enables also the interrupt message
1443 * when force_abort is set and did_emsg unset in case of an interrupt
1444 * from a finally clause after an error.
1445 */
1446 else if (got_int || (did_emsg && force_abort))
1447 suppress_errthrow = TRUE;
1448 }
1449
1450 /*
1451 * The current cstack will be freed when do_cmdline() returns. An uncaught
1452 * exception will have to be rethrown in the previous cstack. If a function
1453 * has just returned or a script file was just finished and the previous
1454 * cstack belongs to the same function or, respectively, script file, it
1455 * will have to be checked for finally clauses to be executed due to the
1456 * ":return" or ":finish". This is done in do_one_cmd().
1457 */
1458 if (did_throw)
1459 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001460 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 && ex_nesting_level > func_level(real_cookie) + 1))
1464 {
1465 if (!did_throw)
1466 check_cstack = TRUE;
1467 }
1468 else
1469 {
1470 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 --ex_nesting_level;
1473 /*
1474 * Go to debug mode when returning from a function in which we are
1475 * single-stepping.
1476 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001477 if ((getline_equal(fgetline, cookie, getsourceline)
1478 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001480 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 ? (char_u *)_("End of sourced file")
1482 : (char_u *)_("End of function"));
1483 }
1484
1485 /*
1486 * Restore the exception environment (done after returning from the
1487 * debugger).
1488 */
1489 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001490 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 msg_list = saved_msg_list;
1493#endif /* FEAT_EVAL */
1494
1495 /*
1496 * If there was too much output to fit on the command line, ask the user to
1497 * hit return before redrawing the screen. With the ":global" command we do
1498 * this only once after the command is finished.
1499 */
1500 if (did_inc)
1501 {
1502 --RedrawingDisabled;
1503 --no_wait_return;
1504 msg_scroll = FALSE;
1505
1506 /*
1507 * When just finished an ":if"-":else" which was typed, no need to
1508 * wait for hit-return. Also for an error situation.
1509 */
1510 if (retval == FAIL
1511#ifdef FEAT_EVAL
1512 || (did_endif && KeyTyped && !did_emsg)
1513#endif
1514 )
1515 {
1516 need_wait_return = FALSE;
1517 msg_didany = FALSE; /* don't wait when restarting edit */
1518 }
1519 else if (need_wait_return)
1520 {
1521 /*
1522 * The msg_start() above clears msg_didout. The wait_return we do
1523 * here should not overwrite the command that may be shown before
1524 * doing that.
1525 */
1526 msg_didout |= msg_didout_before_start;
1527 wait_return(FALSE);
1528 }
1529 }
1530
1531#ifndef FEAT_EVAL
1532 /*
1533 * Reset if_level, in case a sourced script file contains more ":if" than
1534 * ":endif" (could be ":if x | foo | endif").
1535 */
1536 if_level = 0;
1537#endif
1538
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001539 did_endif = FALSE; /* in case do_cmdline used recursively */
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 --call_depth;
1542 return retval;
1543}
1544
1545#ifdef FEAT_EVAL
1546/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 int c;
1552 void *cookie;
1553 int indent;
1554{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 wcmd_T *wp;
1557 char_u *line;
1558
1559 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1560 {
1561 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (cp->getline == NULL)
1566 line = getcmdline(c, 0L, indent);
1567 else
1568 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001569 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 ++cp->current_line;
1571
1572 return line;
1573 }
1574
1575 KeyTyped = FALSE;
1576 ++cp->current_line;
1577 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1578 sourcing_lnum = wp->lnum;
1579 return vim_strsave(wp->line);
1580}
1581
1582/*
1583 * Store a line in "gap" so that a ":while" loop can execute it again.
1584 */
1585 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001586store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 garray_T *gap;
1588 char_u *line;
1589{
1590 if (ga_grow(gap, 1) == FAIL)
1591 return FAIL;
1592 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1593 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1594 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 return OK;
1596}
1597
1598/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001599 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 static void
1602free_cmdlines(gap)
1603 garray_T *gap;
1604{
1605 while (gap->ga_len > 0)
1606 {
1607 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1608 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610}
1611#endif
1612
1613/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001614 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1615 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618getline_equal(fgetline, cookie, func)
1619 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001620 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 char_u *(*func) __ARGS((int, void *, int));
1622{
1623#ifdef FEAT_EVAL
1624 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001625 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001628 * function that's originally used to obtain the lines. This may be
1629 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 gp = cp->getline;
1635 cp = cp->cookie;
1636 }
1637 return gp == func;
1638#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641}
1642
1643#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1644/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001645 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 * getline function. Otherwise return "cookie".
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001650 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653# ifdef FEAT_EVAL
1654 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001655 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar89d40322006-08-29 15:30:07 +00001657 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001658 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001661 cp = (struct loop_cookie *)cookie;
1662 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 gp = cp->getline;
1665 cp = cp->cookie;
1666 }
1667 return cp;
1668# else
1669 return cookie;
1670# endif
1671}
1672#endif
1673
1674/*
1675 * Execute one Ex command.
1676 *
1677 * If 'sourcing' is TRUE, the command will be included in the error message.
1678 *
1679 * 1. skip comment lines and leading space
1680 * 2. handle command modifiers
1681 * 3. parse range
1682 * 4. parse command
1683 * 5. parse arguments
1684 * 6. switch on command name
1685 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001686 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
1688 * This function may be called recursively!
1689 */
1690#if (_MSC_VER == 1200)
1691/*
Bram Moolenaared203462004-06-16 11:19:22 +00001692 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001694 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
1696 static char_u *
1697do_one_cmd(cmdlinep, sourcing,
1698#ifdef FEAT_EVAL
1699 cstack,
1700#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001701 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 char_u **cmdlinep;
1703 int sourcing;
1704#ifdef FEAT_EVAL
1705 struct condstack *cstack;
1706#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 char_u *(*fgetline) __ARGS((int, void *, int));
1708 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 char_u *p;
1711 linenr_T lnum;
1712 long n;
1713 char_u *errormsg = NULL; /* error message */
1714 exarg_T ea; /* Ex command arguments */
1715 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001716 int save_msg_scroll = msg_scroll;
1717 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001719#ifdef HAVE_SANDBOX
1720 int did_sandbox = FALSE;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 cmdmod_T save_cmdmod;
1723 int ni; /* set when Not Implemented */
1724
1725 vim_memset(&ea, 0, sizeof(ea));
1726 ea.line1 = 1;
1727 ea.line2 = 1;
1728#ifdef FEAT_EVAL
1729 ++ex_nesting_level;
1730#endif
1731
1732 /* when not editing the last file :q has to be typed twice */
1733 if (quitmore
1734#ifdef FEAT_EVAL
1735 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
1738 )
1739 --quitmore;
1740
1741 /*
1742 * Reset browse, confirm, etc.. They are restored when returning, for
1743 * recursive calls.
1744 */
1745 save_cmdmod = cmdmod;
1746 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1747
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001748 /* "#!anything" is handled like a comment. */
1749 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1750 goto doend;
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /*
1753 * Repeat until no more command modifiers are found.
1754 */
1755 ea.cmd = *cmdlinep;
1756 for (;;)
1757 {
1758/*
1759 * 1. skip comment lines and leading white space and colons
1760 */
1761 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1762 ++ea.cmd;
1763
1764 /* in ex mode, an empty line works like :+ */
1765 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001766 && (getline_equal(fgetline, cookie, getexmodeline)
1767 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001768 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770 ea.cmd = (char_u *)"+";
1771 ex_pressedreturn = TRUE;
1772 }
1773
1774 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001775 if (*ea.cmd == '"')
1776 goto doend;
1777 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001778 {
1779 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783/*
1784 * 2. handle command modifiers.
1785 */
1786 p = ea.cmd;
1787 if (VIM_ISDIGIT(*ea.cmd))
1788 p = skipwhite(skipdigits(ea.cmd));
1789 switch (*p)
1790 {
1791 /* When adding an entry, also modify cmd_exists(). */
1792 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1793 break;
1794#ifdef FEAT_WINDOWS
1795 cmdmod.split |= WSP_ABOVE;
1796#endif
1797 continue;
1798
1799 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1800 {
1801#ifdef FEAT_WINDOWS
1802 cmdmod.split |= WSP_BELOW;
1803#endif
1804 continue;
1805 }
1806 if (checkforcmd(&ea.cmd, "browse", 3))
1807 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001808#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 cmdmod.browse = TRUE;
1810#endif
1811 continue;
1812 }
1813 if (!checkforcmd(&ea.cmd, "botright", 2))
1814 break;
1815#ifdef FEAT_WINDOWS
1816 cmdmod.split |= WSP_BOT;
1817#endif
1818 continue;
1819
1820 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1821 break;
1822#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1823 cmdmod.confirm = TRUE;
1824#endif
1825 continue;
1826
1827 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1828 {
1829 cmdmod.keepmarks = TRUE;
1830 continue;
1831 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001832 if (checkforcmd(&ea.cmd, "keepalt", 5))
1833 {
1834 cmdmod.keepalt = TRUE;
1835 continue;
1836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1838 break;
1839 cmdmod.keepjumps = TRUE;
1840 continue;
1841
1842 /* ":hide" and ":hide | cmd" are not modifiers */
1843 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1844 || *p == NUL || ends_excmd(*p))
1845 break;
1846 ea.cmd = p;
1847 cmdmod.hide = TRUE;
1848 continue;
1849
1850 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1851 {
1852 cmdmod.lockmarks = TRUE;
1853 continue;
1854 }
1855
1856 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1857 break;
1858#ifdef FEAT_WINDOWS
1859 cmdmod.split |= WSP_ABOVE;
1860#endif
1861 continue;
1862
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001863 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1864 break;
1865#ifdef FEAT_AUTOCMD
1866 if (cmdmod.save_ei == NULL)
1867 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001868 /* Set 'eventignore' to "all". Restore the
1869 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001870 cmdmod.save_ei = vim_strsave(p_ei);
1871 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001872 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001873 }
1874#endif
1875 continue;
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1878 break;
1879#ifdef FEAT_WINDOWS
1880 cmdmod.split |= WSP_BELOW;
1881#endif
1882 continue;
1883
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001884 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1885 {
1886#ifdef HAVE_SANDBOX
1887 if (!did_sandbox)
1888 ++sandbox;
1889 did_sandbox = TRUE;
1890#endif
1891 continue;
1892 }
1893 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001895 if (save_msg_silent == -1)
1896 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1899 {
1900 /* ":silent!", but not "silent !cmd" */
1901 ea.cmd = skipwhite(ea.cmd + 1);
1902 ++emsg_silent;
1903 ++did_esilent;
1904 }
1905 continue;
1906
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001907 case 't': if (checkforcmd(&p, "tab", 3))
1908 {
1909#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001910 if (vim_isdigit(*ea.cmd))
1911 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1912 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001913 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001914 ea.cmd = p;
1915#endif
1916 continue;
1917 }
1918 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 break;
1920#ifdef FEAT_WINDOWS
1921 cmdmod.split |= WSP_TOP;
1922#endif
1923 continue;
1924
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001925 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1926 break;
1927 if (save_msg_silent == -1)
1928 save_msg_silent = msg_silent;
1929 msg_silent = 0;
1930 continue;
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1933 {
1934#ifdef FEAT_VERTSPLIT
1935 cmdmod.split |= WSP_VERT;
1936#endif
1937 continue;
1938 }
1939 if (!checkforcmd(&p, "verbose", 4))
1940 break;
1941 if (verbose_save < 0)
1942 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001943 if (vim_isdigit(*ea.cmd))
1944 p_verbose = atoi((char *)ea.cmd);
1945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 p_verbose = 1;
1947 ea.cmd = p;
1948 continue;
1949 }
1950 break;
1951 }
1952
1953#ifdef FEAT_EVAL
1954 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1955 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1956#else
1957 ea.skip = (if_level > 0);
1958#endif
1959
1960#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001961# ifdef FEAT_PROFILE
1962 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001963 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001964 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001965 if (getline_equal(fgetline, cookie, get_func_line))
1966 func_line_exec(getline_cookie(fgetline, cookie));
1967 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001968 script_line_exec();
1969 }
1970#endif
1971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 /* May go to debug mode. If this happens and the ">quit" debug command is
1973 * used, throw an interrupt exception and skip the next command. */
1974 dbg_check_breakpoint(&ea);
1975 if (!ea.skip && got_int)
1976 {
1977 ea.skip = TRUE;
1978 (void)do_intthrow(cstack);
1979 }
1980#endif
1981
1982/*
1983 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1984 *
1985 * where 'addr' is:
1986 *
1987 * % (entire file)
1988 * $ [+-NUM]
1989 * 'x [+-NUM] (where x denotes a currently defined mark)
1990 * . [+-NUM]
1991 * [+-NUM]..
1992 * NUM
1993 *
1994 * The ea.cmd pointer is updated to point to the first character following the
1995 * range spec. If an initial address is found, but no second, the upper bound
1996 * is equal to the lower.
1997 */
1998
1999 /* repeat for all ',' or ';' separated addresses */
2000 for (;;)
2001 {
2002 ea.line1 = ea.line2;
2003 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2004 ea.cmd = skipwhite(ea.cmd);
2005 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2006 if (ea.cmd == NULL) /* error detected */
2007 goto doend;
2008 if (lnum == MAXLNUM)
2009 {
2010 if (*ea.cmd == '%') /* '%' - all lines */
2011 {
2012 ++ea.cmd;
2013 ea.line1 = 1;
2014 ea.line2 = curbuf->b_ml.ml_line_count;
2015 ++ea.addr_count;
2016 }
2017 /* '*' - visual area */
2018 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2019 {
2020 pos_T *fp;
2021
2022 ++ea.cmd;
2023 if (!ea.skip)
2024 {
2025 fp = getmark('<', FALSE);
2026 if (check_mark(fp) == FAIL)
2027 goto doend;
2028 ea.line1 = fp->lnum;
2029 fp = getmark('>', FALSE);
2030 if (check_mark(fp) == FAIL)
2031 goto doend;
2032 ea.line2 = fp->lnum;
2033 ++ea.addr_count;
2034 }
2035 }
2036 }
2037 else
2038 ea.line2 = lnum;
2039 ea.addr_count++;
2040
2041 if (*ea.cmd == ';')
2042 {
2043 if (!ea.skip)
2044 curwin->w_cursor.lnum = ea.line2;
2045 }
2046 else if (*ea.cmd != ',')
2047 break;
2048 ++ea.cmd;
2049 }
2050
2051 /* One address given: set start and end lines */
2052 if (ea.addr_count == 1)
2053 {
2054 ea.line1 = ea.line2;
2055 /* ... but only implicit: really no address given */
2056 if (lnum == MAXLNUM)
2057 ea.addr_count = 0;
2058 }
2059
2060 /* Don't leave the cursor on an illegal line (caused by ';') */
2061 check_cursor_lnum();
2062
2063/*
2064 * 4. parse command
2065 */
2066
2067 /*
2068 * Skip ':' and any white space
2069 */
2070 ea.cmd = skipwhite(ea.cmd);
2071 while (*ea.cmd == ':')
2072 ea.cmd = skipwhite(ea.cmd + 1);
2073
2074 /*
2075 * If we got a line, but no command, then go to the line.
2076 * If we find a '|' or '\n' we set ea.nextcmd.
2077 */
2078 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2079 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2080 {
2081 /*
2082 * strange vi behaviour:
2083 * ":3" jumps to line 3
2084 * ":3|..." prints line 3
2085 * ":|" prints current line
2086 */
2087 if (ea.skip) /* skip this if inside :if */
2088 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002089 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 ea.cmdidx = CMD_print;
2092 ea.argt = RANGE+COUNT+TRLBAR;
2093 if ((errormsg = invalid_range(&ea)) == NULL)
2094 {
2095 correct_range(&ea);
2096 ex_print(&ea);
2097 }
2098 }
2099 else if (ea.addr_count != 0)
2100 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002101 if (ea.line2 > curbuf->b_ml.ml_line_count)
2102 {
2103 /* With '-' in 'cpoptions' a line number past the file is an
2104 * error, otherwise put it at the end of the file. */
2105 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2106 ea.line2 = -1;
2107 else
2108 ea.line2 = curbuf->b_ml.ml_line_count;
2109 }
2110
2111 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002112 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 else
2114 {
2115 if (ea.line2 == 0)
2116 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else
2118 curwin->w_cursor.lnum = ea.line2;
2119 beginline(BL_SOL | BL_FIX);
2120 }
2121 }
2122 goto doend;
2123 }
2124
2125 /* Find the command and let "p" point to after it. */
2126 p = find_command(&ea, NULL);
2127
2128#ifdef FEAT_USR_CMDS
2129 if (p == NULL)
2130 {
2131 if (!ea.skip)
2132 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2133 goto doend;
2134 }
2135 /* Check for wrong commands. */
2136 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2137 {
2138 errormsg = uc_fun_cmd();
2139 goto doend;
2140 }
2141#endif
2142 if (ea.cmdidx == CMD_SIZE)
2143 {
2144 if (!ea.skip)
2145 {
2146 STRCPY(IObuff, _("E492: Not an editor command"));
2147 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002148 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002150 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152 goto doend;
2153 }
2154
2155 ni = (
2156#ifdef FEAT_USR_CMDS
2157 !USER_CMDIDX(ea.cmdidx) &&
2158#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002159 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002160#ifdef HAVE_EX_SCRIPT_NI
2161 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2162#endif
2163 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165#ifndef FEAT_EVAL
2166 /*
2167 * When the expression evaluation is disabled, recognize the ":if" and
2168 * ":endif" commands and ignore everything in between it.
2169 */
2170 if (ea.cmdidx == CMD_if)
2171 ++if_level;
2172 if (if_level)
2173 {
2174 if (ea.cmdidx == CMD_endif)
2175 --if_level;
2176 goto doend;
2177 }
2178
2179#endif
2180
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002181 /* forced commands */
2182 if (*p == '!' && ea.cmdidx != CMD_substitute
2183 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 ++p;
2186 ea.forceit = TRUE;
2187 }
2188 else
2189 ea.forceit = FALSE;
2190
2191/*
2192 * 5. parse arguments
2193 */
2194#ifdef FEAT_USR_CMDS
2195 if (!USER_CMDIDX(ea.cmdidx))
2196#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002197 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 if (!ea.skip)
2200 {
2201#ifdef HAVE_SANDBOX
2202 if (sandbox != 0 && !(ea.argt & SBOXOK))
2203 {
2204 /* Command not allowed in sandbox. */
2205 errormsg = (char_u *)_(e_sandbox);
2206 goto doend;
2207 }
2208#endif
2209 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2210 {
2211 /* Command not allowed in non-'modifiable' buffer */
2212 errormsg = (char_u *)_(e_modifiable);
2213 goto doend;
2214 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002215
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002216 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# ifdef FEAT_USR_CMDS
2218 && !USER_CMDIDX(ea.cmdidx)
2219# endif
2220 )
2221 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002222 /* Command not allowed when editing the command line. */
2223#ifdef FEAT_CMDWIN
2224 if (cmdwin_type != 0)
2225 errormsg = (char_u *)_(e_cmdwin);
2226 else
2227#endif
2228 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 goto doend;
2230 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002232 /* Disallow editing another buffer when "curbuf_lock" is set.
2233 * Do allow ":edit" (check for argument later).
2234 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002236 && ea.cmdidx != CMD_edit
2237 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238# ifdef FEAT_USR_CMDS
2239 && !USER_CMDIDX(ea.cmdidx)
2240# endif
2241 && curbuf_locked())
2242 goto doend;
2243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
2245 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2246 {
2247 /* no range allowed */
2248 errormsg = (char_u *)_(e_norange);
2249 goto doend;
2250 }
2251 }
2252
2253 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2254 {
2255 errormsg = (char_u *)_(e_nobang);
2256 goto doend;
2257 }
2258
2259 /*
2260 * Don't complain about the range if it is not used
2261 * (could happen if line_count is accidentally set to 0).
2262 */
2263 if (!ea.skip && !ni)
2264 {
2265 /*
2266 * If the range is backwards, ask for confirmation and, if given, swap
2267 * ea.line1 & ea.line2 so it's forwards again.
2268 * When global command is busy, don't ask, will fail below.
2269 */
2270 if (!global_busy && ea.line1 > ea.line2)
2271 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002272 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002274 if (sourcing || exmode_active)
2275 {
2276 errormsg = (char_u *)_("E493: Backwards range given");
2277 goto doend;
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 if (ask_yesno((char_u *)
2280 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002281 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283 lnum = ea.line1;
2284 ea.line1 = ea.line2;
2285 ea.line2 = lnum;
2286 }
2287 if ((errormsg = invalid_range(&ea)) != NULL)
2288 goto doend;
2289 }
2290
2291 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2292 ea.line2 = 1;
2293
2294 correct_range(&ea);
2295
2296#ifdef FEAT_FOLDING
2297 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2298 {
2299 /* Put the first line at the start of a closed fold, put the last line
2300 * at the end of a closed fold. */
2301 (void)hasFolding(ea.line1, &ea.line1, NULL);
2302 (void)hasFolding(ea.line2, NULL, &ea.line2);
2303 }
2304#endif
2305
2306#ifdef FEAT_QUICKFIX
2307 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002308 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * option here, so things like % get expanded.
2310 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002311 p = replace_makeprg(&ea, p, cmdlinep);
2312 if (p == NULL)
2313 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315
2316 /*
2317 * Skip to start of argument.
2318 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2319 */
2320 if (ea.cmdidx == CMD_bang)
2321 ea.arg = p;
2322 else
2323 ea.arg = skipwhite(p);
2324
2325 /*
2326 * Check for "++opt=val" argument.
2327 * Must be first, allow ":w ++enc=utf8 !cmd"
2328 */
2329 if (ea.argt & ARGOPT)
2330 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2331 if (getargopt(&ea) == FAIL && !ni)
2332 {
2333 errormsg = (char_u *)_(e_invarg);
2334 goto doend;
2335 }
2336
2337 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2338 {
2339 if (*ea.arg == '>') /* append */
2340 {
2341 if (*++ea.arg != '>') /* typed wrong */
2342 {
2343 errormsg = (char_u *)_("E494: Use w or w>>");
2344 goto doend;
2345 }
2346 ea.arg = skipwhite(ea.arg + 1);
2347 ea.append = TRUE;
2348 }
2349 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2350 {
2351 ++ea.arg;
2352 ea.usefilter = TRUE;
2353 }
2354 }
2355
2356 if (ea.cmdidx == CMD_read)
2357 {
2358 if (ea.forceit)
2359 {
2360 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2361 ea.forceit = FALSE;
2362 }
2363 else if (*ea.arg == '!') /* :r !filter */
2364 {
2365 ++ea.arg;
2366 ea.usefilter = TRUE;
2367 }
2368 }
2369
2370 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2371 {
2372 ea.amount = 1;
2373 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2374 {
2375 ++ea.arg;
2376 ++ea.amount;
2377 }
2378 ea.arg = skipwhite(ea.arg);
2379 }
2380
2381 /*
2382 * Check for "+command" argument, before checking for next command.
2383 * Don't do this for ":read !cmd" and ":write !cmd".
2384 */
2385 if ((ea.argt & EDITCMD) && !ea.usefilter)
2386 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2387
2388 /*
2389 * Check for '|' to separate commands and '"' to start comments.
2390 * Don't do this for ":read !cmd" and ":write !cmd".
2391 */
2392 if ((ea.argt & TRLBAR) && !ea.usefilter)
2393 separate_nextcmd(&ea);
2394
2395 /*
2396 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002397 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2398 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002400 else if (ea.cmdidx == CMD_bang
2401 || ea.cmdidx == CMD_global
2402 || ea.cmdidx == CMD_vglobal
2403 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
2405 for (p = ea.arg; *p; ++p)
2406 {
2407 /* Remove one backslash before a newline, so that it's possible to
2408 * pass a newline to the shell and also a newline that is preceded
2409 * with a backslash. This makes it impossible to end a shell
2410 * command in a backslash, but that doesn't appear useful.
2411 * Halving the number of backslashes is incompatible with previous
2412 * versions. */
2413 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002414 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 else if (*p == '\n')
2416 {
2417 ea.nextcmd = p + 1;
2418 *p = NUL;
2419 break;
2420 }
2421 }
2422 }
2423
2424 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2425 {
2426 ea.line1 = 1;
2427 ea.line2 = curbuf->b_ml.ml_line_count;
2428 }
2429
2430 /* accept numbered register only when no count allowed (:put) */
2431 if ( (ea.argt & REGSTR)
2432 && *ea.arg != NUL
2433#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /* Do not allow register = for user commands */
2435 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
2437 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2438 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002439#ifndef FEAT_CLIPBOARD
2440 /* check these explicitly for a more specific error message */
2441 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002443 errormsg = (char_u *)_(e_invalidreg);
2444 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002447 if (
2448#ifdef FEAT_USR_CMDS
2449 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2450 && USER_CMDIDX(ea.cmdidx)))
2451#else
2452 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2453#endif
2454 )
2455 {
2456 ea.regname = *ea.arg++;
2457#ifdef FEAT_EVAL
2458 /* for '=' register: accept the rest of the line as an expression */
2459 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2460 {
2461 set_expr_line(vim_strsave(ea.arg));
2462 ea.arg += STRLEN(ea.arg);
2463 }
2464#endif
2465 ea.arg = skipwhite(ea.arg);
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468
2469 /*
2470 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2471 * count, it's a buffer name.
2472 */
2473 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2474 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2475 || vim_iswhite(*p)))
2476 {
2477 n = getdigits(&ea.arg);
2478 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002479 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 errormsg = (char_u *)_(e_zerocount);
2482 goto doend;
2483 }
2484 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2485 {
2486 ea.line2 = n;
2487 if (ea.addr_count == 0)
2488 ea.addr_count = 1;
2489 }
2490 else
2491 {
2492 ea.line1 = ea.line2;
2493 ea.line2 += n - 1;
2494 ++ea.addr_count;
2495 /*
2496 * Be vi compatible: no error message for out of range.
2497 */
2498 if (ea.line2 > curbuf->b_ml.ml_line_count)
2499 ea.line2 = curbuf->b_ml.ml_line_count;
2500 }
2501 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002502
2503 /*
2504 * Check for flags: 'l', 'p' and '#'.
2505 */
2506 if (ea.argt & EXFLAGS)
2507 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002509 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002510 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 errormsg = (char_u *)_(e_trailing);
2513 goto doend;
2514 }
2515
2516 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2517 {
2518 errormsg = (char_u *)_(e_argreq);
2519 goto doend;
2520 }
2521
2522#ifdef FEAT_EVAL
2523 /*
2524 * Skip the command when it's not going to be executed.
2525 * The commands like :if, :endif, etc. always need to be executed.
2526 * Also make an exception for commands that handle a trailing command
2527 * themselves.
2528 */
2529 if (ea.skip)
2530 {
2531 switch (ea.cmdidx)
2532 {
2533 /* commands that need evaluation */
2534 case CMD_while:
2535 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002536 case CMD_for:
2537 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 case CMD_if:
2539 case CMD_elseif:
2540 case CMD_else:
2541 case CMD_endif:
2542 case CMD_try:
2543 case CMD_catch:
2544 case CMD_finally:
2545 case CMD_endtry:
2546 case CMD_function:
2547 break;
2548
2549 /* Commands that handle '|' themselves. Check: A command should
2550 * either have the TRLBAR flag, appear in this list or appear in
2551 * the list at ":help :bar". */
2552 case CMD_aboveleft:
2553 case CMD_and:
2554 case CMD_belowright:
2555 case CMD_botright:
2556 case CMD_browse:
2557 case CMD_call:
2558 case CMD_confirm:
2559 case CMD_delfunction:
2560 case CMD_djump:
2561 case CMD_dlist:
2562 case CMD_dsearch:
2563 case CMD_dsplit:
2564 case CMD_echo:
2565 case CMD_echoerr:
2566 case CMD_echomsg:
2567 case CMD_echon:
2568 case CMD_execute:
2569 case CMD_help:
2570 case CMD_hide:
2571 case CMD_ijump:
2572 case CMD_ilist:
2573 case CMD_isearch:
2574 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002575 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 case CMD_keepjumps:
2577 case CMD_keepmarks:
2578 case CMD_leftabove:
2579 case CMD_let:
2580 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002581 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002583 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 case CMD_perl:
2585 case CMD_psearch:
2586 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002587 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002588 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 case CMD_return:
2590 case CMD_rightbelow:
2591 case CMD_ruby:
2592 case CMD_silent:
2593 case CMD_smagic:
2594 case CMD_snomagic:
2595 case CMD_substitute:
2596 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002597 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 case CMD_tcl:
2599 case CMD_throw:
2600 case CMD_tilde:
2601 case CMD_topleft:
2602 case CMD_unlet:
2603 case CMD_verbose:
2604 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002605 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 break;
2607
2608 default: goto doend;
2609 }
2610 }
2611#endif
2612
2613 if (ea.argt & XFILE)
2614 {
2615 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2616 goto doend;
2617 }
2618
2619#ifdef FEAT_LISTCMDS
2620 /*
2621 * Accept buffer name. Cannot be used at the same time with a buffer
2622 * number. Don't do this for a user command.
2623 */
2624 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2625# ifdef FEAT_USR_CMDS
2626 && !USER_CMDIDX(ea.cmdidx)
2627# endif
2628 )
2629 {
2630 /*
2631 * :bdelete, :bwipeout and :bunload take several arguments, separated
2632 * by spaces: find next space (skipping over escaped characters).
2633 * The others take one argument: ignore trailing spaces.
2634 */
2635 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2636 || ea.cmdidx == CMD_bunload)
2637 p = skiptowhite_esc(ea.arg);
2638 else
2639 {
2640 p = ea.arg + STRLEN(ea.arg);
2641 while (p > ea.arg && vim_iswhite(p[-1]))
2642 --p;
2643 }
2644 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2645 if (ea.line2 < 0) /* failed */
2646 goto doend;
2647 ea.addr_count = 1;
2648 ea.arg = skipwhite(p);
2649 }
2650#endif
2651
2652/*
2653 * 6. switch on command name
2654 *
2655 * The "ea" structure holds the arguments that can be used.
2656 */
2657 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ea.cookie = cookie;
2660#ifdef FEAT_EVAL
2661 ea.cstack = cstack;
2662#endif
2663
2664#ifdef FEAT_USR_CMDS
2665 if (USER_CMDIDX(ea.cmdidx))
2666 {
2667 /*
2668 * Execute a user-defined command.
2669 */
2670 do_ucmd(&ea);
2671 }
2672 else
2673#endif
2674 {
2675 /*
2676 * Call the function to execute the command.
2677 */
2678 ea.errmsg = NULL;
2679 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2680 if (ea.errmsg != NULL)
2681 errormsg = (char_u *)_(ea.errmsg);
2682 }
2683
2684#ifdef FEAT_EVAL
2685 /*
2686 * If the command just executed called do_cmdline(), any throw or ":return"
2687 * or ":finish" encountered there must also check the cstack of the still
2688 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2689 * exception, or reanimate a returned function or finished script file and
2690 * return or finish it again.
2691 */
2692 if (need_rethrow)
2693 do_throw(cstack);
2694 else if (check_cstack)
2695 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002696 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002698 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 && current_func_returned())
2700 do_return(&ea, TRUE, FALSE, NULL);
2701 }
2702 need_rethrow = check_cstack = FALSE;
2703#endif
2704
2705doend:
2706 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2707 curwin->w_cursor.lnum = 1;
2708
2709 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2710 {
2711 if (sourcing)
2712 {
2713 if (errormsg != IObuff)
2714 {
2715 STRCPY(IObuff, errormsg);
2716 errormsg = IObuff;
2717 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002718 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 emsg(errormsg);
2721 }
2722#ifdef FEAT_EVAL
2723 do_errthrow(cstack,
2724 (ea.cmdidx != CMD_SIZE
2725# ifdef FEAT_USR_CMDS
2726 && !USER_CMDIDX(ea.cmdidx)
2727# endif
2728 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2729#endif
2730
2731 if (verbose_save >= 0)
2732 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002733#ifdef FEAT_AUTOCMD
2734 if (cmdmod.save_ei != NULL)
2735 {
2736 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002737 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2738 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002739 free_string_option(cmdmod.save_ei);
2740 }
2741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 cmdmod = save_cmdmod;
2744
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002745 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 /* messages could be enabled for a serious error, need to check if the
2748 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002749 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002750 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 emsg_silent -= did_esilent;
2752 if (emsg_silent < 0)
2753 emsg_silent = 0;
2754 /* Restore msg_scroll, it's set by file I/O commands, even when no
2755 * message is actually displayed. */
2756 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002757
2758 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2759 * somewhere in the line. Put it back in the first column. */
2760 if (redirecting())
2761 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002764#ifdef HAVE_SANDBOX
2765 if (did_sandbox)
2766 --sandbox;
2767#endif
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2770 ea.nextcmd = NULL;
2771
2772#ifdef FEAT_EVAL
2773 --ex_nesting_level;
2774#endif
2775
2776 return ea.nextcmd;
2777}
2778#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002779 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#endif
2781
2782/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002783 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 * If there is a match advance "pp" to the argument and return TRUE.
2785 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002786 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002788 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 char *cmd; /* name of command */
2790 int len; /* required length */
2791{
2792 int i;
2793
2794 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002795 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 break;
2797 if (i >= len && !isalpha((*pp)[i]))
2798 {
2799 *pp = skipwhite(*pp + i);
2800 return TRUE;
2801 }
2802 return FALSE;
2803}
2804
2805/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002806 * Append "cmd" to the error message in IObuff.
2807 * Takes care of limiting the length and handling 0xa0, which would be
2808 * invisible otherwise.
2809 */
2810 static void
2811append_command(cmd)
2812 char_u *cmd;
2813{
2814 char_u *s = cmd;
2815 char_u *d;
2816
2817 STRCAT(IObuff, ": ");
2818 d = IObuff + STRLEN(IObuff);
2819 while (*s != NUL && d - IObuff < IOSIZE - 7)
2820 {
2821 if (
2822#ifdef FEAT_MBYTE
2823 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2824#endif
2825 *s == 0xa0)
2826 {
2827 s +=
2828#ifdef FEAT_MBYTE
2829 enc_utf8 ? 2 :
2830#endif
2831 1;
2832 STRCPY(d, "<a0>");
2833 d += 4;
2834 }
2835 else
2836 MB_COPY_CHAR(s, d);
2837 }
2838 *d = NUL;
2839}
2840
2841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002843 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002845 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * Returns NULL for an ambiguous user command.
2847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 static char_u *
2849find_command(eap, full)
2850 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002851 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 int len;
2854 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002855 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 /*
2858 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002859 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 * - the 'k' command can directly be followed by any character.
2861 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2862 * but :sre[wind] is another command, as are :scrip[tnames],
2863 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002864 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 */
2866 p = eap->cmd;
2867 if (*p == 'k')
2868 {
2869 eap->cmdidx = CMD_k;
2870 ++p;
2871 }
2872 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002873 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2874 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 || p[1] == 'g'
2876 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2877 || p[1] == 'I'
2878 || (p[1] == 'r' && p[2] != 'e')))
2879 {
2880 eap->cmdidx = CMD_substitute;
2881 ++p;
2882 }
2883 else
2884 {
2885 while (ASCII_ISALPHA(*p))
2886 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002887 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002888 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002889 while (ASCII_ISALNUM(*p))
2890 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 /* check for non-alpha command */
2893 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2894 ++p;
2895 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002896 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2897 {
2898 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2899 * :delete with the 'l' flag. Same for 'p'. */
2900 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002901 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002902 break;
2903 if (i == len - 1)
2904 {
2905 --len;
2906 if (p[-1] == 'l')
2907 eap->flags |= EXFLAG_LIST;
2908 else
2909 eap->flags |= EXFLAG_PRINT;
2910 }
2911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 if (ASCII_ISLOWER(*eap->cmd))
2914 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2915 else
2916 eap->cmdidx = cmdidxs[26];
2917
2918 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2919 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2920 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2921 (size_t)len) == 0)
2922 {
2923#ifdef FEAT_EVAL
2924 if (full != NULL
2925 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2926 *full = TRUE;
2927#endif
2928 break;
2929 }
2930
2931#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002932 /* Look for a user defined command as a last resort. Let ":Print" be
2933 * overruled by a user defined command. */
2934 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2935 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002937 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 while (ASCII_ISALNUM(*p))
2939 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002940 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
2942#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002943 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 eap->cmdidx = CMD_SIZE;
2945 }
2946
2947 return p;
2948}
2949
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002950#ifdef FEAT_USR_CMDS
2951/*
2952 * Search for a user command that matches "eap->cmd".
2953 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2954 * Return a pointer to just after the command.
2955 * Return NULL if there is no matching command.
2956 */
2957 static char_u *
2958find_ucmd(eap, p, full, xp, compl)
2959 exarg_T *eap;
2960 char_u *p; /* end of the command (possibly including count) */
2961 int *full; /* set to TRUE for a full match */
2962 expand_T *xp; /* used for completion, NULL otherwise */
2963 int *compl; /* completion flags or NULL */
2964{
2965 int len = (int)(p - eap->cmd);
2966 int j, k, matchlen = 0;
2967 ucmd_T *uc;
2968 int found = FALSE;
2969 int possible = FALSE;
2970 char_u *cp, *np; /* Point into typed cmd and test name */
2971 garray_T *gap;
2972 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2973 only full match global is accepted. */
2974
2975 /*
2976 * Look for buffer-local user commands first, then global ones.
2977 */
2978 gap = &curbuf->b_ucmds;
2979 for (;;)
2980 {
2981 for (j = 0; j < gap->ga_len; ++j)
2982 {
2983 uc = USER_CMD_GA(gap, j);
2984 cp = eap->cmd;
2985 np = uc->uc_name;
2986 k = 0;
2987 while (k < len && *np != NUL && *cp++ == *np++)
2988 k++;
2989 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2990 {
2991 /* If finding a second match, the command is ambiguous. But
2992 * not if a buffer-local command wasn't a full match and a
2993 * global command is a full match. */
2994 if (k == len && found && *np != NUL)
2995 {
2996 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002997 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002998 amb_local = TRUE;
2999 }
3000
3001 if (!found || (k == len && *np == NUL))
3002 {
3003 /* If we matched up to a digit, then there could
3004 * be another command including the digit that we
3005 * should use instead.
3006 */
3007 if (k == len)
3008 found = TRUE;
3009 else
3010 possible = TRUE;
3011
3012 if (gap == &ucmds)
3013 eap->cmdidx = CMD_USER;
3014 else
3015 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003016 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003017 eap->useridx = j;
3018
3019# ifdef FEAT_CMDL_COMPL
3020 if (compl != NULL)
3021 *compl = uc->uc_compl;
3022# ifdef FEAT_EVAL
3023 if (xp != NULL)
3024 {
3025 xp->xp_arg = uc->uc_compl_arg;
3026 xp->xp_scriptID = uc->uc_scriptID;
3027 }
3028# endif
3029# endif
3030 /* Do not search for further abbreviations
3031 * if this is an exact match. */
3032 matchlen = k;
3033 if (k == len && *np == NUL)
3034 {
3035 if (full != NULL)
3036 *full = TRUE;
3037 amb_local = FALSE;
3038 break;
3039 }
3040 }
3041 }
3042 }
3043
3044 /* Stop if we found a full match or searched all. */
3045 if (j < gap->ga_len || gap == &ucmds)
3046 break;
3047 gap = &ucmds;
3048 }
3049
3050 /* Only found ambiguous matches. */
3051 if (amb_local)
3052 {
3053 if (xp != NULL)
3054 xp->xp_context = EXPAND_UNSUCCESSFUL;
3055 return NULL;
3056 }
3057
3058 /* The match we found may be followed immediately by a number. Move "p"
3059 * back to point to it. */
3060 if (found || possible)
3061 return p + (matchlen - len);
3062 return p;
3063}
3064#endif
3065
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003067static struct cmdmod
3068{
3069 char *name;
3070 int minlen;
3071 int has_count; /* :123verbose :3tab */
3072} cmdmods[] = {
3073 {"aboveleft", 3, FALSE},
3074 {"belowright", 3, FALSE},
3075 {"botright", 2, FALSE},
3076 {"browse", 3, FALSE},
3077 {"confirm", 4, FALSE},
3078 {"hide", 3, FALSE},
3079 {"keepalt", 5, FALSE},
3080 {"keepjumps", 5, FALSE},
3081 {"keepmarks", 3, FALSE},
3082 {"leftabove", 5, FALSE},
3083 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003084 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003085 {"rightbelow", 6, FALSE},
3086 {"sandbox", 3, FALSE},
3087 {"silent", 3, FALSE},
3088 {"tab", 3, TRUE},
3089 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003090 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003091 {"verbose", 4, TRUE},
3092 {"vertical", 4, FALSE},
3093};
3094
3095/*
3096 * Return length of a command modifier (including optional count).
3097 * Return zero when it's not a modifier.
3098 */
3099 int
3100modifier_len(cmd)
3101 char_u *cmd;
3102{
3103 int i, j;
3104 char_u *p = cmd;
3105
3106 if (VIM_ISDIGIT(*cmd))
3107 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003108 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003109 {
3110 for (j = 0; p[j] != NUL; ++j)
3111 if (p[j] != cmdmods[i].name[j])
3112 break;
3113 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3114 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003115 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003116 }
3117 return 0;
3118}
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120/*
3121 * Return > 0 if an Ex command "name" exists.
3122 * Return 2 if there is an exact match.
3123 * Return 3 if there is an ambiguous match.
3124 */
3125 int
3126cmd_exists(name)
3127 char_u *name;
3128{
3129 exarg_T ea;
3130 int full = FALSE;
3131 int i;
3132 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003133 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003136 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138 for (j = 0; name[j] != NUL; ++j)
3139 if (name[j] != cmdmods[i].name[j])
3140 break;
3141 if (name[j] == NUL && j >= cmdmods[i].minlen)
3142 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3143 }
3144
Bram Moolenaara9587612006-05-04 21:47:50 +00003145 /* Check built-in commands and user defined commands.
3146 * For ":2match" and ":3match" we need to skip the number. */
3147 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003149 p = find_command(&ea, &full);
3150 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003152 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3153 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003154 if (*skipwhite(p) != NUL)
3155 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3157}
3158#endif
3159
3160/*
3161 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3162 * we don't need/want deleted. Maybe this could be done better if we didn't
3163 * repeat all this stuff. The only problem is that they may not stay
3164 * perfectly compatible with each other, but then the command line syntax
3165 * probably won't change that much -- webb.
3166 */
3167 char_u *
3168set_one_cmd_context(xp, buff)
3169 expand_T *xp;
3170 char_u *buff; /* buffer for command string */
3171{
3172 char_u *p;
3173 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003174 int len = 0;
3175 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3177 int compl = EXPAND_NOTHING;
3178#endif
3179#ifdef FEAT_CMDL_COMPL
3180 int delim;
3181#endif
3182 int forceit = FALSE;
3183 int usefilter = FALSE; /* filter instead of file name */
3184
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003185 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 xp->xp_pattern = buff;
3187 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190/*
3191 * 2. skip comment lines and leading space, colons or bars
3192 */
3193 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3194 ;
3195 xp->xp_pattern = cmd;
3196
3197 if (*cmd == NUL)
3198 return NULL;
3199 if (*cmd == '"') /* ignore comment lines */
3200 {
3201 xp->xp_context = EXPAND_NOTHING;
3202 return NULL;
3203 }
3204
3205/*
3206 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3207 */
3208 cmd = skip_range(cmd, &xp->xp_context);
3209
3210/*
3211 * 4. parse command
3212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 xp->xp_pattern = cmd;
3214 if (*cmd == NUL)
3215 return NULL;
3216 if (*cmd == '"')
3217 {
3218 xp->xp_context = EXPAND_NOTHING;
3219 return NULL;
3220 }
3221
3222 if (*cmd == '|' || *cmd == '\n')
3223 return cmd + 1; /* There's another command */
3224
3225 /*
3226 * Isolate the command and search for it in the command table.
3227 * Exceptions:
3228 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003229 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3231 */
3232 if (*cmd == 'k' && cmd[1] != 'e')
3233 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 p = cmd + 1;
3236 }
3237 else
3238 {
3239 p = cmd;
3240 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3241 ++p;
3242 /* check for non-alpha command */
3243 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3244 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
3249 xp->xp_context = EXPAND_UNSUCCESSFUL;
3250 return NULL;
3251 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003253 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3254 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3255 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 break;
3257
3258#ifdef FEAT_USR_CMDS
3259 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3261 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#endif
3263 }
3264
3265 /*
3266 * If the cursor is touching the command, and it ends in an alpha-numeric
3267 * character, complete the command name.
3268 */
3269 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3270 return NULL;
3271
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3275 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 p = cmd + 1;
3278 }
3279#ifdef FEAT_USR_CMDS
3280 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3281 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003282 ea.cmd = cmd;
3283 p = find_ucmd(&ea, p, NULL, xp,
3284# if defined(FEAT_CMDL_COMPL)
3285 &compl
3286# else
3287 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003290 if (p == NULL)
3291 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293#endif
3294 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 /* Not still touching the command and it was an illegal one */
3298 xp->xp_context = EXPAND_UNSUCCESSFUL;
3299 return NULL;
3300 }
3301
3302 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3303
3304 if (*p == '!') /* forced commands */
3305 {
3306 forceit = TRUE;
3307 ++p;
3308 }
3309
3310/*
3311 * 5. parse arguments
3312 */
3313#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003314 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003316 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 arg = skipwhite(p);
3319
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003320 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322 if (*arg == '>') /* append */
3323 {
3324 if (*++arg == '>')
3325 ++arg;
3326 arg = skipwhite(arg);
3327 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
3330 ++arg;
3331 usefilter = TRUE;
3332 }
3333 }
3334
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003335 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 usefilter = forceit; /* :r! filter if forced */
3338 if (*arg == '!') /* :r !filter */
3339 {
3340 ++arg;
3341 usefilter = TRUE;
3342 }
3343 }
3344
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003345 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 while (*arg == *cmd) /* allow any number of '>' or '<' */
3348 ++arg;
3349 arg = skipwhite(arg);
3350 }
3351
3352 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003353 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
3355 /* Check if we're in the +command */
3356 p = arg + 1;
3357 arg = skip_cmd_arg(arg, FALSE);
3358
3359 /* Still touching the command after '+'? */
3360 if (*arg == NUL)
3361 return p;
3362
3363 /* Skip space(s) after +command to get to the real argument */
3364 arg = skipwhite(arg);
3365 }
3366
3367 /*
3368 * Check for '|' to separate commands and '"' to start comments.
3369 * Don't do this for ":read !cmd" and ":write !cmd".
3370 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 p = arg;
3374 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003375 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 p += 2;
3377 while (*p)
3378 {
3379 if (*p == Ctrl_V)
3380 {
3381 if (p[1] != NUL)
3382 ++p;
3383 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003384 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 || *p == '|' || *p == '\n')
3386 {
3387 if (*(p - 1) != '\\')
3388 {
3389 if (*p == '|' || *p == '\n')
3390 return p + 1;
3391 return NULL; /* It's a comment */
3392 }
3393 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003394 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
3396 }
3397
3398 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003399 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 vim_strchr((char_u *)"|\"", *arg) == NULL)
3401 return NULL;
3402
3403 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003404 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003406 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003407 while (*p && p < buff + len)
3408 {
3409 if (*p == ' ' || *p == TAB)
3410 {
3411 /* argument starts after a space */
3412 xp->xp_pattern = ++p;
3413 }
3414 else
3415 {
3416 if (*p == '\\' && *(p + 1) != NUL)
3417 ++p; /* skip over escaped character */
3418 mb_ptr_adv(p);
3419 }
3420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003422 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003424 int c;
3425 int in_quote = FALSE;
3426 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 /*
3429 * Allow spaces within back-quotes to count as part of the argument
3430 * being expanded.
3431 */
3432 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003433 p = xp->xp_pattern;
3434 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003436#ifdef FEAT_MBYTE
3437 if (has_mbyte)
3438 c = mb_ptr2char(p);
3439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003441 c = *p;
3442 if (c == '\\' && p[1] != NUL)
3443 ++p;
3444 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 if (!in_quote)
3447 {
3448 xp->xp_pattern = p;
3449 bow = p + 1;
3450 }
3451 in_quote = !in_quote;
3452 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003453 /* An argument can contain just about everything, except
3454 * characters that end the command and white space. */
3455 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003456#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003457 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003458#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003459 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003460 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003461 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003462 while (*p != NUL)
3463 {
3464#ifdef FEAT_MBYTE
3465 if (has_mbyte)
3466 c = mb_ptr2char(p);
3467 else
3468#endif
3469 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003470 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003471 break;
3472#ifdef FEAT_MBYTE
3473 if (has_mbyte)
3474 len = (*mb_ptr2len)(p);
3475 else
3476#endif
3477 len = 1;
3478 mb_ptr_adv(p);
3479 }
3480 if (in_quote)
3481 bow = p;
3482 else
3483 xp->xp_pattern = p;
3484 p -= len;
3485 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003486 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488
3489 /*
3490 * If we are still inside the quotes, and we passed a space, just
3491 * expand from there.
3492 */
3493 if (bow != NULL && in_quote)
3494 xp->xp_pattern = bow;
3495 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003496
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003497 /* For a shell command more chars need to be escaped. */
3498 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003499 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003500#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003501 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003502#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003503 /* When still after the command name expand executables. */
3504 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003505 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 /* Check for environment variable */
3509 if (*xp->xp_pattern == '$'
3510#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3511 || *xp->xp_pattern == '%'
3512#endif
3513 )
3514 {
3515 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3516 if (!vim_isIDc(*p))
3517 break;
3518 if (*p == NUL)
3519 {
3520 xp->xp_context = EXPAND_ENV_VARS;
3521 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003522#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3523 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003524 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003525 compl = EXPAND_ENV_VARS;
3526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003529#if defined(FEAT_CMDL_COMPL)
3530 /* Check for user names */
3531 if (*xp->xp_pattern == '~')
3532 {
3533 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3534 ;
3535 /* Complete ~user only if it partially matches a user name.
3536 * A full match ~user<Tab> will be replaced by user's home
3537 * directory i.e. something like ~user<Tab> -> /home/user/ */
3538 if (*p == NUL && p > xp->xp_pattern + 1
3539 && match_user(xp->xp_pattern + 1) == 1)
3540 {
3541 xp->xp_context = EXPAND_USER;
3542 ++xp->xp_pattern;
3543 }
3544 }
3545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548/*
3549 * 6. switch on command name
3550 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003551 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003553 case CMD_find:
3554 case CMD_sfind:
3555 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003556 if (xp->xp_context == EXPAND_FILES)
3557 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003558 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 case CMD_cd:
3560 case CMD_chdir:
3561 case CMD_lcd:
3562 case CMD_lchdir:
3563 if (xp->xp_context == EXPAND_FILES)
3564 xp->xp_context = EXPAND_DIRECTORIES;
3565 break;
3566 case CMD_help:
3567 xp->xp_context = EXPAND_HELP;
3568 xp->xp_pattern = arg;
3569 break;
3570
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003571 /* Command modifiers: return the argument.
3572 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003574 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 case CMD_belowright:
3576 case CMD_botright:
3577 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003578 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003580 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 case CMD_folddoclosed:
3582 case CMD_folddoopen:
3583 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003584 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 case CMD_keepjumps:
3586 case CMD_keepmarks:
3587 case CMD_leftabove:
3588 case CMD_lockmarks:
3589 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003590 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003592 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 case CMD_topleft:
3594 case CMD_verbose:
3595 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003596 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return arg;
3598
Bram Moolenaar4f688582007-07-24 12:34:30 +00003599#ifdef FEAT_CMDL_COMPL
3600# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 case CMD_match:
3602 if (*arg == NUL || !ends_excmd(*arg))
3603 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003604 /* also complete "None" */
3605 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 arg = skipwhite(skiptowhite(arg));
3607 if (*arg != NUL)
3608 {
3609 xp->xp_context = EXPAND_NOTHING;
3610 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3611 }
3612 }
3613 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616/*
3617 * All completion for the +cmdline_compl feature goes here.
3618 */
3619
3620# ifdef FEAT_USR_CMDS
3621 case CMD_command:
3622 /* Check for attributes */
3623 while (*arg == '-')
3624 {
3625 arg++; /* Skip "-" */
3626 p = skiptowhite(arg);
3627 if (*p == NUL)
3628 {
3629 /* Cursor is still in the attribute */
3630 p = vim_strchr(arg, '=');
3631 if (p == NULL)
3632 {
3633 /* No "=", so complete attribute names */
3634 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3635 xp->xp_pattern = arg;
3636 return NULL;
3637 }
3638
3639 /* For the -complete and -nargs attributes, we complete
3640 * their arguments as well.
3641 */
3642 if (STRNICMP(arg, "complete", p - arg) == 0)
3643 {
3644 xp->xp_context = EXPAND_USER_COMPLETE;
3645 xp->xp_pattern = p + 1;
3646 return NULL;
3647 }
3648 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3649 {
3650 xp->xp_context = EXPAND_USER_NARGS;
3651 xp->xp_pattern = p + 1;
3652 return NULL;
3653 }
3654 return NULL;
3655 }
3656 arg = skipwhite(p);
3657 }
3658
3659 /* After the attributes comes the new command name */
3660 p = skiptowhite(arg);
3661 if (*p == NUL)
3662 {
3663 xp->xp_context = EXPAND_USER_COMMANDS;
3664 xp->xp_pattern = arg;
3665 break;
3666 }
3667
3668 /* And finally comes a normal command */
3669 return skipwhite(p);
3670
3671 case CMD_delcommand:
3672 xp->xp_context = EXPAND_USER_COMMANDS;
3673 xp->xp_pattern = arg;
3674 break;
3675# endif
3676
3677 case CMD_global:
3678 case CMD_vglobal:
3679 delim = *arg; /* get the delimiter */
3680 if (delim)
3681 ++arg; /* skip delimiter if there is one */
3682
3683 while (arg[0] != NUL && arg[0] != delim)
3684 {
3685 if (arg[0] == '\\' && arg[1] != NUL)
3686 ++arg;
3687 ++arg;
3688 }
3689 if (arg[0] != NUL)
3690 return arg + 1;
3691 break;
3692 case CMD_and:
3693 case CMD_substitute:
3694 delim = *arg;
3695 if (delim)
3696 {
3697 /* skip "from" part */
3698 ++arg;
3699 arg = skip_regexp(arg, delim, p_magic, NULL);
3700 }
3701 /* skip "to" part */
3702 while (arg[0] != NUL && arg[0] != delim)
3703 {
3704 if (arg[0] == '\\' && arg[1] != NUL)
3705 ++arg;
3706 ++arg;
3707 }
3708 if (arg[0] != NUL) /* skip delimiter */
3709 ++arg;
3710 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3711 ++arg;
3712 if (arg[0] != NUL)
3713 return arg;
3714 break;
3715 case CMD_isearch:
3716 case CMD_dsearch:
3717 case CMD_ilist:
3718 case CMD_dlist:
3719 case CMD_ijump:
3720 case CMD_psearch:
3721 case CMD_djump:
3722 case CMD_isplit:
3723 case CMD_dsplit:
3724 arg = skipwhite(skipdigits(arg)); /* skip count */
3725 if (*arg == '/') /* Match regexp, not just whole words */
3726 {
3727 for (++arg; *arg && *arg != '/'; arg++)
3728 if (*arg == '\\' && arg[1] != NUL)
3729 arg++;
3730 if (*arg)
3731 {
3732 arg = skipwhite(arg + 1);
3733
3734 /* Check for trailing illegal characters */
3735 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3736 xp->xp_context = EXPAND_NOTHING;
3737 else
3738 return arg;
3739 }
3740 }
3741 break;
3742#ifdef FEAT_AUTOCMD
3743 case CMD_autocmd:
3744 return set_context_in_autocmd(xp, arg, FALSE);
3745
3746 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003747 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 return set_context_in_autocmd(xp, arg, TRUE);
3749#endif
3750 case CMD_set:
3751 set_context_in_set_cmd(xp, arg, 0);
3752 break;
3753 case CMD_setglobal:
3754 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3755 break;
3756 case CMD_setlocal:
3757 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3758 break;
3759 case CMD_tag:
3760 case CMD_stag:
3761 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003762 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 case CMD_tselect:
3764 case CMD_stselect:
3765 case CMD_ptselect:
3766 case CMD_tjump:
3767 case CMD_stjump:
3768 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003769 if (*p_wop != NUL)
3770 xp->xp_context = EXPAND_TAGS_LISTFILES;
3771 else
3772 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 xp->xp_pattern = arg;
3774 break;
3775 case CMD_augroup:
3776 xp->xp_context = EXPAND_AUGROUP;
3777 xp->xp_pattern = arg;
3778 break;
3779#ifdef FEAT_SYN_HL
3780 case CMD_syntax:
3781 set_context_in_syntax_cmd(xp, arg);
3782 break;
3783#endif
3784#ifdef FEAT_EVAL
3785 case CMD_let:
3786 case CMD_if:
3787 case CMD_elseif:
3788 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003789 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 case CMD_echo:
3791 case CMD_echon:
3792 case CMD_execute:
3793 case CMD_echomsg:
3794 case CMD_echoerr:
3795 case CMD_call:
3796 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003797 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 break;
3799
3800 case CMD_unlet:
3801 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3802 arg = xp->xp_pattern + 1;
3803 xp->xp_context = EXPAND_USER_VARS;
3804 xp->xp_pattern = arg;
3805 break;
3806
3807 case CMD_function:
3808 case CMD_delfunction:
3809 xp->xp_context = EXPAND_USER_FUNC;
3810 xp->xp_pattern = arg;
3811 break;
3812
3813 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003814 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 break;
3816#endif
3817 case CMD_highlight:
3818 set_context_in_highlight_cmd(xp, arg);
3819 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003820#ifdef FEAT_CSCOPE
3821 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003822 case CMD_lcscope:
3823 case CMD_scscope:
3824 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003825 break;
3826#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003827#ifdef FEAT_SIGNS
3828 case CMD_sign:
3829 set_context_in_sign_cmd(xp, arg);
3830 break;
3831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#ifdef FEAT_LISTCMDS
3833 case CMD_bdelete:
3834 case CMD_bwipeout:
3835 case CMD_bunload:
3836 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3837 arg = xp->xp_pattern + 1;
3838 /*FALLTHROUGH*/
3839 case CMD_buffer:
3840 case CMD_sbuffer:
3841 case CMD_checktime:
3842 xp->xp_context = EXPAND_BUFFERS;
3843 xp->xp_pattern = arg;
3844 break;
3845#endif
3846#ifdef FEAT_USR_CMDS
3847 case CMD_USER:
3848 case CMD_USER_BUF:
3849 if (compl != EXPAND_NOTHING)
3850 {
3851 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003852 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854# ifdef FEAT_MENU
3855 if (compl == EXPAND_MENUS)
3856 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3857# endif
3858 if (compl == EXPAND_COMMANDS)
3859 return arg;
3860 if (compl == EXPAND_MAPPINGS)
3861 return set_context_in_map_cmd(xp, (char_u *)"map",
3862 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003863 /* Find start of last argument. */
3864 p = arg;
3865 while (*p)
3866 {
3867 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003868 /* argument starts after a space */
3869 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003870 else if (*p == '\\' && *(p + 1) != NUL)
3871 ++p; /* skip over escaped character */
3872 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 xp->xp_pattern = arg;
3875 }
3876 xp->xp_context = compl;
3877 }
3878 break;
3879#endif
3880 case CMD_map: case CMD_noremap:
3881 case CMD_nmap: case CMD_nnoremap:
3882 case CMD_vmap: case CMD_vnoremap:
3883 case CMD_omap: case CMD_onoremap:
3884 case CMD_imap: case CMD_inoremap:
3885 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003886 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003888 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_unmap:
3890 case CMD_nunmap:
3891 case CMD_vunmap:
3892 case CMD_ounmap:
3893 case CMD_iunmap:
3894 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003895 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003897 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_abbreviate: case CMD_noreabbrev:
3899 case CMD_cabbrev: case CMD_cnoreabbrev:
3900 case CMD_iabbrev: case CMD_inoreabbrev:
3901 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003902 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_unabbreviate:
3904 case CMD_cunabbrev:
3905 case CMD_iunabbrev:
3906 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003907 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908#ifdef FEAT_MENU
3909 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3910 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3911 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3912 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3913 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3914 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3915 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3916 case CMD_tmenu: case CMD_tunmenu:
3917 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3918 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3919#endif
3920
3921 case CMD_colorscheme:
3922 xp->xp_context = EXPAND_COLORS;
3923 xp->xp_pattern = arg;
3924 break;
3925
3926 case CMD_compiler:
3927 xp->xp_context = EXPAND_COMPILER;
3928 xp->xp_pattern = arg;
3929 break;
3930
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003931 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003932 xp->xp_context = EXPAND_OWNSYNTAX;
3933 xp->xp_pattern = arg;
3934 break;
3935
3936 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003937 xp->xp_context = EXPAND_FILETYPE;
3938 xp->xp_pattern = arg;
3939 break;
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3942 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3943 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003944 p = skiptowhite(arg);
3945 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
3947 xp->xp_context = EXPAND_LANGUAGE;
3948 xp->xp_pattern = arg;
3949 }
3950 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003951 {
3952 if ( STRNCMP(arg, "messages", p - arg) == 0
3953 || STRNCMP(arg, "ctype", p - arg) == 0
3954 || STRNCMP(arg, "time", p - arg) == 0)
3955 {
3956 xp->xp_context = EXPAND_LOCALES;
3957 xp->xp_pattern = skipwhite(p);
3958 }
3959 else
3960 xp->xp_context = EXPAND_NOTHING;
3961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 break;
3963#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003964#if defined(FEAT_PROFILE)
3965 case CMD_profile:
3966 set_context_in_profile_cmd(xp, arg);
3967 break;
3968#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003969 case CMD_behave:
3970 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003971 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003974#if defined(FEAT_CMDHIST)
3975 case CMD_history:
3976 xp->xp_context = EXPAND_HISTORY;
3977 xp->xp_pattern = arg;
3978 break;
3979#endif
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#endif /* FEAT_CMDL_COMPL */
3982
3983 default:
3984 break;
3985 }
3986 return NULL;
3987}
3988
3989/*
3990 * skip a range specifier of the form: addr [,addr] [;addr] ..
3991 *
3992 * Backslashed delimiters after / or ? will be skipped, and commands will
3993 * not be expanded between /'s and ?'s or after "'".
3994 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003995 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * Returns the "cmd" pointer advanced to beyond the range.
3997 */
3998 char_u *
3999skip_range(cmd, ctx)
4000 char_u *cmd;
4001 int *ctx; /* pointer to xp_context or NULL */
4002{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004003 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaardf177f62005-02-22 08:39:57 +00004005 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
4007 if (*cmd == '\'')
4008 {
4009 if (*++cmd == NUL && ctx != NULL)
4010 *ctx = EXPAND_NOTHING;
4011 }
4012 else if (*cmd == '/' || *cmd == '?')
4013 {
4014 delim = *cmd++;
4015 while (*cmd != NUL && *cmd != delim)
4016 if (*cmd++ == '\\' && *cmd != NUL)
4017 ++cmd;
4018 if (*cmd == NUL && ctx != NULL)
4019 *ctx = EXPAND_NOTHING;
4020 }
4021 if (*cmd != NUL)
4022 ++cmd;
4023 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004024
4025 /* Skip ":" and white space. */
4026 while (*cmd == ':')
4027 cmd = skipwhite(cmd + 1);
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return cmd;
4030}
4031
4032/*
4033 * get a single EX address
4034 *
4035 * Set ptr to the next character after the part that was interpreted.
4036 * Set ptr to NULL when an error is encountered.
4037 *
4038 * Return MAXLNUM when no Ex address was found.
4039 */
4040 static linenr_T
4041get_address(ptr, skip, to_other_file)
4042 char_u **ptr;
4043 int skip; /* only skip the address, don't use it */
4044 int to_other_file; /* flag: may jump to other file */
4045{
4046 int c;
4047 int i;
4048 long n;
4049 char_u *cmd;
4050 pos_T pos;
4051 pos_T *fp;
4052 linenr_T lnum;
4053
4054 cmd = skipwhite(*ptr);
4055 lnum = MAXLNUM;
4056 do
4057 {
4058 switch (*cmd)
4059 {
4060 case '.': /* '.' - Cursor position */
4061 ++cmd;
4062 lnum = curwin->w_cursor.lnum;
4063 break;
4064
4065 case '$': /* '$' - last line */
4066 ++cmd;
4067 lnum = curbuf->b_ml.ml_line_count;
4068 break;
4069
4070 case '\'': /* ''' - mark */
4071 if (*++cmd == NUL)
4072 {
4073 cmd = NULL;
4074 goto error;
4075 }
4076 if (skip)
4077 ++cmd;
4078 else
4079 {
4080 /* Only accept a mark in another file when it is
4081 * used by itself: ":'M". */
4082 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4083 ++cmd;
4084 if (fp == (pos_T *)-1)
4085 /* Jumped to another file. */
4086 lnum = curwin->w_cursor.lnum;
4087 else
4088 {
4089 if (check_mark(fp) == FAIL)
4090 {
4091 cmd = NULL;
4092 goto error;
4093 }
4094 lnum = fp->lnum;
4095 }
4096 }
4097 break;
4098
4099 case '/':
4100 case '?': /* '/' or '?' - search */
4101 c = *cmd++;
4102 if (skip) /* skip "/pat/" */
4103 {
4104 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4105 if (*cmd == c)
4106 ++cmd;
4107 }
4108 else
4109 {
4110 pos = curwin->w_cursor; /* save curwin->w_cursor */
4111 /*
4112 * When '/' or '?' follows another address, start
4113 * from there.
4114 */
4115 if (lnum != MAXLNUM)
4116 curwin->w_cursor.lnum = lnum;
4117 /*
4118 * Start a forward search at the end of the line.
4119 * Start a backward search at the start of the line.
4120 * This makes sure we never match in the current
4121 * line, and can match anywhere in the
4122 * next/previous line.
4123 */
4124 if (c == '/')
4125 curwin->w_cursor.col = MAXCOL;
4126 else
4127 curwin->w_cursor.col = 0;
4128 searchcmdlen = 0;
4129 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004130 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132 curwin->w_cursor = pos;
4133 cmd = NULL;
4134 goto error;
4135 }
4136 lnum = curwin->w_cursor.lnum;
4137 curwin->w_cursor = pos;
4138 /* adjust command string pointer */
4139 cmd += searchcmdlen;
4140 }
4141 break;
4142
4143 case '\\': /* "\?", "\/" or "\&", repeat search */
4144 ++cmd;
4145 if (*cmd == '&')
4146 i = RE_SUBST;
4147 else if (*cmd == '?' || *cmd == '/')
4148 i = RE_SEARCH;
4149 else
4150 {
4151 EMSG(_(e_backslash));
4152 cmd = NULL;
4153 goto error;
4154 }
4155
4156 if (!skip)
4157 {
4158 /*
4159 * When search follows another address, start from
4160 * there.
4161 */
4162 if (lnum != MAXLNUM)
4163 pos.lnum = lnum;
4164 else
4165 pos.lnum = curwin->w_cursor.lnum;
4166
4167 /*
4168 * Start the search just like for the above
4169 * do_search().
4170 */
4171 if (*cmd != '?')
4172 pos.col = MAXCOL;
4173 else
4174 pos.col = 0;
4175 if (searchit(curwin, curbuf, &pos,
4176 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004177 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004178 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 lnum = pos.lnum;
4180 else
4181 {
4182 cmd = NULL;
4183 goto error;
4184 }
4185 }
4186 ++cmd;
4187 break;
4188
4189 default:
4190 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4191 lnum = getdigits(&cmd);
4192 }
4193
4194 for (;;)
4195 {
4196 cmd = skipwhite(cmd);
4197 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4198 break;
4199
4200 if (lnum == MAXLNUM)
4201 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4202 if (VIM_ISDIGIT(*cmd))
4203 i = '+'; /* "number" is same as "+number" */
4204 else
4205 i = *cmd++;
4206 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4207 n = 1;
4208 else
4209 n = getdigits(&cmd);
4210 if (i == '-')
4211 lnum -= n;
4212 else
4213 lnum += n;
4214 }
4215 } while (*cmd == '/' || *cmd == '?');
4216
4217error:
4218 *ptr = cmd;
4219 return lnum;
4220}
4221
4222/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004223 * Get flags from an Ex command argument.
4224 */
4225 static void
4226get_flags(eap)
4227 exarg_T *eap;
4228{
4229 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4230 {
4231 if (*eap->arg == 'l')
4232 eap->flags |= EXFLAG_LIST;
4233 else if (*eap->arg == 'p')
4234 eap->flags |= EXFLAG_PRINT;
4235 else
4236 eap->flags |= EXFLAG_NR;
4237 eap->arg = skipwhite(eap->arg + 1);
4238 }
4239}
4240
4241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 * Function called for command which is Not Implemented. NI!
4243 */
4244 void
4245ex_ni(eap)
4246 exarg_T *eap;
4247{
4248 if (!eap->skip)
4249 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4250}
4251
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004252#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253/*
4254 * Function called for script command which is Not Implemented. NI!
4255 * Skips over ":perl <<EOF" constructs.
4256 */
4257 static void
4258ex_script_ni(eap)
4259 exarg_T *eap;
4260{
4261 if (!eap->skip)
4262 ex_ni(eap);
4263 else
4264 vim_free(script_get(eap, eap->arg));
4265}
4266#endif
4267
4268/*
4269 * Check range in Ex command for validity.
4270 * Return NULL when valid, error message when invalid.
4271 */
4272 static char_u *
4273invalid_range(eap)
4274 exarg_T *eap;
4275{
4276 if ( eap->line1 < 0
4277 || eap->line2 < 0
4278 || eap->line1 > eap->line2
4279 || ((eap->argt & RANGE)
4280 && !(eap->argt & NOTADR)
4281 && eap->line2 > curbuf->b_ml.ml_line_count
4282#ifdef FEAT_DIFF
4283 + (eap->cmdidx == CMD_diffget)
4284#endif
4285 ))
4286 return (char_u *)_(e_invrange);
4287 return NULL;
4288}
4289
4290/*
4291 * Correct the range for zero line number, if required.
4292 */
4293 static void
4294correct_range(eap)
4295 exarg_T *eap;
4296{
4297 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4298 {
4299 if (eap->line1 == 0)
4300 eap->line1 = 1;
4301 if (eap->line2 == 0)
4302 eap->line2 = 1;
4303 }
4304}
4305
Bram Moolenaar748bf032005-02-02 23:04:36 +00004306#ifdef FEAT_QUICKFIX
4307static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4308
4309/*
4310 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4311 * pattern. Otherwise return eap->arg.
4312 */
4313 static char_u *
4314skip_grep_pat(eap)
4315 exarg_T *eap;
4316{
4317 char_u *p = eap->arg;
4318
Bram Moolenaara37420f2006-02-04 22:37:47 +00004319 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4320 || eap->cmdidx == CMD_vimgrepadd
4321 || eap->cmdidx == CMD_lvimgrepadd
4322 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004323 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004324 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004325 if (p == NULL)
4326 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004327 }
4328 return p;
4329}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004330
4331/*
4332 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4333 * in the command line, so that things like % get expanded.
4334 */
4335 static char_u *
4336replace_makeprg(eap, p, cmdlinep)
4337 exarg_T *eap;
4338 char_u *p;
4339 char_u **cmdlinep;
4340{
4341 char_u *new_cmdline;
4342 char_u *program;
4343 char_u *pos;
4344 char_u *ptr;
4345 int len;
4346 int i;
4347
4348 /*
4349 * Don't do it when ":vimgrep" is used for ":grep".
4350 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004351 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4352 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4353 || eap->cmdidx == CMD_grepadd
4354 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004355 && !grep_internal(eap->cmdidx))
4356 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004357 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4358 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004359 {
4360 if (*curbuf->b_p_gp == NUL)
4361 program = p_gp;
4362 else
4363 program = curbuf->b_p_gp;
4364 }
4365 else
4366 {
4367 if (*curbuf->b_p_mp == NUL)
4368 program = p_mp;
4369 else
4370 program = curbuf->b_p_mp;
4371 }
4372
4373 p = skipwhite(p);
4374
4375 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4376 {
4377 /* replace $* by given arguments */
4378 i = 1;
4379 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4380 ++i;
4381 len = (int)STRLEN(p);
4382 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4383 if (new_cmdline == NULL)
4384 return NULL; /* out of memory */
4385 ptr = new_cmdline;
4386 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4387 {
4388 i = (int)(pos - program);
4389 STRNCPY(ptr, program, i);
4390 STRCPY(ptr += i, p);
4391 ptr += len;
4392 program = pos + 2;
4393 }
4394 STRCPY(ptr, program);
4395 }
4396 else
4397 {
4398 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4399 if (new_cmdline == NULL)
4400 return NULL; /* out of memory */
4401 STRCPY(new_cmdline, program);
4402 STRCAT(new_cmdline, " ");
4403 STRCAT(new_cmdline, p);
4404 }
4405 msg_make(p);
4406
4407 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4408 vim_free(*cmdlinep);
4409 *cmdlinep = new_cmdline;
4410 p = new_cmdline;
4411 }
4412 return p;
4413}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004414#endif
4415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416/*
4417 * Expand file name in Ex command argument.
4418 * Return FAIL for failure, OK otherwise.
4419 */
4420 int
4421expand_filename(eap, cmdlinep, errormsgp)
4422 exarg_T *eap;
4423 char_u **cmdlinep;
4424 char_u **errormsgp;
4425{
4426 int has_wildcards; /* need to expand wildcards */
4427 char_u *repl;
4428 int srclen;
4429 char_u *p;
4430 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004431 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaar748bf032005-02-02 23:04:36 +00004433#ifdef FEAT_QUICKFIX
4434 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4435 p = skip_grep_pat(eap);
4436#else
4437 p = eap->arg;
4438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 /*
4441 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4442 * the file name contains a wildcard it should not cause expanding.
4443 * (it will be expanded anyway if there is a wildcard before replacing).
4444 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004445 has_wildcards = mch_has_wildcard(p);
4446 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004448#ifdef FEAT_EVAL
4449 /* Skip over `=expr`, wildcards in it are not expanded. */
4450 if (p[0] == '`' && p[1] == '=')
4451 {
4452 p += 2;
4453 (void)skip_expr(&p);
4454 if (*p == '`')
4455 ++p;
4456 continue;
4457 }
4458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 /*
4460 * Quick check if this cannot be the start of a special string.
4461 * Also removes backslash before '%', '#' and '<'.
4462 */
4463 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4464 {
4465 ++p;
4466 continue;
4467 }
4468
4469 /*
4470 * Try to find a match at this position.
4471 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004472 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4473 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (*errormsgp != NULL) /* error detected */
4475 return FAIL;
4476 if (repl == NULL) /* no match found */
4477 {
4478 p += srclen;
4479 continue;
4480 }
4481
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004482 /* Wildcards won't be expanded below, the replacement is taken
4483 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4484 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4485 {
4486 char_u *l = repl;
4487
4488 repl = expand_env_save(repl);
4489 vim_free(l);
4490 }
4491
Bram Moolenaar63b92542007-03-27 14:57:09 +00004492 /* Need to escape white space et al. with a backslash.
4493 * Don't do this for:
4494 * - replacement that already has been escaped: "##"
4495 * - shell commands (may have to use quotes instead).
4496 * - non-unix systems when there is a single argument (spaces don't
4497 * separate arguments then).
4498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004500 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 && eap->cmdidx != CMD_bang
4502 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004503 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004505 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004507 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508#ifndef UNIX
4509 && !(eap->argt & NOSPC)
4510#endif
4511 )
4512 {
4513 char_u *l;
4514#ifdef BACKSLASH_IN_FILENAME
4515 /* Don't escape a backslash here, because rem_backslash() doesn't
4516 * remove it later. */
4517 static char_u *nobslash = (char_u *)" \t\"|";
4518# define ESCAPE_CHARS nobslash
4519#else
4520# define ESCAPE_CHARS escape_chars
4521#endif
4522
4523 for (l = repl; *l; ++l)
4524 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4525 {
4526 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4527 if (l != NULL)
4528 {
4529 vim_free(repl);
4530 repl = l;
4531 }
4532 break;
4533 }
4534 }
4535
4536 /* For a shell command a '!' must be escaped. */
4537 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004538 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
4540 char_u *l;
4541
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004542 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (l != NULL)
4544 {
4545 vim_free(repl);
4546 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004547 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (strstr((char *)p_sh, "sh") != NULL)
4549 {
4550 l = vim_strsave_escaped(repl, (char_u *)"!");
4551 if (l != NULL)
4552 {
4553 vim_free(repl);
4554 repl = l;
4555 }
4556 }
4557 }
4558 }
4559
4560 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4561 vim_free(repl);
4562 if (p == NULL)
4563 return FAIL;
4564 }
4565
4566 /*
4567 * One file argument: Expand wildcards.
4568 * Don't do this with ":r !command" or ":w !command".
4569 */
4570 if ((eap->argt & NOSPC) && !eap->usefilter)
4571 {
4572 /*
4573 * May do this twice:
4574 * 1. Replace environment variables.
4575 * 2. Replace any other wildcards, remove backslashes.
4576 */
4577 for (n = 1; n <= 2; ++n)
4578 {
4579 if (n == 2)
4580 {
4581#ifdef UNIX
4582 /*
4583 * Only for Unix we check for more than one file name.
4584 * For other systems spaces are considered to be part
4585 * of the file name.
4586 * Only check here if there is no wildcard, otherwise
4587 * ExpandOne() will check for errors. This allows
4588 * ":e `ls ve*.c`" on Unix.
4589 */
4590 if (!has_wildcards)
4591 for (p = eap->arg; *p; ++p)
4592 {
4593 /* skip escaped characters */
4594 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4595 ++p;
4596 else if (vim_iswhite(*p))
4597 {
4598 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4599 return FAIL;
4600 }
4601 }
4602#endif
4603
4604 /*
4605 * Halve the number of backslashes (this is Vi compatible).
4606 * For Unix and OS/2, when wildcards are expanded, this is
4607 * done by ExpandOne() below.
4608 */
4609#if defined(UNIX) || defined(OS2)
4610 if (!has_wildcards)
4611#endif
4612 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614
4615 if (has_wildcards)
4616 {
4617 if (n == 1)
4618 {
4619 /*
4620 * First loop: May expand environment variables. This
4621 * can be done much faster with expand_env() than with
4622 * something else (e.g., calling a shell).
4623 * After expanding environment variables, check again
4624 * if there are still wildcards present.
4625 */
4626 if (vim_strchr(eap->arg, '$') != NULL
4627 || vim_strchr(eap->arg, '~') != NULL)
4628 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004629 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004630 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 has_wildcards = mch_has_wildcard(NameBuff);
4632 p = NameBuff;
4633 }
4634 else
4635 p = NULL;
4636 }
4637 else /* n == 2 */
4638 {
4639 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004640 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 ExpandInit(&xpc);
4643 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004644 if (p_wic)
4645 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004647 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (p == NULL)
4649 return FAIL;
4650 }
4651 if (p != NULL)
4652 {
4653 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4654 p, cmdlinep);
4655 if (n == 2) /* p came from ExpandOne() */
4656 vim_free(p);
4657 }
4658 }
4659 }
4660 }
4661 return OK;
4662}
4663
4664/*
4665 * Replace part of the command line, keeping eap->cmd, eap->arg and
4666 * eap->nextcmd correct.
4667 * "src" points to the part that is to be replaced, of length "srclen".
4668 * "repl" is the replacement string.
4669 * Returns a pointer to the character after the replaced string.
4670 * Returns NULL for failure.
4671 */
4672 static char_u *
4673repl_cmdline(eap, src, srclen, repl, cmdlinep)
4674 exarg_T *eap;
4675 char_u *src;
4676 int srclen;
4677 char_u *repl;
4678 char_u **cmdlinep;
4679{
4680 int len;
4681 int i;
4682 char_u *new_cmdline;
4683
4684 /*
4685 * The new command line is build in new_cmdline[].
4686 * First allocate it.
4687 * Careful: a "+cmd" argument may have been NUL terminated.
4688 */
4689 len = (int)STRLEN(repl);
4690 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004691 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4693 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4694 return NULL; /* out of memory! */
4695
4696 /*
4697 * Copy the stuff before the expanded part.
4698 * Copy the expanded stuff.
4699 * Copy what came after the expanded part.
4700 * Copy the next commands, if there are any.
4701 */
4702 i = (int)(src - *cmdlinep); /* length of part before match */
4703 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 mch_memmove(new_cmdline + i, repl, (size_t)len);
4706 i += len; /* remember the end of the string */
4707 STRCPY(new_cmdline + i, src + srclen);
4708 src = new_cmdline + i; /* remember where to continue */
4709
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004710 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
4712 i = (int)STRLEN(new_cmdline) + 1;
4713 STRCPY(new_cmdline + i, eap->nextcmd);
4714 eap->nextcmd = new_cmdline + i;
4715 }
4716 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4717 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4718 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4719 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4720 vim_free(*cmdlinep);
4721 *cmdlinep = new_cmdline;
4722
4723 return src;
4724}
4725
4726/*
4727 * Check for '|' to separate commands and '"' to start comments.
4728 */
4729 void
4730separate_nextcmd(eap)
4731 exarg_T *eap;
4732{
4733 char_u *p;
4734
Bram Moolenaar86b68352004-12-27 21:59:20 +00004735#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004736 p = skip_grep_pat(eap);
4737#else
4738 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004739#endif
4740
4741 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 if (*p == Ctrl_V)
4744 {
4745 if (eap->argt & (USECTRLV | XFILE))
4746 ++p; /* skip CTRL-V and next char */
4747 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004748 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004749 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (*p == NUL) /* stop at NUL after CTRL-V */
4751 break;
4752 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004753
4754#ifdef FEAT_EVAL
4755 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004756 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004757 {
4758 p += 2;
4759 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004760 }
4761#endif
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 /* Check for '"': start of comment or '|': next command */
4764 /* :@" and :*" do not start a comment!
4765 * :redir @" doesn't either. */
4766 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4767 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4768 || p != eap->arg)
4769 && (eap->cmdidx != CMD_redir
4770 || p != eap->arg + 1 || p[-1] != '@'))
4771 || *p == '|' || *p == '\n')
4772 {
4773 /*
4774 * We remove the '\' before the '|', unless USECTRLV is used
4775 * AND 'b' is present in 'cpoptions'.
4776 */
4777 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4778 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4779 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004780 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 --p;
4782 }
4783 else
4784 {
4785 eap->nextcmd = check_nextcmd(p);
4786 *p = NUL;
4787 break;
4788 }
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4793 del_trailing_spaces(eap->arg);
4794}
4795
4796/*
4797 * get + command from ex argument
4798 */
4799 static char_u *
4800getargcmd(argp)
4801 char_u **argp;
4802{
4803 char_u *arg = *argp;
4804 char_u *command = NULL;
4805
4806 if (*arg == '+') /* +[command] */
4807 {
4808 ++arg;
4809 if (vim_isspace(*arg))
4810 command = dollar_command;
4811 else
4812 {
4813 command = arg;
4814 arg = skip_cmd_arg(command, TRUE);
4815 if (*arg != NUL)
4816 *arg++ = NUL; /* terminate command with NUL */
4817 }
4818
4819 arg = skipwhite(arg); /* skip over spaces */
4820 *argp = arg;
4821 }
4822 return command;
4823}
4824
4825/*
4826 * Find end of "+command" argument. Skip over "\ " and "\\".
4827 */
4828 static char_u *
4829skip_cmd_arg(p, rembs)
4830 char_u *p;
4831 int rembs; /* TRUE to halve the number of backslashes */
4832{
4833 while (*p && !vim_isspace(*p))
4834 {
4835 if (*p == '\\' && p[1] != NUL)
4836 {
4837 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004838 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 else
4840 ++p;
4841 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004842 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 return p;
4845}
4846
4847/*
4848 * Get "++opt=arg" argument.
4849 * Return FAIL or OK.
4850 */
4851 static int
4852getargopt(eap)
4853 exarg_T *eap;
4854{
4855 char_u *arg = eap->arg + 2;
4856 int *pp = NULL;
4857#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004858 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_u *p;
4860#endif
4861
4862 /* ":edit ++[no]bin[ary] file" */
4863 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4864 {
4865 if (*arg == 'n')
4866 {
4867 arg += 2;
4868 eap->force_bin = FORCE_NOBIN;
4869 }
4870 else
4871 eap->force_bin = FORCE_BIN;
4872 if (!checkforcmd(&arg, "binary", 3))
4873 return FAIL;
4874 eap->arg = skipwhite(arg);
4875 return OK;
4876 }
4877
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004878 /* ":read ++edit file" */
4879 if (STRNCMP(arg, "edit", 4) == 0)
4880 {
4881 eap->read_edit = TRUE;
4882 eap->arg = skipwhite(arg + 4);
4883 return OK;
4884 }
4885
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (STRNCMP(arg, "ff", 2) == 0)
4887 {
4888 arg += 2;
4889 pp = &eap->force_ff;
4890 }
4891 else if (STRNCMP(arg, "fileformat", 10) == 0)
4892 {
4893 arg += 10;
4894 pp = &eap->force_ff;
4895 }
4896#ifdef FEAT_MBYTE
4897 else if (STRNCMP(arg, "enc", 3) == 0)
4898 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004899 if (STRNCMP(arg, "encoding", 8) == 0)
4900 arg += 8;
4901 else
4902 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 pp = &eap->force_enc;
4904 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004905 else if (STRNCMP(arg, "bad", 3) == 0)
4906 {
4907 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004908 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#endif
4911
4912 if (pp == NULL || *arg != '=')
4913 return FAIL;
4914
4915 ++arg;
4916 *pp = (int)(arg - eap->cmd);
4917 arg = skip_cmd_arg(arg, FALSE);
4918 eap->arg = skipwhite(arg);
4919 *arg = NUL;
4920
4921#ifdef FEAT_MBYTE
4922 if (pp == &eap->force_ff)
4923 {
4924#endif
4925 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4926 return FAIL;
4927#ifdef FEAT_MBYTE
4928 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004929 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
4931 /* Make 'fileencoding' lower case. */
4932 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4933 *p = TOLOWER_ASC(*p);
4934 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004935 else
4936 {
4937 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4938 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004939 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004940 if (STRICMP(p, "keep") == 0)
4941 eap->bad_char = BAD_KEEP;
4942 else if (STRICMP(p, "drop") == 0)
4943 eap->bad_char = BAD_DROP;
4944 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4945 eap->bad_char = *p;
4946 else
4947 return FAIL;
4948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#endif
4950
4951 return OK;
4952}
4953
4954/*
4955 * ":abbreviate" and friends.
4956 */
4957 static void
4958ex_abbreviate(eap)
4959 exarg_T *eap;
4960{
4961 do_exmap(eap, TRUE); /* almost the same as mapping */
4962}
4963
4964/*
4965 * ":map" and friends.
4966 */
4967 static void
4968ex_map(eap)
4969 exarg_T *eap;
4970{
4971 /*
4972 * If we are sourcing .exrc or .vimrc in current directory we
4973 * print the mappings for security reasons.
4974 */
4975 if (secure)
4976 {
4977 secure = 2;
4978 msg_outtrans(eap->cmd);
4979 msg_putchar('\n');
4980 }
4981 do_exmap(eap, FALSE);
4982}
4983
4984/*
4985 * ":unmap" and friends.
4986 */
4987 static void
4988ex_unmap(eap)
4989 exarg_T *eap;
4990{
4991 do_exmap(eap, FALSE);
4992}
4993
4994/*
4995 * ":mapclear" and friends.
4996 */
4997 static void
4998ex_mapclear(eap)
4999 exarg_T *eap;
5000{
5001 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5002}
5003
5004/*
5005 * ":abclear" and friends.
5006 */
5007 static void
5008ex_abclear(eap)
5009 exarg_T *eap;
5010{
5011 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5012}
5013
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005014#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 static void
5016ex_autocmd(eap)
5017 exarg_T *eap;
5018{
5019 /*
5020 * Disallow auto commands from .exrc and .vimrc in current
5021 * directory for security reasons.
5022 */
5023 if (secure)
5024 {
5025 secure = 2;
5026 eap->errmsg = e_curdir;
5027 }
5028 else if (eap->cmdidx == CMD_autocmd)
5029 do_autocmd(eap->arg, eap->forceit);
5030 else
5031 do_augroup(eap->arg, eap->forceit);
5032}
5033
5034/*
5035 * ":doautocmd": Apply the automatic commands to the current buffer.
5036 */
5037 static void
5038ex_doautocmd(eap)
5039 exarg_T *eap;
5040{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005041 char_u *arg = eap->arg;
5042 int call_do_modelines = check_nomodeline(&arg);
5043
5044 (void)do_doautocmd(arg, TRUE);
5045 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5046 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047}
5048#endif
5049
5050#ifdef FEAT_LISTCMDS
5051/*
5052 * :[N]bunload[!] [N] [bufname] unload buffer
5053 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5054 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5055 */
5056 static void
5057ex_bunload(eap)
5058 exarg_T *eap;
5059{
5060 eap->errmsg = do_bufdel(
5061 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5062 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5063 : DOBUF_UNLOAD, eap->arg,
5064 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5065}
5066
5067/*
5068 * :[N]buffer [N] to buffer N
5069 * :[N]sbuffer [N] to buffer N
5070 */
5071 static void
5072ex_buffer(eap)
5073 exarg_T *eap;
5074{
5075 if (*eap->arg)
5076 eap->errmsg = e_trailing;
5077 else
5078 {
5079 if (eap->addr_count == 0) /* default is current buffer */
5080 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5081 else
5082 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5083 }
5084}
5085
5086/*
5087 * :[N]bmodified [N] to next mod. buffer
5088 * :[N]sbmodified [N] to next mod. buffer
5089 */
5090 static void
5091ex_bmodified(eap)
5092 exarg_T *eap;
5093{
5094 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5095}
5096
5097/*
5098 * :[N]bnext [N] to next buffer
5099 * :[N]sbnext [N] split and to next buffer
5100 */
5101 static void
5102ex_bnext(eap)
5103 exarg_T *eap;
5104{
5105 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5106}
5107
5108/*
5109 * :[N]bNext [N] to previous buffer
5110 * :[N]bprevious [N] to previous buffer
5111 * :[N]sbNext [N] split and to previous buffer
5112 * :[N]sbprevious [N] split and to previous buffer
5113 */
5114 static void
5115ex_bprevious(eap)
5116 exarg_T *eap;
5117{
5118 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5119}
5120
5121/*
5122 * :brewind to first buffer
5123 * :bfirst to first buffer
5124 * :sbrewind split and to first buffer
5125 * :sbfirst split and to first buffer
5126 */
5127 static void
5128ex_brewind(eap)
5129 exarg_T *eap;
5130{
5131 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5132}
5133
5134/*
5135 * :blast to last buffer
5136 * :sblast split and to last buffer
5137 */
5138 static void
5139ex_blast(eap)
5140 exarg_T *eap;
5141{
5142 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5143}
5144#endif
5145
5146 int
5147ends_excmd(c)
5148 int c;
5149{
5150 return (c == NUL || c == '|' || c == '"' || c == '\n');
5151}
5152
5153#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5154 || defined(PROTO)
5155/*
5156 * Return the next command, after the first '|' or '\n'.
5157 * Return NULL if not found.
5158 */
5159 char_u *
5160find_nextcmd(p)
5161 char_u *p;
5162{
5163 while (*p != '|' && *p != '\n')
5164 {
5165 if (*p == NUL)
5166 return NULL;
5167 ++p;
5168 }
5169 return (p + 1);
5170}
5171#endif
5172
5173/*
5174 * Check if *p is a separator between Ex commands.
5175 * Return NULL if it isn't, (p + 1) if it is.
5176 */
5177 char_u *
5178check_nextcmd(p)
5179 char_u *p;
5180{
5181 p = skipwhite(p);
5182 if (*p == '|' || *p == '\n')
5183 return (p + 1);
5184 else
5185 return NULL;
5186}
5187
5188/*
5189 * - if there are more files to edit
5190 * - and this is the last window
5191 * - and forceit not used
5192 * - and not repeated twice on a row
5193 * return FAIL and give error message if 'message' TRUE
5194 * return OK otherwise
5195 */
5196 static int
5197check_more(message, forceit)
5198 int message; /* when FALSE check only, no messages */
5199 int forceit;
5200{
5201 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5202
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005203 if (!forceit && only_one_window()
5204 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 if (message)
5207 {
5208#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5209 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5210 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005211 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005214 vim_strncpy(buff,
5215 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005216 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005218 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 _("%d more files to edit. Quit anyway?"), n);
5220 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5221 return OK;
5222 return FAIL;
5223 }
5224#endif
5225 if (n == 1)
5226 EMSG(_("E173: 1 more file to edit"));
5227 else
5228 EMSGN(_("E173: %ld more files to edit"), n);
5229 quitmore = 2; /* next try to quit is allowed */
5230 }
5231 return FAIL;
5232 }
5233 return OK;
5234}
5235
5236#ifdef FEAT_CMDL_COMPL
5237/*
5238 * Function given to ExpandGeneric() to obtain the list of command names.
5239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 char_u *
5241get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005242 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 int idx;
5244{
5245 if (idx >= (int)CMD_SIZE)
5246# ifdef FEAT_USR_CMDS
5247 return get_user_command_name(idx);
5248# else
5249 return NULL;
5250# endif
5251 return cmdnames[idx].cmd_name;
5252}
5253#endif
5254
5255#if defined(FEAT_USR_CMDS) || defined(PROTO)
5256static 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));
5257static void uc_list __ARGS((char_u *name, size_t name_len));
5258static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5259static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5260static 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));
5261
5262 static int
5263uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5264 char_u *name;
5265 size_t name_len;
5266 char_u *rep;
5267 long argt;
5268 long def;
5269 int flags;
5270 int compl;
5271 char_u *compl_arg;
5272 int force;
5273{
5274 ucmd_T *cmd = NULL;
5275 char_u *p;
5276 int i;
5277 int cmp = 1;
5278 char_u *rep_buf = NULL;
5279 garray_T *gap;
5280
Bram Moolenaar9c102382006-05-03 21:26:49 +00005281 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (rep_buf == NULL)
5283 {
5284 /* Can't replace termcodes - try using the string as is */
5285 rep_buf = vim_strsave(rep);
5286
5287 /* Give up if out of memory */
5288 if (rep_buf == NULL)
5289 return FAIL;
5290 }
5291
5292 /* get address of growarray: global or in curbuf */
5293 if (flags & UC_BUFFER)
5294 {
5295 gap = &curbuf->b_ucmds;
5296 if (gap->ga_itemsize == 0)
5297 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5298 }
5299 else
5300 gap = &ucmds;
5301
5302 /* Search for the command in the already defined commands. */
5303 for (i = 0; i < gap->ga_len; ++i)
5304 {
5305 size_t len;
5306
5307 cmd = USER_CMD_GA(gap, i);
5308 len = STRLEN(cmd->uc_name);
5309 cmp = STRNCMP(name, cmd->uc_name, name_len);
5310 if (cmp == 0)
5311 {
5312 if (name_len < len)
5313 cmp = -1;
5314 else if (name_len > len)
5315 cmp = 1;
5316 }
5317
5318 if (cmp == 0)
5319 {
5320 if (!force)
5321 {
5322 EMSG(_("E174: Command already exists: add ! to replace it"));
5323 goto fail;
5324 }
5325
5326 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005327 cmd->uc_rep = NULL;
5328#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5329 vim_free(cmd->uc_compl_arg);
5330 cmd->uc_compl_arg = NULL;
5331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 break;
5333 }
5334
5335 /* Stop as soon as we pass the name to add */
5336 if (cmp < 0)
5337 break;
5338 }
5339
5340 /* Extend the array unless we're replacing an existing command */
5341 if (cmp != 0)
5342 {
5343 if (ga_grow(gap, 1) != OK)
5344 goto fail;
5345 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5346 goto fail;
5347
5348 cmd = USER_CMD_GA(gap, i);
5349 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5350
5351 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
5353 cmd->uc_name = p;
5354 }
5355
5356 cmd->uc_rep = rep_buf;
5357 cmd->uc_argt = argt;
5358 cmd->uc_def = def;
5359 cmd->uc_compl = compl;
5360#ifdef FEAT_EVAL
5361 cmd->uc_scriptID = current_SID;
5362# ifdef FEAT_CMDL_COMPL
5363 cmd->uc_compl_arg = compl_arg;
5364# endif
5365#endif
5366
5367 return OK;
5368
5369fail:
5370 vim_free(rep_buf);
5371#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5372 vim_free(compl_arg);
5373#endif
5374 return FAIL;
5375}
5376
5377/*
5378 * List of names for completion for ":command" with the EXPAND_ flag.
5379 * Must be alphabetical for completion.
5380 */
5381static struct
5382{
5383 int expand;
5384 char *name;
5385} command_complete[] =
5386{
5387 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005388 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005390 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005392 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005393#if defined(FEAT_CSCOPE)
5394 {EXPAND_CSCOPE, "cscope"},
5395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5397 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005398 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#endif
5400 {EXPAND_DIRECTORIES, "dir"},
5401 {EXPAND_ENV_VARS, "environment"},
5402 {EXPAND_EVENTS, "event"},
5403 {EXPAND_EXPRESSION, "expression"},
5404 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005405 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005406 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {EXPAND_FUNCTIONS, "function"},
5408 {EXPAND_HELP, "help"},
5409 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005410#if defined(FEAT_CMDHIST)
5411 {EXPAND_HISTORY, "history"},
5412#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005413#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005414 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005415 {EXPAND_LOCALES, "locale"},
5416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {EXPAND_MAPPINGS, "mapping"},
5418 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005419 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005421 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005422#if defined(FEAT_SIGNS)
5423 {EXPAND_SIGN, "sign"},
5424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {EXPAND_TAGS, "tag"},
5426 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005427 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {EXPAND_USER_VARS, "var"},
5429 {0, NULL}
5430};
5431
5432 static void
5433uc_list(name, name_len)
5434 char_u *name;
5435 size_t name_len;
5436{
5437 int i, j;
5438 int found = FALSE;
5439 ucmd_T *cmd;
5440 int len;
5441 long a;
5442 garray_T *gap;
5443
5444 gap = &curbuf->b_ucmds;
5445 for (;;)
5446 {
5447 for (i = 0; i < gap->ga_len; ++i)
5448 {
5449 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005450 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452 /* Skip commands which don't match the requested prefix */
5453 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5454 continue;
5455
5456 /* Put out the title first time */
5457 if (!found)
5458 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5459 found = TRUE;
5460 msg_putchar('\n');
5461 if (got_int)
5462 break;
5463
5464 /* Special cases */
5465 msg_putchar(a & BANG ? '!' : ' ');
5466 msg_putchar(a & REGSTR ? '"' : ' ');
5467 msg_putchar(gap != &ucmds ? 'b' : ' ');
5468 msg_putchar(' ');
5469
5470 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5471 len = (int)STRLEN(cmd->uc_name) + 4;
5472
5473 do {
5474 msg_putchar(' ');
5475 ++len;
5476 } while (len < 16);
5477
5478 len = 0;
5479
5480 /* Arguments */
5481 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5482 {
5483 case 0: IObuff[len++] = '0'; break;
5484 case (EXTRA): IObuff[len++] = '*'; break;
5485 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5486 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5487 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5488 }
5489
5490 do {
5491 IObuff[len++] = ' ';
5492 } while (len < 5);
5493
5494 /* Range */
5495 if (a & (RANGE|COUNT))
5496 {
5497 if (a & COUNT)
5498 {
5499 /* -count=N */
5500 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5501 len += (int)STRLEN(IObuff + len);
5502 }
5503 else if (a & DFLALL)
5504 IObuff[len++] = '%';
5505 else if (cmd->uc_def >= 0)
5506 {
5507 /* -range=N */
5508 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5509 len += (int)STRLEN(IObuff + len);
5510 }
5511 else
5512 IObuff[len++] = '.';
5513 }
5514
5515 do {
5516 IObuff[len++] = ' ';
5517 } while (len < 11);
5518
5519 /* Completion */
5520 for (j = 0; command_complete[j].expand != 0; ++j)
5521 if (command_complete[j].expand == cmd->uc_compl)
5522 {
5523 STRCPY(IObuff + len, command_complete[j].name);
5524 len += (int)STRLEN(IObuff + len);
5525 break;
5526 }
5527
5528 do {
5529 IObuff[len++] = ' ';
5530 } while (len < 21);
5531
5532 IObuff[len] = '\0';
5533 msg_outtrans(IObuff);
5534
5535 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005536#ifdef FEAT_EVAL
5537 if (p_verbose > 0)
5538 last_set_msg(cmd->uc_scriptID);
5539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 out_flush();
5541 ui_breakcheck();
5542 if (got_int)
5543 break;
5544 }
5545 if (gap == &ucmds || i < gap->ga_len)
5546 break;
5547 gap = &ucmds;
5548 }
5549
5550 if (!found)
5551 MSG(_("No user-defined commands found"));
5552}
5553
5554 static char_u *
5555uc_fun_cmd()
5556{
5557 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5558 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5559 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5560 0xb9, 0x7f, 0};
5561 int i;
5562
5563 for (i = 0; fcmd[i]; ++i)
5564 IObuff[i] = fcmd[i] - 0x40;
5565 IObuff[i] = 0;
5566 return IObuff;
5567}
5568
5569 static int
5570uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5571 char_u *attr;
5572 size_t len;
5573 long *argt;
5574 long *def;
5575 int *flags;
5576 int *compl;
5577 char_u **compl_arg;
5578{
5579 char_u *p;
5580
5581 if (len == 0)
5582 {
5583 EMSG(_("E175: No attribute specified"));
5584 return FAIL;
5585 }
5586
5587 /* First, try the simple attributes (no arguments) */
5588 if (STRNICMP(attr, "bang", len) == 0)
5589 *argt |= BANG;
5590 else if (STRNICMP(attr, "buffer", len) == 0)
5591 *flags |= UC_BUFFER;
5592 else if (STRNICMP(attr, "register", len) == 0)
5593 *argt |= REGSTR;
5594 else if (STRNICMP(attr, "bar", len) == 0)
5595 *argt |= TRLBAR;
5596 else
5597 {
5598 int i;
5599 char_u *val = NULL;
5600 size_t vallen = 0;
5601 size_t attrlen = len;
5602
5603 /* Look for the attribute name - which is the part before any '=' */
5604 for (i = 0; i < (int)len; ++i)
5605 {
5606 if (attr[i] == '=')
5607 {
5608 val = &attr[i + 1];
5609 vallen = len - i - 1;
5610 attrlen = i;
5611 break;
5612 }
5613 }
5614
5615 if (STRNICMP(attr, "nargs", attrlen) == 0)
5616 {
5617 if (vallen == 1)
5618 {
5619 if (*val == '0')
5620 /* Do nothing - this is the default */;
5621 else if (*val == '1')
5622 *argt |= (EXTRA | NOSPC | NEEDARG);
5623 else if (*val == '*')
5624 *argt |= EXTRA;
5625 else if (*val == '?')
5626 *argt |= (EXTRA | NOSPC);
5627 else if (*val == '+')
5628 *argt |= (EXTRA | NEEDARG);
5629 else
5630 goto wrong_nargs;
5631 }
5632 else
5633 {
5634wrong_nargs:
5635 EMSG(_("E176: Invalid number of arguments"));
5636 return FAIL;
5637 }
5638 }
5639 else if (STRNICMP(attr, "range", attrlen) == 0)
5640 {
5641 *argt |= RANGE;
5642 if (vallen == 1 && *val == '%')
5643 *argt |= DFLALL;
5644 else if (val != NULL)
5645 {
5646 p = val;
5647 if (*def >= 0)
5648 {
5649two_count:
5650 EMSG(_("E177: Count cannot be specified twice"));
5651 return FAIL;
5652 }
5653
5654 *def = getdigits(&p);
5655 *argt |= (ZEROR | NOTADR);
5656
5657 if (p != val + vallen || vallen == 0)
5658 {
5659invalid_count:
5660 EMSG(_("E178: Invalid default value for count"));
5661 return FAIL;
5662 }
5663 }
5664 }
5665 else if (STRNICMP(attr, "count", attrlen) == 0)
5666 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005667 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 if (val != NULL)
5670 {
5671 p = val;
5672 if (*def >= 0)
5673 goto two_count;
5674
5675 *def = getdigits(&p);
5676
5677 if (p != val + vallen)
5678 goto invalid_count;
5679 }
5680
5681 if (*def < 0)
5682 *def = 0;
5683 }
5684 else if (STRNICMP(attr, "complete", attrlen) == 0)
5685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (val == NULL)
5687 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005688 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 return FAIL;
5690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005692 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5693 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 else
5697 {
5698 char_u ch = attr[len];
5699 attr[len] = '\0';
5700 EMSG2(_("E181: Invalid attribute: %s"), attr);
5701 attr[len] = ch;
5702 return FAIL;
5703 }
5704 }
5705
5706 return OK;
5707}
5708
Bram Moolenaara850a712009-01-28 14:42:59 +00005709/*
5710 * ":command ..."
5711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 static void
5713ex_command(eap)
5714 exarg_T *eap;
5715{
5716 char_u *name;
5717 char_u *end;
5718 char_u *p;
5719 long argt = 0;
5720 long def = -1;
5721 int flags = 0;
5722 int compl = EXPAND_NOTHING;
5723 char_u *compl_arg = NULL;
5724 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005725 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
5727 p = eap->arg;
5728
5729 /* Check for attributes */
5730 while (*p == '-')
5731 {
5732 ++p;
5733 end = skiptowhite(p);
5734 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5735 == FAIL)
5736 return;
5737 p = skipwhite(end);
5738 }
5739
5740 /* Get the name (if any) and skip to the following argument */
5741 name = p;
5742 if (ASCII_ISALPHA(*p))
5743 while (ASCII_ISALNUM(*p))
5744 ++p;
5745 if (!ends_excmd(*p) && !vim_iswhite(*p))
5746 {
5747 EMSG(_("E182: Invalid command name"));
5748 return;
5749 }
5750 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005751 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
5753 /* If there is nothing after the name, and no attributes were specified,
5754 * we are listing commands
5755 */
5756 p = skipwhite(end);
5757 if (!has_attr && ends_excmd(*p))
5758 {
5759 uc_list(name, end - name);
5760 }
5761 else if (!ASCII_ISUPPER(*name))
5762 {
5763 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5764 return;
5765 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005766 else if ((name_len == 1 && *name == 'X')
5767 || (name_len <= 4
5768 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5769 {
5770 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5771 return;
5772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
5774 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5775 eap->forceit);
5776}
5777
5778/*
5779 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 * Clear all user commands, global and for current buffer.
5781 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005782 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005784 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 uc_clear(&ucmds);
5787 uc_clear(&curbuf->b_ucmds);
5788}
5789
5790/*
5791 * Clear all user commands for "gap".
5792 */
5793 void
5794uc_clear(gap)
5795 garray_T *gap;
5796{
5797 int i;
5798 ucmd_T *cmd;
5799
5800 for (i = 0; i < gap->ga_len; ++i)
5801 {
5802 cmd = USER_CMD_GA(gap, i);
5803 vim_free(cmd->uc_name);
5804 vim_free(cmd->uc_rep);
5805# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5806 vim_free(cmd->uc_compl_arg);
5807# endif
5808 }
5809 ga_clear(gap);
5810}
5811
5812 static void
5813ex_delcommand(eap)
5814 exarg_T *eap;
5815{
5816 int i = 0;
5817 ucmd_T *cmd = NULL;
5818 int cmp = -1;
5819 garray_T *gap;
5820
5821 gap = &curbuf->b_ucmds;
5822 for (;;)
5823 {
5824 for (i = 0; i < gap->ga_len; ++i)
5825 {
5826 cmd = USER_CMD_GA(gap, i);
5827 cmp = STRCMP(eap->arg, cmd->uc_name);
5828 if (cmp <= 0)
5829 break;
5830 }
5831 if (gap == &ucmds || cmp == 0)
5832 break;
5833 gap = &ucmds;
5834 }
5835
5836 if (cmp != 0)
5837 {
5838 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5839 return;
5840 }
5841
5842 vim_free(cmd->uc_name);
5843 vim_free(cmd->uc_rep);
5844# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5845 vim_free(cmd->uc_compl_arg);
5846# endif
5847
5848 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 if (i < gap->ga_len)
5851 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5852}
5853
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005854/*
5855 * split and quote args for <f-args>
5856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 static char_u *
5858uc_split_args(arg, lenp)
5859 char_u *arg;
5860 size_t *lenp;
5861{
5862 char_u *buf;
5863 char_u *p;
5864 char_u *q;
5865 int len;
5866
5867 /* Precalculate length */
5868 p = arg;
5869 len = 2; /* Initial and final quotes */
5870
5871 while (*p)
5872 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005873 if (p[0] == '\\' && p[1] == '\\')
5874 {
5875 len += 2;
5876 p += 2;
5877 }
5878 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
5880 len += 1;
5881 p += 2;
5882 }
5883 else if (*p == '\\' || *p == '"')
5884 {
5885 len += 2;
5886 p += 1;
5887 }
5888 else if (vim_iswhite(*p))
5889 {
5890 p = skipwhite(p);
5891 if (*p == NUL)
5892 break;
5893 len += 3; /* "," */
5894 }
5895 else
5896 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005897#ifdef FEAT_MBYTE
5898 int charlen = (*mb_ptr2len)(p);
5899 len += charlen;
5900 p += charlen;
5901#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 ++len;
5903 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
5906 }
5907
5908 buf = alloc(len + 1);
5909 if (buf == NULL)
5910 {
5911 *lenp = 0;
5912 return buf;
5913 }
5914
5915 p = arg;
5916 q = buf;
5917 *q++ = '"';
5918 while (*p)
5919 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005920 if (p[0] == '\\' && p[1] == '\\')
5921 {
5922 *q++ = '\\';
5923 *q++ = '\\';
5924 p += 2;
5925 }
5926 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 *q++ = p[1];
5929 p += 2;
5930 }
5931 else if (*p == '\\' || *p == '"')
5932 {
5933 *q++ = '\\';
5934 *q++ = *p++;
5935 }
5936 else if (vim_iswhite(*p))
5937 {
5938 p = skipwhite(p);
5939 if (*p == NUL)
5940 break;
5941 *q++ = '"';
5942 *q++ = ',';
5943 *q++ = '"';
5944 }
5945 else
5946 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005947 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
5949 }
5950 *q++ = '"';
5951 *q = 0;
5952
5953 *lenp = len;
5954 return buf;
5955}
5956
5957/*
5958 * Check for a <> code in a user command.
5959 * "code" points to the '<'. "len" the length of the <> (inclusive).
5960 * "buf" is where the result is to be added.
5961 * "split_buf" points to a buffer used for splitting, caller should free it.
5962 * "split_len" is the length of what "split_buf" contains.
5963 * Returns the length of the replacement, which has been added to "buf".
5964 * Returns -1 if there was no match, and only the "<" has been copied.
5965 */
5966 static size_t
5967uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5968 char_u *code;
5969 size_t len;
5970 char_u *buf;
5971 ucmd_T *cmd; /* the user command we're expanding */
5972 exarg_T *eap; /* ex arguments */
5973 char_u **split_buf;
5974 size_t *split_len;
5975{
5976 size_t result = 0;
5977 char_u *p = code + 1;
5978 size_t l = len - 2;
5979 int quote = 0;
5980 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5981 ct_LT, ct_NONE } type = ct_NONE;
5982
5983 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5984 {
5985 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5986 p += 2;
5987 l -= 2;
5988 }
5989
Bram Moolenaar371d5402006-03-20 21:47:49 +00005990 ++l;
5991 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005993 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005995 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005997 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005999 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006001 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006003 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006005 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 type = ct_REGISTER;
6007
6008 switch (type)
6009 {
6010 case ct_ARGS:
6011 /* Simple case first */
6012 if (*eap->arg == NUL)
6013 {
6014 if (quote == 1)
6015 {
6016 result = 2;
6017 if (buf != NULL)
6018 STRCPY(buf, "''");
6019 }
6020 else
6021 result = 0;
6022 break;
6023 }
6024
6025 /* When specified there is a single argument don't split it.
6026 * Works for ":Cmd %" when % is "a b c". */
6027 if ((eap->argt & NOSPC) && quote == 2)
6028 quote = 1;
6029
6030 switch (quote)
6031 {
6032 case 0: /* No quoting, no splitting */
6033 result = STRLEN(eap->arg);
6034 if (buf != NULL)
6035 STRCPY(buf, eap->arg);
6036 break;
6037 case 1: /* Quote, but don't split */
6038 result = STRLEN(eap->arg) + 2;
6039 for (p = eap->arg; *p; ++p)
6040 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006041#ifdef FEAT_MBYTE
6042 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6043 /* DBCS can contain \ in a trail byte, skip the
6044 * double-byte character. */
6045 ++p;
6046 else
6047#endif
6048 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 ++result;
6050 }
6051
6052 if (buf != NULL)
6053 {
6054 *buf++ = '"';
6055 for (p = eap->arg; *p; ++p)
6056 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006057#ifdef FEAT_MBYTE
6058 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6059 /* DBCS can contain \ in a trail byte, copy the
6060 * double-byte character to avoid escaping. */
6061 *buf++ = *p++;
6062 else
6063#endif
6064 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 *buf++ = '\\';
6066 *buf++ = *p;
6067 }
6068 *buf = '"';
6069 }
6070
6071 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006072 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /* This is hard, so only do it once, and cache the result */
6074 if (*split_buf == NULL)
6075 *split_buf = uc_split_args(eap->arg, split_len);
6076
6077 result = *split_len;
6078 if (buf != NULL && result != 0)
6079 STRCPY(buf, *split_buf);
6080
6081 break;
6082 }
6083 break;
6084
6085 case ct_BANG:
6086 result = eap->forceit ? 1 : 0;
6087 if (quote)
6088 result += 2;
6089 if (buf != NULL)
6090 {
6091 if (quote)
6092 *buf++ = '"';
6093 if (eap->forceit)
6094 *buf++ = '!';
6095 if (quote)
6096 *buf = '"';
6097 }
6098 break;
6099
6100 case ct_LINE1:
6101 case ct_LINE2:
6102 case ct_COUNT:
6103 {
6104 char num_buf[20];
6105 long num = (type == ct_LINE1) ? eap->line1 :
6106 (type == ct_LINE2) ? eap->line2 :
6107 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6108 size_t num_len;
6109
6110 sprintf(num_buf, "%ld", num);
6111 num_len = STRLEN(num_buf);
6112 result = num_len;
6113
6114 if (quote)
6115 result += 2;
6116
6117 if (buf != NULL)
6118 {
6119 if (quote)
6120 *buf++ = '"';
6121 STRCPY(buf, num_buf);
6122 buf += num_len;
6123 if (quote)
6124 *buf = '"';
6125 }
6126
6127 break;
6128 }
6129
6130 case ct_REGISTER:
6131 result = eap->regname ? 1 : 0;
6132 if (quote)
6133 result += 2;
6134 if (buf != NULL)
6135 {
6136 if (quote)
6137 *buf++ = '\'';
6138 if (eap->regname)
6139 *buf++ = eap->regname;
6140 if (quote)
6141 *buf = '\'';
6142 }
6143 break;
6144
6145 case ct_LT:
6146 result = 1;
6147 if (buf != NULL)
6148 *buf = '<';
6149 break;
6150
6151 default:
6152 /* Not recognized: just copy the '<' and return -1. */
6153 result = (size_t)-1;
6154 if (buf != NULL)
6155 *buf = '<';
6156 break;
6157 }
6158
6159 return result;
6160}
6161
6162 static void
6163do_ucmd(eap)
6164 exarg_T *eap;
6165{
6166 char_u *buf;
6167 char_u *p;
6168 char_u *q;
6169
6170 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006171 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006172 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 size_t len, totlen;
6174
6175 size_t split_len = 0;
6176 char_u *split_buf = NULL;
6177 ucmd_T *cmd;
6178#ifdef FEAT_EVAL
6179 scid_T save_current_SID = current_SID;
6180#endif
6181
6182 if (eap->cmdidx == CMD_USER)
6183 cmd = USER_CMD(eap->useridx);
6184 else
6185 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6186
6187 /*
6188 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006189 * First round: "buf" is NULL, compute length, allocate "buf".
6190 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
6192 buf = NULL;
6193 for (;;)
6194 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006195 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006196 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006198
6199 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006201 start = vim_strchr(p, '<');
6202 if (start != NULL)
6203 end = vim_strchr(start + 1, '>');
6204 if (buf != NULL)
6205 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006206 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6207 ;
6208 if (*ksp == K_SPECIAL
6209 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006210 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6211# ifdef FEAT_GUI
6212 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6213# endif
6214 ))
6215 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006216 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006217 * KS_SPECIAL KE_FILLER, like for mappings, but
6218 * do_cmdline() doesn't handle that, so convert it back.
6219 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6220 len = ksp - p;
6221 if (len > 0)
6222 {
6223 mch_memmove(q, p, len);
6224 q += len;
6225 }
6226 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6227 p = ksp + 3;
6228 continue;
6229 }
6230 }
6231
6232 /* break if there no <item> is found */
6233 if (start == NULL || end == NULL)
6234 break;
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 /* Include the '>' */
6237 ++end;
6238
6239 /* Take everything up to the '<' */
6240 len = start - p;
6241 if (buf == NULL)
6242 totlen += len;
6243 else
6244 {
6245 mch_memmove(q, p, len);
6246 q += len;
6247 }
6248
6249 len = uc_check_code(start, end - start, q, cmd, eap,
6250 &split_buf, &split_len);
6251 if (len == (size_t)-1)
6252 {
6253 /* no match, continue after '<' */
6254 p = start + 1;
6255 len = 1;
6256 }
6257 else
6258 p = end;
6259 if (buf == NULL)
6260 totlen += len;
6261 else
6262 q += len;
6263 }
6264 if (buf != NULL) /* second time here, finished */
6265 {
6266 STRCPY(q, p);
6267 break;
6268 }
6269
6270 totlen += STRLEN(p); /* Add on the trailing characters */
6271 buf = alloc((unsigned)(totlen + 1));
6272 if (buf == NULL)
6273 {
6274 vim_free(split_buf);
6275 return;
6276 }
6277 }
6278
6279#ifdef FEAT_EVAL
6280 current_SID = cmd->uc_scriptID;
6281#endif
6282 (void)do_cmdline(buf, eap->getline, eap->cookie,
6283 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6284#ifdef FEAT_EVAL
6285 current_SID = save_current_SID;
6286#endif
6287 vim_free(buf);
6288 vim_free(split_buf);
6289}
6290
6291# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6292 static char_u *
6293get_user_command_name(idx)
6294 int idx;
6295{
6296 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6297}
6298
6299/*
6300 * Function given to ExpandGeneric() to obtain the list of user command names.
6301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 char_u *
6303get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006304 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 int idx;
6306{
6307 if (idx < curbuf->b_ucmds.ga_len)
6308 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6309 idx -= curbuf->b_ucmds.ga_len;
6310 if (idx < ucmds.ga_len)
6311 return USER_CMD(idx)->uc_name;
6312 return NULL;
6313}
6314
6315/*
6316 * Function given to ExpandGeneric() to obtain the list of user command
6317 * attributes.
6318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 char_u *
6320get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006321 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 int idx;
6323{
6324 static char *user_cmd_flags[] =
6325 {"bang", "bar", "buffer", "complete", "count",
6326 "nargs", "range", "register"};
6327
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006328 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 return NULL;
6330 return (char_u *)user_cmd_flags[idx];
6331}
6332
6333/*
6334 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 char_u *
6337get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006338 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 int idx;
6340{
6341 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6342
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006343 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 return NULL;
6345 return (char_u *)user_cmd_nargs[idx];
6346}
6347
6348/*
6349 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 char_u *
6352get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006353 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 int idx;
6355{
6356 return (char_u *)command_complete[idx].name;
6357}
6358# endif /* FEAT_CMDL_COMPL */
6359
6360#endif /* FEAT_USR_CMDS */
6361
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006362#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6363/*
6364 * Parse a completion argument "value[vallen]".
6365 * The detected completion goes in "*complp", argument type in "*argt".
6366 * When there is an argument, for function and user defined completion, it's
6367 * copied to allocated memory and stored in "*compl_arg".
6368 * Returns FAIL if something is wrong.
6369 */
6370 int
6371parse_compl_arg(value, vallen, complp, argt, compl_arg)
6372 char_u *value;
6373 int vallen;
6374 int *complp;
6375 long *argt;
6376 char_u **compl_arg;
6377{
6378 char_u *arg = NULL;
6379 size_t arglen = 0;
6380 int i;
6381 int valend = vallen;
6382
6383 /* Look for any argument part - which is the part after any ',' */
6384 for (i = 0; i < vallen; ++i)
6385 {
6386 if (value[i] == ',')
6387 {
6388 arg = &value[i + 1];
6389 arglen = vallen - i - 1;
6390 valend = i;
6391 break;
6392 }
6393 }
6394
6395 for (i = 0; command_complete[i].expand != 0; ++i)
6396 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006397 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006398 && STRNCMP(value, command_complete[i].name, valend) == 0)
6399 {
6400 *complp = command_complete[i].expand;
6401 if (command_complete[i].expand == EXPAND_BUFFERS)
6402 *argt |= BUFNAME;
6403 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6404 || command_complete[i].expand == EXPAND_FILES)
6405 *argt |= XFILE;
6406 break;
6407 }
6408 }
6409
6410 if (command_complete[i].expand == 0)
6411 {
6412 EMSG2(_("E180: Invalid complete value: %s"), value);
6413 return FAIL;
6414 }
6415
6416# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6417 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6418 && arg != NULL)
6419# else
6420 if (arg != NULL)
6421# endif
6422 {
6423 EMSG(_("E468: Completion argument only allowed for custom completion"));
6424 return FAIL;
6425 }
6426
6427# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6428 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6429 && arg == NULL)
6430 {
6431 EMSG(_("E467: Custom completion requires a function argument"));
6432 return FAIL;
6433 }
6434
6435 if (arg != NULL)
6436 *compl_arg = vim_strnsave(arg, (int)arglen);
6437# endif
6438 return OK;
6439}
6440#endif
6441
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 static void
6443ex_colorscheme(eap)
6444 exarg_T *eap;
6445{
Bram Moolenaare6850792010-05-14 15:28:44 +02006446 if (*eap->arg == NUL)
6447 {
6448#ifdef FEAT_EVAL
6449 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6450 char_u *p = NULL;
6451
6452 if (expr != NULL)
6453 {
6454 ++emsg_off;
6455 p = eval_to_string(expr, NULL, FALSE);
6456 --emsg_off;
6457 vim_free(expr);
6458 }
6459 if (p != NULL)
6460 {
6461 MSG(p);
6462 vim_free(p);
6463 }
6464 else
6465 MSG("default");
6466#else
6467 MSG(_("unknown"));
6468#endif
6469 }
6470 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006471 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472}
6473
6474 static void
6475ex_highlight(eap)
6476 exarg_T *eap;
6477{
6478 if (*eap->arg == NUL && eap->cmd[2] == '!')
6479 MSG(_("Greetings, Vim user!"));
6480 do_highlight(eap->arg, eap->forceit, FALSE);
6481}
6482
6483
6484/*
6485 * Call this function if we thought we were going to exit, but we won't
6486 * (because of an error). May need to restore the terminal mode.
6487 */
6488 void
6489not_exiting()
6490{
6491 exiting = FALSE;
6492 settmode(TMODE_RAW);
6493}
6494
6495/*
6496 * ":quit": quit current window, quit Vim if closed the last window.
6497 */
6498 static void
6499ex_quit(eap)
6500 exarg_T *eap;
6501{
6502#ifdef FEAT_CMDWIN
6503 if (cmdwin_type != 0)
6504 {
6505 cmdwin_result = Ctrl_C;
6506 return;
6507 }
6508#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006509 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006510 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006511 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006512 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006513 return;
6514 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006515#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006516 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02006517 /* Refuse to quick when locked or when the buffer in the last window is
6518 * being closed (can only happen in autocommands). */
6519 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006520 return;
6521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
6523#ifdef FEAT_NETBEANS_INTG
6524 netbeansForcedQuit = eap->forceit;
6525#endif
6526
6527 /*
6528 * If there are more files or windows we won't exit.
6529 */
6530 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6531 exiting = TRUE;
6532 if ((!P_HID(curbuf)
6533 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6534 || check_more(TRUE, eap->forceit) == FAIL
6535 || (only_one_window() && check_changed_any(eap->forceit)))
6536 {
6537 not_exiting();
6538 }
6539 else
6540 {
6541#ifdef FEAT_WINDOWS
6542 if (only_one_window()) /* quit last window */
6543#endif
6544 getout(0);
6545#ifdef FEAT_WINDOWS
6546# ifdef FEAT_GUI
6547 need_mouse_correct = TRUE;
6548# endif
6549 /* close window; may free buffer */
6550 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6551#endif
6552 }
6553}
6554
6555/*
6556 * ":cquit".
6557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 static void
6559ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006560 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 getout(1); /* this does not always pass on the exit code to the Manx
6563 compiler. why? */
6564}
6565
6566/*
6567 * ":qall": try to quit all windows
6568 */
6569 static void
6570ex_quit_all(eap)
6571 exarg_T *eap;
6572{
6573# ifdef FEAT_CMDWIN
6574 if (cmdwin_type != 0)
6575 {
6576 if (eap->forceit)
6577 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6578 else
6579 cmdwin_result = K_XF2;
6580 return;
6581 }
6582# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006583
6584 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006585 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006586 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006587 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006588 return;
6589 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006590#ifdef FEAT_AUTOCMD
6591 if (curbuf_locked())
6592 return;
6593#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 exiting = TRUE;
6596 if (eap->forceit || !check_changed_any(FALSE))
6597 getout(0);
6598 not_exiting();
6599}
6600
Bram Moolenaard9967712006-03-11 21:18:15 +00006601#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602/*
6603 * ":close": close current window, unless it is the last one
6604 */
6605 static void
6606ex_close(eap)
6607 exarg_T *eap;
6608{
6609# ifdef FEAT_CMDWIN
6610 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006611 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 else
6613# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006614 if (!text_locked()
6615#ifdef FEAT_AUTOCMD
6616 && !curbuf_locked()
6617#endif
6618 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006619 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620}
6621
Bram Moolenaard9967712006-03-11 21:18:15 +00006622# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006623/*
6624 * ":pclose": Close any preview window.
6625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629{
6630 win_T *win;
6631
6632 for (win = firstwin; win != NULL; win = win->w_next)
6633 if (win->w_p_pvw)
6634 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006635 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006636 break;
6637 }
6638}
Bram Moolenaard9967712006-03-11 21:18:15 +00006639# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006640
Bram Moolenaarf740b292006-02-16 22:11:02 +00006641/*
6642 * Close window "win" and take care of handling closing the last window for a
6643 * modified buffer.
6644 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006646ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006649 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 int need_hide;
6652 buf_T *buf = win->w_buffer;
6653
6654 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006655 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006657# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if ((p_confirm || cmdmod.confirm) && p_write)
6659 {
6660 dialog_changed(buf, FALSE);
6661 if (buf_valid(buf) && bufIsChanged(buf))
6662 return;
6663 need_hide = FALSE;
6664 }
6665 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 EMSG(_(e_nowrtmsg));
6669 return;
6670 }
6671 }
6672
Bram Moolenaard9967712006-03-11 21:18:15 +00006673# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006675# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006678 if (tp == NULL)
6679 win_close(win, !need_hide && !P_HID(buf));
6680 else
6681 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682}
6683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006685 * ":tabclose": close current tab page, unless it is the last one.
6686 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 */
6688 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006689ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 exarg_T *eap;
6691{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006692 tabpage_T *tp;
6693
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006694# ifdef FEAT_CMDWIN
6695 if (cmdwin_type != 0)
6696 cmdwin_result = K_IGNORE;
6697 else
6698# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006699 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006700 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006703 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006704 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006705 tp = find_tabpage((int)eap->line2);
6706 if (tp == NULL)
6707 {
6708 beep_flush();
6709 return;
6710 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006711 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006712 {
6713 tabpage_close_other(tp, eap->forceit);
6714 return;
6715 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006717 if (!text_locked()
6718#ifdef FEAT_AUTOCMD
6719 && !curbuf_locked()
6720#endif
6721 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006722 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006724}
6725
6726/*
6727 * ":tabonly": close all tab pages except the current one
6728 */
6729 static void
6730ex_tabonly(eap)
6731 exarg_T *eap;
6732{
6733 tabpage_T *tp;
6734 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006735
6736# ifdef FEAT_CMDWIN
6737 if (cmdwin_type != 0)
6738 cmdwin_result = K_IGNORE;
6739 else
6740# endif
6741 if (first_tabpage->tp_next == NULL)
6742 MSG(_("Already only one tab page"));
6743 else
6744 {
6745 /* Repeat this up to a 1000 times, because autocommands may mess
6746 * up the lists. */
6747 for (done = 0; done < 1000; ++done)
6748 {
6749 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6750 if (tp->tp_topframe != topframe)
6751 {
6752 tabpage_close_other(tp, eap->forceit);
6753 /* if we failed to close it quit */
6754 if (valid_tabpage(tp))
6755 done = 1000;
6756 /* start over, "tp" is now invalid */
6757 break;
6758 }
6759 if (first_tabpage->tp_next == NULL)
6760 break;
6761 }
6762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
6765/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006766 * Close the current tab page.
6767 */
6768 void
6769tabpage_close(forceit)
6770 int forceit;
6771{
6772 /* First close all the windows but the current one. If that worked then
6773 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006774 if (lastwin != firstwin)
6775 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006776 if (lastwin == firstwin)
6777 ex_win_close(forceit, curwin, NULL);
6778# ifdef FEAT_GUI
6779 need_mouse_correct = TRUE;
6780# endif
6781}
6782
6783/*
6784 * Close tab page "tp", which is not the current tab page.
6785 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006786 * Also takes care of the tab pages line disappearing when closing the
6787 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006788 */
6789 void
6790tabpage_close_other(tp, forceit)
6791 tabpage_T *tp;
6792 int forceit;
6793{
6794 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006795 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006796 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006797
6798 /* Limit to 1000 windows, autocommands may add a window while we close
6799 * one. OK, so I'm paranoid... */
6800 while (++done < 1000)
6801 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006802 wp = tp->tp_firstwin;
6803 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006804
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006805 /* Autocommands may delete the tab page under our fingers and we may
6806 * fail to close a window with a modified buffer. */
6807 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006808 break;
6809 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006810
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006811 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006812 if (h != tabline_height())
6813 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006814}
6815
6816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 * ":only".
6818 */
6819 static void
6820ex_only(eap)
6821 exarg_T *eap;
6822{
6823# ifdef FEAT_GUI
6824 need_mouse_correct = TRUE;
6825# endif
6826 close_others(TRUE, eap->forceit);
6827}
6828
6829/*
6830 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006831 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006833 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834ex_all(eap)
6835 exarg_T *eap;
6836{
6837 if (eap->addr_count == 0)
6838 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006839 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840}
6841#endif /* FEAT_WINDOWS */
6842
6843 static void
6844ex_hide(eap)
6845 exarg_T *eap;
6846{
6847 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6848 eap->errmsg = e_invarg;
6849 else
6850 {
6851 /* ":hide" or ":hide | cmd": hide current window */
6852 eap->nextcmd = check_nextcmd(eap->arg);
6853#ifdef FEAT_WINDOWS
6854 if (!eap->skip)
6855 {
6856# ifdef FEAT_GUI
6857 need_mouse_correct = TRUE;
6858# endif
6859 win_close(curwin, FALSE); /* don't free buffer */
6860 }
6861#endif
6862 }
6863}
6864
6865/*
6866 * ":stop" and ":suspend": Suspend Vim.
6867 */
6868 static void
6869ex_stop(eap)
6870 exarg_T *eap;
6871{
6872 /*
6873 * Disallow suspending for "rvim".
6874 */
6875 if (!check_restricted()
6876#ifdef WIN3264
6877 /*
6878 * Check if external commands are allowed now.
6879 */
6880 && can_end_termcap_mode(TRUE)
6881#endif
6882 )
6883 {
6884 if (!eap->forceit)
6885 autowrite_all();
6886 windgoto((int)Rows - 1, 0);
6887 out_char('\n');
6888 out_flush();
6889 stoptermcap();
6890 out_flush(); /* needed for SUN to restore xterm buffer */
6891#ifdef FEAT_TITLE
6892 mch_restore_title(3); /* restore window titles */
6893#endif
6894 ui_suspend(); /* call machine specific function */
6895#ifdef FEAT_TITLE
6896 maketitle();
6897 resettitle(); /* force updating the title */
6898#endif
6899 starttermcap();
6900 scroll_start(); /* scroll screen before redrawing */
6901 redraw_later_clear();
6902 shell_resized(); /* may have resized window */
6903 }
6904}
6905
6906/*
6907 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6908 */
6909 static void
6910ex_exit(eap)
6911 exarg_T *eap;
6912{
6913#ifdef FEAT_CMDWIN
6914 if (cmdwin_type != 0)
6915 {
6916 cmdwin_result = Ctrl_C;
6917 return;
6918 }
6919#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006920 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006921 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006922 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006923 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006924 return;
6925 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#ifdef FEAT_AUTOCMD
6927 if (curbuf_locked())
6928 return;
6929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
6931 /*
6932 * if more files or windows we won't exit
6933 */
6934 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6935 exiting = TRUE;
6936 if ( ((eap->cmdidx == CMD_wq
6937 || curbufIsChanged())
6938 && do_write(eap) == FAIL)
6939 || check_more(TRUE, eap->forceit) == FAIL
6940 || (only_one_window() && check_changed_any(eap->forceit)))
6941 {
6942 not_exiting();
6943 }
6944 else
6945 {
6946#ifdef FEAT_WINDOWS
6947 if (only_one_window()) /* quit last window, exit Vim */
6948#endif
6949 getout(0);
6950#ifdef FEAT_WINDOWS
6951# ifdef FEAT_GUI
6952 need_mouse_correct = TRUE;
6953# endif
6954 /* quit current window, may free buffer */
6955 win_close(curwin, !P_HID(curwin->w_buffer));
6956#endif
6957 }
6958}
6959
6960/*
6961 * ":print", ":list", ":number".
6962 */
6963 static void
6964ex_print(eap)
6965 exarg_T *eap;
6966{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006967 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6968 EMSG(_(e_emptybuf));
6969 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006971 for ( ;!got_int; ui_breakcheck())
6972 {
6973 print_line(eap->line1,
6974 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6975 || (eap->flags & EXFLAG_NR)),
6976 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6977 if (++eap->line1 > eap->line2)
6978 break;
6979 out_flush(); /* show one line at a time */
6980 }
6981 setpcmark();
6982 /* put cursor at last line */
6983 curwin->w_cursor.lnum = eap->line2;
6984 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988}
6989
6990#ifdef FEAT_BYTEOFF
6991 static void
6992ex_goto(eap)
6993 exarg_T *eap;
6994{
6995 goto_byte(eap->line2);
6996}
6997#endif
6998
6999/*
7000 * ":shell".
7001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 static void
7003ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007004 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 do_shell(NULL, 0);
7007}
7008
7009#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7010 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007011 || defined(FEAT_GUI_MSWIN) \
7012 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 || defined(PROTO)
7014
7015/*
7016 * Handle a file drop. The code is here because a drop is *nearly* like an
7017 * :args command, but not quite (we have a list of exact filenames, so we
7018 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7019 * code is very similar to :args and hence needs access to a lot of the static
7020 * functions in this file.
7021 *
7022 * The list should be allocated using alloc(), as should each item in the
7023 * list. This function takes over responsibility for freeing the list.
7024 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007025 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7027 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7028 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7029 * file functionality is (currently) not in EMX this is not presently a
7030 * problem.
7031 */
7032 void
7033handle_drop(filec, filev, split)
7034 int filec; /* the number of files dropped */
7035 char_u **filev; /* the list of files dropped */
7036 int split; /* force splitting the window */
7037{
7038 exarg_T ea;
7039 int save_msg_scroll = msg_scroll;
7040
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007041 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007042 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007044#ifdef FEAT_AUTOCMD
7045 if (curbuf_locked())
7046 return;
7047#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007048 /* When the screen is being updated we should not change buffers and
7049 * windows structures, it may cause freed memory to be used. */
7050 if (updating_screen)
7051 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
7053 /* Check whether the current buffer is changed. If so, we will need
7054 * to split the current window or data could be lost.
7055 * We don't need to check if the 'hidden' option is set, as in this
7056 * case the buffer won't be lost.
7057 */
7058 if (!P_HID(curbuf) && !split)
7059 {
7060 ++emsg_off;
7061 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7062 --emsg_off;
7063 }
7064 if (split)
7065 {
7066# ifdef FEAT_WINDOWS
7067 if (win_split(0, 0) == FAIL)
7068 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007069 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
7071 /* When splitting the window, create a new alist. Otherwise the
7072 * existing one is overwritten. */
7073 alist_unlink(curwin->w_alist);
7074 alist_new();
7075# else
7076 return; /* can't split, always fail */
7077# endif
7078 }
7079
7080 /*
7081 * Set up the new argument list.
7082 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007083 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085 /*
7086 * Move to the first file.
7087 */
7088 /* Fake up a minimal "next" command for do_argfile() */
7089 vim_memset(&ea, 0, sizeof(ea));
7090 ea.cmd = (char_u *)"next";
7091 do_argfile(&ea, 0);
7092
7093 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7094 * mode that is not needed here. */
7095 need_start_insertmode = FALSE;
7096
7097 /* Restore msg_scroll, otherwise a following command may cause scrolling
7098 * unexpectedly. The screen will be redrawn by the caller, thus
7099 * msg_scroll being set by displaying a message is irrelevant. */
7100 msg_scroll = save_msg_scroll;
7101}
7102#endif
7103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104/*
7105 * Clear an argument list: free all file names and reset it to zero entries.
7106 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007107 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108alist_clear(al)
7109 alist_T *al;
7110{
7111 while (--al->al_ga.ga_len >= 0)
7112 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7113 ga_clear(&al->al_ga);
7114}
7115
7116/*
7117 * Init an argument list.
7118 */
7119 void
7120alist_init(al)
7121 alist_T *al;
7122{
7123 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7124}
7125
7126#if defined(FEAT_WINDOWS) || defined(PROTO)
7127
7128/*
7129 * Remove a reference from an argument list.
7130 * Ignored when the argument list is the global one.
7131 * If the argument list is no longer used by any window, free it.
7132 */
7133 void
7134alist_unlink(al)
7135 alist_T *al;
7136{
7137 if (al != &global_alist && --al->al_refcount <= 0)
7138 {
7139 alist_clear(al);
7140 vim_free(al);
7141 }
7142}
7143
7144# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7145/*
7146 * Create a new argument list and use it for the current window.
7147 */
7148 void
7149alist_new()
7150{
7151 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7152 if (curwin->w_alist == NULL)
7153 {
7154 curwin->w_alist = &global_alist;
7155 ++global_alist.al_refcount;
7156 }
7157 else
7158 {
7159 curwin->w_alist->al_refcount = 1;
7160 alist_init(curwin->w_alist);
7161 }
7162}
7163# endif
7164#endif
7165
7166#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7167/*
7168 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007169 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7170 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
7172 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007173alist_expand(fnum_list, fnum_len)
7174 int *fnum_list;
7175 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007178 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 char_u **new_arg_files;
7180 int new_arg_file_count;
7181 char_u *save_p_su = p_su;
7182 int i;
7183
7184 /* Don't use 'suffixes' here. This should work like the shell did the
7185 * expansion. Also, the vimrc file isn't read yet, thus the user
7186 * can't set the options. */
7187 p_su = empty_option;
7188 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7189 if (old_arg_files != NULL)
7190 {
7191 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007192 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7193 old_arg_count = GARGCOUNT;
7194 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007196 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 && new_arg_file_count > 0)
7198 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007199 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7200 TRUE, fnum_list, fnum_len);
7201 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
7204 p_su = save_p_su;
7205}
7206#endif
7207
7208/*
7209 * Set the argument list for the current window.
7210 * Takes over the allocated files[] and the allocated fnames in it.
7211 */
7212 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007213alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 alist_T *al;
7215 int count;
7216 char_u **files;
7217 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007218 int *fnum_list;
7219 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 int i;
7222
7223 alist_clear(al);
7224 if (ga_grow(&al->al_ga, count) == OK)
7225 {
7226 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007227 {
7228 if (got_int)
7229 {
7230 /* When adding many buffers this can take a long time. Allow
7231 * interrupting here. */
7232 while (i < count)
7233 vim_free(files[i++]);
7234 break;
7235 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007236
7237 /* May set buffer name of a buffer previously used for the
7238 * argument list, so that it's re-used by alist_add. */
7239 if (fnum_list != NULL && i < fnum_len)
7240 buf_set_name(fnum_list[i], files[i]);
7241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007243 ui_breakcheck();
7244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 vim_free(files);
7246 }
7247 else
7248 FreeWild(count, files);
7249#ifdef FEAT_WINDOWS
7250 if (al == &global_alist)
7251#endif
7252 arg_had_last = FALSE;
7253}
7254
7255/*
7256 * Add file "fname" to argument list "al".
7257 * "fname" must have been allocated and "al" must have been checked for room.
7258 */
7259 void
7260alist_add(al, fname, set_fnum)
7261 alist_T *al;
7262 char_u *fname;
7263 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7264{
7265 if (fname == NULL) /* don't add NULL file names */
7266 return;
7267#ifdef BACKSLASH_IN_FILENAME
7268 slash_adjust(fname);
7269#endif
7270 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7271 if (set_fnum > 0)
7272 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7273 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7274 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7278/*
7279 * Adjust slashes in file names. Called after 'shellslash' was set.
7280 */
7281 void
7282alist_slash_adjust()
7283{
7284 int i;
7285# ifdef FEAT_WINDOWS
7286 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007287 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288# endif
7289
7290 for (i = 0; i < GARGCOUNT; ++i)
7291 if (GARGLIST[i].ae_fname != NULL)
7292 slash_adjust(GARGLIST[i].ae_fname);
7293# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007294 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (wp->w_alist != &global_alist)
7296 for (i = 0; i < WARGCOUNT(wp); ++i)
7297 if (WARGLIST(wp)[i].ae_fname != NULL)
7298 slash_adjust(WARGLIST(wp)[i].ae_fname);
7299# endif
7300}
7301#endif
7302
7303/*
7304 * ":preserve".
7305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 static void
7307ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007308 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007310 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 ml_preserve(curbuf, TRUE);
7312}
7313
7314/*
7315 * ":recover".
7316 */
7317 static void
7318ex_recover(eap)
7319 exarg_T *eap;
7320{
7321 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7322 recoverymode = TRUE;
7323 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7324 && (*eap->arg == NUL
7325 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7326 ml_recover();
7327 recoverymode = FALSE;
7328}
7329
7330/*
7331 * Command modifier used in a wrong way.
7332 */
7333 static void
7334ex_wrongmodifier(eap)
7335 exarg_T *eap;
7336{
7337 eap->errmsg = e_invcmd;
7338}
7339
7340#ifdef FEAT_WINDOWS
7341/*
7342 * :sview [+command] file split window with new file, read-only
7343 * :split [[+command] file] split window with current or new file
7344 * :vsplit [[+command] file] split window vertically with current or new file
7345 * :new [[+command] file] split window with no or new file
7346 * :vnew [[+command] file] split vertically window with no or new file
7347 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007348 *
7349 * :tabedit open new Tab page with empty window
7350 * :tabedit [+command] file open new Tab page and edit "file"
7351 * :tabnew [[+command] file] just like :tabedit
7352 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 */
7354 void
7355ex_splitview(eap)
7356 exarg_T *eap;
7357{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007358 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007359# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361# endif
7362# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7368 {
7369 ex_ni(eap);
7370 return;
7371 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007374# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007380 * quickfix windows. But it's OK when doing ":tab split". */
7381 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
7383 if (eap->cmdidx == CMD_split)
7384 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if (eap->cmdidx == CMD_vsplit)
7387 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007393 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7396 FNAME_MESS, TRUE, curbuf->b_ffname);
7397 if (fname == NULL)
7398 goto theend;
7399 eap->arg = fname;
7400 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 && eap->cmdidx != CMD_new)
7411 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007412# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007413 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007414# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007415 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007416# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007417 au_has_group((char_u *)"FileExplorer"))
7418 {
7419 /* No browsing supported but we do have the file explorer:
7420 * Edit the directory. */
7421 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7422 eap->arg = (char_u *)".";
7423 }
7424 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007425# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007426 {
7427 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007429 if (fname == NULL)
7430 goto theend;
7431 eap->arg = fname;
7432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007437 /*
7438 * Either open new tab page or split the window.
7439 */
7440 if (eap->cmdidx == CMD_tabedit
7441 || eap->cmdidx == CMD_tabfind
7442 || eap->cmdidx == CMD_tabnew)
7443 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007444 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7445 : eap->addr_count == 0 ? 0
7446 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007447 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007448 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007449
7450 /* set the alternate buffer for the window we came from */
7451 if (curwin != old_curwin
7452 && win_valid(old_curwin)
7453 && old_curwin->w_buffer != curbuf
7454 && !cmdmod.keepalt)
7455 old_curwin->w_alt_fnum = curbuf->b_fnum;
7456 }
7457 }
7458 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7460 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007461# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 /* Reset 'scrollbind' when editing another file, but keep it when
7463 * doing ":split" without arguments. */
7464 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007469 {
7470 RESET_BINDING(curwin);
7471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 else
7473 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 do_exedit(eap, old_curwin);
7476 }
7477
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007478# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007482# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483theend:
7484 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007487
7488/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007489 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007490 */
7491 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007492tabpage_new()
7493{
7494 exarg_T ea;
7495
7496 vim_memset(&ea, 0, sizeof(ea));
7497 ea.cmdidx = CMD_tabnew;
7498 ea.cmd = (char_u *)"tabn";
7499 ea.arg = (char_u *)"";
7500 ex_splitview(&ea);
7501}
7502
7503/*
7504 * :tabnext command
7505 */
7506 static void
7507ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007508 exarg_T *eap;
7509{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007510 switch (eap->cmdidx)
7511 {
7512 case CMD_tabfirst:
7513 case CMD_tabrewind:
7514 goto_tabpage(1);
7515 break;
7516 case CMD_tablast:
7517 goto_tabpage(9999);
7518 break;
7519 case CMD_tabprevious:
7520 case CMD_tabNext:
7521 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7522 break;
7523 default: /* CMD_tabnext */
7524 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7525 break;
7526 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007527}
7528
7529/*
7530 * :tabmove command
7531 */
7532 static void
7533ex_tabmove(eap)
7534 exarg_T *eap;
7535{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007536 int tab_number = 9999;
7537
7538 if (eap->arg && *eap->arg != NUL)
7539 {
7540 char_u *p = eap->arg;
7541 int relative = 0; /* argument +N/-N means: move N places to the
7542 * right/left relative to the current position. */
7543
7544 if (*eap->arg == '-')
7545 {
7546 relative = -1;
7547 p = eap->arg + 1;
7548 }
7549 else if (*eap->arg == '+')
7550 {
7551 relative = 1;
7552 p = eap->arg + 1;
7553 }
7554 else
7555 p = eap->arg;
7556
7557 if (p == skipdigits(p))
7558 {
7559 /* No numbers as argument. */
7560 eap->errmsg = e_invarg;
7561 return;
7562 }
7563
7564 tab_number = getdigits(&p);
7565 if (relative != 0)
7566 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7567 }
7568 else if (eap->addr_count != 0)
7569 tab_number = eap->line2;
7570
7571 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007572}
7573
7574/*
7575 * :tabs command: List tabs and their contents.
7576 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007577 static void
7578ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007579 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007580{
7581 tabpage_T *tp;
7582 win_T *wp;
7583 int tabcount = 1;
7584
7585 msg_start();
7586 msg_scroll = TRUE;
7587 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7588 {
7589 msg_putchar('\n');
7590 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7591 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7592 out_flush(); /* output one line at a time */
7593 ui_breakcheck();
7594
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007595 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007596 wp = firstwin;
7597 else
7598 wp = tp->tp_firstwin;
7599 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7600 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007601 msg_putchar('\n');
7602 msg_putchar(wp == curwin ? '>' : ' ');
7603 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007604 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7605 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007606 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007607 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007608 else
7609 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7610 IObuff, IOSIZE, TRUE);
7611 msg_outtrans(IObuff);
7612 out_flush(); /* output one line at a time */
7613 ui_breakcheck();
7614 }
7615 }
7616}
7617
7618#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620/*
7621 * ":mode": Set screen mode.
7622 * If no argument given, just get the screen size and redraw.
7623 */
7624 static void
7625ex_mode(eap)
7626 exarg_T *eap;
7627{
7628 if (*eap->arg == NUL)
7629 shell_resized();
7630 else
7631 mch_screenmode(eap->arg);
7632}
7633
7634#ifdef FEAT_WINDOWS
7635/*
7636 * ":resize".
7637 * set, increment or decrement current window height
7638 */
7639 static void
7640ex_resize(eap)
7641 exarg_T *eap;
7642{
7643 int n;
7644 win_T *wp = curwin;
7645
7646 if (eap->addr_count > 0)
7647 {
7648 n = eap->line2;
7649 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7650 ;
7651 }
7652
7653#ifdef FEAT_GUI
7654 need_mouse_correct = TRUE;
7655#endif
7656 n = atol((char *)eap->arg);
7657#ifdef FEAT_VERTSPLIT
7658 if (cmdmod.split & WSP_VERT)
7659 {
7660 if (*eap->arg == '-' || *eap->arg == '+')
7661 n += W_WIDTH(curwin);
7662 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7663 n = 9999;
7664 win_setwidth_win((int)n, wp);
7665 }
7666 else
7667#endif
7668 {
7669 if (*eap->arg == '-' || *eap->arg == '+')
7670 n += curwin->w_height;
7671 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7672 n = 9999;
7673 win_setheight_win((int)n, wp);
7674 }
7675}
7676#endif
7677
7678/*
7679 * ":find [+command] <file>" command.
7680 */
7681 static void
7682ex_find(eap)
7683 exarg_T *eap;
7684{
7685#ifdef FEAT_SEARCHPATH
7686 char_u *fname;
7687 int count;
7688
7689 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7690 TRUE, curbuf->b_ffname);
7691 if (eap->addr_count > 0)
7692 {
7693 /* Repeat finding the file "count" times. This matters when it
7694 * appears several times in the path. */
7695 count = eap->line2;
7696 while (fname != NULL && --count > 0)
7697 {
7698 vim_free(fname);
7699 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7700 FALSE, curbuf->b_ffname);
7701 }
7702 }
7703
7704 if (fname != NULL)
7705 {
7706 eap->arg = fname;
7707#endif
7708 do_exedit(eap, NULL);
7709#ifdef FEAT_SEARCHPATH
7710 vim_free(fname);
7711 }
7712#endif
7713}
7714
7715/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007716 * ":open" simulation: for now just work like ":visual".
7717 */
7718 static void
7719ex_open(eap)
7720 exarg_T *eap;
7721{
7722 regmatch_T regmatch;
7723 char_u *p;
7724
7725 curwin->w_cursor.lnum = eap->line2;
7726 beginline(BL_SOL | BL_FIX);
7727 if (*eap->arg == '/')
7728 {
7729 /* ":open /pattern/": put cursor in column found with pattern */
7730 ++eap->arg;
7731 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7732 *p = NUL;
7733 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7734 if (regmatch.regprog != NULL)
7735 {
7736 regmatch.rm_ic = p_ic;
7737 p = ml_get_curline();
7738 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007739 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007740 else
7741 EMSG(_(e_nomatch));
7742 vim_free(regmatch.regprog);
7743 }
7744 /* Move to the NUL, ignore any other arguments. */
7745 eap->arg += STRLEN(eap->arg);
7746 }
7747 check_cursor();
7748
7749 eap->cmdidx = CMD_visual;
7750 do_exedit(eap, NULL);
7751}
7752
7753/*
7754 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 */
7756 static void
7757ex_edit(eap)
7758 exarg_T *eap;
7759{
7760 do_exedit(eap, NULL);
7761}
7762
7763/*
7764 * ":edit <file>" command and alikes.
7765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 void
7767do_exedit(eap, old_curwin)
7768 exarg_T *eap;
7769 win_T *old_curwin; /* curwin before doing a split or NULL */
7770{
7771 int n;
7772#ifdef FEAT_WINDOWS
7773 int need_hide;
7774#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007775 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 /*
7778 * ":vi" command ends Ex mode.
7779 */
7780 if (exmode_active && (eap->cmdidx == CMD_visual
7781 || eap->cmdidx == CMD_view))
7782 {
7783 exmode_active = FALSE;
7784 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007785 {
7786 /* Special case: ":global/pat/visual\NLvi-commands" */
7787 if (global_busy)
7788 {
7789 int rd = RedrawingDisabled;
7790 int nwr = no_wait_return;
7791 int ms = msg_scroll;
7792#ifdef FEAT_GUI
7793 int he = hold_gui_events;
7794#endif
7795
7796 if (eap->nextcmd != NULL)
7797 {
7798 stuffReadbuff(eap->nextcmd);
7799 eap->nextcmd = NULL;
7800 }
7801
7802 if (exmode_was != EXMODE_VIM)
7803 settmode(TMODE_RAW);
7804 RedrawingDisabled = 0;
7805 no_wait_return = 0;
7806 need_wait_return = FALSE;
7807 msg_scroll = 0;
7808#ifdef FEAT_GUI
7809 hold_gui_events = 0;
7810#endif
7811 must_redraw = CLEAR;
7812
7813 main_loop(FALSE, TRUE);
7814
7815 RedrawingDisabled = rd;
7816 no_wait_return = nwr;
7817 msg_scroll = ms;
7818#ifdef FEAT_GUI
7819 hold_gui_events = he;
7820#endif
7821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825
7826 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007827 || eap->cmdidx == CMD_tabnew
7828 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829#ifdef FEAT_VERTSPLIT
7830 || eap->cmdidx == CMD_vnew
7831#endif
7832 ) && *eap->arg == NUL)
7833 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007834 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 setpcmark();
7836 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007837 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7838 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
7840 else if ((eap->cmdidx != CMD_split
7841#ifdef FEAT_VERTSPLIT
7842 && eap->cmdidx != CMD_vsplit
7843#endif
7844 )
7845 || *eap->arg != NUL
7846#ifdef FEAT_BROWSE
7847 || cmdmod.browse
7848#endif
7849 )
7850 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007851#ifdef FEAT_AUTOCMD
7852 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7853 * can bring us here, others are stopped earlier. */
7854 if (*eap->arg != NUL && curbuf_locked())
7855 return;
7856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 n = readonlymode;
7858 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7859 readonlymode = TRUE;
7860 else if (eap->cmdidx == CMD_enew)
7861 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7862 empty buffer */
7863 setpcmark();
7864 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7865 NULL, eap,
7866 /* ":edit" goes to first line if Vi compatible */
7867 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7868 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7869 ? ECMD_ONE : eap->do_ecmd_lnum,
7870 (P_HID(curbuf) ? ECMD_HIDE : 0)
7871 + (eap->forceit ? ECMD_FORCEIT : 0)
7872#ifdef FEAT_LISTCMDS
7873 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7874#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007875 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
7877 /* Editing the file failed. If the window was split, close it. */
7878#ifdef FEAT_WINDOWS
7879 if (old_curwin != NULL)
7880 {
7881 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7882 if (!need_hide || P_HID(curbuf))
7883 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007884# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7885 cleanup_T cs;
7886
7887 /* Reset the error/interrupt/exception state here so that
7888 * aborting() returns FALSE when closing a window. */
7889 enter_cleanup(&cs);
7890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# ifdef FEAT_GUI
7892 need_mouse_correct = TRUE;
7893# endif
7894 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007895
7896# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7897 /* Restore the error/interrupt/exception state if not
7898 * discarded by a new aborting error, interrupt, or
7899 * uncaught exception. */
7900 leave_cleanup(&cs);
7901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903 }
7904#endif
7905 }
7906 else if (readonlymode && curbuf->b_nwindows == 1)
7907 {
7908 /* When editing an already visited buffer, 'readonly' won't be set
7909 * but the previous value is kept. With ":view" and ":sview" we
7910 * want the file to be readonly, except when another window is
7911 * editing the same buffer. */
7912 curbuf->b_p_ro = TRUE;
7913 }
7914 readonlymode = n;
7915 }
7916 else
7917 {
7918 if (eap->do_ecmd_cmd != NULL)
7919 do_cmdline_cmd(eap->do_ecmd_cmd);
7920#ifdef FEAT_TITLE
7921 n = curwin->w_arg_idx_invalid;
7922#endif
7923 check_arg_idx(curwin);
7924#ifdef FEAT_TITLE
7925 if (n != curwin->w_arg_idx_invalid)
7926 maketitle();
7927#endif
7928 }
7929
7930#ifdef FEAT_WINDOWS
7931 /*
7932 * if ":split file" worked, set alternate file name in old window to new
7933 * file
7934 */
7935 if (old_curwin != NULL
7936 && *eap->arg != NUL
7937 && curwin != old_curwin
7938 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007939 && old_curwin->w_buffer != curbuf
7940 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 old_curwin->w_alt_fnum = curbuf->b_fnum;
7942#endif
7943
7944 ex_no_reprint = TRUE;
7945}
7946
7947#ifndef FEAT_GUI
7948/*
7949 * ":gui" and ":gvim" when there is no GUI.
7950 */
7951 static void
7952ex_nogui(eap)
7953 exarg_T *eap;
7954{
7955 eap->errmsg = e_nogvim;
7956}
7957#endif
7958
7959#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7960 static void
7961ex_tearoff(eap)
7962 exarg_T *eap;
7963{
7964 gui_make_tearoff(eap->arg);
7965}
7966#endif
7967
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007968#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 static void
7970ex_popup(eap)
7971 exarg_T *eap;
7972{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007973 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974}
7975#endif
7976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 static void
7978ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007979 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7982 MSG(_("No swap file"));
7983 else
7984 msg(curbuf->b_ml.ml_mfp->mf_fname);
7985}
7986
7987/*
7988 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7989 * offset.
7990 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 static void
7993ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007994 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
7996#ifdef FEAT_SCROLLBIND
7997 win_T *wp;
7998 long topline;
7999 long y;
8000 linenr_T old_linenr = curwin->w_cursor.lnum;
8001
8002 setpcmark();
8003
8004 /*
8005 * determine max topline
8006 */
8007 if (curwin->w_p_scb)
8008 {
8009 topline = curwin->w_topline;
8010 for (wp = firstwin; wp; wp = wp->w_next)
8011 {
8012 if (wp->w_p_scb && wp->w_buffer)
8013 {
8014 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8015 if (topline > y)
8016 topline = y;
8017 }
8018 }
8019 if (topline < 1)
8020 topline = 1;
8021 }
8022 else
8023 {
8024 topline = 1;
8025 }
8026
8027
8028 /*
8029 * set all scrollbind windows to the same topline
8030 */
8031 wp = curwin;
8032 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8033 {
8034 if (curwin->w_p_scb)
8035 {
8036 y = topline - curwin->w_topline;
8037 if (y > 0)
8038 scrollup(y, TRUE);
8039 else
8040 scrolldown(-y, TRUE);
8041 curwin->w_scbind_pos = topline;
8042 redraw_later(VALID);
8043 cursor_correct();
8044#ifdef FEAT_WINDOWS
8045 curwin->w_redr_status = TRUE;
8046#endif
8047 }
8048 }
8049 curwin = wp;
8050 if (curwin->w_p_scb)
8051 {
8052 did_syncbind = TRUE;
8053 checkpcmark();
8054 if (old_linenr != curwin->w_cursor.lnum)
8055 {
8056 char_u ctrl_o[2];
8057
8058 ctrl_o[0] = Ctrl_O;
8059 ctrl_o[1] = 0;
8060 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8061 }
8062 }
8063#endif
8064}
8065
8066
8067 static void
8068ex_read(eap)
8069 exarg_T *eap;
8070{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008071 int i;
8072 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8073 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074
8075 if (eap->usefilter) /* :r!cmd */
8076 do_bang(1, eap, FALSE, FALSE, TRUE);
8077 else
8078 {
8079 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8080 return;
8081
8082#ifdef FEAT_BROWSE
8083 if (cmdmod.browse)
8084 {
8085 char_u *browseFile;
8086
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008087 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 NULL, NULL, NULL, curbuf);
8089 if (browseFile != NULL)
8090 {
8091 i = readfile(browseFile, NULL,
8092 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8093 vim_free(browseFile);
8094 }
8095 else
8096 i = OK;
8097 }
8098 else
8099#endif
8100 if (*eap->arg == NUL)
8101 {
8102 if (check_fname() == FAIL) /* check for no file name */
8103 return;
8104 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8105 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8106 }
8107 else
8108 {
8109 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8110 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8111 i = readfile(eap->arg, NULL,
8112 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8113
8114 }
8115 if (i == FAIL)
8116 {
8117#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8118 if (!aborting())
8119#endif
8120 EMSG2(_(e_notopen), eap->arg);
8121 }
8122 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008123 {
8124 if (empty && exmode_active)
8125 {
8126 /* Delete the empty line that remains. Historically ex does
8127 * this but vi doesn't. */
8128 if (eap->line2 == 0)
8129 lnum = curbuf->b_ml.ml_line_count;
8130 else
8131 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008132 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008133 {
8134 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008135 if (curwin->w_cursor.lnum > 1
8136 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008137 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008138 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008139 }
8140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
8144}
8145
Bram Moolenaarea408852005-06-25 22:49:46 +00008146static char_u *prev_dir = NULL;
8147
8148#if defined(EXITFREE) || defined(PROTO)
8149 void
8150free_cd_dir()
8151{
8152 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008153 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008154
8155 vim_free(globaldir);
8156 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008157}
8158#endif
8159
8160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161/*
8162 * ":cd", ":lcd", ":chdir" and ":lchdir".
8163 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008164 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165ex_cd(eap)
8166 exarg_T *eap;
8167{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 char_u *new_dir;
8169 char_u *tofree;
8170
8171 new_dir = eap->arg;
8172#if !defined(UNIX) && !defined(VMS)
8173 /* for non-UNIX ":cd" means: print current directory */
8174 if (*new_dir == NUL)
8175 ex_pwd(NULL);
8176 else
8177#endif
8178 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008179#ifdef FEAT_AUTOCMD
8180 if (allbuf_locked())
8181 return;
8182#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008183 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8184 && !eap->forceit)
8185 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008186 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008187 return;
8188 }
8189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 /* ":cd -": Change to previous directory */
8191 if (STRCMP(new_dir, "-") == 0)
8192 {
8193 if (prev_dir == NULL)
8194 {
8195 EMSG(_("E186: No previous directory"));
8196 return;
8197 }
8198 new_dir = prev_dir;
8199 }
8200
8201 /* Save current directory for next ":cd -" */
8202 tofree = prev_dir;
8203 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8204 prev_dir = vim_strsave(NameBuff);
8205 else
8206 prev_dir = NULL;
8207
8208#if defined(UNIX) || defined(VMS)
8209 /* for UNIX ":cd" means: go to home directory */
8210 if (*new_dir == NUL)
8211 {
8212 /* use NameBuff for home directory name */
8213# ifdef VMS
8214 char_u *p;
8215
8216 p = mch_getenv((char_u *)"SYS$LOGIN");
8217 if (p == NULL || *p == NUL) /* empty is the same as not set */
8218 NameBuff[0] = NUL;
8219 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008220 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221# else
8222 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8223# endif
8224 new_dir = NameBuff;
8225 }
8226#endif
8227 if (new_dir == NULL || vim_chdir(new_dir))
8228 EMSG(_(e_failed));
8229 else
8230 {
8231 vim_free(curwin->w_localdir);
8232 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8233 {
8234 /* If still in global directory, need to remember current
8235 * directory as global directory. */
8236 if (globaldir == NULL && prev_dir != NULL)
8237 globaldir = vim_strsave(prev_dir);
8238 /* Remember this local directory for the window. */
8239 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8240 curwin->w_localdir = vim_strsave(NameBuff);
8241 }
8242 else
8243 {
8244 /* We are now in the global directory, no need to remember its
8245 * name. */
8246 vim_free(globaldir);
8247 globaldir = NULL;
8248 curwin->w_localdir = NULL;
8249 }
8250
8251 shorten_fnames(TRUE);
8252
8253 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008254 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 ex_pwd(eap);
8256 }
8257 vim_free(tofree);
8258 }
8259}
8260
8261/*
8262 * ":pwd".
8263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 static void
8265ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008266 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267{
8268 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8269 {
8270#ifdef BACKSLASH_IN_FILENAME
8271 slash_adjust(NameBuff);
8272#endif
8273 msg(NameBuff);
8274 }
8275 else
8276 EMSG(_("E187: Unknown"));
8277}
8278
8279/*
8280 * ":=".
8281 */
8282 static void
8283ex_equal(eap)
8284 exarg_T *eap;
8285{
8286 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008287 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288}
8289
8290 static void
8291ex_sleep(eap)
8292 exarg_T *eap;
8293{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008294 int n;
8295 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296
8297 if (cursor_valid())
8298 {
8299 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8300 if (n >= 0)
8301 windgoto((int)n, curwin->w_wcol);
8302 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008303
8304 len = eap->line2;
8305 switch (*eap->arg)
8306 {
8307 case 'm': break;
8308 case NUL: len *= 1000L; break;
8309 default: EMSG2(_(e_invarg2), eap->arg); return;
8310 }
8311 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312}
8313
8314/*
8315 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8316 */
8317 void
8318do_sleep(msec)
8319 long msec;
8320{
8321 long done;
8322
8323 cursor_on();
8324 out_flush();
8325 for (done = 0; !got_int && done < msec; done += 1000L)
8326 {
8327 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8328 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008329#ifdef FEAT_NETBEANS_INTG
8330 /* Process the netbeans messages that may have been received in the
8331 * call to ui_breakcheck() when the GUI is in use. This may occur when
8332 * running a test case. */
8333 netbeans_parse_messages();
8334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336}
8337
8338 static void
8339do_exmap(eap, isabbrev)
8340 exarg_T *eap;
8341 int isabbrev;
8342{
8343 int mode;
8344 char_u *cmdp;
8345
8346 cmdp = eap->cmd;
8347 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8348
8349 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8350 eap->arg, mode, isabbrev))
8351 {
8352 case 1: EMSG(_(e_invarg));
8353 break;
8354 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8355 break;
8356 }
8357}
8358
8359/*
8360 * ":winsize" command (obsolete).
8361 */
8362 static void
8363ex_winsize(eap)
8364 exarg_T *eap;
8365{
8366 int w, h;
8367 char_u *arg = eap->arg;
8368 char_u *p;
8369
8370 w = getdigits(&arg);
8371 arg = skipwhite(arg);
8372 p = arg;
8373 h = getdigits(&arg);
8374 if (*p != NUL && *arg == NUL)
8375 set_shellsize(w, h, TRUE);
8376 else
8377 EMSG(_("E465: :winsize requires two number arguments"));
8378}
8379
8380#ifdef FEAT_WINDOWS
8381 static void
8382ex_wincmd(eap)
8383 exarg_T *eap;
8384{
8385 int xchar = NUL;
8386 char_u *p;
8387
8388 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8389 {
8390 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8391 if (eap->arg[1] == NUL)
8392 {
8393 EMSG(_(e_invarg));
8394 return;
8395 }
8396 xchar = eap->arg[1];
8397 p = eap->arg + 2;
8398 }
8399 else
8400 p = eap->arg + 1;
8401
8402 eap->nextcmd = check_nextcmd(p);
8403 p = skipwhite(p);
8404 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8405 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008406 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {
8408 /* Pass flags on for ":vertical wincmd ]". */
8409 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008410 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8412 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008413 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415}
8416#endif
8417
Bram Moolenaar843ee412004-06-30 16:16:41 +00008418#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419/*
8420 * ":winpos".
8421 */
8422 static void
8423ex_winpos(eap)
8424 exarg_T *eap;
8425{
8426 int x, y;
8427 char_u *arg = eap->arg;
8428 char_u *p;
8429
8430 if (*arg == NUL)
8431 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008432# if defined(FEAT_GUI) || defined(MSWIN)
8433# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008435# else
8436 if (mch_get_winpos(&x, &y) != FAIL)
8437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
8439 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8440 msg(IObuff);
8441 }
8442 else
8443# endif
8444 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8445 }
8446 else
8447 {
8448 x = getdigits(&arg);
8449 arg = skipwhite(arg);
8450 p = arg;
8451 y = getdigits(&arg);
8452 if (*p == NUL || *arg != NUL)
8453 {
8454 EMSG(_("E466: :winpos requires two number arguments"));
8455 return;
8456 }
8457# ifdef FEAT_GUI
8458 if (gui.in_use)
8459 gui_mch_set_winpos(x, y);
8460 else if (gui.starting)
8461 {
8462 /* Remember the coordinates for when the window is opened. */
8463 gui_win_x = x;
8464 gui_win_y = y;
8465 }
8466# ifdef HAVE_TGETENT
8467 else
8468# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008469# else
8470# ifdef MSWIN
8471 mch_set_winpos(x, y);
8472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473# endif
8474# ifdef HAVE_TGETENT
8475 if (*T_CWP)
8476 term_set_winpos(x, y);
8477# endif
8478 }
8479}
8480#endif
8481
8482/*
8483 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8484 */
8485 static void
8486ex_operators(eap)
8487 exarg_T *eap;
8488{
8489 oparg_T oa;
8490
8491 clear_oparg(&oa);
8492 oa.regname = eap->regname;
8493 oa.start.lnum = eap->line1;
8494 oa.end.lnum = eap->line2;
8495 oa.line_count = eap->line2 - eap->line1 + 1;
8496 oa.motion_type = MLINE;
8497#ifdef FEAT_VIRTUALEDIT
8498 virtual_op = FALSE;
8499#endif
8500 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8501 {
8502 setpcmark();
8503 curwin->w_cursor.lnum = eap->line1;
8504 beginline(BL_SOL | BL_FIX);
8505 }
8506
8507 switch (eap->cmdidx)
8508 {
8509 case CMD_delete:
8510 oa.op_type = OP_DELETE;
8511 op_delete(&oa);
8512 break;
8513
8514 case CMD_yank:
8515 oa.op_type = OP_YANK;
8516 (void)op_yank(&oa, FALSE, TRUE);
8517 break;
8518
8519 default: /* CMD_rshift or CMD_lshift */
8520 if ((eap->cmdidx == CMD_rshift)
8521#ifdef FEAT_RIGHTLEFT
8522 ^ curwin->w_p_rl
8523#endif
8524 )
8525 oa.op_type = OP_RSHIFT;
8526 else
8527 oa.op_type = OP_LSHIFT;
8528 op_shift(&oa, FALSE, eap->amount);
8529 break;
8530 }
8531#ifdef FEAT_VIRTUALEDIT
8532 virtual_op = MAYBE;
8533#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008534 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535}
8536
8537/*
8538 * ":put".
8539 */
8540 static void
8541ex_put(eap)
8542 exarg_T *eap;
8543{
8544 /* ":0put" works like ":1put!". */
8545 if (eap->line2 == 0)
8546 {
8547 eap->line2 = 1;
8548 eap->forceit = TRUE;
8549 }
8550 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008551 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8552 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
8556 * Handle ":copy" and ":move".
8557 */
8558 static void
8559ex_copymove(eap)
8560 exarg_T *eap;
8561{
8562 long n;
8563
8564 n = get_address(&eap->arg, FALSE, FALSE);
8565 if (eap->arg == NULL) /* error detected */
8566 {
8567 eap->nextcmd = NULL;
8568 return;
8569 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008570 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
8572 /*
8573 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8574 */
8575 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8576 {
8577 EMSG(_(e_invaddr));
8578 return;
8579 }
8580
8581 if (eap->cmdidx == CMD_move)
8582 {
8583 if (do_move(eap->line1, eap->line2, n) == FAIL)
8584 return;
8585 }
8586 else
8587 ex_copy(eap->line1, eap->line2, n);
8588 u_clearline();
8589 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008590 ex_may_print(eap);
8591}
8592
8593/*
8594 * Print the current line if flags were given to the Ex command.
8595 */
8596 static void
8597ex_may_print(eap)
8598 exarg_T *eap;
8599{
8600 if (eap->flags != 0)
8601 {
8602 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8603 (eap->flags & EXFLAG_LIST));
8604 ex_no_reprint = TRUE;
8605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606}
8607
8608/*
8609 * ":smagic" and ":snomagic".
8610 */
8611 static void
8612ex_submagic(eap)
8613 exarg_T *eap;
8614{
8615 int magic_save = p_magic;
8616
8617 p_magic = (eap->cmdidx == CMD_smagic);
8618 do_sub(eap);
8619 p_magic = magic_save;
8620}
8621
8622/*
8623 * ":join".
8624 */
8625 static void
8626ex_join(eap)
8627 exarg_T *eap;
8628{
8629 curwin->w_cursor.lnum = eap->line1;
8630 if (eap->line1 == eap->line2)
8631 {
8632 if (eap->addr_count >= 2) /* :2,2join does nothing */
8633 return;
8634 if (eap->line2 == curbuf->b_ml.ml_line_count)
8635 {
8636 beep_flush();
8637 return;
8638 }
8639 ++eap->line2;
8640 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008641 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008643 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645
8646/*
8647 * ":[addr]@r" or ":[addr]*r": execute register
8648 */
8649 static void
8650ex_at(eap)
8651 exarg_T *eap;
8652{
8653 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008654 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
8656 curwin->w_cursor.lnum = eap->line2;
8657
8658#ifdef USE_ON_FLY_SCROLL
8659 dont_scroll = TRUE; /* disallow scrolling here */
8660#endif
8661
8662 /* get the register name. No name means to use the previous one */
8663 c = *eap->arg;
8664 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8665 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008666 /* Put the register in the typeahead buffer with the "silent" flag. */
8667 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8668 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 else
8673 {
8674 int save_efr = exec_from_reg;
8675
8676 exec_from_reg = TRUE;
8677
8678 /*
8679 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008680 * Continue until the stuff buffer is empty and all added characters
8681 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008683 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8685
8686 exec_from_reg = save_efr;
8687 }
8688}
8689
8690/*
8691 * ":!".
8692 */
8693 static void
8694ex_bang(eap)
8695 exarg_T *eap;
8696{
8697 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8698}
8699
8700/*
8701 * ":undo".
8702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 static void
8704ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008705 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008707 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008708 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008709 else
8710 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711}
8712
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008713#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008714 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008715ex_wundo(eap)
8716 exarg_T *eap;
8717{
8718 char_u hash[UNDO_HASH_SIZE];
8719
8720 u_compute_hash(hash);
8721 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8722}
8723
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008724 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008725ex_rundo(eap)
8726 exarg_T *eap;
8727{
8728 char_u hash[UNDO_HASH_SIZE];
8729
8730 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008731 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008732}
8733#endif
8734
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735/*
8736 * ":redo".
8737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 static void
8739ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008740 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
8742 u_redo(1);
8743}
8744
8745/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008746 * ":earlier" and ":later".
8747 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008748 static void
8749ex_later(eap)
8750 exarg_T *eap;
8751{
8752 long count = 0;
8753 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008754 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008755 char_u *p = eap->arg;
8756
8757 if (*p == NUL)
8758 count = 1;
8759 else if (isdigit(*p))
8760 {
8761 count = getdigits(&p);
8762 switch (*p)
8763 {
8764 case 's': ++p; sec = TRUE; break;
8765 case 'm': ++p; sec = TRUE; count *= 60; break;
8766 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008767 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8768 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008769 }
8770 }
8771
8772 if (*p != NUL)
8773 EMSG2(_(e_invarg2), eap->arg);
8774 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008775 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8776 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008777}
8778
8779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 * ":redir": start/stop redirection.
8781 */
8782 static void
8783ex_redir(eap)
8784 exarg_T *eap;
8785{
8786 char *mode;
8787 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008788 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
8790 if (STRICMP(eap->arg, "END") == 0)
8791 close_redir();
8792 else
8793 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008794 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008796 ++arg;
8797 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008799 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 mode = "a";
8801 }
8802 else
8803 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008804 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
8806 close_redir();
8807
8808 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008809 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (fname == NULL)
8811 return;
8812#ifdef FEAT_BROWSE
8813 if (cmdmod.browse)
8814 {
8815 char_u *browseFile;
8816
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008817 browseFile = do_browse(BROWSE_SAVE,
8818 (char_u *)_("Save Redirection"),
8819 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 if (browseFile == NULL)
8821 return; /* operation cancelled */
8822 vim_free(fname);
8823 fname = browseFile;
8824 eap->forceit = TRUE; /* since dialog already asked */
8825 }
8826#endif
8827
8828 redir_fd = open_exfile(fname, eap->forceit, mode);
8829 vim_free(fname);
8830 }
8831#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008832 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {
8834 /* redirect to a register a-z (resp. A-Z for appending) */
8835 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008836 ++arg;
8837 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008839 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008840 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008842 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008844 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008845 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008846 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008849 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008850 if (*arg == '>')
8851 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008852 /* Make register empty when not using @A-@Z and the
8853 * command is valid. */
8854 if (*arg == NUL && !isupper(redir_reg))
8855 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008857 }
8858 if (*arg != NUL)
8859 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008860 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008861 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008862 }
8863 }
8864 else if (*arg == '=' && arg[1] == '>')
8865 {
8866 int append;
8867
8868 /* redirect to a variable */
8869 close_redir();
8870 arg += 2;
8871
8872 if (*arg == '>')
8873 {
8874 ++arg;
8875 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 }
8877 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008878 append = FALSE;
8879
8880 if (var_redir_start(skipwhite(arg), append) == OK)
8881 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 }
8883#endif
8884
8885 /* TODO: redirect to a buffer */
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008888 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008890
8891 /* Make sure redirection is not off. Can happen for cmdline completion
8892 * that indirectly invokes a command to catch its output. */
8893 if (redir_fd != NULL
8894#ifdef FEAT_EVAL
8895 || redir_reg || redir_vname
8896#endif
8897 )
8898 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899}
8900
8901/*
8902 * ":redraw": force redraw
8903 */
8904 static void
8905ex_redraw(eap)
8906 exarg_T *eap;
8907{
8908 int r = RedrawingDisabled;
8909 int p = p_lz;
8910
8911 RedrawingDisabled = 0;
8912 p_lz = FALSE;
8913 update_topline();
8914 update_screen(eap->forceit ? CLEAR :
8915#ifdef FEAT_VISUAL
8916 VIsual_active ? INVERTED :
8917#endif
8918 0);
8919#ifdef FEAT_TITLE
8920 if (need_maketitle)
8921 maketitle();
8922#endif
8923 RedrawingDisabled = r;
8924 p_lz = p;
8925
8926 /* Reset msg_didout, so that a message that's there is overwritten. */
8927 msg_didout = FALSE;
8928 msg_col = 0;
8929
8930 out_flush();
8931}
8932
8933/*
8934 * ":redrawstatus": force redraw of status line(s)
8935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 static void
8937ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008938 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940#if defined(FEAT_WINDOWS)
8941 int r = RedrawingDisabled;
8942 int p = p_lz;
8943
8944 RedrawingDisabled = 0;
8945 p_lz = FALSE;
8946 if (eap->forceit)
8947 status_redraw_all();
8948 else
8949 status_redraw_curbuf();
8950 update_screen(
8951# ifdef FEAT_VISUAL
8952 VIsual_active ? INVERTED :
8953# endif
8954 0);
8955 RedrawingDisabled = r;
8956 p_lz = p;
8957 out_flush();
8958#endif
8959}
8960
8961 static void
8962close_redir()
8963{
8964 if (redir_fd != NULL)
8965 {
8966 fclose(redir_fd);
8967 redir_fd = NULL;
8968 }
8969#ifdef FEAT_EVAL
8970 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008971 if (redir_vname)
8972 {
8973 var_redir_stop();
8974 redir_vname = 0;
8975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976#endif
8977}
8978
8979#if defined(FEAT_SESSION) && defined(USE_CRNL)
8980# define MKSESSION_NL
8981static int mksession_nl = FALSE; /* use NL only in put_eol() */
8982#endif
8983
8984/*
8985 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8986 */
8987 static void
8988ex_mkrc(eap)
8989 exarg_T *eap;
8990{
8991 FILE *fd;
8992 int failed = FALSE;
8993 char_u *fname;
8994#ifdef FEAT_BROWSE
8995 char_u *browseFile = NULL;
8996#endif
8997#ifdef FEAT_SESSION
8998 int view_session = FALSE;
8999 int using_vdir = FALSE; /* using 'viewdir'? */
9000 char_u *viewFile = NULL;
9001 unsigned *flagp;
9002#endif
9003
9004 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9005 {
9006#ifdef FEAT_SESSION
9007 view_session = TRUE;
9008#else
9009 ex_ni(eap);
9010 return;
9011#endif
9012 }
9013
9014#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009015 /* Use the short file name until ":lcd" is used. We also don't use the
9016 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009017 did_lcd = FALSE;
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9020 if (eap->cmdidx == CMD_mkview
9021 && (*eap->arg == NUL
9022 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9023 {
9024 eap->forceit = TRUE;
9025 fname = get_view_file(*eap->arg);
9026 if (fname == NULL)
9027 return;
9028 viewFile = fname;
9029 using_vdir = TRUE;
9030 }
9031 else
9032#endif
9033 if (*eap->arg != NUL)
9034 fname = eap->arg;
9035 else if (eap->cmdidx == CMD_mkvimrc)
9036 fname = (char_u *)VIMRC_FILE;
9037#ifdef FEAT_SESSION
9038 else if (eap->cmdidx == CMD_mksession)
9039 fname = (char_u *)SESSION_FILE;
9040#endif
9041 else
9042 fname = (char_u *)EXRC_FILE;
9043
9044#ifdef FEAT_BROWSE
9045 if (cmdmod.browse)
9046 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009047 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048# ifdef FEAT_SESSION
9049 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9050 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9051# endif
9052 (char_u *)_("Save Setup"),
9053 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9054 if (browseFile == NULL)
9055 goto theend;
9056 fname = browseFile;
9057 eap->forceit = TRUE; /* since dialog already asked */
9058 }
9059#endif
9060
9061#if defined(FEAT_SESSION) && defined(vim_mkdir)
9062 /* When using 'viewdir' may have to create the directory. */
9063 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009064 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065#endif
9066
9067 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9068 if (fd != NULL)
9069 {
9070#ifdef FEAT_SESSION
9071 if (eap->cmdidx == CMD_mkview)
9072 flagp = &vop_flags;
9073 else
9074 flagp = &ssop_flags;
9075#endif
9076
9077#ifdef MKSESSION_NL
9078 /* "unix" in 'sessionoptions': use NL line separator */
9079 if (view_session && (*flagp & SSOP_UNIX))
9080 mksession_nl = TRUE;
9081#endif
9082
9083 /* Write the version command for :mkvimrc */
9084 if (eap->cmdidx == CMD_mkvimrc)
9085 (void)put_line(fd, "version 6.0");
9086
9087#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009088 if (eap->cmdidx == CMD_mksession)
9089 {
9090 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9091 failed = TRUE;
9092 }
9093
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 if (eap->cmdidx != CMD_mkview)
9095#endif
9096 {
9097 /* Write setting 'compatible' first, because it has side effects.
9098 * For that same reason only do it when needed. */
9099 if (p_cp)
9100 (void)put_line(fd, "if !&cp | set cp | endif");
9101 else
9102 (void)put_line(fd, "if &cp | set nocp | endif");
9103 }
9104
9105#ifdef FEAT_SESSION
9106 if (!view_session
9107 || (eap->cmdidx == CMD_mksession
9108 && (*flagp & SSOP_OPTIONS)))
9109#endif
9110 failed |= (makemap(fd, NULL) == FAIL
9111 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9112
9113#ifdef FEAT_SESSION
9114 if (!failed && view_session)
9115 {
9116 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9117 failed = TRUE;
9118 if (eap->cmdidx == CMD_mksession)
9119 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009120 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
Bram Moolenaard9462e32011-04-11 21:35:11 +02009122 dirnow = alloc(MAXPATHL);
9123 if (dirnow == NULL)
9124 failed = TRUE;
9125 else
9126 {
9127 /*
9128 * Change to session file's dir.
9129 */
9130 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009132 *dirnow = NUL;
9133 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9134 {
9135 if (vim_chdirfile(fname) == OK)
9136 shorten_fnames(TRUE);
9137 }
9138 else if (*dirnow != NUL
9139 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9140 {
9141 if (mch_chdir((char *)globaldir) == 0)
9142 shorten_fnames(TRUE);
9143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
Bram Moolenaard9462e32011-04-11 21:35:11 +02009145 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146
Bram Moolenaard9462e32011-04-11 21:35:11 +02009147 /* restore original dir */
9148 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009150 {
9151 if (mch_chdir((char *)dirnow) != 0)
9152 EMSG(_(e_prev_dir));
9153 shorten_fnames(TRUE);
9154 }
9155 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 }
9157 }
9158 else
9159 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009160 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9161 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9164 == FAIL)
9165 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009166 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9167 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009168 if (eap->cmdidx == CMD_mksession)
9169 {
9170 if (put_line(fd, "unlet SessionLoad") == FAIL)
9171 failed = TRUE;
9172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
9174#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009175 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9176 failed = TRUE;
9177
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 failed |= fclose(fd);
9179
9180 if (failed)
9181 EMSG(_(e_write));
9182#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9183 else if (eap->cmdidx == CMD_mksession)
9184 {
9185 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009186 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187
Bram Moolenaard9462e32011-04-11 21:35:11 +02009188 tbuf = alloc(MAXPATHL);
9189 if (tbuf != NULL)
9190 {
9191 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9192 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9193 vim_free(tbuf);
9194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
9196#endif
9197#ifdef MKSESSION_NL
9198 mksession_nl = FALSE;
9199#endif
9200 }
9201
9202#ifdef FEAT_BROWSE
9203theend:
9204 vim_free(browseFile);
9205#endif
9206#ifdef FEAT_SESSION
9207 vim_free(viewFile);
9208#endif
9209}
9210
Bram Moolenaardf177f62005-02-22 08:39:57 +00009211#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9212 || defined(PROTO)
9213 int
9214vim_mkdir_emsg(name, prot)
9215 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009216 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009217{
9218 if (vim_mkdir(name, prot) != 0)
9219 {
9220 EMSG2(_("E739: Cannot create directory: %s"), name);
9221 return FAIL;
9222 }
9223 return OK;
9224}
9225#endif
9226
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227/*
9228 * Open a file for writing for an Ex command, with some checks.
9229 * Return file descriptor, or NULL on failure.
9230 */
9231 FILE *
9232open_exfile(fname, forceit, mode)
9233 char_u *fname;
9234 int forceit;
9235 char *mode; /* "w" for create new file or "a" for append */
9236{
9237 FILE *fd;
9238
9239#ifdef UNIX
9240 /* with Unix it is possible to open a directory */
9241 if (mch_isdir(fname))
9242 {
9243 EMSG2(_(e_isadir2), fname);
9244 return NULL;
9245 }
9246#endif
9247 if (!forceit && *mode != 'a' && vim_fexists(fname))
9248 {
9249 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9250 return NULL;
9251 }
9252
9253 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9254 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9255
9256 return fd;
9257}
9258
9259/*
9260 * ":mark" and ":k".
9261 */
9262 static void
9263ex_mark(eap)
9264 exarg_T *eap;
9265{
9266 pos_T pos;
9267
9268 if (*eap->arg == NUL) /* No argument? */
9269 EMSG(_(e_argreq));
9270 else if (eap->arg[1] != NUL) /* more than one character? */
9271 EMSG(_(e_trailing));
9272 else
9273 {
9274 pos = curwin->w_cursor; /* save curwin->w_cursor */
9275 curwin->w_cursor.lnum = eap->line2;
9276 beginline(BL_WHITE | BL_FIX);
9277 if (setmark(*eap->arg) == FAIL) /* set mark */
9278 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9279 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9280 }
9281}
9282
9283/*
9284 * Update w_topline, w_leftcol and the cursor position.
9285 */
9286 void
9287update_topline_cursor()
9288{
9289 check_cursor(); /* put cursor on valid line */
9290 update_topline();
9291 if (!curwin->w_p_wrap)
9292 validate_cursor();
9293 update_curswant();
9294}
9295
9296#ifdef FEAT_EX_EXTRA
9297/*
9298 * ":normal[!] {commands}": Execute normal mode commands.
9299 */
9300 static void
9301ex_normal(eap)
9302 exarg_T *eap;
9303{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 int save_msg_scroll = msg_scroll;
9305 int save_restart_edit = restart_edit;
9306 int save_msg_didout = msg_didout;
9307 int save_State = State;
9308 tasave_T tabuf;
9309 int save_insertmode = p_im;
9310 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009311 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312#ifdef FEAT_MBYTE
9313 char_u *arg = NULL;
9314 int l;
9315 char_u *p;
9316#endif
9317
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009318 if (ex_normal_lock > 0)
9319 {
9320 EMSG(_(e_secure));
9321 return;
9322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 if (ex_normal_busy >= p_mmd)
9324 {
9325 EMSG(_("E192: Recursive use of :normal too deep"));
9326 return;
9327 }
9328 ++ex_normal_busy;
9329
9330 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9331 restart_edit = 0; /* don't go to Insert mode */
9332 p_im = FALSE; /* don't use 'insertmode' */
9333
9334#ifdef FEAT_MBYTE
9335 /*
9336 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9337 * this for the K_SPECIAL leading byte, otherwise special keys will not
9338 * work.
9339 */
9340 if (has_mbyte)
9341 {
9342 int len = 0;
9343
9344 /* Count the number of characters to be escaped. */
9345 for (p = eap->arg; *p != NUL; ++p)
9346 {
9347# ifdef FEAT_GUI
9348 if (*p == CSI) /* leadbyte CSI */
9349 len += 2;
9350# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009351 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9353# ifdef FEAT_GUI
9354 || *p == CSI
9355# endif
9356 )
9357 len += 2;
9358 }
9359 if (len > 0)
9360 {
9361 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9362 if (arg != NULL)
9363 {
9364 len = 0;
9365 for (p = eap->arg; *p != NUL; ++p)
9366 {
9367 arg[len++] = *p;
9368# ifdef FEAT_GUI
9369 if (*p == CSI)
9370 {
9371 arg[len++] = KS_EXTRA;
9372 arg[len++] = (int)KE_CSI;
9373 }
9374# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009375 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
9377 arg[len++] = *++p;
9378 if (*p == K_SPECIAL)
9379 {
9380 arg[len++] = KS_SPECIAL;
9381 arg[len++] = KE_FILLER;
9382 }
9383# ifdef FEAT_GUI
9384 else if (*p == CSI)
9385 {
9386 arg[len++] = KS_EXTRA;
9387 arg[len++] = (int)KE_CSI;
9388 }
9389# endif
9390 }
9391 arg[len] = NUL;
9392 }
9393 }
9394 }
9395 }
9396#endif
9397
9398 /*
9399 * Save the current typeahead. This is required to allow using ":normal"
9400 * from an event handler and makes sure we don't hang when the argument
9401 * ends with half a command.
9402 */
9403 save_typeahead(&tabuf);
9404 if (tabuf.typebuf_valid)
9405 {
9406 /*
9407 * Repeat the :normal command for each line in the range. When no
9408 * range given, execute it just once, without positioning the cursor
9409 * first.
9410 */
9411 do
9412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 if (eap->addr_count != 0)
9414 {
9415 curwin->w_cursor.lnum = eap->line1++;
9416 curwin->w_cursor.col = 0;
9417 }
9418
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009419 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420#ifdef FEAT_MBYTE
9421 arg != NULL ? arg :
9422#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009423 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 }
9425 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9426 }
9427
9428 /* Might not return to the main loop when in an event handler. */
9429 update_topline_cursor();
9430
9431 /* Restore the previous typeahead. */
9432 restore_typeahead(&tabuf);
9433
9434 --ex_normal_busy;
9435 msg_scroll = save_msg_scroll;
9436 restart_edit = save_restart_edit;
9437 p_im = save_insertmode;
9438 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009439 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9441
9442 /* Restore the state (needed when called from a function executed for
9443 * 'indentexpr'). */
9444 State = save_State;
9445#ifdef FEAT_MBYTE
9446 vim_free(arg);
9447#endif
9448}
9449
9450/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009451 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 */
9453 static void
9454ex_startinsert(eap)
9455 exarg_T *eap;
9456{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009457 if (eap->forceit)
9458 {
9459 coladvance((colnr_T)MAXCOL);
9460 curwin->w_curswant = MAXCOL;
9461 curwin->w_set_curswant = FALSE;
9462 }
9463
Bram Moolenaara40c5002005-01-09 21:16:21 +00009464 /* Ignore the command when already in Insert mode. Inserting an
9465 * expression register that invokes a function can do this. */
9466 if (State & INSERT)
9467 return;
9468
Bram Moolenaar64969662005-12-14 21:59:55 +00009469 if (eap->cmdidx == CMD_startinsert)
9470 restart_edit = 'a';
9471 else if (eap->cmdidx == CMD_startreplace)
9472 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009474 restart_edit = 'V';
9475
9476 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009478 if (eap->cmdidx == CMD_startinsert)
9479 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 curwin->w_curswant = 0; /* avoid MAXCOL */
9481 }
9482}
9483
9484/*
9485 * ":stopinsert"
9486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 static void
9488ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009489 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491 restart_edit = 0;
9492 stop_insert_mode = TRUE;
9493}
9494#endif
9495
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009496#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9497/*
9498 * Execute normal mode command "cmd".
9499 * "remap" can be REMAP_NONE or REMAP_YES.
9500 */
9501 void
9502exec_normal_cmd(cmd, remap, silent)
9503 char_u *cmd;
9504 int remap;
9505 int silent;
9506{
9507 oparg_T oa;
9508
9509 /*
9510 * Stuff the argument into the typeahead buffer.
9511 * Execute normal_cmd() until there is no typeahead left.
9512 */
9513 clear_oparg(&oa);
9514 finish_op = FALSE;
9515 ins_typebuf(cmd, remap, 0, TRUE, silent);
9516 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9517 && !got_int)
9518 {
9519 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009520 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009521 }
9522}
9523#endif
9524
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525#ifdef FEAT_FIND_ID
9526 static void
9527ex_checkpath(eap)
9528 exarg_T *eap;
9529{
9530 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9531 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9532 (linenr_T)1, (linenr_T)MAXLNUM);
9533}
9534
9535#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9536/*
9537 * ":psearch"
9538 */
9539 static void
9540ex_psearch(eap)
9541 exarg_T *eap;
9542{
9543 g_do_tagpreview = p_pvh;
9544 ex_findpat(eap);
9545 g_do_tagpreview = 0;
9546}
9547#endif
9548
9549 static void
9550ex_findpat(eap)
9551 exarg_T *eap;
9552{
9553 int whole = TRUE;
9554 long n;
9555 char_u *p;
9556 int action;
9557
9558 switch (cmdnames[eap->cmdidx].cmd_name[2])
9559 {
9560 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9561 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9562 action = ACTION_GOTO;
9563 else
9564 action = ACTION_SHOW;
9565 break;
9566 case 'i': /* ":ilist" and ":dlist" */
9567 action = ACTION_SHOW_ALL;
9568 break;
9569 case 'u': /* ":ijump" and ":djump" */
9570 action = ACTION_GOTO;
9571 break;
9572 default: /* ":isplit" and ":dsplit" */
9573 action = ACTION_SPLIT;
9574 break;
9575 }
9576
9577 n = 1;
9578 if (vim_isdigit(*eap->arg)) /* get count */
9579 {
9580 n = getdigits(&eap->arg);
9581 eap->arg = skipwhite(eap->arg);
9582 }
9583 if (*eap->arg == '/') /* Match regexp, not just whole words */
9584 {
9585 whole = FALSE;
9586 ++eap->arg;
9587 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9588 if (*p)
9589 {
9590 *p++ = NUL;
9591 p = skipwhite(p);
9592
9593 /* Check for trailing illegal characters */
9594 if (!ends_excmd(*p))
9595 eap->errmsg = e_trailing;
9596 else
9597 eap->nextcmd = check_nextcmd(p);
9598 }
9599 }
9600 if (!eap->skip)
9601 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9602 whole, !eap->forceit,
9603 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9604 n, action, eap->line1, eap->line2);
9605}
9606#endif
9607
9608#ifdef FEAT_WINDOWS
9609
9610# ifdef FEAT_QUICKFIX
9611/*
9612 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9613 */
9614 static void
9615ex_ptag(eap)
9616 exarg_T *eap;
9617{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009618 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9620}
9621
9622/*
9623 * ":pedit"
9624 */
9625 static void
9626ex_pedit(eap)
9627 exarg_T *eap;
9628{
9629 win_T *curwin_save = curwin;
9630
9631 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009632 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 keep_help_flag = curwin_save->w_buffer->b_help;
9634 do_exedit(eap, NULL);
9635 keep_help_flag = FALSE;
9636 if (curwin != curwin_save && win_valid(curwin_save))
9637 {
9638 /* Return cursor to where we were */
9639 validate_cursor();
9640 redraw_later(VALID);
9641 win_enter(curwin_save, TRUE);
9642 }
9643 g_do_tagpreview = 0;
9644}
9645# endif
9646
9647/*
9648 * ":stag", ":stselect" and ":stjump".
9649 */
9650 static void
9651ex_stag(eap)
9652 exarg_T *eap;
9653{
9654 postponed_split = -1;
9655 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009656 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9658 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009659 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660}
9661#endif
9662
9663/*
9664 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9665 */
9666 static void
9667ex_tag(eap)
9668 exarg_T *eap;
9669{
9670 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9671}
9672
9673 static void
9674ex_tag_cmd(eap, name)
9675 exarg_T *eap;
9676 char_u *name;
9677{
9678 int cmd;
9679
9680 switch (name[1])
9681 {
9682 case 'j': cmd = DT_JUMP; /* ":tjump" */
9683 break;
9684 case 's': cmd = DT_SELECT; /* ":tselect" */
9685 break;
9686 case 'p': cmd = DT_PREV; /* ":tprevious" */
9687 break;
9688 case 'N': cmd = DT_PREV; /* ":tNext" */
9689 break;
9690 case 'n': cmd = DT_NEXT; /* ":tnext" */
9691 break;
9692 case 'o': cmd = DT_POP; /* ":pop" */
9693 break;
9694 case 'f': /* ":tfirst" */
9695 case 'r': cmd = DT_FIRST; /* ":trewind" */
9696 break;
9697 case 'l': cmd = DT_LAST; /* ":tlast" */
9698 break;
9699 default: /* ":tag" */
9700#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009701 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 {
9703 do_cstag(eap);
9704 return;
9705 }
9706#endif
9707 cmd = DT_TAG;
9708 break;
9709 }
9710
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009711 if (name[0] == 'l')
9712 {
9713#ifndef FEAT_QUICKFIX
9714 ex_ni(eap);
9715 return;
9716#else
9717 cmd = DT_LTAG;
9718#endif
9719 }
9720
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9722 eap->forceit, TRUE);
9723}
9724
9725/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009726 * Check "str" for starting with a special cmdline variable.
9727 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9728 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9729 */
9730 int
9731find_cmdline_var(src, usedlen)
9732 char_u *src;
9733 int *usedlen;
9734{
9735 int len;
9736 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009737 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009738 "%",
9739#define SPEC_PERC 0
9740 "#",
9741#define SPEC_HASH 1
9742 "<cword>", /* cursor word */
9743#define SPEC_CWORD 2
9744 "<cWORD>", /* cursor WORD */
9745#define SPEC_CCWORD 3
9746 "<cfile>", /* cursor path name */
9747#define SPEC_CFILE 4
9748 "<sfile>", /* ":so" file name */
9749#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009750 "<slnum>", /* ":so" file line number */
9751#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009752#ifdef FEAT_AUTOCMD
9753 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009754# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009755 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009756# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009757 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009758# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009759#endif
9760#ifdef FEAT_CLIENTSERVER
9761 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009762# ifdef FEAT_AUTOCMD
9763# define SPEC_CLIENT 10
9764# else
9765# define SPEC_CLIENT 7
9766# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009767#endif
9768 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009769
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009770 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009771 {
9772 len = (int)STRLEN(spec_str[i]);
9773 if (STRNCMP(src, spec_str[i], len) == 0)
9774 {
9775 *usedlen = len;
9776 return i;
9777 }
9778 }
9779 return -1;
9780}
9781
9782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 * Evaluate cmdline variables.
9784 *
9785 * change '%' to curbuf->b_ffname
9786 * '#' to curwin->w_altfile
9787 * '<cword>' to word under the cursor
9788 * '<cWORD>' to WORD under the cursor
9789 * '<cfile>' to path name under the cursor
9790 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009791 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 * '<afile>' to file name for autocommand
9793 * '<abuf>' to buffer number for autocommand
9794 * '<amatch>' to matching name for autocommand
9795 *
9796 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9797 * "" for error without a message) and NULL is returned.
9798 * Returns an allocated string if a valid match was found.
9799 * Returns NULL if no match was found. "usedlen" then still contains the
9800 * number of characters to skip.
9801 */
9802 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009803eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009805 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 int *usedlen; /* characters after src that are used */
9807 linenr_T *lnump; /* line number for :e command, or NULL */
9808 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009809 int *escaped; /* return value has escaped white space (can
9810 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 int i;
9813 char_u *s;
9814 char_u *result;
9815 char_u *resultbuf = NULL;
9816 int resultlen;
9817 buf_T *buf;
9818 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9819 int spec_idx;
9820#ifdef FEAT_MODIFY_FNAME
9821 int skip_mod = FALSE;
9822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824
9825 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009826 if (escaped != NULL)
9827 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
9829 /*
9830 * Check if there is something to do.
9831 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009832 spec_idx = find_cmdline_var(src, usedlen);
9833 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 {
9835 *usedlen = 1;
9836 return NULL;
9837 }
9838
9839 /*
9840 * Skip when preceded with a backslash "\%" and "\#".
9841 * Note: In "\\%" the % is also not recognized!
9842 */
9843 if (src > srcstart && src[-1] == '\\')
9844 {
9845 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009846 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 return NULL;
9848 }
9849
9850 /*
9851 * word or WORD under cursor
9852 */
9853 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9854 {
9855 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9856 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9857 if (resultlen == 0)
9858 {
9859 *errormsg = (char_u *)"";
9860 return NULL;
9861 }
9862 }
9863
9864 /*
9865 * '#': Alternate file name
9866 * '%': Current file name
9867 * File name under the cursor
9868 * File name for autocommand
9869 * and following modifiers
9870 */
9871 else
9872 {
9873 switch (spec_idx)
9874 {
9875 case SPEC_PERC: /* '%': current file */
9876 if (curbuf->b_fname == NULL)
9877 {
9878 result = (char_u *)"";
9879 valid = 0; /* Must have ":p:h" to be valid */
9880 }
9881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 break;
9884
9885 case SPEC_HASH: /* '#' or "#99": alternate file */
9886 if (src[1] == '#') /* "##": the argument list */
9887 {
9888 result = arg_all();
9889 resultbuf = result;
9890 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009891 if (escaped != NULL)
9892 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893#ifdef FEAT_MODIFY_FNAME
9894 skip_mod = TRUE;
9895#endif
9896 break;
9897 }
9898 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009899 if (*s == '<') /* "#<99" uses v:oldfiles */
9900 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 i = (int)getdigits(&s);
9902 *usedlen = (int)(s - src); /* length of what we expand */
9903
Bram Moolenaard812df62008-11-09 12:46:09 +00009904 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009906 if (*usedlen < 2)
9907 {
9908 /* Should we give an error message for #<text? */
9909 *usedlen = 1;
9910 return NULL;
9911 }
9912#ifdef FEAT_EVAL
9913 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9914 (long)i);
9915 if (result == NULL)
9916 {
9917 *errormsg = (char_u *)"";
9918 return NULL;
9919 }
9920#else
9921 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 }
9925 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009926 {
9927 buf = buflist_findnr(i);
9928 if (buf == NULL)
9929 {
9930 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9931 return NULL;
9932 }
9933 if (lnump != NULL)
9934 *lnump = ECMD_LAST;
9935 if (buf->b_fname == NULL)
9936 {
9937 result = (char_u *)"";
9938 valid = 0; /* Must have ":p:h" to be valid */
9939 }
9940 else
9941 result = buf->b_fname;
9942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 break;
9944
9945#ifdef FEAT_SEARCHPATH
9946 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009947 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 if (result == NULL)
9949 {
9950 *errormsg = (char_u *)"";
9951 return NULL;
9952 }
9953 resultbuf = result; /* remember allocated string */
9954 break;
9955#endif
9956
9957#ifdef FEAT_AUTOCMD
9958 case SPEC_AFILE: /* file name for autocommand */
9959 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009960 if (result != NULL && !autocmd_fname_full)
9961 {
9962 /* Still need to turn the fname into a full path. It is
9963 * postponed to avoid a delay when <afile> is not used. */
9964 autocmd_fname_full = TRUE;
9965 result = FullName_save(autocmd_fname, FALSE);
9966 vim_free(autocmd_fname);
9967 autocmd_fname = result;
9968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 if (result == NULL)
9970 {
9971 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9972 return NULL;
9973 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009974 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 break;
9976
9977 case SPEC_ABUF: /* buffer number for autocommand */
9978 if (autocmd_bufnr <= 0)
9979 {
9980 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9981 return NULL;
9982 }
9983 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9984 result = strbuf;
9985 break;
9986
9987 case SPEC_AMATCH: /* match name for autocommand */
9988 result = autocmd_match;
9989 if (result == NULL)
9990 {
9991 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9992 return NULL;
9993 }
9994 break;
9995
9996#endif
9997 case SPEC_SFILE: /* file name for ":so" command */
9998 result = sourcing_name;
9999 if (result == NULL)
10000 {
10001 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10002 return NULL;
10003 }
10004 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010005 case SPEC_SLNUM: /* line in file for ":so" command */
10006 if (sourcing_name == NULL || sourcing_lnum == 0)
10007 {
10008 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10009 return NULL;
10010 }
10011 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10012 result = strbuf;
10013 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#if defined(FEAT_CLIENTSERVER)
10015 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010016 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10017 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 result = strbuf;
10019 break;
10020#endif
10021 }
10022
10023 resultlen = (int)STRLEN(result); /* length of new string */
10024 if (src[*usedlen] == '<') /* remove the file name extension */
10025 {
10026 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 resultlen = (int)(s - result);
10029 }
10030#ifdef FEAT_MODIFY_FNAME
10031 else if (!skip_mod)
10032 {
10033 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10034 &resultlen);
10035 if (result == NULL)
10036 {
10037 *errormsg = (char_u *)"";
10038 return NULL;
10039 }
10040 }
10041#endif
10042 }
10043
10044 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10045 {
10046 if (valid != VALID_HEAD + VALID_PATH)
10047 /* xgettext:no-c-format */
10048 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10049 else
10050 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10051 result = NULL;
10052 }
10053 else
10054 result = vim_strnsave(result, resultlen);
10055 vim_free(resultbuf);
10056 return result;
10057}
10058
10059/*
10060 * Concatenate all files in the argument list, separated by spaces, and return
10061 * it in one allocated string.
10062 * Spaces and backslashes in the file names are escaped with a backslash.
10063 * Returns NULL when out of memory.
10064 */
10065 static char_u *
10066arg_all()
10067{
10068 int len;
10069 int idx;
10070 char_u *retval = NULL;
10071 char_u *p;
10072
10073 /*
10074 * Do this loop two times:
10075 * first time: compute the total length
10076 * second time: concatenate the names
10077 */
10078 for (;;)
10079 {
10080 len = 0;
10081 for (idx = 0; idx < ARGCOUNT; ++idx)
10082 {
10083 p = alist_name(&ARGLIST[idx]);
10084 if (p != NULL)
10085 {
10086 if (len > 0)
10087 {
10088 /* insert a space in between names */
10089 if (retval != NULL)
10090 retval[len] = ' ';
10091 ++len;
10092 }
10093 for ( ; *p != NUL; ++p)
10094 {
10095 if (*p == ' ' || *p == '\\')
10096 {
10097 /* insert a backslash */
10098 if (retval != NULL)
10099 retval[len] = '\\';
10100 ++len;
10101 }
10102 if (retval != NULL)
10103 retval[len] = *p;
10104 ++len;
10105 }
10106 }
10107 }
10108
10109 /* second time: break here */
10110 if (retval != NULL)
10111 {
10112 retval[len] = NUL;
10113 break;
10114 }
10115
10116 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010117 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 if (retval == NULL)
10119 break;
10120 }
10121
10122 return retval;
10123}
10124
10125#if defined(FEAT_AUTOCMD) || defined(PROTO)
10126/*
10127 * Expand the <sfile> string in "arg".
10128 *
10129 * Returns an allocated string, or NULL for any error.
10130 */
10131 char_u *
10132expand_sfile(arg)
10133 char_u *arg;
10134{
10135 char_u *errormsg;
10136 int len;
10137 char_u *result;
10138 char_u *newres;
10139 char_u *repl;
10140 int srclen;
10141 char_u *p;
10142
10143 result = vim_strsave(arg);
10144 if (result == NULL)
10145 return NULL;
10146
10147 for (p = result; *p; )
10148 {
10149 if (STRNCMP(p, "<sfile>", 7) != 0)
10150 ++p;
10151 else
10152 {
10153 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010154 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 if (errormsg != NULL)
10156 {
10157 if (*errormsg)
10158 emsg(errormsg);
10159 vim_free(result);
10160 return NULL;
10161 }
10162 if (repl == NULL) /* no match (cannot happen) */
10163 {
10164 p += srclen;
10165 continue;
10166 }
10167 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10168 newres = alloc(len);
10169 if (newres == NULL)
10170 {
10171 vim_free(repl);
10172 vim_free(result);
10173 return NULL;
10174 }
10175 mch_memmove(newres, result, (size_t)(p - result));
10176 STRCPY(newres + (p - result), repl);
10177 len = (int)STRLEN(newres);
10178 STRCAT(newres, p + srclen);
10179 vim_free(repl);
10180 vim_free(result);
10181 result = newres;
10182 p = newres + len; /* continue after the match */
10183 }
10184 }
10185
10186 return result;
10187}
10188#endif
10189
10190#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010191static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10192 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10194static frame_T *ses_skipframe __ARGS((frame_T *fr));
10195static int ses_do_frame __ARGS((frame_T *fr));
10196static int ses_do_win __ARGS((win_T *wp));
10197static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10198static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10199static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10200
10201/*
10202 * Write openfile commands for the current buffers to an .exrc file.
10203 * Return FAIL on error, OK otherwise.
10204 */
10205 static int
10206makeopens(fd, dirnow)
10207 FILE *fd;
10208 char_u *dirnow; /* Current directory name */
10209{
10210 buf_T *buf;
10211 int only_save_windows = TRUE;
10212 int nr;
10213 int cnr = 1;
10214 int restore_size = TRUE;
10215 win_T *wp;
10216 char_u *sname;
10217 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010218 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010219 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010220 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010221 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010222 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223
10224 if (ssop_flags & SSOP_BUFFERS)
10225 only_save_windows = FALSE; /* Save ALL buffers */
10226
10227 /*
10228 * Begin by setting the this_session variable, and then other
10229 * sessionable variables.
10230 */
10231#ifdef FEAT_EVAL
10232 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10233 return FAIL;
10234 if (ssop_flags & SSOP_GLOBALS)
10235 if (store_session_globals(fd) == FAIL)
10236 return FAIL;
10237#endif
10238
10239 /*
10240 * Close all windows but one.
10241 */
10242 if (put_line(fd, "silent only") == FAIL)
10243 return FAIL;
10244
10245 /*
10246 * Now a :cd command to the session directory or the current directory
10247 */
10248 if (ssop_flags & SSOP_SESDIR)
10249 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010250 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10251 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 return FAIL;
10253 }
10254 else if (ssop_flags & SSOP_CURDIR)
10255 {
10256 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10257 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010258 || fputs("cd ", fd) < 0
10259 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10260 || put_eol(fd) == FAIL)
10261 {
10262 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 vim_free(sname);
10266 }
10267
10268 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010269 * If there is an empty, unnamed buffer we will wipe it out later.
10270 * Remember the buffer number.
10271 */
10272 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10273 return FAIL;
10274 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10275 return FAIL;
10276 if (put_line(fd, "endif") == FAIL)
10277 return FAIL;
10278
10279 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 * Now save the current files, current buffer first.
10281 */
10282 if (put_line(fd, "set shortmess=aoO") == FAIL)
10283 return FAIL;
10284
10285 /* Now put the other buffers into the buffer list */
10286 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10287 {
10288 if (!(only_save_windows && buf->b_nwindows == 0)
10289 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10290 && buf->b_fname != NULL
10291 && buf->b_p_bl)
10292 {
10293 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10294 : buf->b_wininfo->wi_fpos.lnum) < 0
10295 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10296 return FAIL;
10297 }
10298 }
10299
10300 /* the global argument list */
10301 if (ses_arglist(fd, "args", &global_alist.al_ga,
10302 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10303 return FAIL;
10304
10305 if (ssop_flags & SSOP_RESIZE)
10306 {
10307 /* Note: after the restore we still check it worked!*/
10308 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10309 || put_eol(fd) == FAIL)
10310 return FAIL;
10311 }
10312
10313#ifdef FEAT_GUI
10314 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10315 {
10316 int x, y;
10317
10318 if (gui_mch_get_winpos(&x, &y) == OK)
10319 {
10320 /* Note: after the restore we still check it worked!*/
10321 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10322 return FAIL;
10323 }
10324 }
10325#endif
10326
10327 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010328 * May repeat putting Windows for each tab, when "tabpages" is in
10329 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010330 * Don't use goto_tabpage(), it may change directory and trigger
10331 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010333 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010334 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010335 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010337 int need_tabnew = FALSE;
10338
Bram Moolenaar18144c82006-04-12 21:52:12 +000010339 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010341 tabpage_T *tp = find_tabpage(tabnr);
10342
10343 if (tp == NULL)
10344 break; /* done all tab pages */
10345 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010346 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010347 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010348 tab_topframe = topframe;
10349 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010350 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010351 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010352 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010353 tab_topframe = tp->tp_topframe;
10354 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010355 if (tabnr > 1)
10356 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358
Bram Moolenaar18144c82006-04-12 21:52:12 +000010359 /*
10360 * Before creating the window layout, try loading one file. If this
10361 * is aborted we don't end up with a number of useless windows.
10362 * This may have side effects! (e.g., compressed or network file).
10363 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010364 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010365 {
10366 if (ses_do_win(wp)
10367 && wp->w_buffer->b_ffname != NULL
10368 && !wp->w_buffer->b_help
10369#ifdef FEAT_QUICKFIX
10370 && !bt_nofile(wp->w_buffer)
10371#endif
10372 )
10373 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010374 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010375 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10376 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010377 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010378 if (!wp->w_arg_idx_invalid)
10379 edited_win = wp;
10380 break;
10381 }
10382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010384 /* If no file got edited create an empty tab page. */
10385 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10386 return FAIL;
10387
Bram Moolenaar18144c82006-04-12 21:52:12 +000010388 /*
10389 * Save current window layout.
10390 */
10391 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010393 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010395 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10396 return FAIL;
10397 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
Bram Moolenaar18144c82006-04-12 21:52:12 +000010400 /*
10401 * Check if window sizes can be restored (no windows omitted).
10402 * Remember the window number of the current window after restoring.
10403 */
10404 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010405 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010406 {
10407 if (ses_do_win(wp))
10408 ++nr;
10409 else
10410 restore_size = FALSE;
10411 if (curwin == wp)
10412 cnr = nr;
10413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414
Bram Moolenaar18144c82006-04-12 21:52:12 +000010415 /* Go to the first window. */
10416 if (put_line(fd, "wincmd t") == FAIL)
10417 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010418
Bram Moolenaar18144c82006-04-12 21:52:12 +000010419 /*
10420 * If more than one window, see if sizes can be restored.
10421 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10422 * resized when moving between windows.
10423 * Do this before restoring the view, so that the topline and the
10424 * cursor can be set. This is done again below.
10425 */
10426 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10427 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010428 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010429 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
Bram Moolenaar18144c82006-04-12 21:52:12 +000010431 /*
10432 * Restore the view of the window (options, file, cursor, etc.).
10433 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010434 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010435 {
10436 if (!ses_do_win(wp))
10437 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010438 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10439 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010440 return FAIL;
10441 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10442 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010443 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010444 }
10445
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010446 /* The argument index in the first tab page is zero, need to set it in
10447 * each window. For further tab pages it's the window where we do
10448 * "tabedit". */
10449 cur_arg_idx = next_arg_idx;
10450
Bram Moolenaar18144c82006-04-12 21:52:12 +000010451 /*
10452 * Restore cursor to the current window if it's not the first one.
10453 */
10454 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10455 || put_eol(fd) == FAIL))
10456 return FAIL;
10457
10458 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010459 * Restore window sizes again after jumping around in windows, because
10460 * the current window has a minimum size while others may not.
10461 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010462 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 return FAIL;
10464
Bram Moolenaar18144c82006-04-12 21:52:12 +000010465 /* Don't continue in another tab page when doing only the current one
10466 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010467 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010468 break;
10469 }
10470
10471 if (ssop_flags & SSOP_TABPAGES)
10472 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010473 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10474 || put_eol(fd) == FAIL)
10475 return FAIL;
10476 }
10477
Bram Moolenaar9c102382006-05-03 21:26:49 +000010478 /*
10479 * Wipe out an empty unnamed buffer we started in.
10480 */
10481 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10482 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010483 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010484 return FAIL;
10485 if (put_line(fd, "endif") == FAIL)
10486 return FAIL;
10487 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10488 return FAIL;
10489
10490 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10491 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10492 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494
10495 /*
10496 * Lastly, execute the x.vim file if it exists.
10497 */
10498 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10499 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010500 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 || put_line(fd, "endif") == FAIL)
10502 return FAIL;
10503
10504 return OK;
10505}
10506
10507 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010508ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 FILE *fd;
10510 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010511 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512{
10513 int n = 0;
10514 win_T *wp;
10515
10516 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10517 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010518 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 {
10520 if (!ses_do_win(wp))
10521 continue;
10522 ++n;
10523
10524 /* restore height when not full height */
10525 if (wp->w_height + wp->w_status_height < topframe->fr_height
10526 && (fprintf(fd,
10527 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10528 n, (long)wp->w_height, Rows / 2, Rows) < 0
10529 || put_eol(fd) == FAIL))
10530 return FAIL;
10531
10532 /* restore width when not full width */
10533 if (wp->w_width < Columns && (fprintf(fd,
10534 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10535 n, (long)wp->w_width, Columns / 2, Columns) < 0
10536 || put_eol(fd) == FAIL))
10537 return FAIL;
10538 }
10539 }
10540 else
10541 {
10542 /* Just equalise window sizes */
10543 if (put_line(fd, "wincmd =") == FAIL)
10544 return FAIL;
10545 }
10546 return OK;
10547}
10548
10549/*
10550 * Write commands to "fd" to recursively create windows for frame "fr",
10551 * horizontally and vertically split.
10552 * After the commands the last window in the frame is the current window.
10553 * Returns FAIL when writing the commands to "fd" fails.
10554 */
10555 static int
10556ses_win_rec(fd, fr)
10557 FILE *fd;
10558 frame_T *fr;
10559{
10560 frame_T *frc;
10561 int count = 0;
10562
10563 if (fr->fr_layout != FR_LEAF)
10564 {
10565 /* Find first frame that's not skipped and then create a window for
10566 * each following one (first frame is already there). */
10567 frc = ses_skipframe(fr->fr_child);
10568 if (frc != NULL)
10569 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10570 {
10571 /* Make window as big as possible so that we have lots of room
10572 * to split. */
10573 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10574 || put_line(fd, fr->fr_layout == FR_COL
10575 ? "split" : "vsplit") == FAIL)
10576 return FAIL;
10577 ++count;
10578 }
10579
10580 /* Go back to the first window. */
10581 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10582 ? "%dwincmd k" : "%dwincmd h", count) < 0
10583 || put_eol(fd) == FAIL))
10584 return FAIL;
10585
10586 /* Recursively create frames/windows in each window of this column or
10587 * row. */
10588 frc = ses_skipframe(fr->fr_child);
10589 while (frc != NULL)
10590 {
10591 ses_win_rec(fd, frc);
10592 frc = ses_skipframe(frc->fr_next);
10593 /* Go to next window. */
10594 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10595 return FAIL;
10596 }
10597 }
10598 return OK;
10599}
10600
10601/*
10602 * Skip frames that don't contain windows we want to save in the Session.
10603 * Returns NULL when there none.
10604 */
10605 static frame_T *
10606ses_skipframe(fr)
10607 frame_T *fr;
10608{
10609 frame_T *frc;
10610
10611 for (frc = fr; frc != NULL; frc = frc->fr_next)
10612 if (ses_do_frame(frc))
10613 break;
10614 return frc;
10615}
10616
10617/*
10618 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10619 * the Session.
10620 */
10621 static int
10622ses_do_frame(fr)
10623 frame_T *fr;
10624{
10625 frame_T *frc;
10626
10627 if (fr->fr_layout == FR_LEAF)
10628 return ses_do_win(fr->fr_win);
10629 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10630 if (ses_do_frame(frc))
10631 return TRUE;
10632 return FALSE;
10633}
10634
10635/*
10636 * Return non-zero if window "wp" is to be stored in the Session.
10637 */
10638 static int
10639ses_do_win(wp)
10640 win_T *wp;
10641{
10642 if (wp->w_buffer->b_fname == NULL
10643#ifdef FEAT_QUICKFIX
10644 /* When 'buftype' is "nofile" can't restore the window contents. */
10645 || bt_nofile(wp->w_buffer)
10646#endif
10647 )
10648 return (ssop_flags & SSOP_BLANK);
10649 if (wp->w_buffer->b_help)
10650 return (ssop_flags & SSOP_HELP);
10651 return TRUE;
10652}
10653
10654/*
10655 * Write commands to "fd" to restore the view of a window.
10656 * Caller must make sure 'scrolloff' is zero.
10657 */
10658 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010659put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 FILE *fd;
10661 win_T *wp;
10662 int add_edit; /* add ":edit" command to view */
10663 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010664 int current_arg_idx; /* current argument index of the window, use
10665 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
10667 win_T *save_curwin;
10668 int f;
10669 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010670 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671
10672 /* Always restore cursor position for ":mksession". For ":mkview" only
10673 * when 'viewoptions' contains "cursor". */
10674 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10675
10676 /*
10677 * Local argument list.
10678 */
10679 if (wp->w_alist == &global_alist)
10680 {
10681 if (put_line(fd, "argglobal") == FAIL)
10682 return FAIL;
10683 }
10684 else
10685 {
10686 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10687 flagp == &vop_flags
10688 || !(*flagp & SSOP_CURDIR)
10689 || wp->w_localdir != NULL, flagp) == FAIL)
10690 return FAIL;
10691 }
10692
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010693 /* Only when part of a session: restore the argument index. Some
10694 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010695 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010696 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010698 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 || put_eol(fd) == FAIL)
10700 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010701 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 }
10703
10704 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010705 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
10707 /*
10708 * Load the file.
10709 */
10710 if (wp->w_buffer->b_ffname != NULL
10711#ifdef FEAT_QUICKFIX
10712 && !bt_nofile(wp->w_buffer)
10713#endif
10714 )
10715 {
10716 /*
10717 * Editing a file in this buffer: use ":edit file".
10718 * This may have side effects! (e.g., compressed or network file).
10719 */
10720 if (fputs("edit ", fd) < 0
10721 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10722 return FAIL;
10723 }
10724 else
10725 {
10726 /* No file in this buffer, just make it empty. */
10727 if (put_line(fd, "enew") == FAIL)
10728 return FAIL;
10729#ifdef FEAT_QUICKFIX
10730 if (wp->w_buffer->b_ffname != NULL)
10731 {
10732 /* The buffer does have a name, but it's not a file name. */
10733 if (fputs("file ", fd) < 0
10734 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10735 return FAIL;
10736 }
10737#endif
10738 do_cursor = FALSE;
10739 }
10740 }
10741
10742 /*
10743 * Local mappings and abbreviations.
10744 */
10745 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10746 && makemap(fd, wp->w_buffer) == FAIL)
10747 return FAIL;
10748
10749 /*
10750 * Local options. Need to go to the window temporarily.
10751 * Store only local values when using ":mkview" and when ":mksession" is
10752 * used and 'sessionoptions' doesn't include "options".
10753 * Some folding options are always stored when "folds" is included,
10754 * otherwise the folds would not be restored correctly.
10755 */
10756 save_curwin = curwin;
10757 curwin = wp;
10758 curbuf = curwin->w_buffer;
10759 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10760 f = makeset(fd, OPT_LOCAL,
10761 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10762#ifdef FEAT_FOLDING
10763 else if (*flagp & SSOP_FOLDS)
10764 f = makefoldset(fd);
10765#endif
10766 else
10767 f = OK;
10768 curwin = save_curwin;
10769 curbuf = curwin->w_buffer;
10770 if (f == FAIL)
10771 return FAIL;
10772
10773#ifdef FEAT_FOLDING
10774 /*
10775 * Save Folds when 'buftype' is empty and for help files.
10776 */
10777 if ((*flagp & SSOP_FOLDS)
10778 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010779# ifdef FEAT_QUICKFIX
10780 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10781# endif
10782 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 {
10784 if (put_folds(fd, wp) == FAIL)
10785 return FAIL;
10786 }
10787#endif
10788
10789 /*
10790 * Set the cursor after creating folds, since that moves the cursor.
10791 */
10792 if (do_cursor)
10793 {
10794
10795 /* Restore the cursor line in the file and relatively in the
10796 * window. Don't use "G", it changes the jumplist. */
10797 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10798 (long)wp->w_cursor.lnum,
10799 (long)(wp->w_cursor.lnum - wp->w_topline),
10800 (long)wp->w_height / 2, (long)wp->w_height) < 0
10801 || put_eol(fd) == FAIL
10802 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10803 || put_line(fd, "exe s:l") == FAIL
10804 || put_line(fd, "normal! zt") == FAIL
10805 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10806 || put_eol(fd) == FAIL)
10807 return FAIL;
10808 /* Restore the cursor column and left offset when not wrapping. */
10809 if (wp->w_cursor.col == 0)
10810 {
10811 if (put_line(fd, "normal! 0") == FAIL)
10812 return FAIL;
10813 }
10814 else
10815 {
10816 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10817 {
10818 if (fprintf(fd,
10819 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10820 (long)wp->w_cursor.col,
10821 (long)(wp->w_cursor.col - wp->w_leftcol),
10822 (long)wp->w_width / 2, (long)wp->w_width) < 0
10823 || put_eol(fd) == FAIL
10824 || put_line(fd, "if s:c > 0") == FAIL
10825 || fprintf(fd,
10826 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10827 (long)wp->w_cursor.col) < 0
10828 || put_eol(fd) == FAIL
10829 || put_line(fd, "else") == FAIL
10830 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10831 || put_eol(fd) == FAIL
10832 || put_line(fd, "endif") == FAIL)
10833 return FAIL;
10834 }
10835 else
10836 {
10837 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10838 || put_eol(fd) == FAIL)
10839 return FAIL;
10840 }
10841 }
10842 }
10843
10844 /*
10845 * Local directory.
10846 */
10847 if (wp->w_localdir != NULL)
10848 {
10849 if (fputs("lcd ", fd) < 0
10850 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10851 || put_eol(fd) == FAIL)
10852 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010853 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 }
10855
10856 return OK;
10857}
10858
10859/*
10860 * Write an argument list to the session file.
10861 * Returns FAIL if writing fails.
10862 */
10863 static int
10864ses_arglist(fd, cmd, gap, fullname, flagp)
10865 FILE *fd;
10866 char *cmd;
10867 garray_T *gap;
10868 int fullname; /* TRUE: use full path name */
10869 unsigned *flagp;
10870{
10871 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010872 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 char_u *s;
10874
10875 if (gap->ga_len == 0)
10876 return put_line(fd, "silent! argdel *");
10877 if (fputs(cmd, fd) < 0)
10878 return FAIL;
10879 for (i = 0; i < gap->ga_len; ++i)
10880 {
10881 /* NULL file names are skipped (only happens when out of memory). */
10882 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10883 if (s != NULL)
10884 {
10885 if (fullname)
10886 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010887 buf = alloc(MAXPATHL);
10888 if (buf != NULL)
10889 {
10890 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10891 s = buf;
10892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 }
10894 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010895 {
10896 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010898 }
10899 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901 }
10902 return put_eol(fd);
10903}
10904
10905/*
10906 * Write a buffer name to the session file.
10907 * Also ends the line.
10908 * Returns FAIL if writing fails.
10909 */
10910 static int
10911ses_fname(fd, buf, flagp)
10912 FILE *fd;
10913 buf_T *buf;
10914 unsigned *flagp;
10915{
10916 char_u *name;
10917
10918 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010919 * the session file will be sourced.
10920 * Don't do this for ":mkview", we don't know the current directory.
10921 * Don't do this after ":lcd", we don't keep track of what the current
10922 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 if (buf->b_sfname != NULL
10924 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010925 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010926#ifdef FEAT_AUTOCHDIR
10927 && !p_acd
10928#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010929 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 name = buf->b_sfname;
10931 else
10932 name = buf->b_ffname;
10933 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10934 return FAIL;
10935 return OK;
10936}
10937
10938/*
10939 * Write a file name to the session file.
10940 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10941 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010942 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 */
10944 static int
10945ses_put_fname(fd, name, flagp)
10946 FILE *fd;
10947 char_u *name;
10948 unsigned *flagp;
10949{
10950 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010951 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953
10954 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010955 if (sname == NULL)
10956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010958 if (*flagp & SSOP_SLASH)
10959 {
10960 /* change all backslashes to forward slashes */
10961 for (p = sname; *p != NUL; mb_ptr_adv(p))
10962 if (*p == '\\')
10963 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010965
10966 /* escapse special characters */
10967 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010969 if (p == NULL)
10970 return FAIL;
10971
10972 /* write the result */
10973 if (fputs((char *)p, fd) < 0)
10974 retval = FAIL;
10975
10976 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 return retval;
10978}
10979
10980/*
10981 * ":loadview [nr]"
10982 */
10983 static void
10984ex_loadview(eap)
10985 exarg_T *eap;
10986{
10987 char_u *fname;
10988
10989 fname = get_view_file(*eap->arg);
10990 if (fname != NULL)
10991 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010992 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 vim_free(fname);
10994 }
10995}
10996
10997/*
10998 * Get the name of the view file for the current buffer.
10999 */
11000 static char_u *
11001get_view_file(c)
11002 int c;
11003{
11004 int len = 0;
11005 char_u *p, *s;
11006 char_u *retval;
11007 char_u *sname;
11008
11009 if (curbuf->b_ffname == NULL)
11010 {
11011 EMSG(_(e_noname));
11012 return NULL;
11013 }
11014 sname = home_replace_save(NULL, curbuf->b_ffname);
11015 if (sname == NULL)
11016 return NULL;
11017
11018 /*
11019 * We want a file name without separators, because we're not going to make
11020 * a directory.
11021 * "normal" path separator -> "=+"
11022 * "=" -> "=="
11023 * ":" path separator -> "=-"
11024 */
11025 for (p = sname; *p; ++p)
11026 if (*p == '=' || vim_ispathsep(*p))
11027 ++len;
11028 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11029 if (retval != NULL)
11030 {
11031 STRCPY(retval, p_vdir);
11032 add_pathsep(retval);
11033 s = retval + STRLEN(retval);
11034 for (p = sname; *p; ++p)
11035 {
11036 if (*p == '=')
11037 {
11038 *s++ = '=';
11039 *s++ = '=';
11040 }
11041 else if (vim_ispathsep(*p))
11042 {
11043 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011044#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 if (*p == ':')
11046 *s++ = '-';
11047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011049 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 }
11051 else
11052 *s++ = *p;
11053 }
11054 *s++ = '=';
11055 *s++ = c;
11056 STRCPY(s, ".vim");
11057 }
11058
11059 vim_free(sname);
11060 return retval;
11061}
11062
11063#endif /* FEAT_SESSION */
11064
11065/*
11066 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11067 * Return FAIL for a write error.
11068 */
11069 int
11070put_eol(fd)
11071 FILE *fd;
11072{
11073 if (
11074#ifdef USE_CRNL
11075 (
11076# ifdef MKSESSION_NL
11077 !mksession_nl &&
11078# endif
11079 (putc('\r', fd) < 0)) ||
11080#endif
11081 (putc('\n', fd) < 0))
11082 return FAIL;
11083 return OK;
11084}
11085
11086/*
11087 * Write a line to "fd".
11088 * Return FAIL for a write error.
11089 */
11090 int
11091put_line(fd, s)
11092 FILE *fd;
11093 char *s;
11094{
11095 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11096 return FAIL;
11097 return OK;
11098}
11099
11100#ifdef FEAT_VIMINFO
11101/*
11102 * ":rviminfo" and ":wviminfo".
11103 */
11104 static void
11105ex_viminfo(eap)
11106 exarg_T *eap;
11107{
11108 char_u *save_viminfo;
11109
11110 save_viminfo = p_viminfo;
11111 if (*p_viminfo == NUL)
11112 p_viminfo = (char_u *)"'100";
11113 if (eap->cmdidx == CMD_rviminfo)
11114 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011115 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11116 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 EMSG(_("E195: Cannot open viminfo file for reading"));
11118 }
11119 else
11120 write_viminfo(eap->arg, eap->forceit);
11121 p_viminfo = save_viminfo;
11122}
11123#endif
11124
11125#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011126/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011127 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011128 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 void
11131dialog_msg(buff, format, fname)
11132 char_u *buff;
11133 char *format;
11134 char_u *fname;
11135{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 if (fname == NULL)
11137 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011138 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140#endif
11141
11142/*
11143 * ":behave {mswin,xterm}"
11144 */
11145 static void
11146ex_behave(eap)
11147 exarg_T *eap;
11148{
11149 if (STRCMP(eap->arg, "mswin") == 0)
11150 {
11151 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11152 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11153 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11154 set_option_value((char_u *)"keymodel", 0L,
11155 (char_u *)"startsel,stopsel", 0);
11156 }
11157 else if (STRCMP(eap->arg, "xterm") == 0)
11158 {
11159 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11160 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11161 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11162 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11163 }
11164 else
11165 EMSG2(_(e_invarg2), eap->arg);
11166}
11167
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011168#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11169/*
11170 * Function given to ExpandGeneric() to obtain the possible arguments of the
11171 * ":behave {mswin,xterm}" command.
11172 */
11173 char_u *
11174get_behave_arg(xp, idx)
11175 expand_T *xp UNUSED;
11176 int idx;
11177{
11178 if (idx == 0)
11179 return (char_u *)"mswin";
11180 if (idx == 1)
11181 return (char_u *)"xterm";
11182 return NULL;
11183}
11184#endif
11185
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186#ifdef FEAT_AUTOCMD
11187static int filetype_detect = FALSE;
11188static int filetype_plugin = FALSE;
11189static int filetype_indent = FALSE;
11190
11191/*
11192 * ":filetype [plugin] [indent] {on,off,detect}"
11193 * on: Load the filetype.vim file to install autocommands for file types.
11194 * off: Load the ftoff.vim file to remove all autocommands for file types.
11195 * plugin on: load filetype.vim and ftplugin.vim
11196 * plugin off: load ftplugof.vim
11197 * indent on: load filetype.vim and indent.vim
11198 * indent off: load indoff.vim
11199 */
11200 static void
11201ex_filetype(eap)
11202 exarg_T *eap;
11203{
11204 char_u *arg = eap->arg;
11205 int plugin = FALSE;
11206 int indent = FALSE;
11207
11208 if (*eap->arg == NUL)
11209 {
11210 /* Print current status. */
11211 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11212 filetype_detect ? "ON" : "OFF",
11213 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11214 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11215 return;
11216 }
11217
11218 /* Accept "plugin" and "indent" in any order. */
11219 for (;;)
11220 {
11221 if (STRNCMP(arg, "plugin", 6) == 0)
11222 {
11223 plugin = TRUE;
11224 arg = skipwhite(arg + 6);
11225 continue;
11226 }
11227 if (STRNCMP(arg, "indent", 6) == 0)
11228 {
11229 indent = TRUE;
11230 arg = skipwhite(arg + 6);
11231 continue;
11232 }
11233 break;
11234 }
11235 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11236 {
11237 if (*arg == 'o' || !filetype_detect)
11238 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011239 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 filetype_detect = TRUE;
11241 if (plugin)
11242 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011243 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 filetype_plugin = TRUE;
11245 }
11246 if (indent)
11247 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011248 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 filetype_indent = TRUE;
11250 }
11251 }
11252 if (*arg == 'd')
11253 {
11254 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011255 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 }
11257 }
11258 else if (STRCMP(arg, "off") == 0)
11259 {
11260 if (plugin || indent)
11261 {
11262 if (plugin)
11263 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011264 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 filetype_plugin = FALSE;
11266 }
11267 if (indent)
11268 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011269 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 filetype_indent = FALSE;
11271 }
11272 }
11273 else
11274 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011275 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 filetype_detect = FALSE;
11277 }
11278 }
11279 else
11280 EMSG2(_(e_invarg2), arg);
11281}
11282
11283/*
11284 * ":setfiletype {name}"
11285 */
11286 static void
11287ex_setfiletype(eap)
11288 exarg_T *eap;
11289{
11290 if (!did_filetype)
11291 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11292}
11293#endif
11294
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 static void
11296ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011297 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299#ifdef FEAT_DIGRAPHS
11300 if (*eap->arg != NUL)
11301 putdigraph(eap->arg);
11302 else
11303 listdigraphs();
11304#else
11305 EMSG(_("E196: No digraphs in this version"));
11306#endif
11307}
11308
11309 static void
11310ex_set(eap)
11311 exarg_T *eap;
11312{
11313 int flags = 0;
11314
11315 if (eap->cmdidx == CMD_setlocal)
11316 flags = OPT_LOCAL;
11317 else if (eap->cmdidx == CMD_setglobal)
11318 flags = OPT_GLOBAL;
11319#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11320 if (cmdmod.browse && flags == 0)
11321 ex_options(eap);
11322 else
11323#endif
11324 (void)do_set(eap->arg, flags);
11325}
11326
11327#ifdef FEAT_SEARCH_EXTRA
11328/*
11329 * ":nohlsearch"
11330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 static void
11332ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011333 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011336 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337}
11338
11339/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011340 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 * Sets nextcmd to the start of the next command, if any. Also called when
11342 * skipping commands to find the next command.
11343 */
11344 static void
11345ex_match(eap)
11346 exarg_T *eap;
11347{
11348 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011349 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 char_u *end;
11351 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011352 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011353
11354 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011355 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011356 else
11357 {
11358 EMSG(e_invcmd);
11359 return;
11360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361
11362 /* First clear any old pattern. */
11363 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011364 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
11366 if (ends_excmd(*eap->arg))
11367 end = eap->arg;
11368 else if ((STRNICMP(eap->arg, "none", 4) == 0
11369 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11370 end = eap->arg + 4;
11371 else
11372 {
11373 p = skiptowhite(eap->arg);
11374 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011375 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 p = skipwhite(p);
11377 if (*p == NUL)
11378 {
11379 /* There must be two arguments. */
11380 EMSG2(_(e_invarg2), eap->arg);
11381 return;
11382 }
11383 end = skip_regexp(p + 1, *p, TRUE, NULL);
11384 if (!eap->skip)
11385 {
11386 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11387 {
11388 eap->errmsg = e_trailing;
11389 return;
11390 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011391 if (*end != *p)
11392 {
11393 EMSG2(_(e_invarg2), p);
11394 return;
11395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
11397 c = *end;
11398 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011399 match_add(curwin, g, p + 1, 10, id);
11400 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011401 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 }
11403 }
11404 eap->nextcmd = find_nextcmd(end);
11405}
11406#endif
11407
11408#ifdef FEAT_CRYPT
11409/*
11410 * ":X": Get crypt key
11411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 static void
11413ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011414 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011416 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011417 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418}
11419#endif
11420
11421#ifdef FEAT_FOLDING
11422 static void
11423ex_fold(eap)
11424 exarg_T *eap;
11425{
11426 if (foldManualAllowed(TRUE))
11427 foldCreate(eap->line1, eap->line2);
11428}
11429
11430 static void
11431ex_foldopen(eap)
11432 exarg_T *eap;
11433{
11434 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11435 eap->forceit, FALSE);
11436}
11437
11438 static void
11439ex_folddo(eap)
11440 exarg_T *eap;
11441{
11442 linenr_T lnum;
11443
11444 /* First set the marks for all lines closed/open. */
11445 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11446 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11447 ml_setmarked(lnum);
11448
11449 /* Execute the command on the marked lines. */
11450 global_exe(eap->arg);
11451 ml_clearmarked(); /* clear rest of the marks */
11452}
11453#endif