blob: 590f36b327589cbc56f49a54e2bf74491a792037 [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 Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200132#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
133 || !defined(FEAT_TCL) || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000134# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static void ex_script_ni __ARGS((exarg_T *eap));
136#endif
137static char_u *invalid_range __ARGS((exarg_T *eap));
138static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000139#ifdef FEAT_QUICKFIX
140static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
143static void ex_highlight __ARGS((exarg_T *eap));
144static void ex_colorscheme __ARGS((exarg_T *eap));
145static void ex_quit __ARGS((exarg_T *eap));
146static void ex_cquit __ARGS((exarg_T *eap));
147static void ex_quit_all __ARGS((exarg_T *eap));
148#ifdef FEAT_WINDOWS
149static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000150static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_resize __ARGS((exarg_T *eap));
153static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000154static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000155static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000156static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000157static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#else
160# define ex_close ex_ni
161# define ex_only ex_ni
162# define ex_all ex_ni
163# define ex_resize ex_ni
164# define ex_splitview ex_ni
165# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000166# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168# define ex_tabs ex_ni
169# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000170# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
173static void ex_pclose __ARGS((exarg_T *eap));
174static void ex_ptag __ARGS((exarg_T *eap));
175static void ex_pedit __ARGS((exarg_T *eap));
176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
181static void ex_hide __ARGS((exarg_T *eap));
182static void ex_stop __ARGS((exarg_T *eap));
183static void ex_exit __ARGS((exarg_T *eap));
184static void ex_print __ARGS((exarg_T *eap));
185#ifdef FEAT_BYTEOFF
186static void ex_goto __ARGS((exarg_T *eap));
187#else
188# define ex_goto ex_ni
189#endif
190static void ex_shell __ARGS((exarg_T *eap));
191static void ex_preserve __ARGS((exarg_T *eap));
192static void ex_recover __ARGS((exarg_T *eap));
193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
199static void ex_mode __ARGS((exarg_T *eap));
200static void ex_wrongmodifier __ARGS((exarg_T *eap));
201static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000202static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static void ex_edit __ARGS((exarg_T *eap));
204#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
205# define ex_drop ex_ni
206#endif
207#ifndef FEAT_GUI
208# define ex_gui ex_nogui
209static void ex_nogui __ARGS((exarg_T *eap));
210#endif
211#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
212static void ex_tearoff __ARGS((exarg_T *eap));
213#else
214# define ex_tearoff ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static void ex_popup __ARGS((exarg_T *eap));
218#else
219# define ex_popup ex_ni
220#endif
221#ifndef FEAT_GUI_MSWIN
222# define ex_simalt ex_ni
223#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000224#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define gui_mch_find_dialog ex_ni
226# define gui_mch_replace_dialog ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define ex_helpfind ex_ni
230#endif
231#ifndef FEAT_CSCOPE
232# define do_cscope ex_ni
233# define do_scscope ex_ni
234# define do_cstag ex_ni
235#endif
236#ifndef FEAT_SYN_HL
237# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200238# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200247#ifndef FEAT_PERSISTENT_UNDO
248# define ex_rundo ex_ni
249# define ex_wundo ex_ni
250#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200251#ifndef FEAT_LUA
252# define ex_lua ex_script_ni
253# define ex_luado ex_ni
254# define ex_luafile ex_ni
255#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000256#ifndef FEAT_MZSCHEME
257# define ex_mzscheme ex_script_ni
258# define ex_mzfile ex_ni
259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#ifndef FEAT_PERL
261# define ex_perl ex_script_ni
262# define ex_perldo ex_ni
263#endif
264#ifndef FEAT_PYTHON
265# define ex_python ex_script_ni
266# define ex_pyfile ex_ni
267#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200268#ifndef FEAT_PYTHON3
269# define ex_python3 ex_script_ni
270# define ex_py3file ex_ni
271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#ifndef FEAT_TCL
273# define ex_tcl ex_script_ni
274# define ex_tcldo ex_ni
275# define ex_tclfile ex_ni
276#endif
277#ifndef FEAT_RUBY
278# define ex_ruby ex_script_ni
279# define ex_rubydo ex_ni
280# define ex_rubyfile ex_ni
281#endif
282#ifndef FEAT_SNIFF
283# define ex_sniff ex_ni
284#endif
285#ifndef FEAT_KEYMAP
286# define ex_loadkeymap ex_ni
287#endif
288static void ex_swapname __ARGS((exarg_T *eap));
289static void ex_syncbind __ARGS((exarg_T *eap));
290static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291static void ex_pwd __ARGS((exarg_T *eap));
292static void ex_equal __ARGS((exarg_T *eap));
293static void ex_sleep __ARGS((exarg_T *eap));
294static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
295static void ex_winsize __ARGS((exarg_T *eap));
296#ifdef FEAT_WINDOWS
297static void ex_wincmd __ARGS((exarg_T *eap));
298#else
299# define ex_wincmd ex_ni
300#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000301#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302static void ex_winpos __ARGS((exarg_T *eap));
303#else
304# define ex_winpos ex_ni
305#endif
306static void ex_operators __ARGS((exarg_T *eap));
307static void ex_put __ARGS((exarg_T *eap));
308static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000309static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static void ex_submagic __ARGS((exarg_T *eap));
311static void ex_join __ARGS((exarg_T *eap));
312static void ex_at __ARGS((exarg_T *eap));
313static void ex_bang __ARGS((exarg_T *eap));
314static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200315#ifdef FEAT_PERSISTENT_UNDO
316static void ex_wundo __ARGS((exarg_T *eap));
317static void ex_rundo __ARGS((exarg_T *eap));
318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000320static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321static void ex_redir __ARGS((exarg_T *eap));
322static void ex_redraw __ARGS((exarg_T *eap));
323static void ex_redrawstatus __ARGS((exarg_T *eap));
324static void close_redir __ARGS((void));
325static void ex_mkrc __ARGS((exarg_T *eap));
326static void ex_mark __ARGS((exarg_T *eap));
327#ifdef FEAT_USR_CMDS
328static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000329static 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 +0000330#endif
331#ifdef FEAT_EX_EXTRA
332static void ex_normal __ARGS((exarg_T *eap));
333static void ex_startinsert __ARGS((exarg_T *eap));
334static void ex_stopinsert __ARGS((exarg_T *eap));
335#else
336# define ex_normal ex_ni
337# define ex_align ex_ni
338# define ex_retab ex_ni
339# define ex_startinsert ex_ni
340# define ex_stopinsert ex_ni
341# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000342# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
344#ifdef FEAT_FIND_ID
345static void ex_checkpath __ARGS((exarg_T *eap));
346static void ex_findpat __ARGS((exarg_T *eap));
347#else
348# define ex_findpat ex_ni
349# define ex_checkpath ex_ni
350#endif
351#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
352static void ex_psearch __ARGS((exarg_T *eap));
353#else
354# define ex_psearch ex_ni
355#endif
356static void ex_tag __ARGS((exarg_T *eap));
357static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
358#ifndef FEAT_EVAL
359# define ex_scriptnames ex_ni
360# define ex_finish ex_ni
361# define ex_echo ex_ni
362# define ex_echohl ex_ni
363# define ex_execute ex_ni
364# define ex_call ex_ni
365# define ex_if ex_ni
366# define ex_endif ex_ni
367# define ex_else ex_ni
368# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000369# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370# define ex_continue ex_ni
371# define ex_break ex_ni
372# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000373# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_throw ex_ni
375# define ex_try ex_ni
376# define ex_catch ex_ni
377# define ex_finally ex_ni
378# define ex_endtry ex_ni
379# define ex_endfunction ex_ni
380# define ex_let ex_ni
381# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000382# define ex_lockvar ex_ni
383# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# define ex_function ex_ni
385# define ex_delfunction ex_ni
386# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000387# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#endif
389static char_u *arg_all __ARGS((void));
390#ifdef FEAT_SESSION
391static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000392static 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 +0000393static void ex_loadview __ARGS((exarg_T *eap));
394static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000395static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#else
397# define ex_loadview ex_ni
398#endif
399#ifndef FEAT_EVAL
400# define ex_compiler ex_ni
401#endif
402#ifdef FEAT_VIMINFO
403static void ex_viminfo __ARGS((exarg_T *eap));
404#else
405# define ex_viminfo ex_ni
406#endif
407static void ex_behave __ARGS((exarg_T *eap));
408#ifdef FEAT_AUTOCMD
409static void ex_filetype __ARGS((exarg_T *eap));
410static void ex_setfiletype __ARGS((exarg_T *eap));
411#else
412# define ex_filetype ex_ni
413# define ex_setfiletype ex_ni
414#endif
415#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000416# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417# define ex_diffpatch ex_ni
418# define ex_diffgetput ex_ni
419# define ex_diffsplit ex_ni
420# define ex_diffthis ex_ni
421# define ex_diffupdate ex_ni
422#endif
423static void ex_digraphs __ARGS((exarg_T *eap));
424static void ex_set __ARGS((exarg_T *eap));
425#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
426# define ex_options ex_ni
427#endif
428#ifdef FEAT_SEARCH_EXTRA
429static void ex_nohlsearch __ARGS((exarg_T *eap));
430static void ex_match __ARGS((exarg_T *eap));
431#else
432# define ex_nohlsearch ex_ni
433# define ex_match ex_ni
434#endif
435#ifdef FEAT_CRYPT
436static void ex_X __ARGS((exarg_T *eap));
437#else
438# define ex_X ex_ni
439#endif
440#ifdef FEAT_FOLDING
441static void ex_fold __ARGS((exarg_T *eap));
442static void ex_foldopen __ARGS((exarg_T *eap));
443static void ex_folddo __ARGS((exarg_T *eap));
444#else
445# define ex_fold ex_ni
446# define ex_foldopen ex_ni
447# define ex_folddo ex_ni
448#endif
449#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
450 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
451# define ex_language ex_ni
452#endif
453#ifndef FEAT_SIGNS
454# define ex_sign ex_ni
455#endif
456#ifndef FEAT_SUN_WORKSHOP
457# define ex_wsverb ex_ni
458#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000459#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200460# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000461# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200462# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
465#ifndef FEAT_EVAL
466# define ex_debug ex_ni
467# define ex_breakadd ex_ni
468# define ex_debuggreedy ex_ni
469# define ex_breakdel ex_ni
470# define ex_breaklist ex_ni
471#endif
472
473#ifndef FEAT_CMDHIST
474# define ex_history ex_ni
475#endif
476#ifndef FEAT_JUMPLIST
477# define ex_jumps ex_ni
478# define ex_changes ex_ni
479#endif
480
Bram Moolenaar05159a02005-02-26 23:04:13 +0000481#ifndef FEAT_PROFILE
482# define ex_profile ex_ni
483#endif
484
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485/*
486 * Declare cmdnames[].
487 */
488#define DO_DECLARE_EXCMD
489#include "ex_cmds.h"
490
491/*
492 * Table used to quickly search for a command, based on its first character.
493 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000494static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496 CMD_append,
497 CMD_buffer,
498 CMD_change,
499 CMD_delete,
500 CMD_edit,
501 CMD_file,
502 CMD_global,
503 CMD_help,
504 CMD_insert,
505 CMD_join,
506 CMD_k,
507 CMD_list,
508 CMD_move,
509 CMD_next,
510 CMD_open,
511 CMD_print,
512 CMD_quit,
513 CMD_read,
514 CMD_substitute,
515 CMD_t,
516 CMD_undo,
517 CMD_vglobal,
518 CMD_write,
519 CMD_xit,
520 CMD_yank,
521 CMD_z,
522 CMD_bang
523};
524
525static char_u dollar_command[2] = {'$', 0};
526
527
528#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000529/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530typedef struct
531{
532 char_u *line; /* command line */
533 linenr_T lnum; /* sourcing_lnum of the line */
534} wcmd_T;
535
536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000537 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 garray_T *lines_gap; /* growarray with line info */
544 int current_line; /* last read line from growarray */
545 int repeating; /* TRUE when looping a second time */
546 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
547 char_u *(*getline) __ARGS((int, void *, int));
548 void *cookie;
549};
550
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000551static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
552static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000554
555/* Struct to save a few things while debugging. Used in do_cmdline() only. */
556struct dbg_stuff
557{
558 int trylevel;
559 int force_abort;
560 except_T *caught_stack;
561 char_u *vv_exception;
562 char_u *vv_throwpoint;
563 int did_emsg;
564 int got_int;
565 int did_throw;
566 int need_rethrow;
567 int check_cstack;
568 except_T *current_exception;
569};
570
571static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
572static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
573
574 static void
575save_dbg_stuff(dsp)
576 struct dbg_stuff *dsp;
577{
578 dsp->trylevel = trylevel; trylevel = 0;
579 dsp->force_abort = force_abort; force_abort = FALSE;
580 dsp->caught_stack = caught_stack; caught_stack = NULL;
581 dsp->vv_exception = v_exception(NULL);
582 dsp->vv_throwpoint = v_throwpoint(NULL);
583
584 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
585 dsp->did_emsg = did_emsg; did_emsg = FALSE;
586 dsp->got_int = got_int; got_int = FALSE;
587 dsp->did_throw = did_throw; did_throw = FALSE;
588 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
589 dsp->check_cstack = check_cstack; check_cstack = FALSE;
590 dsp->current_exception = current_exception; current_exception = NULL;
591}
592
593 static void
594restore_dbg_stuff(dsp)
595 struct dbg_stuff *dsp;
596{
597 suppress_errthrow = FALSE;
598 trylevel = dsp->trylevel;
599 force_abort = dsp->force_abort;
600 caught_stack = dsp->caught_stack;
601 (void)v_exception(dsp->vv_exception);
602 (void)v_throwpoint(dsp->vv_throwpoint);
603 did_emsg = dsp->did_emsg;
604 got_int = dsp->got_int;
605 did_throw = dsp->did_throw;
606 need_rethrow = dsp->need_rethrow;
607 check_cstack = dsp->check_cstack;
608 current_exception = dsp->current_exception;
609}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611
612
613/*
614 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
615 * command is given.
616 */
617 void
618do_exmode(improved)
619 int improved; /* TRUE for "improved Ex" mode */
620{
621 int save_msg_scroll;
622 int prev_msg_row;
623 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 int changedtick;
625
626 if (improved)
627 exmode_active = EXMODE_VIM;
628 else
629 exmode_active = EXMODE_NORMAL;
630 State = NORMAL;
631
632 /* When using ":global /pat/ visual" and then "Q" we return to continue
633 * the :global command. */
634 if (global_busy)
635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 save_msg_scroll = msg_scroll;
638 ++RedrawingDisabled; /* don't redisplay the window */
639 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_GUI
641 /* Ignore scrollbar and mouse events in Ex mode */
642 ++hold_gui_events;
643#endif
644#ifdef FEAT_SNIFF
645 want_sniff_request = 0; /* No K_SNIFF wanted */
646#endif
647
648 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
649 while (exmode_active)
650 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000651#ifdef FEAT_EX_EXTRA
652 /* Check for a ":normal" command and no more characters left. */
653 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
654 {
655 exmode_active = FALSE;
656 break;
657 }
658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 msg_scroll = TRUE;
660 need_wait_return = FALSE;
661 ex_pressedreturn = FALSE;
662 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000663 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 prev_msg_row = msg_row;
665 prev_line = curwin->w_cursor.lnum;
666#ifdef FEAT_SNIFF
667 ProcessSniffRequests();
668#endif
669 if (improved)
670 {
671 cmdline_row = msg_row;
672 do_cmdline(NULL, getexline, NULL, 0);
673 }
674 else
675 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
676 lines_left = Rows - 1;
677
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 if ((prev_line != curwin->w_cursor.lnum
679 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if (curbuf->b_ml.ml_flags & ML_EMPTY)
682 EMSG(_(e_emptybuf));
683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (ex_pressedreturn)
686 {
687 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000688 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 msg_row = prev_msg_row;
690 if (prev_msg_row == Rows - 1)
691 msg_row--;
692 }
693 msg_col = 0;
694 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
695 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000698 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
699 {
700 if (curbuf->b_ml.ml_flags & ML_EMPTY)
701 EMSG(_(e_emptybuf));
702 else
703 EMSG(_("E501: At end-of-file"));
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706
707#ifdef FEAT_GUI
708 --hold_gui_events;
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 --RedrawingDisabled;
711 --no_wait_return;
712 update_screen(CLEAR);
713 need_wait_return = FALSE;
714 msg_scroll = save_msg_scroll;
715}
716
717/*
718 * Execute a simple command line. Used for translated commands like "*".
719 */
720 int
721do_cmdline_cmd(cmd)
722 char_u *cmd;
723{
724 return do_cmdline(cmd, NULL, NULL,
725 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
726}
727
728/*
729 * do_cmdline(): execute one Ex command line
730 *
731 * 1. Execute "cmdline" when it is not NULL.
732 * If "cmdline" is NULL, or more lines are needed, getline() is used.
733 * 2. Split up in parts separated with '|'.
734 *
735 * This function can be called recursively!
736 *
737 * flags:
738 * DOCMD_VERBOSE - The command will be included in the error message.
739 * DOCMD_NOWAIT - Don't call wait_return() and friends.
740 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
741 * DOCMD_KEYTYPED - Don't reset KeyTyped.
742 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
743 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
744 *
745 * return FAIL if cmdline could not be executed, OK otherwise
746 */
747 int
748do_cmdline(cmdline, getline, cookie, flags)
749 char_u *cmdline;
750 char_u *(*getline) __ARGS((int, void *, int));
751 void *cookie; /* argument for getline() */
752 int flags;
753{
754 char_u *next_cmdline; /* next cmd to execute */
755 char_u *cmdline_copy = NULL; /* copy of cmd line */
756 int used_getline = FALSE; /* used "getline" to obtain command */
757 static int recursive = 0; /* recursive depth */
758 int msg_didout_before_start = 0;
759 int count = 0; /* line number count */
760 int did_inc = FALSE; /* incremented RedrawingDisabled */
761 int retval = OK;
762#ifdef FEAT_EVAL
763 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 int current_line = 0; /* active line in lines_ga */
766 char_u *fname = NULL; /* function or script name */
767 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
768 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000769 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 int initial_trylevel;
771 struct msglist **saved_msg_list = NULL;
772 struct msglist *private_msg_list;
773
774 /* "getline" and "cookie" passed to do_one_cmd() */
775 char_u *(*cmd_getline) __ARGS((int, void *, int));
776 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#else
781# define cmd_getline getline
782# define cmd_cookie cookie
783#endif
784 static int call_depth = 0; /* recursiveness */
785
786#ifdef FEAT_EVAL
787 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
788 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000789 * This ensures that the do_errthrow() call in do_one_cmd() does not
790 * combine the messages stored by an earlier invocation of do_one_cmd()
791 * with the command name of the later one. This would happen when
792 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 saved_msg_list = msg_list;
794 msg_list = &private_msg_list;
795 private_msg_list = NULL;
796#endif
797
798 /* It's possible to create an endless loop with ":execute", catch that
799 * here. The value of 200 allows nested function calls, ":source", etc. */
800 if (call_depth == 200)
801 {
802 EMSG(_("E169: Command too recursive"));
803#ifdef FEAT_EVAL
804 /* When converting to an exception, we do not include the command name
805 * since this is not an error of the specific command. */
806 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
807 msg_list = saved_msg_list;
808#endif
809 return FAIL;
810 }
811 ++call_depth;
812
813#ifdef FEAT_EVAL
814 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000815 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 cstack.cs_trylevel = 0;
817 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
820
821 real_cookie = getline_cookie(getline, cookie);
822
823 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824 getline_is_func = getline_equal(getline, cookie, get_func_line);
825 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 ++ex_nesting_level;
827
828 /* Get the function or script name and the address where the next breakpoint
829 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
832 fname = func_name(real_cookie);
833 breakpoint = func_breakpoint(real_cookie);
834 dbg_tick = func_dbg_tick(real_cookie);
835 }
836 else if (getline_equal(getline, cookie, getsourceline))
837 {
838 fname = sourcing_name;
839 breakpoint = source_breakpoint(real_cookie);
840 dbg_tick = source_dbg_tick(real_cookie);
841 }
842
843 /*
844 * Initialize "force_abort" and "suppress_errthrow" at the top level.
845 */
846 if (!recursive)
847 {
848 force_abort = FALSE;
849 suppress_errthrow = FALSE;
850 }
851
852 /*
853 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000854 * exception handling (used when debugging). Otherwise clear it to avoid
855 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000857 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000858 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000859 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200860 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 initial_trylevel = trylevel;
863
864 /*
865 * "did_throw" will be set to TRUE when an exception is being thrown.
866 */
867 did_throw = FALSE;
868#endif
869 /*
870 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000871 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * If force_abort is set, we cancel everything.
873 */
874 did_emsg = FALSE;
875
876 /*
877 * KeyTyped is only set when calling vgetc(). Reset it here when not
878 * calling vgetc() (sourced command lines).
879 */
880 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
881 KeyTyped = FALSE;
882
883 /*
884 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * - for multiple commands on one line, separated with '|'
887 * - when repeating until there are no more lines (for ":source")
888 */
889 next_cmdline = cmdline;
890 do
891 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892#ifdef FEAT_EVAL
893 getline_is_func = getline_equal(getline, cookie, get_func_line);
894#endif
895
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000896 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (next_cmdline == NULL
898#ifdef FEAT_EVAL
899 && !force_abort
900 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
903 )
904 did_emsg = FALSE;
905
906 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000907 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 * 2. If no line given: Get an allocated line with getline().
909 * 3. If a line is given: Make a copy, so we can mess with it.
910 */
911
912#ifdef FEAT_EVAL
913 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000914 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 {
916 /* Each '|' separated command is stored separately in lines_ga, to
917 * be able to jump to it. Don't use next_cmdline now. */
918 vim_free(cmdline_copy);
919 cmdline_copy = NULL;
920
921 /* Check if a function has returned or, unless it has an unclosed
922 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 func_line_end(real_cookie);
928# endif
929 if (func_has_ended(real_cookie))
930 {
931 retval = FAIL;
932 break;
933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000935#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000936 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937 && getline_equal(getline, cookie, getsourceline))
938 script_line_end();
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 /* Check if a sourced file hit a ":finish" command. */
942 if (source_finished(getline, cookie))
943 {
944 retval = FAIL;
945 break;
946 }
947
948 /* If breakpoints have been added/deleted need to check for it. */
949 if (breakpoint != NULL && dbg_tick != NULL
950 && *dbg_tick != debug_tick)
951 {
952 *breakpoint = dbg_find_breakpoint(
953 getline_equal(getline, cookie, getsourceline),
954 fname, sourcing_lnum);
955 *dbg_tick = debug_tick;
956 }
957
958 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
959 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
960
961 /* Did we encounter a breakpoint? */
962 if (breakpoint != NULL && *breakpoint != 0
963 && *breakpoint <= sourcing_lnum)
964 {
965 dbg_breakpoint(fname, sourcing_lnum);
966 /* Find next breakpoint. */
967 *breakpoint = dbg_find_breakpoint(
968 getline_equal(getline, cookie, getsourceline),
969 fname, sourcing_lnum);
970 *dbg_tick = debug_tick;
971 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000972# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000973 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000974 {
975 if (getline_is_func)
976 func_line_start(real_cookie);
977 else if (getline_equal(getline, cookie, getsourceline))
978 script_line_start();
979 }
980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000983 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 * again. Pass a different "getline" function to do_one_cmd()
987 * below, so that it stores lines in or reads them from
988 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 * while/for loop. */
990 cmd_getline = get_loop_line;
991 cmd_cookie = (void *)&cmd_loop_cookie;
992 cmd_loop_cookie.lines_gap = &lines_ga;
993 cmd_loop_cookie.current_line = current_line;
994 cmd_loop_cookie.getline = getline;
995 cmd_loop_cookie.cookie = cookie;
996 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
998 else
999 {
1000 cmd_getline = getline;
1001 cmd_cookie = cookie;
1002 }
1003#endif
1004
1005 /* 2. If no line given, get an allocated line with getline(). */
1006 if (next_cmdline == NULL)
1007 {
1008 /*
1009 * Need to set msg_didout for the first line after an ":if",
1010 * otherwise the ":if" will be overwritten.
1011 */
1012 if (count == 1 && getline_equal(getline, cookie, getexline))
1013 msg_didout = TRUE;
1014 if (getline == NULL || (next_cmdline = getline(':', cookie,
1015#ifdef FEAT_EVAL
1016 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1017#else
1018 0
1019#endif
1020 )) == NULL)
1021 {
1022 /* Don't call wait_return for aborted command line. The NULL
1023 * returned for the end of a sourced file or executed function
1024 * doesn't do this. */
1025 if (KeyTyped && !(flags & DOCMD_REPEAT))
1026 need_wait_return = FALSE;
1027 retval = FAIL;
1028 break;
1029 }
1030 used_getline = TRUE;
1031
1032 /*
1033 * Keep the first typed line. Clear it when more lines are typed.
1034 */
1035 if (flags & DOCMD_KEEPLINE)
1036 {
1037 vim_free(repeat_cmdline);
1038 if (count == 0)
1039 repeat_cmdline = vim_strsave(next_cmdline);
1040 else
1041 repeat_cmdline = NULL;
1042 }
1043 }
1044
1045 /* 3. Make a copy of the command so we can mess with it. */
1046 else if (cmdline_copy == NULL)
1047 {
1048 next_cmdline = vim_strsave(next_cmdline);
1049 if (next_cmdline == NULL)
1050 {
1051 EMSG(_(e_outofmem));
1052 retval = FAIL;
1053 break;
1054 }
1055 }
1056 cmdline_copy = next_cmdline;
1057
1058#ifdef FEAT_EVAL
1059 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 * Save the current line when inside a ":while" or ":for", and when
1061 * the command looks like a ":while" or ":for", because we may need it
1062 * later. When there is a '|' and another command, it is stored
1063 * separately, because we need to be able to jump back to it from an
1064 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 if (current_line == lines_ga.ga_len
1067 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
1071 retval = FAIL;
1072 break;
1073 }
1074 }
1075 did_endif = FALSE;
1076#endif
1077
1078 if (count++ == 0)
1079 {
1080 /*
1081 * All output from the commands is put below each other, without
1082 * waiting for a return. Don't do this when executing commands
1083 * from a script or when being called recursive (e.g. for ":e
1084 * +command file").
1085 */
1086 if (!(flags & DOCMD_NOWAIT) && !recursive)
1087 {
1088 msg_didout_before_start = msg_didout;
1089 msg_didany = FALSE; /* no output yet */
1090 msg_start();
1091 msg_scroll = TRUE; /* put messages below each other */
1092 ++no_wait_return; /* dont wait for return until finished */
1093 ++RedrawingDisabled;
1094 did_inc = TRUE;
1095 }
1096 }
1097
1098 if (p_verbose >= 15 && sourcing_name != NULL)
1099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001101 verbose_enter_scroll();
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 smsg((char_u *)_("line %ld: %s"),
1104 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 if (msg_silent == 0)
1106 msg_puts((char_u *)"\n"); /* don't overwrite this */
1107
1108 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 --no_wait_return;
1110 }
1111
1112 /*
1113 * 2. Execute one '|' separated command.
1114 * do_one_cmd() will return NULL if there is no trailing '|'.
1115 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1116 */
1117 ++recursive;
1118 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1119#ifdef FEAT_EVAL
1120 &cstack,
1121#endif
1122 cmd_getline, cmd_cookie);
1123 --recursive;
1124
1125#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 if (cmd_cookie == (void *)&cmd_loop_cookie)
1127 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#endif
1131
1132 if (next_cmdline == NULL)
1133 {
1134 vim_free(cmdline_copy);
1135 cmdline_copy = NULL;
1136#ifdef FEAT_CMDHIST
1137 /*
1138 * If the command was typed, remember it for the ':' register.
1139 * Do this AFTER executing the command to make :@: work.
1140 */
1141 if (getline_equal(getline, cookie, getexline)
1142 && new_last_cmdline != NULL)
1143 {
1144 vim_free(last_cmdline);
1145 last_cmdline = new_last_cmdline;
1146 new_last_cmdline = NULL;
1147 }
1148#endif
1149 }
1150 else
1151 {
1152 /* need to copy the command after the '|' to cmdline_copy, for the
1153 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001154 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 next_cmdline = cmdline_copy;
1156 }
1157
1158
1159#ifdef FEAT_EVAL
1160 /* reset did_emsg for a function that is not aborted by an error */
1161 if (did_emsg && !force_abort
1162 && getline_equal(getline, cookie, get_func_line)
1163 && !func_has_abort(real_cookie))
1164 did_emsg = FALSE;
1165
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168 ++current_line;
1169
1170 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 * An ":endwhile", ":endfor" and ":continue" is handled here.
1172 * If we were executing commands, jump back to the ":while" or
1173 * ":for".
1174 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 /* Jump back to the matching ":while" or ":for". Be careful
1181 * not to use a cs_line[] from an entry that isn't a ":while"
1182 * or ":for": It would make "current_line" invalid and can
1183 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (!did_emsg && !got_int && !did_throw
1185 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 && (cstack.cs_flags[cstack.cs_idx]
1187 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 && cstack.cs_line[cstack.cs_idx] >= 0
1189 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1190 {
1191 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 /* remember we jumped there */
1193 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 line_breakcheck(); /* check if CTRL-C typed */
1195
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* Check for the next breakpoint at or after the ":while"
1197 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (breakpoint != NULL)
1199 {
1200 *breakpoint = dbg_find_breakpoint(
1201 getline_equal(getline, cookie, getsourceline),
1202 fname,
1203 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1204 *dbg_tick = debug_tick;
1205 }
1206 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1212 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214 }
1215
1216 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1223 }
1224 }
1225
1226 /*
1227 * When not inside any ":while" loop, clear remembered lines.
1228 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001229 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
1231 if (lines_ga.ga_len > 0)
1232 {
1233 sourcing_lnum =
1234 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1235 free_cmdlines(&lines_ga);
1236 }
1237 current_line = 0;
1238 }
1239
1240 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1242 * being restored at the ":endtry". Reset them here and set the
1243 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1244 * This includes the case where a missing ":endif", ":endwhile" or
1245 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001247 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 cstack.cs_lflags &= ~CSL_HAD_FINA;
1250 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1251 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 did_throw ? (void *)current_exception : NULL);
1253 did_emsg = got_int = did_throw = FALSE;
1254 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1255 }
1256
1257 /* Update global "trylevel" for recursive calls to do_cmdline() from
1258 * within this loop. */
1259 trylevel = initial_trylevel + cstack.cs_trylevel;
1260
1261 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001262 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 * files) is aborted because of an error, an interrupt, or an uncaught
1264 * exception, cancel everything. If it is left normally, reset
1265 * force_abort to get the non-EH compatible abortion behavior for
1266 * the rest of the script.
1267 */
1268 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1269 force_abort = FALSE;
1270
1271 /* Convert an interrupt to an exception if appropriate. */
1272 (void)do_intthrow(&cstack);
1273#endif /* FEAT_EVAL */
1274
1275 }
1276 /*
1277 * Continue executing command lines when:
1278 * - no CTRL-C typed, no aborting error, no exception thrown or try
1279 * conditionals need to be checked for executing finally clauses or
1280 * catching an interrupt exception
1281 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001282 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 * looping for ":source" command or function call.
1284 */
1285 while (!((got_int
1286#ifdef FEAT_EVAL
1287 || (did_emsg && force_abort) || did_throw
1288#endif
1289 )
1290#ifdef FEAT_EVAL
1291 && cstack.cs_trylevel == 0
1292#endif
1293 )
1294 && !(did_emsg && used_getline
1295 && (getline_equal(getline, cookie, getexmodeline)
1296 || getline_equal(getline, cookie, getexline)))
1297 && (next_cmdline != NULL
1298#ifdef FEAT_EVAL
1299 || cstack.cs_idx >= 0
1300#endif
1301 || (flags & DOCMD_REPEAT)));
1302
1303 vim_free(cmdline_copy);
1304#ifdef FEAT_EVAL
1305 free_cmdlines(&lines_ga);
1306 ga_clear(&lines_ga);
1307
1308 if (cstack.cs_idx >= 0)
1309 {
1310 /*
1311 * If a sourced file or executed function ran to its end, report the
1312 * unclosed conditional.
1313 */
1314 if (!got_int && !did_throw
1315 && ((getline_equal(getline, cookie, getsourceline)
1316 && !source_finished(getline, cookie))
1317 || (getline_equal(getline, cookie, get_func_line)
1318 && !func_has_ended(real_cookie))))
1319 {
1320 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1321 EMSG(_(e_endtry));
1322 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1323 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1325 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 EMSG(_(e_endif));
1328 }
1329
1330 /*
1331 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1332 * ":endtry" in a sourced file or executed function. If the try
1333 * conditional is in its finally clause, ignore anything pending.
1334 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001335 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001338 {
1339 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1340
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001341 if (idx >= 0)
1342 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1344 &cstack.cs_looplevel);
1345 }
1346 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 trylevel = initial_trylevel;
1348 }
1349
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001350 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1351 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * exception, do this now after rewinding the cstack. */
1353 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1354 ? (char_u *)"endfunction" : (char_u *)NULL);
1355
1356 if (trylevel == 0)
1357 {
1358 /*
1359 * When an exception is being thrown out of the outermost try
1360 * conditional, discard the uncaught exception, disable the conversion
1361 * of interrupts or errors to exceptions, and ensure that no more
1362 * commands are executed.
1363 */
1364 if (did_throw)
1365 {
1366 void *p = NULL;
1367 char_u *saved_sourcing_name;
1368 int saved_sourcing_lnum;
1369 struct msglist *messages = NULL, *next;
1370
1371 /*
1372 * If the uncaught exception is a user exception, report it as an
1373 * error. If it is an error exception, display the saved error
1374 * message now. For an interrupt exception, do nothing; the
1375 * interrupt message is given elsewhere.
1376 */
1377 switch (current_exception->type)
1378 {
1379 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001380 vim_snprintf((char *)IObuff, IOSIZE,
1381 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 current_exception->value);
1383 p = vim_strsave(IObuff);
1384 break;
1385 case ET_ERROR:
1386 messages = current_exception->messages;
1387 current_exception->messages = NULL;
1388 break;
1389 case ET_INTERRUPT:
1390 break;
1391 default:
1392 p = vim_strsave((char_u *)_(e_internal));
1393 }
1394
1395 saved_sourcing_name = sourcing_name;
1396 saved_sourcing_lnum = sourcing_lnum;
1397 sourcing_name = current_exception->throw_name;
1398 sourcing_lnum = current_exception->throw_lnum;
1399 current_exception->throw_name = NULL;
1400
1401 discard_current_exception(); /* uses IObuff if 'verbose' */
1402 suppress_errthrow = TRUE;
1403 force_abort = TRUE;
1404
1405 if (messages != NULL)
1406 {
1407 do
1408 {
1409 next = messages->next;
1410 emsg(messages->msg);
1411 vim_free(messages->msg);
1412 vim_free(messages);
1413 messages = next;
1414 }
1415 while (messages != NULL);
1416 }
1417 else if (p != NULL)
1418 {
1419 emsg(p);
1420 vim_free(p);
1421 }
1422 vim_free(sourcing_name);
1423 sourcing_name = saved_sourcing_name;
1424 sourcing_lnum = saved_sourcing_lnum;
1425 }
1426
1427 /*
1428 * On an interrupt or an aborting error not converted to an exception,
1429 * disable the conversion of errors to exceptions. (Interrupts are not
1430 * converted any more, here.) This enables also the interrupt message
1431 * when force_abort is set and did_emsg unset in case of an interrupt
1432 * from a finally clause after an error.
1433 */
1434 else if (got_int || (did_emsg && force_abort))
1435 suppress_errthrow = TRUE;
1436 }
1437
1438 /*
1439 * The current cstack will be freed when do_cmdline() returns. An uncaught
1440 * exception will have to be rethrown in the previous cstack. If a function
1441 * has just returned or a script file was just finished and the previous
1442 * cstack belongs to the same function or, respectively, script file, it
1443 * will have to be checked for finally clauses to be executed due to the
1444 * ":return" or ":finish". This is done in do_one_cmd().
1445 */
1446 if (did_throw)
1447 need_rethrow = TRUE;
1448 if ((getline_equal(getline, cookie, getsourceline)
1449 && ex_nesting_level > source_level(real_cookie))
1450 || (getline_equal(getline, cookie, get_func_line)
1451 && ex_nesting_level > func_level(real_cookie) + 1))
1452 {
1453 if (!did_throw)
1454 check_cstack = TRUE;
1455 }
1456 else
1457 {
1458 /* When leaving a function, reduce nesting level. */
1459 if (getline_equal(getline, cookie, get_func_line))
1460 --ex_nesting_level;
1461 /*
1462 * Go to debug mode when returning from a function in which we are
1463 * single-stepping.
1464 */
1465 if ((getline_equal(getline, cookie, getsourceline)
1466 || getline_equal(getline, cookie, get_func_line))
1467 && ex_nesting_level + 1 <= debug_break_level)
1468 do_debug(getline_equal(getline, cookie, getsourceline)
1469 ? (char_u *)_("End of sourced file")
1470 : (char_u *)_("End of function"));
1471 }
1472
1473 /*
1474 * Restore the exception environment (done after returning from the
1475 * debugger).
1476 */
1477 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001478 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 msg_list = saved_msg_list;
1481#endif /* FEAT_EVAL */
1482
1483 /*
1484 * If there was too much output to fit on the command line, ask the user to
1485 * hit return before redrawing the screen. With the ":global" command we do
1486 * this only once after the command is finished.
1487 */
1488 if (did_inc)
1489 {
1490 --RedrawingDisabled;
1491 --no_wait_return;
1492 msg_scroll = FALSE;
1493
1494 /*
1495 * When just finished an ":if"-":else" which was typed, no need to
1496 * wait for hit-return. Also for an error situation.
1497 */
1498 if (retval == FAIL
1499#ifdef FEAT_EVAL
1500 || (did_endif && KeyTyped && !did_emsg)
1501#endif
1502 )
1503 {
1504 need_wait_return = FALSE;
1505 msg_didany = FALSE; /* don't wait when restarting edit */
1506 }
1507 else if (need_wait_return)
1508 {
1509 /*
1510 * The msg_start() above clears msg_didout. The wait_return we do
1511 * here should not overwrite the command that may be shown before
1512 * doing that.
1513 */
1514 msg_didout |= msg_didout_before_start;
1515 wait_return(FALSE);
1516 }
1517 }
1518
1519#ifndef FEAT_EVAL
1520 /*
1521 * Reset if_level, in case a sourced script file contains more ":if" than
1522 * ":endif" (could be ":if x | foo | endif").
1523 */
1524 if_level = 0;
1525#endif
1526
1527 --call_depth;
1528 return retval;
1529}
1530
1531#ifdef FEAT_EVAL
1532/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 */
1535 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001536get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 int c;
1538 void *cookie;
1539 int indent;
1540{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 wcmd_T *wp;
1543 char_u *line;
1544
1545 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1546 {
1547 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (cp->getline == NULL)
1552 line = getcmdline(c, 0L, indent);
1553 else
1554 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001555 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 ++cp->current_line;
1557
1558 return line;
1559 }
1560
1561 KeyTyped = FALSE;
1562 ++cp->current_line;
1563 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1564 sourcing_lnum = wp->lnum;
1565 return vim_strsave(wp->line);
1566}
1567
1568/*
1569 * Store a line in "gap" so that a ":while" loop can execute it again.
1570 */
1571 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 garray_T *gap;
1574 char_u *line;
1575{
1576 if (ga_grow(gap, 1) == FAIL)
1577 return FAIL;
1578 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1579 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1580 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return OK;
1582}
1583
1584/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001585 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 */
1587 static void
1588free_cmdlines(gap)
1589 garray_T *gap;
1590{
1591 while (gap->ga_len > 0)
1592 {
1593 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1594 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596}
1597#endif
1598
1599/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001600 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1601 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604getline_equal(fgetline, cookie, func)
1605 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001606 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 char_u *(*func) __ARGS((int, void *, int));
1608{
1609#ifdef FEAT_EVAL
1610 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar89d40322006-08-29 15:30:07 +00001613 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001614 * function that's originally used to obtain the lines. This may be
1615 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001617 cp = (struct loop_cookie *)cookie;
1618 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 gp = cp->getline;
1621 cp = cp->cookie;
1622 }
1623 return gp == func;
1624#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626#endif
1627}
1628
1629#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1630/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 * getline function. Otherwise return "cookie".
1633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001636 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639# ifdef FEAT_EVAL
1640 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar89d40322006-08-29 15:30:07 +00001643 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001644 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001646 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001647 cp = (struct loop_cookie *)cookie;
1648 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 gp = cp->getline;
1651 cp = cp->cookie;
1652 }
1653 return cp;
1654# else
1655 return cookie;
1656# endif
1657}
1658#endif
1659
1660/*
1661 * Execute one Ex command.
1662 *
1663 * If 'sourcing' is TRUE, the command will be included in the error message.
1664 *
1665 * 1. skip comment lines and leading space
1666 * 2. handle command modifiers
1667 * 3. parse range
1668 * 4. parse command
1669 * 5. parse arguments
1670 * 6. switch on command name
1671 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001672 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 *
1674 * This function may be called recursively!
1675 */
1676#if (_MSC_VER == 1200)
1677/*
Bram Moolenaared203462004-06-16 11:19:22 +00001678 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001680 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681#endif
1682 static char_u *
1683do_one_cmd(cmdlinep, sourcing,
1684#ifdef FEAT_EVAL
1685 cstack,
1686#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001687 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 char_u **cmdlinep;
1689 int sourcing;
1690#ifdef FEAT_EVAL
1691 struct condstack *cstack;
1692#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001693 char_u *(*fgetline) __ARGS((int, void *, int));
1694 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 char_u *p;
1697 linenr_T lnum;
1698 long n;
1699 char_u *errormsg = NULL; /* error message */
1700 exarg_T ea; /* Ex command arguments */
1701 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001702 int save_msg_scroll = msg_scroll;
1703 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001705#ifdef HAVE_SANDBOX
1706 int did_sandbox = FALSE;
1707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 cmdmod_T save_cmdmod;
1709 int ni; /* set when Not Implemented */
1710
1711 vim_memset(&ea, 0, sizeof(ea));
1712 ea.line1 = 1;
1713 ea.line2 = 1;
1714#ifdef FEAT_EVAL
1715 ++ex_nesting_level;
1716#endif
1717
1718 /* when not editing the last file :q has to be typed twice */
1719 if (quitmore
1720#ifdef FEAT_EVAL
1721 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001722 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724 )
1725 --quitmore;
1726
1727 /*
1728 * Reset browse, confirm, etc.. They are restored when returning, for
1729 * recursive calls.
1730 */
1731 save_cmdmod = cmdmod;
1732 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1733
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001734 /* "#!anything" is handled like a comment. */
1735 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1736 goto doend;
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 /*
1739 * Repeat until no more command modifiers are found.
1740 */
1741 ea.cmd = *cmdlinep;
1742 for (;;)
1743 {
1744/*
1745 * 1. skip comment lines and leading white space and colons
1746 */
1747 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1748 ++ea.cmd;
1749
1750 /* in ex mode, an empty line works like :+ */
1751 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 && (getline_equal(fgetline, cookie, getexmodeline)
1753 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001754 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 {
1756 ea.cmd = (char_u *)"+";
1757 ex_pressedreturn = TRUE;
1758 }
1759
1760 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001761 if (*ea.cmd == '"')
1762 goto doend;
1763 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001764 {
1765 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769/*
1770 * 2. handle command modifiers.
1771 */
1772 p = ea.cmd;
1773 if (VIM_ISDIGIT(*ea.cmd))
1774 p = skipwhite(skipdigits(ea.cmd));
1775 switch (*p)
1776 {
1777 /* When adding an entry, also modify cmd_exists(). */
1778 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1779 break;
1780#ifdef FEAT_WINDOWS
1781 cmdmod.split |= WSP_ABOVE;
1782#endif
1783 continue;
1784
1785 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1786 {
1787#ifdef FEAT_WINDOWS
1788 cmdmod.split |= WSP_BELOW;
1789#endif
1790 continue;
1791 }
1792 if (checkforcmd(&ea.cmd, "browse", 3))
1793 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001794#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 cmdmod.browse = TRUE;
1796#endif
1797 continue;
1798 }
1799 if (!checkforcmd(&ea.cmd, "botright", 2))
1800 break;
1801#ifdef FEAT_WINDOWS
1802 cmdmod.split |= WSP_BOT;
1803#endif
1804 continue;
1805
1806 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1807 break;
1808#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1809 cmdmod.confirm = TRUE;
1810#endif
1811 continue;
1812
1813 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1814 {
1815 cmdmod.keepmarks = TRUE;
1816 continue;
1817 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001818 if (checkforcmd(&ea.cmd, "keepalt", 5))
1819 {
1820 cmdmod.keepalt = TRUE;
1821 continue;
1822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1824 break;
1825 cmdmod.keepjumps = TRUE;
1826 continue;
1827
1828 /* ":hide" and ":hide | cmd" are not modifiers */
1829 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1830 || *p == NUL || ends_excmd(*p))
1831 break;
1832 ea.cmd = p;
1833 cmdmod.hide = TRUE;
1834 continue;
1835
1836 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1837 {
1838 cmdmod.lockmarks = TRUE;
1839 continue;
1840 }
1841
1842 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1843 break;
1844#ifdef FEAT_WINDOWS
1845 cmdmod.split |= WSP_ABOVE;
1846#endif
1847 continue;
1848
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001849 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1850 break;
1851#ifdef FEAT_AUTOCMD
1852 if (cmdmod.save_ei == NULL)
1853 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001854 /* Set 'eventignore' to "all". Restore the
1855 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001856 cmdmod.save_ei = vim_strsave(p_ei);
1857 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001858 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001859 }
1860#endif
1861 continue;
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1864 break;
1865#ifdef FEAT_WINDOWS
1866 cmdmod.split |= WSP_BELOW;
1867#endif
1868 continue;
1869
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001870 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1871 {
1872#ifdef HAVE_SANDBOX
1873 if (!did_sandbox)
1874 ++sandbox;
1875 did_sandbox = TRUE;
1876#endif
1877 continue;
1878 }
1879 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001881 if (save_msg_silent == -1)
1882 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1885 {
1886 /* ":silent!", but not "silent !cmd" */
1887 ea.cmd = skipwhite(ea.cmd + 1);
1888 ++emsg_silent;
1889 ++did_esilent;
1890 }
1891 continue;
1892
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001893 case 't': if (checkforcmd(&p, "tab", 3))
1894 {
1895#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001896 if (vim_isdigit(*ea.cmd))
1897 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1898 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001899 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001900 ea.cmd = p;
1901#endif
1902 continue;
1903 }
1904 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 break;
1906#ifdef FEAT_WINDOWS
1907 cmdmod.split |= WSP_TOP;
1908#endif
1909 continue;
1910
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001911 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1912 break;
1913 if (save_msg_silent == -1)
1914 save_msg_silent = msg_silent;
1915 msg_silent = 0;
1916 continue;
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1919 {
1920#ifdef FEAT_VERTSPLIT
1921 cmdmod.split |= WSP_VERT;
1922#endif
1923 continue;
1924 }
1925 if (!checkforcmd(&p, "verbose", 4))
1926 break;
1927 if (verbose_save < 0)
1928 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001929 if (vim_isdigit(*ea.cmd))
1930 p_verbose = atoi((char *)ea.cmd);
1931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 p_verbose = 1;
1933 ea.cmd = p;
1934 continue;
1935 }
1936 break;
1937 }
1938
1939#ifdef FEAT_EVAL
1940 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1941 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1942#else
1943 ea.skip = (if_level > 0);
1944#endif
1945
1946#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001947# ifdef FEAT_PROFILE
1948 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001949 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001950 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001951 if (getline_equal(fgetline, cookie, get_func_line))
1952 func_line_exec(getline_cookie(fgetline, cookie));
1953 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 script_line_exec();
1955 }
1956#endif
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 /* May go to debug mode. If this happens and the ">quit" debug command is
1959 * used, throw an interrupt exception and skip the next command. */
1960 dbg_check_breakpoint(&ea);
1961 if (!ea.skip && got_int)
1962 {
1963 ea.skip = TRUE;
1964 (void)do_intthrow(cstack);
1965 }
1966#endif
1967
1968/*
1969 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1970 *
1971 * where 'addr' is:
1972 *
1973 * % (entire file)
1974 * $ [+-NUM]
1975 * 'x [+-NUM] (where x denotes a currently defined mark)
1976 * . [+-NUM]
1977 * [+-NUM]..
1978 * NUM
1979 *
1980 * The ea.cmd pointer is updated to point to the first character following the
1981 * range spec. If an initial address is found, but no second, the upper bound
1982 * is equal to the lower.
1983 */
1984
1985 /* repeat for all ',' or ';' separated addresses */
1986 for (;;)
1987 {
1988 ea.line1 = ea.line2;
1989 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1990 ea.cmd = skipwhite(ea.cmd);
1991 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1992 if (ea.cmd == NULL) /* error detected */
1993 goto doend;
1994 if (lnum == MAXLNUM)
1995 {
1996 if (*ea.cmd == '%') /* '%' - all lines */
1997 {
1998 ++ea.cmd;
1999 ea.line1 = 1;
2000 ea.line2 = curbuf->b_ml.ml_line_count;
2001 ++ea.addr_count;
2002 }
2003 /* '*' - visual area */
2004 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2005 {
2006 pos_T *fp;
2007
2008 ++ea.cmd;
2009 if (!ea.skip)
2010 {
2011 fp = getmark('<', FALSE);
2012 if (check_mark(fp) == FAIL)
2013 goto doend;
2014 ea.line1 = fp->lnum;
2015 fp = getmark('>', FALSE);
2016 if (check_mark(fp) == FAIL)
2017 goto doend;
2018 ea.line2 = fp->lnum;
2019 ++ea.addr_count;
2020 }
2021 }
2022 }
2023 else
2024 ea.line2 = lnum;
2025 ea.addr_count++;
2026
2027 if (*ea.cmd == ';')
2028 {
2029 if (!ea.skip)
2030 curwin->w_cursor.lnum = ea.line2;
2031 }
2032 else if (*ea.cmd != ',')
2033 break;
2034 ++ea.cmd;
2035 }
2036
2037 /* One address given: set start and end lines */
2038 if (ea.addr_count == 1)
2039 {
2040 ea.line1 = ea.line2;
2041 /* ... but only implicit: really no address given */
2042 if (lnum == MAXLNUM)
2043 ea.addr_count = 0;
2044 }
2045
2046 /* Don't leave the cursor on an illegal line (caused by ';') */
2047 check_cursor_lnum();
2048
2049/*
2050 * 4. parse command
2051 */
2052
2053 /*
2054 * Skip ':' and any white space
2055 */
2056 ea.cmd = skipwhite(ea.cmd);
2057 while (*ea.cmd == ':')
2058 ea.cmd = skipwhite(ea.cmd + 1);
2059
2060 /*
2061 * If we got a line, but no command, then go to the line.
2062 * If we find a '|' or '\n' we set ea.nextcmd.
2063 */
2064 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2065 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2066 {
2067 /*
2068 * strange vi behaviour:
2069 * ":3" jumps to line 3
2070 * ":3|..." prints line 3
2071 * ":|" prints current line
2072 */
2073 if (ea.skip) /* skip this if inside :if */
2074 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002075 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
2077 ea.cmdidx = CMD_print;
2078 ea.argt = RANGE+COUNT+TRLBAR;
2079 if ((errormsg = invalid_range(&ea)) == NULL)
2080 {
2081 correct_range(&ea);
2082 ex_print(&ea);
2083 }
2084 }
2085 else if (ea.addr_count != 0)
2086 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002087 if (ea.line2 > curbuf->b_ml.ml_line_count)
2088 {
2089 /* With '-' in 'cpoptions' a line number past the file is an
2090 * error, otherwise put it at the end of the file. */
2091 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2092 ea.line2 = -1;
2093 else
2094 ea.line2 = curbuf->b_ml.ml_line_count;
2095 }
2096
2097 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002098 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 else
2100 {
2101 if (ea.line2 == 0)
2102 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 curwin->w_cursor.lnum = ea.line2;
2105 beginline(BL_SOL | BL_FIX);
2106 }
2107 }
2108 goto doend;
2109 }
2110
2111 /* Find the command and let "p" point to after it. */
2112 p = find_command(&ea, NULL);
2113
2114#ifdef FEAT_USR_CMDS
2115 if (p == NULL)
2116 {
2117 if (!ea.skip)
2118 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2119 goto doend;
2120 }
2121 /* Check for wrong commands. */
2122 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2123 {
2124 errormsg = uc_fun_cmd();
2125 goto doend;
2126 }
2127#endif
2128 if (ea.cmdidx == CMD_SIZE)
2129 {
2130 if (!ea.skip)
2131 {
2132 STRCPY(IObuff, _("E492: Not an editor command"));
2133 if (!sourcing)
2134 {
2135 STRCAT(IObuff, ": ");
2136 STRNCAT(IObuff, *cmdlinep, 40);
2137 }
2138 errormsg = IObuff;
2139 }
2140 goto doend;
2141 }
2142
2143 ni = (
2144#ifdef FEAT_USR_CMDS
2145 !USER_CMDIDX(ea.cmdidx) &&
2146#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002147 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002148#ifdef HAVE_EX_SCRIPT_NI
2149 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2150#endif
2151 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153#ifndef FEAT_EVAL
2154 /*
2155 * When the expression evaluation is disabled, recognize the ":if" and
2156 * ":endif" commands and ignore everything in between it.
2157 */
2158 if (ea.cmdidx == CMD_if)
2159 ++if_level;
2160 if (if_level)
2161 {
2162 if (ea.cmdidx == CMD_endif)
2163 --if_level;
2164 goto doend;
2165 }
2166
2167#endif
2168
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002169 /* forced commands */
2170 if (*p == '!' && ea.cmdidx != CMD_substitute
2171 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 ++p;
2174 ea.forceit = TRUE;
2175 }
2176 else
2177 ea.forceit = FALSE;
2178
2179/*
2180 * 5. parse arguments
2181 */
2182#ifdef FEAT_USR_CMDS
2183 if (!USER_CMDIDX(ea.cmdidx))
2184#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002185 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 if (!ea.skip)
2188 {
2189#ifdef HAVE_SANDBOX
2190 if (sandbox != 0 && !(ea.argt & SBOXOK))
2191 {
2192 /* Command not allowed in sandbox. */
2193 errormsg = (char_u *)_(e_sandbox);
2194 goto doend;
2195 }
2196#endif
2197 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2198 {
2199 /* Command not allowed in non-'modifiable' buffer */
2200 errormsg = (char_u *)_(e_modifiable);
2201 goto doend;
2202 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002203
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002204 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205# ifdef FEAT_USR_CMDS
2206 && !USER_CMDIDX(ea.cmdidx)
2207# endif
2208 )
2209 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002210 /* Command not allowed when editing the command line. */
2211#ifdef FEAT_CMDWIN
2212 if (cmdwin_type != 0)
2213 errormsg = (char_u *)_(e_cmdwin);
2214 else
2215#endif
2216 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 goto doend;
2218 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002220 /* Disallow editing another buffer when "curbuf_lock" is set.
2221 * Do allow ":edit" (check for argument later).
2222 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002224 && ea.cmdidx != CMD_edit
2225 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226# ifdef FEAT_USR_CMDS
2227 && !USER_CMDIDX(ea.cmdidx)
2228# endif
2229 && curbuf_locked())
2230 goto doend;
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2234 {
2235 /* no range allowed */
2236 errormsg = (char_u *)_(e_norange);
2237 goto doend;
2238 }
2239 }
2240
2241 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2242 {
2243 errormsg = (char_u *)_(e_nobang);
2244 goto doend;
2245 }
2246
2247 /*
2248 * Don't complain about the range if it is not used
2249 * (could happen if line_count is accidentally set to 0).
2250 */
2251 if (!ea.skip && !ni)
2252 {
2253 /*
2254 * If the range is backwards, ask for confirmation and, if given, swap
2255 * ea.line1 & ea.line2 so it's forwards again.
2256 * When global command is busy, don't ask, will fail below.
2257 */
2258 if (!global_busy && ea.line1 > ea.line2)
2259 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002260 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002262 if (sourcing || exmode_active)
2263 {
2264 errormsg = (char_u *)_("E493: Backwards range given");
2265 goto doend;
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (ask_yesno((char_u *)
2268 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002269 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 lnum = ea.line1;
2272 ea.line1 = ea.line2;
2273 ea.line2 = lnum;
2274 }
2275 if ((errormsg = invalid_range(&ea)) != NULL)
2276 goto doend;
2277 }
2278
2279 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2280 ea.line2 = 1;
2281
2282 correct_range(&ea);
2283
2284#ifdef FEAT_FOLDING
2285 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2286 {
2287 /* Put the first line at the start of a closed fold, put the last line
2288 * at the end of a closed fold. */
2289 (void)hasFolding(ea.line1, &ea.line1, NULL);
2290 (void)hasFolding(ea.line2, NULL, &ea.line2);
2291 }
2292#endif
2293
2294#ifdef FEAT_QUICKFIX
2295 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002296 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 * option here, so things like % get expanded.
2298 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002299 p = replace_makeprg(&ea, p, cmdlinep);
2300 if (p == NULL)
2301 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#endif
2303
2304 /*
2305 * Skip to start of argument.
2306 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2307 */
2308 if (ea.cmdidx == CMD_bang)
2309 ea.arg = p;
2310 else
2311 ea.arg = skipwhite(p);
2312
2313 /*
2314 * Check for "++opt=val" argument.
2315 * Must be first, allow ":w ++enc=utf8 !cmd"
2316 */
2317 if (ea.argt & ARGOPT)
2318 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2319 if (getargopt(&ea) == FAIL && !ni)
2320 {
2321 errormsg = (char_u *)_(e_invarg);
2322 goto doend;
2323 }
2324
2325 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2326 {
2327 if (*ea.arg == '>') /* append */
2328 {
2329 if (*++ea.arg != '>') /* typed wrong */
2330 {
2331 errormsg = (char_u *)_("E494: Use w or w>>");
2332 goto doend;
2333 }
2334 ea.arg = skipwhite(ea.arg + 1);
2335 ea.append = TRUE;
2336 }
2337 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2338 {
2339 ++ea.arg;
2340 ea.usefilter = TRUE;
2341 }
2342 }
2343
2344 if (ea.cmdidx == CMD_read)
2345 {
2346 if (ea.forceit)
2347 {
2348 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2349 ea.forceit = FALSE;
2350 }
2351 else if (*ea.arg == '!') /* :r !filter */
2352 {
2353 ++ea.arg;
2354 ea.usefilter = TRUE;
2355 }
2356 }
2357
2358 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2359 {
2360 ea.amount = 1;
2361 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2362 {
2363 ++ea.arg;
2364 ++ea.amount;
2365 }
2366 ea.arg = skipwhite(ea.arg);
2367 }
2368
2369 /*
2370 * Check for "+command" argument, before checking for next command.
2371 * Don't do this for ":read !cmd" and ":write !cmd".
2372 */
2373 if ((ea.argt & EDITCMD) && !ea.usefilter)
2374 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2375
2376 /*
2377 * Check for '|' to separate commands and '"' to start comments.
2378 * Don't do this for ":read !cmd" and ":write !cmd".
2379 */
2380 if ((ea.argt & TRLBAR) && !ea.usefilter)
2381 separate_nextcmd(&ea);
2382
2383 /*
2384 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002385 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2386 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388 else if (ea.cmdidx == CMD_bang
2389 || ea.cmdidx == CMD_global
2390 || ea.cmdidx == CMD_vglobal
2391 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 for (p = ea.arg; *p; ++p)
2394 {
2395 /* Remove one backslash before a newline, so that it's possible to
2396 * pass a newline to the shell and also a newline that is preceded
2397 * with a backslash. This makes it impossible to end a shell
2398 * command in a backslash, but that doesn't appear useful.
2399 * Halving the number of backslashes is incompatible with previous
2400 * versions. */
2401 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002402 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 else if (*p == '\n')
2404 {
2405 ea.nextcmd = p + 1;
2406 *p = NUL;
2407 break;
2408 }
2409 }
2410 }
2411
2412 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2413 {
2414 ea.line1 = 1;
2415 ea.line2 = curbuf->b_ml.ml_line_count;
2416 }
2417
2418 /* accept numbered register only when no count allowed (:put) */
2419 if ( (ea.argt & REGSTR)
2420 && *ea.arg != NUL
2421#ifdef FEAT_USR_CMDS
2422 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2423 && USER_CMDIDX(ea.cmdidx)))
2424 /* Do not allow register = for user commands */
2425 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2426#else
2427 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2428#endif
2429 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2430 {
2431 ea.regname = *ea.arg++;
2432#ifdef FEAT_EVAL
2433 /* for '=' register: accept the rest of the line as an expression */
2434 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2435 {
2436 set_expr_line(vim_strsave(ea.arg));
2437 ea.arg += STRLEN(ea.arg);
2438 }
2439#endif
2440 ea.arg = skipwhite(ea.arg);
2441 }
2442
2443 /*
2444 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2445 * count, it's a buffer name.
2446 */
2447 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2448 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2449 || vim_iswhite(*p)))
2450 {
2451 n = getdigits(&ea.arg);
2452 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002453 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455 errormsg = (char_u *)_(e_zerocount);
2456 goto doend;
2457 }
2458 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2459 {
2460 ea.line2 = n;
2461 if (ea.addr_count == 0)
2462 ea.addr_count = 1;
2463 }
2464 else
2465 {
2466 ea.line1 = ea.line2;
2467 ea.line2 += n - 1;
2468 ++ea.addr_count;
2469 /*
2470 * Be vi compatible: no error message for out of range.
2471 */
2472 if (ea.line2 > curbuf->b_ml.ml_line_count)
2473 ea.line2 = curbuf->b_ml.ml_line_count;
2474 }
2475 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002476
2477 /*
2478 * Check for flags: 'l', 'p' and '#'.
2479 */
2480 if (ea.argt & EXFLAGS)
2481 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002483 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002484 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 errormsg = (char_u *)_(e_trailing);
2487 goto doend;
2488 }
2489
2490 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2491 {
2492 errormsg = (char_u *)_(e_argreq);
2493 goto doend;
2494 }
2495
2496#ifdef FEAT_EVAL
2497 /*
2498 * Skip the command when it's not going to be executed.
2499 * The commands like :if, :endif, etc. always need to be executed.
2500 * Also make an exception for commands that handle a trailing command
2501 * themselves.
2502 */
2503 if (ea.skip)
2504 {
2505 switch (ea.cmdidx)
2506 {
2507 /* commands that need evaluation */
2508 case CMD_while:
2509 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002510 case CMD_for:
2511 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_if:
2513 case CMD_elseif:
2514 case CMD_else:
2515 case CMD_endif:
2516 case CMD_try:
2517 case CMD_catch:
2518 case CMD_finally:
2519 case CMD_endtry:
2520 case CMD_function:
2521 break;
2522
2523 /* Commands that handle '|' themselves. Check: A command should
2524 * either have the TRLBAR flag, appear in this list or appear in
2525 * the list at ":help :bar". */
2526 case CMD_aboveleft:
2527 case CMD_and:
2528 case CMD_belowright:
2529 case CMD_botright:
2530 case CMD_browse:
2531 case CMD_call:
2532 case CMD_confirm:
2533 case CMD_delfunction:
2534 case CMD_djump:
2535 case CMD_dlist:
2536 case CMD_dsearch:
2537 case CMD_dsplit:
2538 case CMD_echo:
2539 case CMD_echoerr:
2540 case CMD_echomsg:
2541 case CMD_echon:
2542 case CMD_execute:
2543 case CMD_help:
2544 case CMD_hide:
2545 case CMD_ijump:
2546 case CMD_ilist:
2547 case CMD_isearch:
2548 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002549 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 case CMD_keepjumps:
2551 case CMD_keepmarks:
2552 case CMD_leftabove:
2553 case CMD_let:
2554 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002555 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002557 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 case CMD_perl:
2559 case CMD_psearch:
2560 case CMD_python:
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002561 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 case CMD_return:
2563 case CMD_rightbelow:
2564 case CMD_ruby:
2565 case CMD_silent:
2566 case CMD_smagic:
2567 case CMD_snomagic:
2568 case CMD_substitute:
2569 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002570 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 case CMD_tcl:
2572 case CMD_throw:
2573 case CMD_tilde:
2574 case CMD_topleft:
2575 case CMD_unlet:
2576 case CMD_verbose:
2577 case CMD_vertical:
2578 break;
2579
2580 default: goto doend;
2581 }
2582 }
2583#endif
2584
2585 if (ea.argt & XFILE)
2586 {
2587 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2588 goto doend;
2589 }
2590
2591#ifdef FEAT_LISTCMDS
2592 /*
2593 * Accept buffer name. Cannot be used at the same time with a buffer
2594 * number. Don't do this for a user command.
2595 */
2596 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2597# ifdef FEAT_USR_CMDS
2598 && !USER_CMDIDX(ea.cmdidx)
2599# endif
2600 )
2601 {
2602 /*
2603 * :bdelete, :bwipeout and :bunload take several arguments, separated
2604 * by spaces: find next space (skipping over escaped characters).
2605 * The others take one argument: ignore trailing spaces.
2606 */
2607 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2608 || ea.cmdidx == CMD_bunload)
2609 p = skiptowhite_esc(ea.arg);
2610 else
2611 {
2612 p = ea.arg + STRLEN(ea.arg);
2613 while (p > ea.arg && vim_iswhite(p[-1]))
2614 --p;
2615 }
2616 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2617 if (ea.line2 < 0) /* failed */
2618 goto doend;
2619 ea.addr_count = 1;
2620 ea.arg = skipwhite(p);
2621 }
2622#endif
2623
2624/*
2625 * 6. switch on command name
2626 *
2627 * The "ea" structure holds the arguments that can be used.
2628 */
2629 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002630 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 ea.cookie = cookie;
2632#ifdef FEAT_EVAL
2633 ea.cstack = cstack;
2634#endif
2635
2636#ifdef FEAT_USR_CMDS
2637 if (USER_CMDIDX(ea.cmdidx))
2638 {
2639 /*
2640 * Execute a user-defined command.
2641 */
2642 do_ucmd(&ea);
2643 }
2644 else
2645#endif
2646 {
2647 /*
2648 * Call the function to execute the command.
2649 */
2650 ea.errmsg = NULL;
2651 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2652 if (ea.errmsg != NULL)
2653 errormsg = (char_u *)_(ea.errmsg);
2654 }
2655
2656#ifdef FEAT_EVAL
2657 /*
2658 * If the command just executed called do_cmdline(), any throw or ":return"
2659 * or ":finish" encountered there must also check the cstack of the still
2660 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2661 * exception, or reanimate a returned function or finished script file and
2662 * return or finish it again.
2663 */
2664 if (need_rethrow)
2665 do_throw(cstack);
2666 else if (check_cstack)
2667 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002668 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002670 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 && current_func_returned())
2672 do_return(&ea, TRUE, FALSE, NULL);
2673 }
2674 need_rethrow = check_cstack = FALSE;
2675#endif
2676
2677doend:
2678 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2679 curwin->w_cursor.lnum = 1;
2680
2681 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2682 {
2683 if (sourcing)
2684 {
2685 if (errormsg != IObuff)
2686 {
2687 STRCPY(IObuff, errormsg);
2688 errormsg = IObuff;
2689 }
2690 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002691 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
2693 emsg(errormsg);
2694 }
2695#ifdef FEAT_EVAL
2696 do_errthrow(cstack,
2697 (ea.cmdidx != CMD_SIZE
2698# ifdef FEAT_USR_CMDS
2699 && !USER_CMDIDX(ea.cmdidx)
2700# endif
2701 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2702#endif
2703
2704 if (verbose_save >= 0)
2705 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002706#ifdef FEAT_AUTOCMD
2707 if (cmdmod.save_ei != NULL)
2708 {
2709 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002710 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2711 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002712 free_string_option(cmdmod.save_ei);
2713 }
2714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 cmdmod = save_cmdmod;
2717
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002718 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 /* messages could be enabled for a serious error, need to check if the
2721 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002722 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002723 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 emsg_silent -= did_esilent;
2725 if (emsg_silent < 0)
2726 emsg_silent = 0;
2727 /* Restore msg_scroll, it's set by file I/O commands, even when no
2728 * message is actually displayed. */
2729 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002730
2731 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2732 * somewhere in the line. Put it back in the first column. */
2733 if (redirecting())
2734 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002737#ifdef HAVE_SANDBOX
2738 if (did_sandbox)
2739 --sandbox;
2740#endif
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2743 ea.nextcmd = NULL;
2744
2745#ifdef FEAT_EVAL
2746 --ex_nesting_level;
2747#endif
2748
2749 return ea.nextcmd;
2750}
2751#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002752 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif
2754
2755/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002756 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 * If there is a match advance "pp" to the argument and return TRUE.
2758 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002759 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002761 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 char *cmd; /* name of command */
2763 int len; /* required length */
2764{
2765 int i;
2766
2767 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002768 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 break;
2770 if (i >= len && !isalpha((*pp)[i]))
2771 {
2772 *pp = skipwhite(*pp + i);
2773 return TRUE;
2774 }
2775 return FALSE;
2776}
2777
2778/*
2779 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002780 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002782 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 * Returns NULL for an ambiguous user command.
2784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 static char_u *
2786find_command(eap, full)
2787 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002788 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 int len;
2791 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002792 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /*
2795 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002796 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 * - the 'k' command can directly be followed by any character.
2798 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2799 * but :sre[wind] is another command, as are :scrip[tnames],
2800 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002801 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
2803 p = eap->cmd;
2804 if (*p == 'k')
2805 {
2806 eap->cmdidx = CMD_k;
2807 ++p;
2808 }
2809 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002810 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2811 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 || p[1] == 'g'
2813 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2814 || p[1] == 'I'
2815 || (p[1] == 'r' && p[2] != 'e')))
2816 {
2817 eap->cmdidx = CMD_substitute;
2818 ++p;
2819 }
2820 else
2821 {
2822 while (ASCII_ISALPHA(*p))
2823 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002824 /* for python 3.x support (:py3, :python3) */
2825 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
2826 p = skipdigits(p);
2827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 /* check for non-alpha command */
2829 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2830 ++p;
2831 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002832 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2833 {
2834 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2835 * :delete with the 'l' flag. Same for 'p'. */
2836 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002837 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002838 break;
2839 if (i == len - 1)
2840 {
2841 --len;
2842 if (p[-1] == 'l')
2843 eap->flags |= EXFLAG_LIST;
2844 else
2845 eap->flags |= EXFLAG_PRINT;
2846 }
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
2849 if (ASCII_ISLOWER(*eap->cmd))
2850 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2851 else
2852 eap->cmdidx = cmdidxs[26];
2853
2854 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2855 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2856 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2857 (size_t)len) == 0)
2858 {
2859#ifdef FEAT_EVAL
2860 if (full != NULL
2861 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2862 *full = TRUE;
2863#endif
2864 break;
2865 }
2866
2867#ifdef FEAT_USR_CMDS
2868 /* Look for a user defined command as a last resort */
2869 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2870 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002871 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 while (ASCII_ISALNUM(*p))
2873 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002874 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002877 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 eap->cmdidx = CMD_SIZE;
2879 }
2880
2881 return p;
2882}
2883
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002884#ifdef FEAT_USR_CMDS
2885/*
2886 * Search for a user command that matches "eap->cmd".
2887 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2888 * Return a pointer to just after the command.
2889 * Return NULL if there is no matching command.
2890 */
2891 static char_u *
2892find_ucmd(eap, p, full, xp, compl)
2893 exarg_T *eap;
2894 char_u *p; /* end of the command (possibly including count) */
2895 int *full; /* set to TRUE for a full match */
2896 expand_T *xp; /* used for completion, NULL otherwise */
2897 int *compl; /* completion flags or NULL */
2898{
2899 int len = (int)(p - eap->cmd);
2900 int j, k, matchlen = 0;
2901 ucmd_T *uc;
2902 int found = FALSE;
2903 int possible = FALSE;
2904 char_u *cp, *np; /* Point into typed cmd and test name */
2905 garray_T *gap;
2906 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2907 only full match global is accepted. */
2908
2909 /*
2910 * Look for buffer-local user commands first, then global ones.
2911 */
2912 gap = &curbuf->b_ucmds;
2913 for (;;)
2914 {
2915 for (j = 0; j < gap->ga_len; ++j)
2916 {
2917 uc = USER_CMD_GA(gap, j);
2918 cp = eap->cmd;
2919 np = uc->uc_name;
2920 k = 0;
2921 while (k < len && *np != NUL && *cp++ == *np++)
2922 k++;
2923 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2924 {
2925 /* If finding a second match, the command is ambiguous. But
2926 * not if a buffer-local command wasn't a full match and a
2927 * global command is a full match. */
2928 if (k == len && found && *np != NUL)
2929 {
2930 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002931 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002932 amb_local = TRUE;
2933 }
2934
2935 if (!found || (k == len && *np == NUL))
2936 {
2937 /* If we matched up to a digit, then there could
2938 * be another command including the digit that we
2939 * should use instead.
2940 */
2941 if (k == len)
2942 found = TRUE;
2943 else
2944 possible = TRUE;
2945
2946 if (gap == &ucmds)
2947 eap->cmdidx = CMD_USER;
2948 else
2949 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002950 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002951 eap->useridx = j;
2952
2953# ifdef FEAT_CMDL_COMPL
2954 if (compl != NULL)
2955 *compl = uc->uc_compl;
2956# ifdef FEAT_EVAL
2957 if (xp != NULL)
2958 {
2959 xp->xp_arg = uc->uc_compl_arg;
2960 xp->xp_scriptID = uc->uc_scriptID;
2961 }
2962# endif
2963# endif
2964 /* Do not search for further abbreviations
2965 * if this is an exact match. */
2966 matchlen = k;
2967 if (k == len && *np == NUL)
2968 {
2969 if (full != NULL)
2970 *full = TRUE;
2971 amb_local = FALSE;
2972 break;
2973 }
2974 }
2975 }
2976 }
2977
2978 /* Stop if we found a full match or searched all. */
2979 if (j < gap->ga_len || gap == &ucmds)
2980 break;
2981 gap = &ucmds;
2982 }
2983
2984 /* Only found ambiguous matches. */
2985 if (amb_local)
2986 {
2987 if (xp != NULL)
2988 xp->xp_context = EXPAND_UNSUCCESSFUL;
2989 return NULL;
2990 }
2991
2992 /* The match we found may be followed immediately by a number. Move "p"
2993 * back to point to it. */
2994 if (found || possible)
2995 return p + (matchlen - len);
2996 return p;
2997}
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003001static struct cmdmod
3002{
3003 char *name;
3004 int minlen;
3005 int has_count; /* :123verbose :3tab */
3006} cmdmods[] = {
3007 {"aboveleft", 3, FALSE},
3008 {"belowright", 3, FALSE},
3009 {"botright", 2, FALSE},
3010 {"browse", 3, FALSE},
3011 {"confirm", 4, FALSE},
3012 {"hide", 3, FALSE},
3013 {"keepalt", 5, FALSE},
3014 {"keepjumps", 5, FALSE},
3015 {"keepmarks", 3, FALSE},
3016 {"leftabove", 5, FALSE},
3017 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003018 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003019 {"rightbelow", 6, FALSE},
3020 {"sandbox", 3, FALSE},
3021 {"silent", 3, FALSE},
3022 {"tab", 3, TRUE},
3023 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003024 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003025 {"verbose", 4, TRUE},
3026 {"vertical", 4, FALSE},
3027};
3028
3029/*
3030 * Return length of a command modifier (including optional count).
3031 * Return zero when it's not a modifier.
3032 */
3033 int
3034modifier_len(cmd)
3035 char_u *cmd;
3036{
3037 int i, j;
3038 char_u *p = cmd;
3039
3040 if (VIM_ISDIGIT(*cmd))
3041 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003042 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003043 {
3044 for (j = 0; p[j] != NUL; ++j)
3045 if (p[j] != cmdmods[i].name[j])
3046 break;
3047 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3048 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003049 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003050 }
3051 return 0;
3052}
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054/*
3055 * Return > 0 if an Ex command "name" exists.
3056 * Return 2 if there is an exact match.
3057 * Return 3 if there is an ambiguous match.
3058 */
3059 int
3060cmd_exists(name)
3061 char_u *name;
3062{
3063 exarg_T ea;
3064 int full = FALSE;
3065 int i;
3066 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003067 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003070 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 for (j = 0; name[j] != NUL; ++j)
3073 if (name[j] != cmdmods[i].name[j])
3074 break;
3075 if (name[j] == NUL && j >= cmdmods[i].minlen)
3076 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3077 }
3078
Bram Moolenaara9587612006-05-04 21:47:50 +00003079 /* Check built-in commands and user defined commands.
3080 * For ":2match" and ":3match" we need to skip the number. */
3081 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003083 p = find_command(&ea, &full);
3084 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003086 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3087 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003088 if (*skipwhite(p) != NUL)
3089 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3091}
3092#endif
3093
3094/*
3095 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3096 * we don't need/want deleted. Maybe this could be done better if we didn't
3097 * repeat all this stuff. The only problem is that they may not stay
3098 * perfectly compatible with each other, but then the command line syntax
3099 * probably won't change that much -- webb.
3100 */
3101 char_u *
3102set_one_cmd_context(xp, buff)
3103 expand_T *xp;
3104 char_u *buff; /* buffer for command string */
3105{
3106 char_u *p;
3107 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 int len = 0;
3109 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3111 int compl = EXPAND_NOTHING;
3112#endif
3113#ifdef FEAT_CMDL_COMPL
3114 int delim;
3115#endif
3116 int forceit = FALSE;
3117 int usefilter = FALSE; /* filter instead of file name */
3118
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003119 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 xp->xp_pattern = buff;
3121 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124/*
3125 * 2. skip comment lines and leading space, colons or bars
3126 */
3127 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3128 ;
3129 xp->xp_pattern = cmd;
3130
3131 if (*cmd == NUL)
3132 return NULL;
3133 if (*cmd == '"') /* ignore comment lines */
3134 {
3135 xp->xp_context = EXPAND_NOTHING;
3136 return NULL;
3137 }
3138
3139/*
3140 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3141 */
3142 cmd = skip_range(cmd, &xp->xp_context);
3143
3144/*
3145 * 4. parse command
3146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 xp->xp_pattern = cmd;
3148 if (*cmd == NUL)
3149 return NULL;
3150 if (*cmd == '"')
3151 {
3152 xp->xp_context = EXPAND_NOTHING;
3153 return NULL;
3154 }
3155
3156 if (*cmd == '|' || *cmd == '\n')
3157 return cmd + 1; /* There's another command */
3158
3159 /*
3160 * Isolate the command and search for it in the command table.
3161 * Exceptions:
3162 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003163 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3165 */
3166 if (*cmd == 'k' && cmd[1] != 'e')
3167 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003168 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 p = cmd + 1;
3170 }
3171 else
3172 {
3173 p = cmd;
3174 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3175 ++p;
3176 /* check for non-alpha command */
3177 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3178 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 xp->xp_context = EXPAND_UNSUCCESSFUL;
3184 return NULL;
3185 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003187 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3188 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3189 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 break;
3191
3192#ifdef FEAT_USR_CMDS
3193 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3195 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#endif
3197 }
3198
3199 /*
3200 * If the cursor is touching the command, and it ends in an alpha-numeric
3201 * character, complete the command name.
3202 */
3203 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3204 return NULL;
3205
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003206 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3209 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 p = cmd + 1;
3212 }
3213#ifdef FEAT_USR_CMDS
3214 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3215 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003216 ea.cmd = cmd;
3217 p = find_ucmd(&ea, p, NULL, xp,
3218# if defined(FEAT_CMDL_COMPL)
3219 &compl
3220# else
3221 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003224 if (p == NULL)
3225 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227#endif
3228 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 /* Not still touching the command and it was an illegal one */
3232 xp->xp_context = EXPAND_UNSUCCESSFUL;
3233 return NULL;
3234 }
3235
3236 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3237
3238 if (*p == '!') /* forced commands */
3239 {
3240 forceit = TRUE;
3241 ++p;
3242 }
3243
3244/*
3245 * 5. parse arguments
3246 */
3247#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003250 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 arg = skipwhite(p);
3253
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 if (*arg == '>') /* append */
3257 {
3258 if (*++arg == '>')
3259 ++arg;
3260 arg = skipwhite(arg);
3261 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
3264 ++arg;
3265 usefilter = TRUE;
3266 }
3267 }
3268
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003269 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 usefilter = forceit; /* :r! filter if forced */
3272 if (*arg == '!') /* :r !filter */
3273 {
3274 ++arg;
3275 usefilter = TRUE;
3276 }
3277 }
3278
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003279 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 while (*arg == *cmd) /* allow any number of '>' or '<' */
3282 ++arg;
3283 arg = skipwhite(arg);
3284 }
3285
3286 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
3289 /* Check if we're in the +command */
3290 p = arg + 1;
3291 arg = skip_cmd_arg(arg, FALSE);
3292
3293 /* Still touching the command after '+'? */
3294 if (*arg == NUL)
3295 return p;
3296
3297 /* Skip space(s) after +command to get to the real argument */
3298 arg = skipwhite(arg);
3299 }
3300
3301 /*
3302 * Check for '|' to separate commands and '"' to start comments.
3303 * Don't do this for ":read !cmd" and ":write !cmd".
3304 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 p = arg;
3308 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 p += 2;
3311 while (*p)
3312 {
3313 if (*p == Ctrl_V)
3314 {
3315 if (p[1] != NUL)
3316 ++p;
3317 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 || *p == '|' || *p == '\n')
3320 {
3321 if (*(p - 1) != '\\')
3322 {
3323 if (*p == '|' || *p == '\n')
3324 return p + 1;
3325 return NULL; /* It's a comment */
3326 }
3327 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003328 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330 }
3331
3332 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 vim_strchr((char_u *)"|\"", *arg) == NULL)
3335 return NULL;
3336
3337 /* Find start of last argument (argument just before cursor): */
3338 p = buff + STRLEN(buff);
3339 while (p != arg && *p != ' ' && *p != TAB)
3340 p--;
3341 if (*p == ' ' || *p == TAB)
3342 p++;
3343 xp->xp_pattern = p;
3344
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003345 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003347 int c;
3348 int in_quote = FALSE;
3349 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 /*
3352 * Allow spaces within back-quotes to count as part of the argument
3353 * being expanded.
3354 */
3355 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003356 p = xp->xp_pattern;
3357 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003359#ifdef FEAT_MBYTE
3360 if (has_mbyte)
3361 c = mb_ptr2char(p);
3362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003364 c = *p;
3365 if (c == '\\' && p[1] != NUL)
3366 ++p;
3367 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
3369 if (!in_quote)
3370 {
3371 xp->xp_pattern = p;
3372 bow = p + 1;
3373 }
3374 in_quote = !in_quote;
3375 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003376 /* An argument can contain just about everything, except
3377 * characters that end the command and white space. */
3378 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003379#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003380 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003381#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003382 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003383 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003384 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003385 while (*p != NUL)
3386 {
3387#ifdef FEAT_MBYTE
3388 if (has_mbyte)
3389 c = mb_ptr2char(p);
3390 else
3391#endif
3392 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003393 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003394 break;
3395#ifdef FEAT_MBYTE
3396 if (has_mbyte)
3397 len = (*mb_ptr2len)(p);
3398 else
3399#endif
3400 len = 1;
3401 mb_ptr_adv(p);
3402 }
3403 if (in_quote)
3404 bow = p;
3405 else
3406 xp->xp_pattern = p;
3407 p -= len;
3408 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003409 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411
3412 /*
3413 * If we are still inside the quotes, and we passed a space, just
3414 * expand from there.
3415 */
3416 if (bow != NULL && in_quote)
3417 xp->xp_pattern = bow;
3418 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003419
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003420#ifndef BACKSLASH_IN_FILENAME
3421 /* For a shell command more chars need to be escaped. */
3422 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003423 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003424 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003425
3426 /* When still after the command name expand executables. */
3427 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003428 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003429 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 /* Check for environment variable */
3433 if (*xp->xp_pattern == '$'
3434#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3435 || *xp->xp_pattern == '%'
3436#endif
3437 )
3438 {
3439 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3440 if (!vim_isIDc(*p))
3441 break;
3442 if (*p == NUL)
3443 {
3444 xp->xp_context = EXPAND_ENV_VARS;
3445 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003446#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3447 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003448 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003449 compl = EXPAND_ENV_VARS;
3450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452 }
3453 }
3454
3455/*
3456 * 6. switch on command name
3457 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003460 case CMD_find:
3461 case CMD_sfind:
3462 case CMD_tabfind:
3463 xp->xp_context = EXPAND_FILES_IN_PATH;
3464 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 case CMD_cd:
3466 case CMD_chdir:
3467 case CMD_lcd:
3468 case CMD_lchdir:
3469 if (xp->xp_context == EXPAND_FILES)
3470 xp->xp_context = EXPAND_DIRECTORIES;
3471 break;
3472 case CMD_help:
3473 xp->xp_context = EXPAND_HELP;
3474 xp->xp_pattern = arg;
3475 break;
3476
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003477 /* Command modifiers: return the argument.
3478 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003480 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 case CMD_belowright:
3482 case CMD_botright:
3483 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003484 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003486 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 case CMD_folddoclosed:
3488 case CMD_folddoopen:
3489 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003490 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 case CMD_keepjumps:
3492 case CMD_keepmarks:
3493 case CMD_leftabove:
3494 case CMD_lockmarks:
3495 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003496 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003498 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 case CMD_topleft:
3500 case CMD_verbose:
3501 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003502 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return arg;
3504
Bram Moolenaar4f688582007-07-24 12:34:30 +00003505#ifdef FEAT_CMDL_COMPL
3506# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 case CMD_match:
3508 if (*arg == NUL || !ends_excmd(*arg))
3509 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003510 /* also complete "None" */
3511 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 arg = skipwhite(skiptowhite(arg));
3513 if (*arg != NUL)
3514 {
3515 xp->xp_context = EXPAND_NOTHING;
3516 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3517 }
3518 }
3519 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522/*
3523 * All completion for the +cmdline_compl feature goes here.
3524 */
3525
3526# ifdef FEAT_USR_CMDS
3527 case CMD_command:
3528 /* Check for attributes */
3529 while (*arg == '-')
3530 {
3531 arg++; /* Skip "-" */
3532 p = skiptowhite(arg);
3533 if (*p == NUL)
3534 {
3535 /* Cursor is still in the attribute */
3536 p = vim_strchr(arg, '=');
3537 if (p == NULL)
3538 {
3539 /* No "=", so complete attribute names */
3540 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3541 xp->xp_pattern = arg;
3542 return NULL;
3543 }
3544
3545 /* For the -complete and -nargs attributes, we complete
3546 * their arguments as well.
3547 */
3548 if (STRNICMP(arg, "complete", p - arg) == 0)
3549 {
3550 xp->xp_context = EXPAND_USER_COMPLETE;
3551 xp->xp_pattern = p + 1;
3552 return NULL;
3553 }
3554 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3555 {
3556 xp->xp_context = EXPAND_USER_NARGS;
3557 xp->xp_pattern = p + 1;
3558 return NULL;
3559 }
3560 return NULL;
3561 }
3562 arg = skipwhite(p);
3563 }
3564
3565 /* After the attributes comes the new command name */
3566 p = skiptowhite(arg);
3567 if (*p == NUL)
3568 {
3569 xp->xp_context = EXPAND_USER_COMMANDS;
3570 xp->xp_pattern = arg;
3571 break;
3572 }
3573
3574 /* And finally comes a normal command */
3575 return skipwhite(p);
3576
3577 case CMD_delcommand:
3578 xp->xp_context = EXPAND_USER_COMMANDS;
3579 xp->xp_pattern = arg;
3580 break;
3581# endif
3582
3583 case CMD_global:
3584 case CMD_vglobal:
3585 delim = *arg; /* get the delimiter */
3586 if (delim)
3587 ++arg; /* skip delimiter if there is one */
3588
3589 while (arg[0] != NUL && arg[0] != delim)
3590 {
3591 if (arg[0] == '\\' && arg[1] != NUL)
3592 ++arg;
3593 ++arg;
3594 }
3595 if (arg[0] != NUL)
3596 return arg + 1;
3597 break;
3598 case CMD_and:
3599 case CMD_substitute:
3600 delim = *arg;
3601 if (delim)
3602 {
3603 /* skip "from" part */
3604 ++arg;
3605 arg = skip_regexp(arg, delim, p_magic, NULL);
3606 }
3607 /* skip "to" part */
3608 while (arg[0] != NUL && arg[0] != delim)
3609 {
3610 if (arg[0] == '\\' && arg[1] != NUL)
3611 ++arg;
3612 ++arg;
3613 }
3614 if (arg[0] != NUL) /* skip delimiter */
3615 ++arg;
3616 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3617 ++arg;
3618 if (arg[0] != NUL)
3619 return arg;
3620 break;
3621 case CMD_isearch:
3622 case CMD_dsearch:
3623 case CMD_ilist:
3624 case CMD_dlist:
3625 case CMD_ijump:
3626 case CMD_psearch:
3627 case CMD_djump:
3628 case CMD_isplit:
3629 case CMD_dsplit:
3630 arg = skipwhite(skipdigits(arg)); /* skip count */
3631 if (*arg == '/') /* Match regexp, not just whole words */
3632 {
3633 for (++arg; *arg && *arg != '/'; arg++)
3634 if (*arg == '\\' && arg[1] != NUL)
3635 arg++;
3636 if (*arg)
3637 {
3638 arg = skipwhite(arg + 1);
3639
3640 /* Check for trailing illegal characters */
3641 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3642 xp->xp_context = EXPAND_NOTHING;
3643 else
3644 return arg;
3645 }
3646 }
3647 break;
3648#ifdef FEAT_AUTOCMD
3649 case CMD_autocmd:
3650 return set_context_in_autocmd(xp, arg, FALSE);
3651
3652 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003653 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return set_context_in_autocmd(xp, arg, TRUE);
3655#endif
3656 case CMD_set:
3657 set_context_in_set_cmd(xp, arg, 0);
3658 break;
3659 case CMD_setglobal:
3660 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3661 break;
3662 case CMD_setlocal:
3663 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3664 break;
3665 case CMD_tag:
3666 case CMD_stag:
3667 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003668 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 case CMD_tselect:
3670 case CMD_stselect:
3671 case CMD_ptselect:
3672 case CMD_tjump:
3673 case CMD_stjump:
3674 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003675 if (*p_wop != NUL)
3676 xp->xp_context = EXPAND_TAGS_LISTFILES;
3677 else
3678 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 xp->xp_pattern = arg;
3680 break;
3681 case CMD_augroup:
3682 xp->xp_context = EXPAND_AUGROUP;
3683 xp->xp_pattern = arg;
3684 break;
3685#ifdef FEAT_SYN_HL
3686 case CMD_syntax:
3687 set_context_in_syntax_cmd(xp, arg);
3688 break;
3689#endif
3690#ifdef FEAT_EVAL
3691 case CMD_let:
3692 case CMD_if:
3693 case CMD_elseif:
3694 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003695 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 case CMD_echo:
3697 case CMD_echon:
3698 case CMD_execute:
3699 case CMD_echomsg:
3700 case CMD_echoerr:
3701 case CMD_call:
3702 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003703 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 break;
3705
3706 case CMD_unlet:
3707 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3708 arg = xp->xp_pattern + 1;
3709 xp->xp_context = EXPAND_USER_VARS;
3710 xp->xp_pattern = arg;
3711 break;
3712
3713 case CMD_function:
3714 case CMD_delfunction:
3715 xp->xp_context = EXPAND_USER_FUNC;
3716 xp->xp_pattern = arg;
3717 break;
3718
3719 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003720 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 break;
3722#endif
3723 case CMD_highlight:
3724 set_context_in_highlight_cmd(xp, arg);
3725 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003726#ifdef FEAT_CSCOPE
3727 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003728 case CMD_lcscope:
3729 case CMD_scscope:
3730 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003731 break;
3732#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003733#ifdef FEAT_SIGNS
3734 case CMD_sign:
3735 set_context_in_sign_cmd(xp, arg);
3736 break;
3737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738#ifdef FEAT_LISTCMDS
3739 case CMD_bdelete:
3740 case CMD_bwipeout:
3741 case CMD_bunload:
3742 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3743 arg = xp->xp_pattern + 1;
3744 /*FALLTHROUGH*/
3745 case CMD_buffer:
3746 case CMD_sbuffer:
3747 case CMD_checktime:
3748 xp->xp_context = EXPAND_BUFFERS;
3749 xp->xp_pattern = arg;
3750 break;
3751#endif
3752#ifdef FEAT_USR_CMDS
3753 case CMD_USER:
3754 case CMD_USER_BUF:
3755 if (compl != EXPAND_NOTHING)
3756 {
3757 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003758 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760# ifdef FEAT_MENU
3761 if (compl == EXPAND_MENUS)
3762 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3763# endif
3764 if (compl == EXPAND_COMMANDS)
3765 return arg;
3766 if (compl == EXPAND_MAPPINGS)
3767 return set_context_in_map_cmd(xp, (char_u *)"map",
3768 arg, forceit, FALSE, FALSE, CMD_map);
3769 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3770 arg = xp->xp_pattern + 1;
3771 xp->xp_pattern = arg;
3772 }
3773 xp->xp_context = compl;
3774 }
3775 break;
3776#endif
3777 case CMD_map: case CMD_noremap:
3778 case CMD_nmap: case CMD_nnoremap:
3779 case CMD_vmap: case CMD_vnoremap:
3780 case CMD_omap: case CMD_onoremap:
3781 case CMD_imap: case CMD_inoremap:
3782 case CMD_cmap: case CMD_cnoremap:
3783 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003784 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 case CMD_unmap:
3786 case CMD_nunmap:
3787 case CMD_vunmap:
3788 case CMD_ounmap:
3789 case CMD_iunmap:
3790 case CMD_cunmap:
3791 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003792 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 case CMD_abbreviate: case CMD_noreabbrev:
3794 case CMD_cabbrev: case CMD_cnoreabbrev:
3795 case CMD_iabbrev: case CMD_inoreabbrev:
3796 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003797 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 case CMD_unabbreviate:
3799 case CMD_cunabbrev:
3800 case CMD_iunabbrev:
3801 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003802 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#ifdef FEAT_MENU
3804 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3805 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3806 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3807 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3808 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3809 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3810 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3811 case CMD_tmenu: case CMD_tunmenu:
3812 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3813 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3814#endif
3815
3816 case CMD_colorscheme:
3817 xp->xp_context = EXPAND_COLORS;
3818 xp->xp_pattern = arg;
3819 break;
3820
3821 case CMD_compiler:
3822 xp->xp_context = EXPAND_COMPILER;
3823 xp->xp_pattern = arg;
3824 break;
3825
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003826 case CMD_ownsyntax:
3827 xp->xp_context = EXPAND_FILETYPE;
3828 xp->xp_pattern = arg;
3829 break;
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3832 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3833 case CMD_language:
3834 if (*skiptowhite(arg) == NUL)
3835 {
3836 xp->xp_context = EXPAND_LANGUAGE;
3837 xp->xp_pattern = arg;
3838 }
3839 else
3840 xp->xp_context = EXPAND_NOTHING;
3841 break;
3842#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003843#if defined(FEAT_PROFILE)
3844 case CMD_profile:
3845 set_context_in_profile_cmd(xp, arg);
3846 break;
3847#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003848 case CMD_behave:
3849 xp->xp_context = EXPAND_BEHAVE;
3850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
3852#endif /* FEAT_CMDL_COMPL */
3853
3854 default:
3855 break;
3856 }
3857 return NULL;
3858}
3859
3860/*
3861 * skip a range specifier of the form: addr [,addr] [;addr] ..
3862 *
3863 * Backslashed delimiters after / or ? will be skipped, and commands will
3864 * not be expanded between /'s and ?'s or after "'".
3865 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003866 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * Returns the "cmd" pointer advanced to beyond the range.
3868 */
3869 char_u *
3870skip_range(cmd, ctx)
3871 char_u *cmd;
3872 int *ctx; /* pointer to xp_context or NULL */
3873{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003874 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaardf177f62005-02-22 08:39:57 +00003876 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 if (*cmd == '\'')
3879 {
3880 if (*++cmd == NUL && ctx != NULL)
3881 *ctx = EXPAND_NOTHING;
3882 }
3883 else if (*cmd == '/' || *cmd == '?')
3884 {
3885 delim = *cmd++;
3886 while (*cmd != NUL && *cmd != delim)
3887 if (*cmd++ == '\\' && *cmd != NUL)
3888 ++cmd;
3889 if (*cmd == NUL && ctx != NULL)
3890 *ctx = EXPAND_NOTHING;
3891 }
3892 if (*cmd != NUL)
3893 ++cmd;
3894 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003895
3896 /* Skip ":" and white space. */
3897 while (*cmd == ':')
3898 cmd = skipwhite(cmd + 1);
3899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return cmd;
3901}
3902
3903/*
3904 * get a single EX address
3905 *
3906 * Set ptr to the next character after the part that was interpreted.
3907 * Set ptr to NULL when an error is encountered.
3908 *
3909 * Return MAXLNUM when no Ex address was found.
3910 */
3911 static linenr_T
3912get_address(ptr, skip, to_other_file)
3913 char_u **ptr;
3914 int skip; /* only skip the address, don't use it */
3915 int to_other_file; /* flag: may jump to other file */
3916{
3917 int c;
3918 int i;
3919 long n;
3920 char_u *cmd;
3921 pos_T pos;
3922 pos_T *fp;
3923 linenr_T lnum;
3924
3925 cmd = skipwhite(*ptr);
3926 lnum = MAXLNUM;
3927 do
3928 {
3929 switch (*cmd)
3930 {
3931 case '.': /* '.' - Cursor position */
3932 ++cmd;
3933 lnum = curwin->w_cursor.lnum;
3934 break;
3935
3936 case '$': /* '$' - last line */
3937 ++cmd;
3938 lnum = curbuf->b_ml.ml_line_count;
3939 break;
3940
3941 case '\'': /* ''' - mark */
3942 if (*++cmd == NUL)
3943 {
3944 cmd = NULL;
3945 goto error;
3946 }
3947 if (skip)
3948 ++cmd;
3949 else
3950 {
3951 /* Only accept a mark in another file when it is
3952 * used by itself: ":'M". */
3953 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3954 ++cmd;
3955 if (fp == (pos_T *)-1)
3956 /* Jumped to another file. */
3957 lnum = curwin->w_cursor.lnum;
3958 else
3959 {
3960 if (check_mark(fp) == FAIL)
3961 {
3962 cmd = NULL;
3963 goto error;
3964 }
3965 lnum = fp->lnum;
3966 }
3967 }
3968 break;
3969
3970 case '/':
3971 case '?': /* '/' or '?' - search */
3972 c = *cmd++;
3973 if (skip) /* skip "/pat/" */
3974 {
3975 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3976 if (*cmd == c)
3977 ++cmd;
3978 }
3979 else
3980 {
3981 pos = curwin->w_cursor; /* save curwin->w_cursor */
3982 /*
3983 * When '/' or '?' follows another address, start
3984 * from there.
3985 */
3986 if (lnum != MAXLNUM)
3987 curwin->w_cursor.lnum = lnum;
3988 /*
3989 * Start a forward search at the end of the line.
3990 * Start a backward search at the start of the line.
3991 * This makes sure we never match in the current
3992 * line, and can match anywhere in the
3993 * next/previous line.
3994 */
3995 if (c == '/')
3996 curwin->w_cursor.col = MAXCOL;
3997 else
3998 curwin->w_cursor.col = 0;
3999 searchcmdlen = 0;
4000 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004001 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
4003 curwin->w_cursor = pos;
4004 cmd = NULL;
4005 goto error;
4006 }
4007 lnum = curwin->w_cursor.lnum;
4008 curwin->w_cursor = pos;
4009 /* adjust command string pointer */
4010 cmd += searchcmdlen;
4011 }
4012 break;
4013
4014 case '\\': /* "\?", "\/" or "\&", repeat search */
4015 ++cmd;
4016 if (*cmd == '&')
4017 i = RE_SUBST;
4018 else if (*cmd == '?' || *cmd == '/')
4019 i = RE_SEARCH;
4020 else
4021 {
4022 EMSG(_(e_backslash));
4023 cmd = NULL;
4024 goto error;
4025 }
4026
4027 if (!skip)
4028 {
4029 /*
4030 * When search follows another address, start from
4031 * there.
4032 */
4033 if (lnum != MAXLNUM)
4034 pos.lnum = lnum;
4035 else
4036 pos.lnum = curwin->w_cursor.lnum;
4037
4038 /*
4039 * Start the search just like for the above
4040 * do_search().
4041 */
4042 if (*cmd != '?')
4043 pos.col = MAXCOL;
4044 else
4045 pos.col = 0;
4046 if (searchit(curwin, curbuf, &pos,
4047 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004048 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004049 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 lnum = pos.lnum;
4051 else
4052 {
4053 cmd = NULL;
4054 goto error;
4055 }
4056 }
4057 ++cmd;
4058 break;
4059
4060 default:
4061 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4062 lnum = getdigits(&cmd);
4063 }
4064
4065 for (;;)
4066 {
4067 cmd = skipwhite(cmd);
4068 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4069 break;
4070
4071 if (lnum == MAXLNUM)
4072 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4073 if (VIM_ISDIGIT(*cmd))
4074 i = '+'; /* "number" is same as "+number" */
4075 else
4076 i = *cmd++;
4077 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4078 n = 1;
4079 else
4080 n = getdigits(&cmd);
4081 if (i == '-')
4082 lnum -= n;
4083 else
4084 lnum += n;
4085 }
4086 } while (*cmd == '/' || *cmd == '?');
4087
4088error:
4089 *ptr = cmd;
4090 return lnum;
4091}
4092
4093/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004094 * Get flags from an Ex command argument.
4095 */
4096 static void
4097get_flags(eap)
4098 exarg_T *eap;
4099{
4100 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4101 {
4102 if (*eap->arg == 'l')
4103 eap->flags |= EXFLAG_LIST;
4104 else if (*eap->arg == 'p')
4105 eap->flags |= EXFLAG_PRINT;
4106 else
4107 eap->flags |= EXFLAG_NR;
4108 eap->arg = skipwhite(eap->arg + 1);
4109 }
4110}
4111
4112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * Function called for command which is Not Implemented. NI!
4114 */
4115 void
4116ex_ni(eap)
4117 exarg_T *eap;
4118{
4119 if (!eap->skip)
4120 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4121}
4122
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004123#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124/*
4125 * Function called for script command which is Not Implemented. NI!
4126 * Skips over ":perl <<EOF" constructs.
4127 */
4128 static void
4129ex_script_ni(eap)
4130 exarg_T *eap;
4131{
4132 if (!eap->skip)
4133 ex_ni(eap);
4134 else
4135 vim_free(script_get(eap, eap->arg));
4136}
4137#endif
4138
4139/*
4140 * Check range in Ex command for validity.
4141 * Return NULL when valid, error message when invalid.
4142 */
4143 static char_u *
4144invalid_range(eap)
4145 exarg_T *eap;
4146{
4147 if ( eap->line1 < 0
4148 || eap->line2 < 0
4149 || eap->line1 > eap->line2
4150 || ((eap->argt & RANGE)
4151 && !(eap->argt & NOTADR)
4152 && eap->line2 > curbuf->b_ml.ml_line_count
4153#ifdef FEAT_DIFF
4154 + (eap->cmdidx == CMD_diffget)
4155#endif
4156 ))
4157 return (char_u *)_(e_invrange);
4158 return NULL;
4159}
4160
4161/*
4162 * Correct the range for zero line number, if required.
4163 */
4164 static void
4165correct_range(eap)
4166 exarg_T *eap;
4167{
4168 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4169 {
4170 if (eap->line1 == 0)
4171 eap->line1 = 1;
4172 if (eap->line2 == 0)
4173 eap->line2 = 1;
4174 }
4175}
4176
Bram Moolenaar748bf032005-02-02 23:04:36 +00004177#ifdef FEAT_QUICKFIX
4178static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4179
4180/*
4181 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4182 * pattern. Otherwise return eap->arg.
4183 */
4184 static char_u *
4185skip_grep_pat(eap)
4186 exarg_T *eap;
4187{
4188 char_u *p = eap->arg;
4189
Bram Moolenaara37420f2006-02-04 22:37:47 +00004190 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4191 || eap->cmdidx == CMD_vimgrepadd
4192 || eap->cmdidx == CMD_lvimgrepadd
4193 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004194 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004196 if (p == NULL)
4197 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004198 }
4199 return p;
4200}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004201
4202/*
4203 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4204 * in the command line, so that things like % get expanded.
4205 */
4206 static char_u *
4207replace_makeprg(eap, p, cmdlinep)
4208 exarg_T *eap;
4209 char_u *p;
4210 char_u **cmdlinep;
4211{
4212 char_u *new_cmdline;
4213 char_u *program;
4214 char_u *pos;
4215 char_u *ptr;
4216 int len;
4217 int i;
4218
4219 /*
4220 * Don't do it when ":vimgrep" is used for ":grep".
4221 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004222 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4223 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4224 || eap->cmdidx == CMD_grepadd
4225 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004226 && !grep_internal(eap->cmdidx))
4227 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004228 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4229 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004230 {
4231 if (*curbuf->b_p_gp == NUL)
4232 program = p_gp;
4233 else
4234 program = curbuf->b_p_gp;
4235 }
4236 else
4237 {
4238 if (*curbuf->b_p_mp == NUL)
4239 program = p_mp;
4240 else
4241 program = curbuf->b_p_mp;
4242 }
4243
4244 p = skipwhite(p);
4245
4246 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4247 {
4248 /* replace $* by given arguments */
4249 i = 1;
4250 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4251 ++i;
4252 len = (int)STRLEN(p);
4253 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4254 if (new_cmdline == NULL)
4255 return NULL; /* out of memory */
4256 ptr = new_cmdline;
4257 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4258 {
4259 i = (int)(pos - program);
4260 STRNCPY(ptr, program, i);
4261 STRCPY(ptr += i, p);
4262 ptr += len;
4263 program = pos + 2;
4264 }
4265 STRCPY(ptr, program);
4266 }
4267 else
4268 {
4269 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4270 if (new_cmdline == NULL)
4271 return NULL; /* out of memory */
4272 STRCPY(new_cmdline, program);
4273 STRCAT(new_cmdline, " ");
4274 STRCAT(new_cmdline, p);
4275 }
4276 msg_make(p);
4277
4278 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4279 vim_free(*cmdlinep);
4280 *cmdlinep = new_cmdline;
4281 p = new_cmdline;
4282 }
4283 return p;
4284}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004285#endif
4286
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287/*
4288 * Expand file name in Ex command argument.
4289 * Return FAIL for failure, OK otherwise.
4290 */
4291 int
4292expand_filename(eap, cmdlinep, errormsgp)
4293 exarg_T *eap;
4294 char_u **cmdlinep;
4295 char_u **errormsgp;
4296{
4297 int has_wildcards; /* need to expand wildcards */
4298 char_u *repl;
4299 int srclen;
4300 char_u *p;
4301 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004302 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar748bf032005-02-02 23:04:36 +00004304#ifdef FEAT_QUICKFIX
4305 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4306 p = skip_grep_pat(eap);
4307#else
4308 p = eap->arg;
4309#endif
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 /*
4312 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4313 * the file name contains a wildcard it should not cause expanding.
4314 * (it will be expanded anyway if there is a wildcard before replacing).
4315 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004316 has_wildcards = mch_has_wildcard(p);
4317 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004319#ifdef FEAT_EVAL
4320 /* Skip over `=expr`, wildcards in it are not expanded. */
4321 if (p[0] == '`' && p[1] == '=')
4322 {
4323 p += 2;
4324 (void)skip_expr(&p);
4325 if (*p == '`')
4326 ++p;
4327 continue;
4328 }
4329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 /*
4331 * Quick check if this cannot be the start of a special string.
4332 * Also removes backslash before '%', '#' and '<'.
4333 */
4334 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4335 {
4336 ++p;
4337 continue;
4338 }
4339
4340 /*
4341 * Try to find a match at this position.
4342 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004343 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4344 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (*errormsgp != NULL) /* error detected */
4346 return FAIL;
4347 if (repl == NULL) /* no match found */
4348 {
4349 p += srclen;
4350 continue;
4351 }
4352
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004353 /* Wildcards won't be expanded below, the replacement is taken
4354 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4355 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4356 {
4357 char_u *l = repl;
4358
4359 repl = expand_env_save(repl);
4360 vim_free(l);
4361 }
4362
Bram Moolenaar63b92542007-03-27 14:57:09 +00004363 /* Need to escape white space et al. with a backslash.
4364 * Don't do this for:
4365 * - replacement that already has been escaped: "##"
4366 * - shell commands (may have to use quotes instead).
4367 * - non-unix systems when there is a single argument (spaces don't
4368 * separate arguments then).
4369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004371 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 && eap->cmdidx != CMD_bang
4373 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004374 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004376 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004378 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifndef UNIX
4380 && !(eap->argt & NOSPC)
4381#endif
4382 )
4383 {
4384 char_u *l;
4385#ifdef BACKSLASH_IN_FILENAME
4386 /* Don't escape a backslash here, because rem_backslash() doesn't
4387 * remove it later. */
4388 static char_u *nobslash = (char_u *)" \t\"|";
4389# define ESCAPE_CHARS nobslash
4390#else
4391# define ESCAPE_CHARS escape_chars
4392#endif
4393
4394 for (l = repl; *l; ++l)
4395 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4396 {
4397 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4398 if (l != NULL)
4399 {
4400 vim_free(repl);
4401 repl = l;
4402 }
4403 break;
4404 }
4405 }
4406
4407 /* For a shell command a '!' must be escaped. */
4408 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004409 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
4411 char_u *l;
4412
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004413 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (l != NULL)
4415 {
4416 vim_free(repl);
4417 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004418 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (strstr((char *)p_sh, "sh") != NULL)
4420 {
4421 l = vim_strsave_escaped(repl, (char_u *)"!");
4422 if (l != NULL)
4423 {
4424 vim_free(repl);
4425 repl = l;
4426 }
4427 }
4428 }
4429 }
4430
4431 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4432 vim_free(repl);
4433 if (p == NULL)
4434 return FAIL;
4435 }
4436
4437 /*
4438 * One file argument: Expand wildcards.
4439 * Don't do this with ":r !command" or ":w !command".
4440 */
4441 if ((eap->argt & NOSPC) && !eap->usefilter)
4442 {
4443 /*
4444 * May do this twice:
4445 * 1. Replace environment variables.
4446 * 2. Replace any other wildcards, remove backslashes.
4447 */
4448 for (n = 1; n <= 2; ++n)
4449 {
4450 if (n == 2)
4451 {
4452#ifdef UNIX
4453 /*
4454 * Only for Unix we check for more than one file name.
4455 * For other systems spaces are considered to be part
4456 * of the file name.
4457 * Only check here if there is no wildcard, otherwise
4458 * ExpandOne() will check for errors. This allows
4459 * ":e `ls ve*.c`" on Unix.
4460 */
4461 if (!has_wildcards)
4462 for (p = eap->arg; *p; ++p)
4463 {
4464 /* skip escaped characters */
4465 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4466 ++p;
4467 else if (vim_iswhite(*p))
4468 {
4469 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4470 return FAIL;
4471 }
4472 }
4473#endif
4474
4475 /*
4476 * Halve the number of backslashes (this is Vi compatible).
4477 * For Unix and OS/2, when wildcards are expanded, this is
4478 * done by ExpandOne() below.
4479 */
4480#if defined(UNIX) || defined(OS2)
4481 if (!has_wildcards)
4482#endif
4483 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485
4486 if (has_wildcards)
4487 {
4488 if (n == 1)
4489 {
4490 /*
4491 * First loop: May expand environment variables. This
4492 * can be done much faster with expand_env() than with
4493 * something else (e.g., calling a shell).
4494 * After expanding environment variables, check again
4495 * if there are still wildcards present.
4496 */
4497 if (vim_strchr(eap->arg, '$') != NULL
4498 || vim_strchr(eap->arg, '~') != NULL)
4499 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004500 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004501 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 has_wildcards = mch_has_wildcard(NameBuff);
4503 p = NameBuff;
4504 }
4505 else
4506 p = NULL;
4507 }
4508 else /* n == 2 */
4509 {
4510 expand_T xpc;
4511
4512 ExpandInit(&xpc);
4513 xpc.xp_context = EXPAND_FILES;
4514 p = ExpandOne(&xpc, eap->arg, NULL,
4515 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4516 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (p == NULL)
4518 return FAIL;
4519 }
4520 if (p != NULL)
4521 {
4522 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4523 p, cmdlinep);
4524 if (n == 2) /* p came from ExpandOne() */
4525 vim_free(p);
4526 }
4527 }
4528 }
4529 }
4530 return OK;
4531}
4532
4533/*
4534 * Replace part of the command line, keeping eap->cmd, eap->arg and
4535 * eap->nextcmd correct.
4536 * "src" points to the part that is to be replaced, of length "srclen".
4537 * "repl" is the replacement string.
4538 * Returns a pointer to the character after the replaced string.
4539 * Returns NULL for failure.
4540 */
4541 static char_u *
4542repl_cmdline(eap, src, srclen, repl, cmdlinep)
4543 exarg_T *eap;
4544 char_u *src;
4545 int srclen;
4546 char_u *repl;
4547 char_u **cmdlinep;
4548{
4549 int len;
4550 int i;
4551 char_u *new_cmdline;
4552
4553 /*
4554 * The new command line is build in new_cmdline[].
4555 * First allocate it.
4556 * Careful: a "+cmd" argument may have been NUL terminated.
4557 */
4558 len = (int)STRLEN(repl);
4559 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004560 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4562 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4563 return NULL; /* out of memory! */
4564
4565 /*
4566 * Copy the stuff before the expanded part.
4567 * Copy the expanded stuff.
4568 * Copy what came after the expanded part.
4569 * Copy the next commands, if there are any.
4570 */
4571 i = (int)(src - *cmdlinep); /* length of part before match */
4572 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 mch_memmove(new_cmdline + i, repl, (size_t)len);
4575 i += len; /* remember the end of the string */
4576 STRCPY(new_cmdline + i, src + srclen);
4577 src = new_cmdline + i; /* remember where to continue */
4578
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004579 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 i = (int)STRLEN(new_cmdline) + 1;
4582 STRCPY(new_cmdline + i, eap->nextcmd);
4583 eap->nextcmd = new_cmdline + i;
4584 }
4585 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4586 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4587 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4588 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4589 vim_free(*cmdlinep);
4590 *cmdlinep = new_cmdline;
4591
4592 return src;
4593}
4594
4595/*
4596 * Check for '|' to separate commands and '"' to start comments.
4597 */
4598 void
4599separate_nextcmd(eap)
4600 exarg_T *eap;
4601{
4602 char_u *p;
4603
Bram Moolenaar86b68352004-12-27 21:59:20 +00004604#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004605 p = skip_grep_pat(eap);
4606#else
4607 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004608#endif
4609
4610 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612 if (*p == Ctrl_V)
4613 {
4614 if (eap->argt & (USECTRLV | XFILE))
4615 ++p; /* skip CTRL-V and next char */
4616 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004617 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004618 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (*p == NUL) /* stop at NUL after CTRL-V */
4620 break;
4621 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004622
4623#ifdef FEAT_EVAL
4624 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004625 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004626 {
4627 p += 2;
4628 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004629 }
4630#endif
4631
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 /* Check for '"': start of comment or '|': next command */
4633 /* :@" and :*" do not start a comment!
4634 * :redir @" doesn't either. */
4635 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4636 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4637 || p != eap->arg)
4638 && (eap->cmdidx != CMD_redir
4639 || p != eap->arg + 1 || p[-1] != '@'))
4640 || *p == '|' || *p == '\n')
4641 {
4642 /*
4643 * We remove the '\' before the '|', unless USECTRLV is used
4644 * AND 'b' is present in 'cpoptions'.
4645 */
4646 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4647 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4648 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004649 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 --p;
4651 }
4652 else
4653 {
4654 eap->nextcmd = check_nextcmd(p);
4655 *p = NUL;
4656 break;
4657 }
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004660
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4662 del_trailing_spaces(eap->arg);
4663}
4664
4665/*
4666 * get + command from ex argument
4667 */
4668 static char_u *
4669getargcmd(argp)
4670 char_u **argp;
4671{
4672 char_u *arg = *argp;
4673 char_u *command = NULL;
4674
4675 if (*arg == '+') /* +[command] */
4676 {
4677 ++arg;
4678 if (vim_isspace(*arg))
4679 command = dollar_command;
4680 else
4681 {
4682 command = arg;
4683 arg = skip_cmd_arg(command, TRUE);
4684 if (*arg != NUL)
4685 *arg++ = NUL; /* terminate command with NUL */
4686 }
4687
4688 arg = skipwhite(arg); /* skip over spaces */
4689 *argp = arg;
4690 }
4691 return command;
4692}
4693
4694/*
4695 * Find end of "+command" argument. Skip over "\ " and "\\".
4696 */
4697 static char_u *
4698skip_cmd_arg(p, rembs)
4699 char_u *p;
4700 int rembs; /* TRUE to halve the number of backslashes */
4701{
4702 while (*p && !vim_isspace(*p))
4703 {
4704 if (*p == '\\' && p[1] != NUL)
4705 {
4706 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004707 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 else
4709 ++p;
4710 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004711 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713 return p;
4714}
4715
4716/*
4717 * Get "++opt=arg" argument.
4718 * Return FAIL or OK.
4719 */
4720 static int
4721getargopt(eap)
4722 exarg_T *eap;
4723{
4724 char_u *arg = eap->arg + 2;
4725 int *pp = NULL;
4726#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004727 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 char_u *p;
4729#endif
4730
4731 /* ":edit ++[no]bin[ary] file" */
4732 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4733 {
4734 if (*arg == 'n')
4735 {
4736 arg += 2;
4737 eap->force_bin = FORCE_NOBIN;
4738 }
4739 else
4740 eap->force_bin = FORCE_BIN;
4741 if (!checkforcmd(&arg, "binary", 3))
4742 return FAIL;
4743 eap->arg = skipwhite(arg);
4744 return OK;
4745 }
4746
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004747 /* ":read ++edit file" */
4748 if (STRNCMP(arg, "edit", 4) == 0)
4749 {
4750 eap->read_edit = TRUE;
4751 eap->arg = skipwhite(arg + 4);
4752 return OK;
4753 }
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (STRNCMP(arg, "ff", 2) == 0)
4756 {
4757 arg += 2;
4758 pp = &eap->force_ff;
4759 }
4760 else if (STRNCMP(arg, "fileformat", 10) == 0)
4761 {
4762 arg += 10;
4763 pp = &eap->force_ff;
4764 }
4765#ifdef FEAT_MBYTE
4766 else if (STRNCMP(arg, "enc", 3) == 0)
4767 {
4768 arg += 3;
4769 pp = &eap->force_enc;
4770 }
4771 else if (STRNCMP(arg, "encoding", 8) == 0)
4772 {
4773 arg += 8;
4774 pp = &eap->force_enc;
4775 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004776 else if (STRNCMP(arg, "bad", 3) == 0)
4777 {
4778 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004779 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#endif
4782
4783 if (pp == NULL || *arg != '=')
4784 return FAIL;
4785
4786 ++arg;
4787 *pp = (int)(arg - eap->cmd);
4788 arg = skip_cmd_arg(arg, FALSE);
4789 eap->arg = skipwhite(arg);
4790 *arg = NUL;
4791
4792#ifdef FEAT_MBYTE
4793 if (pp == &eap->force_ff)
4794 {
4795#endif
4796 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4797 return FAIL;
4798#ifdef FEAT_MBYTE
4799 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004800 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 /* Make 'fileencoding' lower case. */
4803 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4804 *p = TOLOWER_ASC(*p);
4805 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004806 else
4807 {
4808 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4809 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004810 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004811 if (STRICMP(p, "keep") == 0)
4812 eap->bad_char = BAD_KEEP;
4813 else if (STRICMP(p, "drop") == 0)
4814 eap->bad_char = BAD_DROP;
4815 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4816 eap->bad_char = *p;
4817 else
4818 return FAIL;
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
4821
4822 return OK;
4823}
4824
4825/*
4826 * ":abbreviate" and friends.
4827 */
4828 static void
4829ex_abbreviate(eap)
4830 exarg_T *eap;
4831{
4832 do_exmap(eap, TRUE); /* almost the same as mapping */
4833}
4834
4835/*
4836 * ":map" and friends.
4837 */
4838 static void
4839ex_map(eap)
4840 exarg_T *eap;
4841{
4842 /*
4843 * If we are sourcing .exrc or .vimrc in current directory we
4844 * print the mappings for security reasons.
4845 */
4846 if (secure)
4847 {
4848 secure = 2;
4849 msg_outtrans(eap->cmd);
4850 msg_putchar('\n');
4851 }
4852 do_exmap(eap, FALSE);
4853}
4854
4855/*
4856 * ":unmap" and friends.
4857 */
4858 static void
4859ex_unmap(eap)
4860 exarg_T *eap;
4861{
4862 do_exmap(eap, FALSE);
4863}
4864
4865/*
4866 * ":mapclear" and friends.
4867 */
4868 static void
4869ex_mapclear(eap)
4870 exarg_T *eap;
4871{
4872 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4873}
4874
4875/*
4876 * ":abclear" and friends.
4877 */
4878 static void
4879ex_abclear(eap)
4880 exarg_T *eap;
4881{
4882 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4883}
4884
4885#ifdef FEAT_AUTOCMD
4886 static void
4887ex_autocmd(eap)
4888 exarg_T *eap;
4889{
4890 /*
4891 * Disallow auto commands from .exrc and .vimrc in current
4892 * directory for security reasons.
4893 */
4894 if (secure)
4895 {
4896 secure = 2;
4897 eap->errmsg = e_curdir;
4898 }
4899 else if (eap->cmdidx == CMD_autocmd)
4900 do_autocmd(eap->arg, eap->forceit);
4901 else
4902 do_augroup(eap->arg, eap->forceit);
4903}
4904
4905/*
4906 * ":doautocmd": Apply the automatic commands to the current buffer.
4907 */
4908 static void
4909ex_doautocmd(eap)
4910 exarg_T *eap;
4911{
4912 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004913 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914}
4915#endif
4916
4917#ifdef FEAT_LISTCMDS
4918/*
4919 * :[N]bunload[!] [N] [bufname] unload buffer
4920 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4921 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4922 */
4923 static void
4924ex_bunload(eap)
4925 exarg_T *eap;
4926{
4927 eap->errmsg = do_bufdel(
4928 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4929 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4930 : DOBUF_UNLOAD, eap->arg,
4931 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4932}
4933
4934/*
4935 * :[N]buffer [N] to buffer N
4936 * :[N]sbuffer [N] to buffer N
4937 */
4938 static void
4939ex_buffer(eap)
4940 exarg_T *eap;
4941{
4942 if (*eap->arg)
4943 eap->errmsg = e_trailing;
4944 else
4945 {
4946 if (eap->addr_count == 0) /* default is current buffer */
4947 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4948 else
4949 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4950 }
4951}
4952
4953/*
4954 * :[N]bmodified [N] to next mod. buffer
4955 * :[N]sbmodified [N] to next mod. buffer
4956 */
4957 static void
4958ex_bmodified(eap)
4959 exarg_T *eap;
4960{
4961 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4962}
4963
4964/*
4965 * :[N]bnext [N] to next buffer
4966 * :[N]sbnext [N] split and to next buffer
4967 */
4968 static void
4969ex_bnext(eap)
4970 exarg_T *eap;
4971{
4972 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4973}
4974
4975/*
4976 * :[N]bNext [N] to previous buffer
4977 * :[N]bprevious [N] to previous buffer
4978 * :[N]sbNext [N] split and to previous buffer
4979 * :[N]sbprevious [N] split and to previous buffer
4980 */
4981 static void
4982ex_bprevious(eap)
4983 exarg_T *eap;
4984{
4985 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4986}
4987
4988/*
4989 * :brewind to first buffer
4990 * :bfirst to first buffer
4991 * :sbrewind split and to first buffer
4992 * :sbfirst split and to first buffer
4993 */
4994 static void
4995ex_brewind(eap)
4996 exarg_T *eap;
4997{
4998 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4999}
5000
5001/*
5002 * :blast to last buffer
5003 * :sblast split and to last buffer
5004 */
5005 static void
5006ex_blast(eap)
5007 exarg_T *eap;
5008{
5009 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5010}
5011#endif
5012
5013 int
5014ends_excmd(c)
5015 int c;
5016{
5017 return (c == NUL || c == '|' || c == '"' || c == '\n');
5018}
5019
5020#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5021 || defined(PROTO)
5022/*
5023 * Return the next command, after the first '|' or '\n'.
5024 * Return NULL if not found.
5025 */
5026 char_u *
5027find_nextcmd(p)
5028 char_u *p;
5029{
5030 while (*p != '|' && *p != '\n')
5031 {
5032 if (*p == NUL)
5033 return NULL;
5034 ++p;
5035 }
5036 return (p + 1);
5037}
5038#endif
5039
5040/*
5041 * Check if *p is a separator between Ex commands.
5042 * Return NULL if it isn't, (p + 1) if it is.
5043 */
5044 char_u *
5045check_nextcmd(p)
5046 char_u *p;
5047{
5048 p = skipwhite(p);
5049 if (*p == '|' || *p == '\n')
5050 return (p + 1);
5051 else
5052 return NULL;
5053}
5054
5055/*
5056 * - if there are more files to edit
5057 * - and this is the last window
5058 * - and forceit not used
5059 * - and not repeated twice on a row
5060 * return FAIL and give error message if 'message' TRUE
5061 * return OK otherwise
5062 */
5063 static int
5064check_more(message, forceit)
5065 int message; /* when FALSE check only, no messages */
5066 int forceit;
5067{
5068 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5069
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005070 if (!forceit && only_one_window()
5071 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 if (message)
5074 {
5075#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5076 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5077 {
5078 char_u buff[IOSIZE];
5079
5080 if (n == 1)
5081 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5082 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005083 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 _("%d more files to edit. Quit anyway?"), n);
5085 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5086 return OK;
5087 return FAIL;
5088 }
5089#endif
5090 if (n == 1)
5091 EMSG(_("E173: 1 more file to edit"));
5092 else
5093 EMSGN(_("E173: %ld more files to edit"), n);
5094 quitmore = 2; /* next try to quit is allowed */
5095 }
5096 return FAIL;
5097 }
5098 return OK;
5099}
5100
5101#ifdef FEAT_CMDL_COMPL
5102/*
5103 * Function given to ExpandGeneric() to obtain the list of command names.
5104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 char_u *
5106get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005107 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 int idx;
5109{
5110 if (idx >= (int)CMD_SIZE)
5111# ifdef FEAT_USR_CMDS
5112 return get_user_command_name(idx);
5113# else
5114 return NULL;
5115# endif
5116 return cmdnames[idx].cmd_name;
5117}
5118#endif
5119
5120#if defined(FEAT_USR_CMDS) || defined(PROTO)
5121static 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));
5122static void uc_list __ARGS((char_u *name, size_t name_len));
5123static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5124static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5125static 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));
5126
5127 static int
5128uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5129 char_u *name;
5130 size_t name_len;
5131 char_u *rep;
5132 long argt;
5133 long def;
5134 int flags;
5135 int compl;
5136 char_u *compl_arg;
5137 int force;
5138{
5139 ucmd_T *cmd = NULL;
5140 char_u *p;
5141 int i;
5142 int cmp = 1;
5143 char_u *rep_buf = NULL;
5144 garray_T *gap;
5145
Bram Moolenaar9c102382006-05-03 21:26:49 +00005146 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (rep_buf == NULL)
5148 {
5149 /* Can't replace termcodes - try using the string as is */
5150 rep_buf = vim_strsave(rep);
5151
5152 /* Give up if out of memory */
5153 if (rep_buf == NULL)
5154 return FAIL;
5155 }
5156
5157 /* get address of growarray: global or in curbuf */
5158 if (flags & UC_BUFFER)
5159 {
5160 gap = &curbuf->b_ucmds;
5161 if (gap->ga_itemsize == 0)
5162 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5163 }
5164 else
5165 gap = &ucmds;
5166
5167 /* Search for the command in the already defined commands. */
5168 for (i = 0; i < gap->ga_len; ++i)
5169 {
5170 size_t len;
5171
5172 cmd = USER_CMD_GA(gap, i);
5173 len = STRLEN(cmd->uc_name);
5174 cmp = STRNCMP(name, cmd->uc_name, name_len);
5175 if (cmp == 0)
5176 {
5177 if (name_len < len)
5178 cmp = -1;
5179 else if (name_len > len)
5180 cmp = 1;
5181 }
5182
5183 if (cmp == 0)
5184 {
5185 if (!force)
5186 {
5187 EMSG(_("E174: Command already exists: add ! to replace it"));
5188 goto fail;
5189 }
5190
5191 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005192 cmd->uc_rep = NULL;
5193#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5194 vim_free(cmd->uc_compl_arg);
5195 cmd->uc_compl_arg = NULL;
5196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 break;
5198 }
5199
5200 /* Stop as soon as we pass the name to add */
5201 if (cmp < 0)
5202 break;
5203 }
5204
5205 /* Extend the array unless we're replacing an existing command */
5206 if (cmp != 0)
5207 {
5208 if (ga_grow(gap, 1) != OK)
5209 goto fail;
5210 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5211 goto fail;
5212
5213 cmd = USER_CMD_GA(gap, i);
5214 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5215
5216 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 cmd->uc_name = p;
5219 }
5220
5221 cmd->uc_rep = rep_buf;
5222 cmd->uc_argt = argt;
5223 cmd->uc_def = def;
5224 cmd->uc_compl = compl;
5225#ifdef FEAT_EVAL
5226 cmd->uc_scriptID = current_SID;
5227# ifdef FEAT_CMDL_COMPL
5228 cmd->uc_compl_arg = compl_arg;
5229# endif
5230#endif
5231
5232 return OK;
5233
5234fail:
5235 vim_free(rep_buf);
5236#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5237 vim_free(compl_arg);
5238#endif
5239 return FAIL;
5240}
5241
5242/*
5243 * List of names for completion for ":command" with the EXPAND_ flag.
5244 * Must be alphabetical for completion.
5245 */
5246static struct
5247{
5248 int expand;
5249 char *name;
5250} command_complete[] =
5251{
5252 {EXPAND_AUGROUP, "augroup"},
5253 {EXPAND_BUFFERS, "buffer"},
5254 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005255#if defined(FEAT_CSCOPE)
5256 {EXPAND_CSCOPE, "cscope"},
5257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5259 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005260 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261#endif
5262 {EXPAND_DIRECTORIES, "dir"},
5263 {EXPAND_ENV_VARS, "environment"},
5264 {EXPAND_EVENTS, "event"},
5265 {EXPAND_EXPRESSION, "expression"},
5266 {EXPAND_FILES, "file"},
5267 {EXPAND_FUNCTIONS, "function"},
5268 {EXPAND_HELP, "help"},
5269 {EXPAND_HIGHLIGHT, "highlight"},
5270 {EXPAND_MAPPINGS, "mapping"},
5271 {EXPAND_MENUS, "menu"},
5272 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005273 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005274#if defined(FEAT_SIGNS)
5275 {EXPAND_SIGN, "sign"},
5276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {EXPAND_TAGS, "tag"},
5278 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5279 {EXPAND_USER_VARS, "var"},
5280 {0, NULL}
5281};
5282
5283 static void
5284uc_list(name, name_len)
5285 char_u *name;
5286 size_t name_len;
5287{
5288 int i, j;
5289 int found = FALSE;
5290 ucmd_T *cmd;
5291 int len;
5292 long a;
5293 garray_T *gap;
5294
5295 gap = &curbuf->b_ucmds;
5296 for (;;)
5297 {
5298 for (i = 0; i < gap->ga_len; ++i)
5299 {
5300 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005301 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 /* Skip commands which don't match the requested prefix */
5304 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5305 continue;
5306
5307 /* Put out the title first time */
5308 if (!found)
5309 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5310 found = TRUE;
5311 msg_putchar('\n');
5312 if (got_int)
5313 break;
5314
5315 /* Special cases */
5316 msg_putchar(a & BANG ? '!' : ' ');
5317 msg_putchar(a & REGSTR ? '"' : ' ');
5318 msg_putchar(gap != &ucmds ? 'b' : ' ');
5319 msg_putchar(' ');
5320
5321 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5322 len = (int)STRLEN(cmd->uc_name) + 4;
5323
5324 do {
5325 msg_putchar(' ');
5326 ++len;
5327 } while (len < 16);
5328
5329 len = 0;
5330
5331 /* Arguments */
5332 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5333 {
5334 case 0: IObuff[len++] = '0'; break;
5335 case (EXTRA): IObuff[len++] = '*'; break;
5336 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5337 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5338 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5339 }
5340
5341 do {
5342 IObuff[len++] = ' ';
5343 } while (len < 5);
5344
5345 /* Range */
5346 if (a & (RANGE|COUNT))
5347 {
5348 if (a & COUNT)
5349 {
5350 /* -count=N */
5351 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5352 len += (int)STRLEN(IObuff + len);
5353 }
5354 else if (a & DFLALL)
5355 IObuff[len++] = '%';
5356 else if (cmd->uc_def >= 0)
5357 {
5358 /* -range=N */
5359 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5360 len += (int)STRLEN(IObuff + len);
5361 }
5362 else
5363 IObuff[len++] = '.';
5364 }
5365
5366 do {
5367 IObuff[len++] = ' ';
5368 } while (len < 11);
5369
5370 /* Completion */
5371 for (j = 0; command_complete[j].expand != 0; ++j)
5372 if (command_complete[j].expand == cmd->uc_compl)
5373 {
5374 STRCPY(IObuff + len, command_complete[j].name);
5375 len += (int)STRLEN(IObuff + len);
5376 break;
5377 }
5378
5379 do {
5380 IObuff[len++] = ' ';
5381 } while (len < 21);
5382
5383 IObuff[len] = '\0';
5384 msg_outtrans(IObuff);
5385
5386 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005387#ifdef FEAT_EVAL
5388 if (p_verbose > 0)
5389 last_set_msg(cmd->uc_scriptID);
5390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 out_flush();
5392 ui_breakcheck();
5393 if (got_int)
5394 break;
5395 }
5396 if (gap == &ucmds || i < gap->ga_len)
5397 break;
5398 gap = &ucmds;
5399 }
5400
5401 if (!found)
5402 MSG(_("No user-defined commands found"));
5403}
5404
5405 static char_u *
5406uc_fun_cmd()
5407{
5408 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5409 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5410 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5411 0xb9, 0x7f, 0};
5412 int i;
5413
5414 for (i = 0; fcmd[i]; ++i)
5415 IObuff[i] = fcmd[i] - 0x40;
5416 IObuff[i] = 0;
5417 return IObuff;
5418}
5419
5420 static int
5421uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5422 char_u *attr;
5423 size_t len;
5424 long *argt;
5425 long *def;
5426 int *flags;
5427 int *compl;
5428 char_u **compl_arg;
5429{
5430 char_u *p;
5431
5432 if (len == 0)
5433 {
5434 EMSG(_("E175: No attribute specified"));
5435 return FAIL;
5436 }
5437
5438 /* First, try the simple attributes (no arguments) */
5439 if (STRNICMP(attr, "bang", len) == 0)
5440 *argt |= BANG;
5441 else if (STRNICMP(attr, "buffer", len) == 0)
5442 *flags |= UC_BUFFER;
5443 else if (STRNICMP(attr, "register", len) == 0)
5444 *argt |= REGSTR;
5445 else if (STRNICMP(attr, "bar", len) == 0)
5446 *argt |= TRLBAR;
5447 else
5448 {
5449 int i;
5450 char_u *val = NULL;
5451 size_t vallen = 0;
5452 size_t attrlen = len;
5453
5454 /* Look for the attribute name - which is the part before any '=' */
5455 for (i = 0; i < (int)len; ++i)
5456 {
5457 if (attr[i] == '=')
5458 {
5459 val = &attr[i + 1];
5460 vallen = len - i - 1;
5461 attrlen = i;
5462 break;
5463 }
5464 }
5465
5466 if (STRNICMP(attr, "nargs", attrlen) == 0)
5467 {
5468 if (vallen == 1)
5469 {
5470 if (*val == '0')
5471 /* Do nothing - this is the default */;
5472 else if (*val == '1')
5473 *argt |= (EXTRA | NOSPC | NEEDARG);
5474 else if (*val == '*')
5475 *argt |= EXTRA;
5476 else if (*val == '?')
5477 *argt |= (EXTRA | NOSPC);
5478 else if (*val == '+')
5479 *argt |= (EXTRA | NEEDARG);
5480 else
5481 goto wrong_nargs;
5482 }
5483 else
5484 {
5485wrong_nargs:
5486 EMSG(_("E176: Invalid number of arguments"));
5487 return FAIL;
5488 }
5489 }
5490 else if (STRNICMP(attr, "range", attrlen) == 0)
5491 {
5492 *argt |= RANGE;
5493 if (vallen == 1 && *val == '%')
5494 *argt |= DFLALL;
5495 else if (val != NULL)
5496 {
5497 p = val;
5498 if (*def >= 0)
5499 {
5500two_count:
5501 EMSG(_("E177: Count cannot be specified twice"));
5502 return FAIL;
5503 }
5504
5505 *def = getdigits(&p);
5506 *argt |= (ZEROR | NOTADR);
5507
5508 if (p != val + vallen || vallen == 0)
5509 {
5510invalid_count:
5511 EMSG(_("E178: Invalid default value for count"));
5512 return FAIL;
5513 }
5514 }
5515 }
5516 else if (STRNICMP(attr, "count", attrlen) == 0)
5517 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005518 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 if (val != NULL)
5521 {
5522 p = val;
5523 if (*def >= 0)
5524 goto two_count;
5525
5526 *def = getdigits(&p);
5527
5528 if (p != val + vallen)
5529 goto invalid_count;
5530 }
5531
5532 if (*def < 0)
5533 *def = 0;
5534 }
5535 else if (STRNICMP(attr, "complete", attrlen) == 0)
5536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (val == NULL)
5538 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005539 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 return FAIL;
5541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005543 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5544 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547 else
5548 {
5549 char_u ch = attr[len];
5550 attr[len] = '\0';
5551 EMSG2(_("E181: Invalid attribute: %s"), attr);
5552 attr[len] = ch;
5553 return FAIL;
5554 }
5555 }
5556
5557 return OK;
5558}
5559
Bram Moolenaara850a712009-01-28 14:42:59 +00005560/*
5561 * ":command ..."
5562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 static void
5564ex_command(eap)
5565 exarg_T *eap;
5566{
5567 char_u *name;
5568 char_u *end;
5569 char_u *p;
5570 long argt = 0;
5571 long def = -1;
5572 int flags = 0;
5573 int compl = EXPAND_NOTHING;
5574 char_u *compl_arg = NULL;
5575 int has_attr = (eap->arg[0] == '-');
5576
5577 p = eap->arg;
5578
5579 /* Check for attributes */
5580 while (*p == '-')
5581 {
5582 ++p;
5583 end = skiptowhite(p);
5584 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5585 == FAIL)
5586 return;
5587 p = skipwhite(end);
5588 }
5589
5590 /* Get the name (if any) and skip to the following argument */
5591 name = p;
5592 if (ASCII_ISALPHA(*p))
5593 while (ASCII_ISALNUM(*p))
5594 ++p;
5595 if (!ends_excmd(*p) && !vim_iswhite(*p))
5596 {
5597 EMSG(_("E182: Invalid command name"));
5598 return;
5599 }
5600 end = p;
5601
5602 /* If there is nothing after the name, and no attributes were specified,
5603 * we are listing commands
5604 */
5605 p = skipwhite(end);
5606 if (!has_attr && ends_excmd(*p))
5607 {
5608 uc_list(name, end - name);
5609 }
5610 else if (!ASCII_ISUPPER(*name))
5611 {
5612 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5613 return;
5614 }
5615 else
5616 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5617 eap->forceit);
5618}
5619
5620/*
5621 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 * Clear all user commands, global and for current buffer.
5623 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005624 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005626 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 uc_clear(&ucmds);
5629 uc_clear(&curbuf->b_ucmds);
5630}
5631
5632/*
5633 * Clear all user commands for "gap".
5634 */
5635 void
5636uc_clear(gap)
5637 garray_T *gap;
5638{
5639 int i;
5640 ucmd_T *cmd;
5641
5642 for (i = 0; i < gap->ga_len; ++i)
5643 {
5644 cmd = USER_CMD_GA(gap, i);
5645 vim_free(cmd->uc_name);
5646 vim_free(cmd->uc_rep);
5647# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5648 vim_free(cmd->uc_compl_arg);
5649# endif
5650 }
5651 ga_clear(gap);
5652}
5653
5654 static void
5655ex_delcommand(eap)
5656 exarg_T *eap;
5657{
5658 int i = 0;
5659 ucmd_T *cmd = NULL;
5660 int cmp = -1;
5661 garray_T *gap;
5662
5663 gap = &curbuf->b_ucmds;
5664 for (;;)
5665 {
5666 for (i = 0; i < gap->ga_len; ++i)
5667 {
5668 cmd = USER_CMD_GA(gap, i);
5669 cmp = STRCMP(eap->arg, cmd->uc_name);
5670 if (cmp <= 0)
5671 break;
5672 }
5673 if (gap == &ucmds || cmp == 0)
5674 break;
5675 gap = &ucmds;
5676 }
5677
5678 if (cmp != 0)
5679 {
5680 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5681 return;
5682 }
5683
5684 vim_free(cmd->uc_name);
5685 vim_free(cmd->uc_rep);
5686# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5687 vim_free(cmd->uc_compl_arg);
5688# endif
5689
5690 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692 if (i < gap->ga_len)
5693 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5694}
5695
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005696/*
5697 * split and quote args for <f-args>
5698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 static char_u *
5700uc_split_args(arg, lenp)
5701 char_u *arg;
5702 size_t *lenp;
5703{
5704 char_u *buf;
5705 char_u *p;
5706 char_u *q;
5707 int len;
5708
5709 /* Precalculate length */
5710 p = arg;
5711 len = 2; /* Initial and final quotes */
5712
5713 while (*p)
5714 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005715 if (p[0] == '\\' && p[1] == '\\')
5716 {
5717 len += 2;
5718 p += 2;
5719 }
5720 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 len += 1;
5723 p += 2;
5724 }
5725 else if (*p == '\\' || *p == '"')
5726 {
5727 len += 2;
5728 p += 1;
5729 }
5730 else if (vim_iswhite(*p))
5731 {
5732 p = skipwhite(p);
5733 if (*p == NUL)
5734 break;
5735 len += 3; /* "," */
5736 }
5737 else
5738 {
5739 ++len;
5740 ++p;
5741 }
5742 }
5743
5744 buf = alloc(len + 1);
5745 if (buf == NULL)
5746 {
5747 *lenp = 0;
5748 return buf;
5749 }
5750
5751 p = arg;
5752 q = buf;
5753 *q++ = '"';
5754 while (*p)
5755 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005756 if (p[0] == '\\' && p[1] == '\\')
5757 {
5758 *q++ = '\\';
5759 *q++ = '\\';
5760 p += 2;
5761 }
5762 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
5764 *q++ = p[1];
5765 p += 2;
5766 }
5767 else if (*p == '\\' || *p == '"')
5768 {
5769 *q++ = '\\';
5770 *q++ = *p++;
5771 }
5772 else if (vim_iswhite(*p))
5773 {
5774 p = skipwhite(p);
5775 if (*p == NUL)
5776 break;
5777 *q++ = '"';
5778 *q++ = ',';
5779 *q++ = '"';
5780 }
5781 else
5782 {
5783 *q++ = *p++;
5784 }
5785 }
5786 *q++ = '"';
5787 *q = 0;
5788
5789 *lenp = len;
5790 return buf;
5791}
5792
5793/*
5794 * Check for a <> code in a user command.
5795 * "code" points to the '<'. "len" the length of the <> (inclusive).
5796 * "buf" is where the result is to be added.
5797 * "split_buf" points to a buffer used for splitting, caller should free it.
5798 * "split_len" is the length of what "split_buf" contains.
5799 * Returns the length of the replacement, which has been added to "buf".
5800 * Returns -1 if there was no match, and only the "<" has been copied.
5801 */
5802 static size_t
5803uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5804 char_u *code;
5805 size_t len;
5806 char_u *buf;
5807 ucmd_T *cmd; /* the user command we're expanding */
5808 exarg_T *eap; /* ex arguments */
5809 char_u **split_buf;
5810 size_t *split_len;
5811{
5812 size_t result = 0;
5813 char_u *p = code + 1;
5814 size_t l = len - 2;
5815 int quote = 0;
5816 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5817 ct_LT, ct_NONE } type = ct_NONE;
5818
5819 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5820 {
5821 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5822 p += 2;
5823 l -= 2;
5824 }
5825
Bram Moolenaar371d5402006-03-20 21:47:49 +00005826 ++l;
5827 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005829 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005831 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005833 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005835 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005837 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005839 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005841 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 type = ct_REGISTER;
5843
5844 switch (type)
5845 {
5846 case ct_ARGS:
5847 /* Simple case first */
5848 if (*eap->arg == NUL)
5849 {
5850 if (quote == 1)
5851 {
5852 result = 2;
5853 if (buf != NULL)
5854 STRCPY(buf, "''");
5855 }
5856 else
5857 result = 0;
5858 break;
5859 }
5860
5861 /* When specified there is a single argument don't split it.
5862 * Works for ":Cmd %" when % is "a b c". */
5863 if ((eap->argt & NOSPC) && quote == 2)
5864 quote = 1;
5865
5866 switch (quote)
5867 {
5868 case 0: /* No quoting, no splitting */
5869 result = STRLEN(eap->arg);
5870 if (buf != NULL)
5871 STRCPY(buf, eap->arg);
5872 break;
5873 case 1: /* Quote, but don't split */
5874 result = STRLEN(eap->arg) + 2;
5875 for (p = eap->arg; *p; ++p)
5876 {
5877 if (*p == '\\' || *p == '"')
5878 ++result;
5879 }
5880
5881 if (buf != NULL)
5882 {
5883 *buf++ = '"';
5884 for (p = eap->arg; *p; ++p)
5885 {
5886 if (*p == '\\' || *p == '"')
5887 *buf++ = '\\';
5888 *buf++ = *p;
5889 }
5890 *buf = '"';
5891 }
5892
5893 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005894 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 /* This is hard, so only do it once, and cache the result */
5896 if (*split_buf == NULL)
5897 *split_buf = uc_split_args(eap->arg, split_len);
5898
5899 result = *split_len;
5900 if (buf != NULL && result != 0)
5901 STRCPY(buf, *split_buf);
5902
5903 break;
5904 }
5905 break;
5906
5907 case ct_BANG:
5908 result = eap->forceit ? 1 : 0;
5909 if (quote)
5910 result += 2;
5911 if (buf != NULL)
5912 {
5913 if (quote)
5914 *buf++ = '"';
5915 if (eap->forceit)
5916 *buf++ = '!';
5917 if (quote)
5918 *buf = '"';
5919 }
5920 break;
5921
5922 case ct_LINE1:
5923 case ct_LINE2:
5924 case ct_COUNT:
5925 {
5926 char num_buf[20];
5927 long num = (type == ct_LINE1) ? eap->line1 :
5928 (type == ct_LINE2) ? eap->line2 :
5929 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5930 size_t num_len;
5931
5932 sprintf(num_buf, "%ld", num);
5933 num_len = STRLEN(num_buf);
5934 result = num_len;
5935
5936 if (quote)
5937 result += 2;
5938
5939 if (buf != NULL)
5940 {
5941 if (quote)
5942 *buf++ = '"';
5943 STRCPY(buf, num_buf);
5944 buf += num_len;
5945 if (quote)
5946 *buf = '"';
5947 }
5948
5949 break;
5950 }
5951
5952 case ct_REGISTER:
5953 result = eap->regname ? 1 : 0;
5954 if (quote)
5955 result += 2;
5956 if (buf != NULL)
5957 {
5958 if (quote)
5959 *buf++ = '\'';
5960 if (eap->regname)
5961 *buf++ = eap->regname;
5962 if (quote)
5963 *buf = '\'';
5964 }
5965 break;
5966
5967 case ct_LT:
5968 result = 1;
5969 if (buf != NULL)
5970 *buf = '<';
5971 break;
5972
5973 default:
5974 /* Not recognized: just copy the '<' and return -1. */
5975 result = (size_t)-1;
5976 if (buf != NULL)
5977 *buf = '<';
5978 break;
5979 }
5980
5981 return result;
5982}
5983
5984 static void
5985do_ucmd(eap)
5986 exarg_T *eap;
5987{
5988 char_u *buf;
5989 char_u *p;
5990 char_u *q;
5991
5992 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005993 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005994 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 size_t len, totlen;
5996
5997 size_t split_len = 0;
5998 char_u *split_buf = NULL;
5999 ucmd_T *cmd;
6000#ifdef FEAT_EVAL
6001 scid_T save_current_SID = current_SID;
6002#endif
6003
6004 if (eap->cmdidx == CMD_USER)
6005 cmd = USER_CMD(eap->useridx);
6006 else
6007 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6008
6009 /*
6010 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006011 * First round: "buf" is NULL, compute length, allocate "buf".
6012 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 */
6014 buf = NULL;
6015 for (;;)
6016 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006017 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006018 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006020
6021 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006023 start = vim_strchr(p, '<');
6024 if (start != NULL)
6025 end = vim_strchr(start + 1, '>');
6026 if (buf != NULL)
6027 {
6028 ksp = vim_strchr(p, K_SPECIAL);
6029 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6030 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6031# ifdef FEAT_GUI
6032 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6033# endif
6034 ))
6035 {
6036 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6037 * KS_SPECIAL KE_FILLER, like for mappings, but
6038 * do_cmdline() doesn't handle that, so convert it back.
6039 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6040 len = ksp - p;
6041 if (len > 0)
6042 {
6043 mch_memmove(q, p, len);
6044 q += len;
6045 }
6046 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6047 p = ksp + 3;
6048 continue;
6049 }
6050 }
6051
6052 /* break if there no <item> is found */
6053 if (start == NULL || end == NULL)
6054 break;
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 /* Include the '>' */
6057 ++end;
6058
6059 /* Take everything up to the '<' */
6060 len = start - p;
6061 if (buf == NULL)
6062 totlen += len;
6063 else
6064 {
6065 mch_memmove(q, p, len);
6066 q += len;
6067 }
6068
6069 len = uc_check_code(start, end - start, q, cmd, eap,
6070 &split_buf, &split_len);
6071 if (len == (size_t)-1)
6072 {
6073 /* no match, continue after '<' */
6074 p = start + 1;
6075 len = 1;
6076 }
6077 else
6078 p = end;
6079 if (buf == NULL)
6080 totlen += len;
6081 else
6082 q += len;
6083 }
6084 if (buf != NULL) /* second time here, finished */
6085 {
6086 STRCPY(q, p);
6087 break;
6088 }
6089
6090 totlen += STRLEN(p); /* Add on the trailing characters */
6091 buf = alloc((unsigned)(totlen + 1));
6092 if (buf == NULL)
6093 {
6094 vim_free(split_buf);
6095 return;
6096 }
6097 }
6098
6099#ifdef FEAT_EVAL
6100 current_SID = cmd->uc_scriptID;
6101#endif
6102 (void)do_cmdline(buf, eap->getline, eap->cookie,
6103 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6104#ifdef FEAT_EVAL
6105 current_SID = save_current_SID;
6106#endif
6107 vim_free(buf);
6108 vim_free(split_buf);
6109}
6110
6111# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6112 static char_u *
6113get_user_command_name(idx)
6114 int idx;
6115{
6116 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6117}
6118
6119/*
6120 * Function given to ExpandGeneric() to obtain the list of user command names.
6121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 char_u *
6123get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006124 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int idx;
6126{
6127 if (idx < curbuf->b_ucmds.ga_len)
6128 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6129 idx -= curbuf->b_ucmds.ga_len;
6130 if (idx < ucmds.ga_len)
6131 return USER_CMD(idx)->uc_name;
6132 return NULL;
6133}
6134
6135/*
6136 * Function given to ExpandGeneric() to obtain the list of user command
6137 * attributes.
6138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 char_u *
6140get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006141 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 int idx;
6143{
6144 static char *user_cmd_flags[] =
6145 {"bang", "bar", "buffer", "complete", "count",
6146 "nargs", "range", "register"};
6147
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006148 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 return NULL;
6150 return (char_u *)user_cmd_flags[idx];
6151}
6152
6153/*
6154 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 char_u *
6157get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006158 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 int idx;
6160{
6161 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6162
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006163 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 return NULL;
6165 return (char_u *)user_cmd_nargs[idx];
6166}
6167
6168/*
6169 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 char_u *
6172get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006173 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 int idx;
6175{
6176 return (char_u *)command_complete[idx].name;
6177}
6178# endif /* FEAT_CMDL_COMPL */
6179
6180#endif /* FEAT_USR_CMDS */
6181
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006182#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6183/*
6184 * Parse a completion argument "value[vallen]".
6185 * The detected completion goes in "*complp", argument type in "*argt".
6186 * When there is an argument, for function and user defined completion, it's
6187 * copied to allocated memory and stored in "*compl_arg".
6188 * Returns FAIL if something is wrong.
6189 */
6190 int
6191parse_compl_arg(value, vallen, complp, argt, compl_arg)
6192 char_u *value;
6193 int vallen;
6194 int *complp;
6195 long *argt;
6196 char_u **compl_arg;
6197{
6198 char_u *arg = NULL;
6199 size_t arglen = 0;
6200 int i;
6201 int valend = vallen;
6202
6203 /* Look for any argument part - which is the part after any ',' */
6204 for (i = 0; i < vallen; ++i)
6205 {
6206 if (value[i] == ',')
6207 {
6208 arg = &value[i + 1];
6209 arglen = vallen - i - 1;
6210 valend = i;
6211 break;
6212 }
6213 }
6214
6215 for (i = 0; command_complete[i].expand != 0; ++i)
6216 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006217 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006218 && STRNCMP(value, command_complete[i].name, valend) == 0)
6219 {
6220 *complp = command_complete[i].expand;
6221 if (command_complete[i].expand == EXPAND_BUFFERS)
6222 *argt |= BUFNAME;
6223 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6224 || command_complete[i].expand == EXPAND_FILES)
6225 *argt |= XFILE;
6226 break;
6227 }
6228 }
6229
6230 if (command_complete[i].expand == 0)
6231 {
6232 EMSG2(_("E180: Invalid complete value: %s"), value);
6233 return FAIL;
6234 }
6235
6236# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6237 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6238 && arg != NULL)
6239# else
6240 if (arg != NULL)
6241# endif
6242 {
6243 EMSG(_("E468: Completion argument only allowed for custom completion"));
6244 return FAIL;
6245 }
6246
6247# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6248 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6249 && arg == NULL)
6250 {
6251 EMSG(_("E467: Custom completion requires a function argument"));
6252 return FAIL;
6253 }
6254
6255 if (arg != NULL)
6256 *compl_arg = vim_strnsave(arg, (int)arglen);
6257# endif
6258 return OK;
6259}
6260#endif
6261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 static void
6263ex_colorscheme(eap)
6264 exarg_T *eap;
6265{
Bram Moolenaare6850792010-05-14 15:28:44 +02006266 if (*eap->arg == NUL)
6267 {
6268#ifdef FEAT_EVAL
6269 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6270 char_u *p = NULL;
6271
6272 if (expr != NULL)
6273 {
6274 ++emsg_off;
6275 p = eval_to_string(expr, NULL, FALSE);
6276 --emsg_off;
6277 vim_free(expr);
6278 }
6279 if (p != NULL)
6280 {
6281 MSG(p);
6282 vim_free(p);
6283 }
6284 else
6285 MSG("default");
6286#else
6287 MSG(_("unknown"));
6288#endif
6289 }
6290 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6292}
6293
6294 static void
6295ex_highlight(eap)
6296 exarg_T *eap;
6297{
6298 if (*eap->arg == NUL && eap->cmd[2] == '!')
6299 MSG(_("Greetings, Vim user!"));
6300 do_highlight(eap->arg, eap->forceit, FALSE);
6301}
6302
6303
6304/*
6305 * Call this function if we thought we were going to exit, but we won't
6306 * (because of an error). May need to restore the terminal mode.
6307 */
6308 void
6309not_exiting()
6310{
6311 exiting = FALSE;
6312 settmode(TMODE_RAW);
6313}
6314
6315/*
6316 * ":quit": quit current window, quit Vim if closed the last window.
6317 */
6318 static void
6319ex_quit(eap)
6320 exarg_T *eap;
6321{
6322#ifdef FEAT_CMDWIN
6323 if (cmdwin_type != 0)
6324 {
6325 cmdwin_result = Ctrl_C;
6326 return;
6327 }
6328#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006329 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006330 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006331 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006332 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006333 return;
6334 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006335#ifdef FEAT_AUTOCMD
6336 if (curbuf_locked())
6337 return;
6338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340#ifdef FEAT_NETBEANS_INTG
6341 netbeansForcedQuit = eap->forceit;
6342#endif
6343
6344 /*
6345 * If there are more files or windows we won't exit.
6346 */
6347 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6348 exiting = TRUE;
6349 if ((!P_HID(curbuf)
6350 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6351 || check_more(TRUE, eap->forceit) == FAIL
6352 || (only_one_window() && check_changed_any(eap->forceit)))
6353 {
6354 not_exiting();
6355 }
6356 else
6357 {
6358#ifdef FEAT_WINDOWS
6359 if (only_one_window()) /* quit last window */
6360#endif
6361 getout(0);
6362#ifdef FEAT_WINDOWS
6363# ifdef FEAT_GUI
6364 need_mouse_correct = TRUE;
6365# endif
6366 /* close window; may free buffer */
6367 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6368#endif
6369 }
6370}
6371
6372/*
6373 * ":cquit".
6374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 static void
6376ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006377 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 getout(1); /* this does not always pass on the exit code to the Manx
6380 compiler. why? */
6381}
6382
6383/*
6384 * ":qall": try to quit all windows
6385 */
6386 static void
6387ex_quit_all(eap)
6388 exarg_T *eap;
6389{
6390# ifdef FEAT_CMDWIN
6391 if (cmdwin_type != 0)
6392 {
6393 if (eap->forceit)
6394 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6395 else
6396 cmdwin_result = K_XF2;
6397 return;
6398 }
6399# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006400
6401 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006402 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006403 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006404 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006405 return;
6406 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006407#ifdef FEAT_AUTOCMD
6408 if (curbuf_locked())
6409 return;
6410#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 exiting = TRUE;
6413 if (eap->forceit || !check_changed_any(FALSE))
6414 getout(0);
6415 not_exiting();
6416}
6417
Bram Moolenaard9967712006-03-11 21:18:15 +00006418#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419/*
6420 * ":close": close current window, unless it is the last one
6421 */
6422 static void
6423ex_close(eap)
6424 exarg_T *eap;
6425{
6426# ifdef FEAT_CMDWIN
6427 if (cmdwin_type != 0)
6428 cmdwin_result = K_IGNORE;
6429 else
6430# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006431 if (!text_locked()
6432#ifdef FEAT_AUTOCMD
6433 && !curbuf_locked()
6434#endif
6435 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006436 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437}
6438
Bram Moolenaard9967712006-03-11 21:18:15 +00006439# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440/*
6441 * ":pclose": Close any preview window.
6442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446{
6447 win_T *win;
6448
6449 for (win = firstwin; win != NULL; win = win->w_next)
6450 if (win->w_p_pvw)
6451 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006452 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 break;
6454 }
6455}
Bram Moolenaard9967712006-03-11 21:18:15 +00006456# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457
Bram Moolenaarf740b292006-02-16 22:11:02 +00006458/*
6459 * Close window "win" and take care of handling closing the last window for a
6460 * modified buffer.
6461 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006462 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006463ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006466 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 int need_hide;
6469 buf_T *buf = win->w_buffer;
6470
6471 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006474# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if ((p_confirm || cmdmod.confirm) && p_write)
6476 {
6477 dialog_changed(buf, FALSE);
6478 if (buf_valid(buf) && bufIsChanged(buf))
6479 return;
6480 need_hide = FALSE;
6481 }
6482 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 {
6485 EMSG(_(e_nowrtmsg));
6486 return;
6487 }
6488 }
6489
Bram Moolenaard9967712006-03-11 21:18:15 +00006490# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006492# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006495 if (tp == NULL)
6496 win_close(win, !need_hide && !P_HID(buf));
6497 else
6498 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
6500
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006502 * ":tabclose": close current tab page, unless it is the last one.
6503 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 */
6505 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 exarg_T *eap;
6508{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006509 tabpage_T *tp;
6510
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511# ifdef FEAT_CMDWIN
6512 if (cmdwin_type != 0)
6513 cmdwin_result = K_IGNORE;
6514 else
6515# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006516 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006517 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006518 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006520 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006521 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006522 tp = find_tabpage((int)eap->line2);
6523 if (tp == NULL)
6524 {
6525 beep_flush();
6526 return;
6527 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006528 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006529 {
6530 tabpage_close_other(tp, eap->forceit);
6531 return;
6532 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006534 if (!text_locked()
6535#ifdef FEAT_AUTOCMD
6536 && !curbuf_locked()
6537#endif
6538 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006539 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006541}
6542
6543/*
6544 * ":tabonly": close all tab pages except the current one
6545 */
6546 static void
6547ex_tabonly(eap)
6548 exarg_T *eap;
6549{
6550 tabpage_T *tp;
6551 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006552
6553# ifdef FEAT_CMDWIN
6554 if (cmdwin_type != 0)
6555 cmdwin_result = K_IGNORE;
6556 else
6557# endif
6558 if (first_tabpage->tp_next == NULL)
6559 MSG(_("Already only one tab page"));
6560 else
6561 {
6562 /* Repeat this up to a 1000 times, because autocommands may mess
6563 * up the lists. */
6564 for (done = 0; done < 1000; ++done)
6565 {
6566 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6567 if (tp->tp_topframe != topframe)
6568 {
6569 tabpage_close_other(tp, eap->forceit);
6570 /* if we failed to close it quit */
6571 if (valid_tabpage(tp))
6572 done = 1000;
6573 /* start over, "tp" is now invalid */
6574 break;
6575 }
6576 if (first_tabpage->tp_next == NULL)
6577 break;
6578 }
6579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006583 * Close the current tab page.
6584 */
6585 void
6586tabpage_close(forceit)
6587 int forceit;
6588{
6589 /* First close all the windows but the current one. If that worked then
6590 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006591 if (lastwin != firstwin)
6592 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006593 if (lastwin == firstwin)
6594 ex_win_close(forceit, curwin, NULL);
6595# ifdef FEAT_GUI
6596 need_mouse_correct = TRUE;
6597# endif
6598}
6599
6600/*
6601 * Close tab page "tp", which is not the current tab page.
6602 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006603 * Also takes care of the tab pages line disappearing when closing the
6604 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006605 */
6606 void
6607tabpage_close_other(tp, forceit)
6608 tabpage_T *tp;
6609 int forceit;
6610{
6611 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006612 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006613 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006614
6615 /* Limit to 1000 windows, autocommands may add a window while we close
6616 * one. OK, so I'm paranoid... */
6617 while (++done < 1000)
6618 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006619 wp = tp->tp_firstwin;
6620 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006621
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006622 /* Autocommands may delete the tab page under our fingers and we may
6623 * fail to close a window with a modified buffer. */
6624 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006625 break;
6626 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006627
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006628 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006629 if (h != tabline_height())
6630 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006631}
6632
6633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 * ":only".
6635 */
6636 static void
6637ex_only(eap)
6638 exarg_T *eap;
6639{
6640# ifdef FEAT_GUI
6641 need_mouse_correct = TRUE;
6642# endif
6643 close_others(TRUE, eap->forceit);
6644}
6645
6646/*
6647 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006648 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006650 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651ex_all(eap)
6652 exarg_T *eap;
6653{
6654 if (eap->addr_count == 0)
6655 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006656 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658#endif /* FEAT_WINDOWS */
6659
6660 static void
6661ex_hide(eap)
6662 exarg_T *eap;
6663{
6664 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6665 eap->errmsg = e_invarg;
6666 else
6667 {
6668 /* ":hide" or ":hide | cmd": hide current window */
6669 eap->nextcmd = check_nextcmd(eap->arg);
6670#ifdef FEAT_WINDOWS
6671 if (!eap->skip)
6672 {
6673# ifdef FEAT_GUI
6674 need_mouse_correct = TRUE;
6675# endif
6676 win_close(curwin, FALSE); /* don't free buffer */
6677 }
6678#endif
6679 }
6680}
6681
6682/*
6683 * ":stop" and ":suspend": Suspend Vim.
6684 */
6685 static void
6686ex_stop(eap)
6687 exarg_T *eap;
6688{
6689 /*
6690 * Disallow suspending for "rvim".
6691 */
6692 if (!check_restricted()
6693#ifdef WIN3264
6694 /*
6695 * Check if external commands are allowed now.
6696 */
6697 && can_end_termcap_mode(TRUE)
6698#endif
6699 )
6700 {
6701 if (!eap->forceit)
6702 autowrite_all();
6703 windgoto((int)Rows - 1, 0);
6704 out_char('\n');
6705 out_flush();
6706 stoptermcap();
6707 out_flush(); /* needed for SUN to restore xterm buffer */
6708#ifdef FEAT_TITLE
6709 mch_restore_title(3); /* restore window titles */
6710#endif
6711 ui_suspend(); /* call machine specific function */
6712#ifdef FEAT_TITLE
6713 maketitle();
6714 resettitle(); /* force updating the title */
6715#endif
6716 starttermcap();
6717 scroll_start(); /* scroll screen before redrawing */
6718 redraw_later_clear();
6719 shell_resized(); /* may have resized window */
6720 }
6721}
6722
6723/*
6724 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6725 */
6726 static void
6727ex_exit(eap)
6728 exarg_T *eap;
6729{
6730#ifdef FEAT_CMDWIN
6731 if (cmdwin_type != 0)
6732 {
6733 cmdwin_result = Ctrl_C;
6734 return;
6735 }
6736#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006737 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006738 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006739 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006740 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006741 return;
6742 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006743#ifdef FEAT_AUTOCMD
6744 if (curbuf_locked())
6745 return;
6746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
6748 /*
6749 * if more files or windows we won't exit
6750 */
6751 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6752 exiting = TRUE;
6753 if ( ((eap->cmdidx == CMD_wq
6754 || curbufIsChanged())
6755 && do_write(eap) == FAIL)
6756 || check_more(TRUE, eap->forceit) == FAIL
6757 || (only_one_window() && check_changed_any(eap->forceit)))
6758 {
6759 not_exiting();
6760 }
6761 else
6762 {
6763#ifdef FEAT_WINDOWS
6764 if (only_one_window()) /* quit last window, exit Vim */
6765#endif
6766 getout(0);
6767#ifdef FEAT_WINDOWS
6768# ifdef FEAT_GUI
6769 need_mouse_correct = TRUE;
6770# endif
6771 /* quit current window, may free buffer */
6772 win_close(curwin, !P_HID(curwin->w_buffer));
6773#endif
6774 }
6775}
6776
6777/*
6778 * ":print", ":list", ":number".
6779 */
6780 static void
6781ex_print(eap)
6782 exarg_T *eap;
6783{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006784 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6785 EMSG(_(e_emptybuf));
6786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006788 for ( ;!got_int; ui_breakcheck())
6789 {
6790 print_line(eap->line1,
6791 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6792 || (eap->flags & EXFLAG_NR)),
6793 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6794 if (++eap->line1 > eap->line2)
6795 break;
6796 out_flush(); /* show one line at a time */
6797 }
6798 setpcmark();
6799 /* put cursor at last line */
6800 curwin->w_cursor.lnum = eap->line2;
6801 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805}
6806
6807#ifdef FEAT_BYTEOFF
6808 static void
6809ex_goto(eap)
6810 exarg_T *eap;
6811{
6812 goto_byte(eap->line2);
6813}
6814#endif
6815
6816/*
6817 * ":shell".
6818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 static void
6820ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006821 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 do_shell(NULL, 0);
6824}
6825
6826#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6827 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006828 || defined(FEAT_GUI_MSWIN) \
6829 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 || defined(PROTO)
6831
6832/*
6833 * Handle a file drop. The code is here because a drop is *nearly* like an
6834 * :args command, but not quite (we have a list of exact filenames, so we
6835 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6836 * code is very similar to :args and hence needs access to a lot of the static
6837 * functions in this file.
6838 *
6839 * The list should be allocated using alloc(), as should each item in the
6840 * list. This function takes over responsibility for freeing the list.
6841 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006842 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6844 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6845 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6846 * file functionality is (currently) not in EMX this is not presently a
6847 * problem.
6848 */
6849 void
6850handle_drop(filec, filev, split)
6851 int filec; /* the number of files dropped */
6852 char_u **filev; /* the list of files dropped */
6853 int split; /* force splitting the window */
6854{
6855 exarg_T ea;
6856 int save_msg_scroll = msg_scroll;
6857
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006858 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006859 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006861#ifdef FEAT_AUTOCMD
6862 if (curbuf_locked())
6863 return;
6864#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006865 /* When the screen is being updated we should not change buffers and
6866 * windows structures, it may cause freed memory to be used. */
6867 if (updating_screen)
6868 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
6870 /* Check whether the current buffer is changed. If so, we will need
6871 * to split the current window or data could be lost.
6872 * We don't need to check if the 'hidden' option is set, as in this
6873 * case the buffer won't be lost.
6874 */
6875 if (!P_HID(curbuf) && !split)
6876 {
6877 ++emsg_off;
6878 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6879 --emsg_off;
6880 }
6881 if (split)
6882 {
6883# ifdef FEAT_WINDOWS
6884 if (win_split(0, 0) == FAIL)
6885 return;
6886# ifdef FEAT_SCROLLBIND
6887 curwin->w_p_scb = FALSE;
6888# endif
6889
6890 /* When splitting the window, create a new alist. Otherwise the
6891 * existing one is overwritten. */
6892 alist_unlink(curwin->w_alist);
6893 alist_new();
6894# else
6895 return; /* can't split, always fail */
6896# endif
6897 }
6898
6899 /*
6900 * Set up the new argument list.
6901 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006902 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904 /*
6905 * Move to the first file.
6906 */
6907 /* Fake up a minimal "next" command for do_argfile() */
6908 vim_memset(&ea, 0, sizeof(ea));
6909 ea.cmd = (char_u *)"next";
6910 do_argfile(&ea, 0);
6911
6912 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6913 * mode that is not needed here. */
6914 need_start_insertmode = FALSE;
6915
6916 /* Restore msg_scroll, otherwise a following command may cause scrolling
6917 * unexpectedly. The screen will be redrawn by the caller, thus
6918 * msg_scroll being set by displaying a message is irrelevant. */
6919 msg_scroll = save_msg_scroll;
6920}
6921#endif
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923/*
6924 * Clear an argument list: free all file names and reset it to zero entries.
6925 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006926 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927alist_clear(al)
6928 alist_T *al;
6929{
6930 while (--al->al_ga.ga_len >= 0)
6931 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6932 ga_clear(&al->al_ga);
6933}
6934
6935/*
6936 * Init an argument list.
6937 */
6938 void
6939alist_init(al)
6940 alist_T *al;
6941{
6942 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6943}
6944
6945#if defined(FEAT_WINDOWS) || defined(PROTO)
6946
6947/*
6948 * Remove a reference from an argument list.
6949 * Ignored when the argument list is the global one.
6950 * If the argument list is no longer used by any window, free it.
6951 */
6952 void
6953alist_unlink(al)
6954 alist_T *al;
6955{
6956 if (al != &global_alist && --al->al_refcount <= 0)
6957 {
6958 alist_clear(al);
6959 vim_free(al);
6960 }
6961}
6962
6963# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6964/*
6965 * Create a new argument list and use it for the current window.
6966 */
6967 void
6968alist_new()
6969{
6970 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6971 if (curwin->w_alist == NULL)
6972 {
6973 curwin->w_alist = &global_alist;
6974 ++global_alist.al_refcount;
6975 }
6976 else
6977 {
6978 curwin->w_alist->al_refcount = 1;
6979 alist_init(curwin->w_alist);
6980 }
6981}
6982# endif
6983#endif
6984
6985#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6986/*
6987 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006988 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6989 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 */
6991 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006992alist_expand(fnum_list, fnum_len)
6993 int *fnum_list;
6994 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006997 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 char_u **new_arg_files;
6999 int new_arg_file_count;
7000 char_u *save_p_su = p_su;
7001 int i;
7002
7003 /* Don't use 'suffixes' here. This should work like the shell did the
7004 * expansion. Also, the vimrc file isn't read yet, thus the user
7005 * can't set the options. */
7006 p_su = empty_option;
7007 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7008 if (old_arg_files != NULL)
7009 {
7010 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007011 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7012 old_arg_count = GARGCOUNT;
7013 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 &new_arg_file_count, &new_arg_files,
7015 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7016 && new_arg_file_count > 0)
7017 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007018 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7019 TRUE, fnum_list, fnum_len);
7020 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 }
7023 p_su = save_p_su;
7024}
7025#endif
7026
7027/*
7028 * Set the argument list for the current window.
7029 * Takes over the allocated files[] and the allocated fnames in it.
7030 */
7031 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007032alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 alist_T *al;
7034 int count;
7035 char_u **files;
7036 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007037 int *fnum_list;
7038 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 int i;
7041
7042 alist_clear(al);
7043 if (ga_grow(&al->al_ga, count) == OK)
7044 {
7045 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007046 {
7047 if (got_int)
7048 {
7049 /* When adding many buffers this can take a long time. Allow
7050 * interrupting here. */
7051 while (i < count)
7052 vim_free(files[i++]);
7053 break;
7054 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007055
7056 /* May set buffer name of a buffer previously used for the
7057 * argument list, so that it's re-used by alist_add. */
7058 if (fnum_list != NULL && i < fnum_len)
7059 buf_set_name(fnum_list[i], files[i]);
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007062 ui_breakcheck();
7063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 vim_free(files);
7065 }
7066 else
7067 FreeWild(count, files);
7068#ifdef FEAT_WINDOWS
7069 if (al == &global_alist)
7070#endif
7071 arg_had_last = FALSE;
7072}
7073
7074/*
7075 * Add file "fname" to argument list "al".
7076 * "fname" must have been allocated and "al" must have been checked for room.
7077 */
7078 void
7079alist_add(al, fname, set_fnum)
7080 alist_T *al;
7081 char_u *fname;
7082 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7083{
7084 if (fname == NULL) /* don't add NULL file names */
7085 return;
7086#ifdef BACKSLASH_IN_FILENAME
7087 slash_adjust(fname);
7088#endif
7089 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7090 if (set_fnum > 0)
7091 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7092 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7093 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094}
7095
7096#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7097/*
7098 * Adjust slashes in file names. Called after 'shellslash' was set.
7099 */
7100 void
7101alist_slash_adjust()
7102{
7103 int i;
7104# ifdef FEAT_WINDOWS
7105 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007106 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107# endif
7108
7109 for (i = 0; i < GARGCOUNT; ++i)
7110 if (GARGLIST[i].ae_fname != NULL)
7111 slash_adjust(GARGLIST[i].ae_fname);
7112# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007113 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (wp->w_alist != &global_alist)
7115 for (i = 0; i < WARGCOUNT(wp); ++i)
7116 if (WARGLIST(wp)[i].ae_fname != NULL)
7117 slash_adjust(WARGLIST(wp)[i].ae_fname);
7118# endif
7119}
7120#endif
7121
7122/*
7123 * ":preserve".
7124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 static void
7126ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007127 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007129 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 ml_preserve(curbuf, TRUE);
7131}
7132
7133/*
7134 * ":recover".
7135 */
7136 static void
7137ex_recover(eap)
7138 exarg_T *eap;
7139{
7140 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7141 recoverymode = TRUE;
7142 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7143 && (*eap->arg == NUL
7144 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7145 ml_recover();
7146 recoverymode = FALSE;
7147}
7148
7149/*
7150 * Command modifier used in a wrong way.
7151 */
7152 static void
7153ex_wrongmodifier(eap)
7154 exarg_T *eap;
7155{
7156 eap->errmsg = e_invcmd;
7157}
7158
7159#ifdef FEAT_WINDOWS
7160/*
7161 * :sview [+command] file split window with new file, read-only
7162 * :split [[+command] file] split window with current or new file
7163 * :vsplit [[+command] file] split window vertically with current or new file
7164 * :new [[+command] file] split window with no or new file
7165 * :vnew [[+command] file] split vertically window with no or new file
7166 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007167 *
7168 * :tabedit open new Tab page with empty window
7169 * :tabedit [+command] file open new Tab page and edit "file"
7170 * :tabnew [[+command] file] just like :tabedit
7171 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 */
7173 void
7174ex_splitview(eap)
7175 exarg_T *eap;
7176{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007177 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007178# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007180# endif
7181# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007185# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7187 {
7188 ex_ni(eap);
7189 return;
7190 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007193# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007197# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007199 * quickfix windows. But it's OK when doing ":tab split". */
7200 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
7202 if (eap->cmdidx == CMD_split)
7203 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 if (eap->cmdidx == CMD_vsplit)
7206 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007211# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007212 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7215 FNAME_MESS, TRUE, curbuf->b_ffname);
7216 if (fname == NULL)
7217 goto theend;
7218 eap->arg = fname;
7219 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007220# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007224# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007226# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 && eap->cmdidx != CMD_new)
7230 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007231# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007232 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007233# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007234 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007235# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007236 au_has_group((char_u *)"FileExplorer"))
7237 {
7238 /* No browsing supported but we do have the file explorer:
7239 * Edit the directory. */
7240 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7241 eap->arg = (char_u *)".";
7242 }
7243 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007244# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007245 {
7246 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007248 if (fname == NULL)
7249 goto theend;
7250 eap->arg = fname;
7251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007256 /*
7257 * Either open new tab page or split the window.
7258 */
7259 if (eap->cmdidx == CMD_tabedit
7260 || eap->cmdidx == CMD_tabfind
7261 || eap->cmdidx == CMD_tabnew)
7262 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007263 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7264 : eap->addr_count == 0 ? 0
7265 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007266 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007267 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007268
7269 /* set the alternate buffer for the window we came from */
7270 if (curwin != old_curwin
7271 && win_valid(old_curwin)
7272 && old_curwin->w_buffer != curbuf
7273 && !cmdmod.keepalt)
7274 old_curwin->w_alt_fnum = curbuf->b_fnum;
7275 }
7276 }
7277 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7279 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007280# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 /* Reset 'scrollbind' when editing another file, but keep it when
7282 * doing ":split" without arguments. */
7283 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 )
7288 curwin->w_p_scb = FALSE;
7289 else
7290 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 do_exedit(eap, old_curwin);
7293 }
7294
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007299# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300theend:
7301 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304
7305/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007306 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007307 */
7308 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007309tabpage_new()
7310{
7311 exarg_T ea;
7312
7313 vim_memset(&ea, 0, sizeof(ea));
7314 ea.cmdidx = CMD_tabnew;
7315 ea.cmd = (char_u *)"tabn";
7316 ea.arg = (char_u *)"";
7317 ex_splitview(&ea);
7318}
7319
7320/*
7321 * :tabnext command
7322 */
7323 static void
7324ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007325 exarg_T *eap;
7326{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007327 switch (eap->cmdidx)
7328 {
7329 case CMD_tabfirst:
7330 case CMD_tabrewind:
7331 goto_tabpage(1);
7332 break;
7333 case CMD_tablast:
7334 goto_tabpage(9999);
7335 break;
7336 case CMD_tabprevious:
7337 case CMD_tabNext:
7338 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7339 break;
7340 default: /* CMD_tabnext */
7341 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7342 break;
7343 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007344}
7345
7346/*
7347 * :tabmove command
7348 */
7349 static void
7350ex_tabmove(eap)
7351 exarg_T *eap;
7352{
7353 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354}
7355
7356/*
7357 * :tabs command: List tabs and their contents.
7358 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007359 static void
7360ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007361 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007362{
7363 tabpage_T *tp;
7364 win_T *wp;
7365 int tabcount = 1;
7366
7367 msg_start();
7368 msg_scroll = TRUE;
7369 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7370 {
7371 msg_putchar('\n');
7372 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7373 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7374 out_flush(); /* output one line at a time */
7375 ui_breakcheck();
7376
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007377 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378 wp = firstwin;
7379 else
7380 wp = tp->tp_firstwin;
7381 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7382 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007383 msg_putchar('\n');
7384 msg_putchar(wp == curwin ? '>' : ' ');
7385 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007386 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7387 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388 if (buf_spname(wp->w_buffer) != NULL)
7389 STRCPY(IObuff, buf_spname(wp->w_buffer));
7390 else
7391 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7392 IObuff, IOSIZE, TRUE);
7393 msg_outtrans(IObuff);
7394 out_flush(); /* output one line at a time */
7395 ui_breakcheck();
7396 }
7397 }
7398}
7399
7400#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402/*
7403 * ":mode": Set screen mode.
7404 * If no argument given, just get the screen size and redraw.
7405 */
7406 static void
7407ex_mode(eap)
7408 exarg_T *eap;
7409{
7410 if (*eap->arg == NUL)
7411 shell_resized();
7412 else
7413 mch_screenmode(eap->arg);
7414}
7415
7416#ifdef FEAT_WINDOWS
7417/*
7418 * ":resize".
7419 * set, increment or decrement current window height
7420 */
7421 static void
7422ex_resize(eap)
7423 exarg_T *eap;
7424{
7425 int n;
7426 win_T *wp = curwin;
7427
7428 if (eap->addr_count > 0)
7429 {
7430 n = eap->line2;
7431 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7432 ;
7433 }
7434
7435#ifdef FEAT_GUI
7436 need_mouse_correct = TRUE;
7437#endif
7438 n = atol((char *)eap->arg);
7439#ifdef FEAT_VERTSPLIT
7440 if (cmdmod.split & WSP_VERT)
7441 {
7442 if (*eap->arg == '-' || *eap->arg == '+')
7443 n += W_WIDTH(curwin);
7444 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7445 n = 9999;
7446 win_setwidth_win((int)n, wp);
7447 }
7448 else
7449#endif
7450 {
7451 if (*eap->arg == '-' || *eap->arg == '+')
7452 n += curwin->w_height;
7453 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7454 n = 9999;
7455 win_setheight_win((int)n, wp);
7456 }
7457}
7458#endif
7459
7460/*
7461 * ":find [+command] <file>" command.
7462 */
7463 static void
7464ex_find(eap)
7465 exarg_T *eap;
7466{
7467#ifdef FEAT_SEARCHPATH
7468 char_u *fname;
7469 int count;
7470
7471 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7472 TRUE, curbuf->b_ffname);
7473 if (eap->addr_count > 0)
7474 {
7475 /* Repeat finding the file "count" times. This matters when it
7476 * appears several times in the path. */
7477 count = eap->line2;
7478 while (fname != NULL && --count > 0)
7479 {
7480 vim_free(fname);
7481 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7482 FALSE, curbuf->b_ffname);
7483 }
7484 }
7485
7486 if (fname != NULL)
7487 {
7488 eap->arg = fname;
7489#endif
7490 do_exedit(eap, NULL);
7491#ifdef FEAT_SEARCHPATH
7492 vim_free(fname);
7493 }
7494#endif
7495}
7496
7497/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007498 * ":open" simulation: for now just work like ":visual".
7499 */
7500 static void
7501ex_open(eap)
7502 exarg_T *eap;
7503{
7504 regmatch_T regmatch;
7505 char_u *p;
7506
7507 curwin->w_cursor.lnum = eap->line2;
7508 beginline(BL_SOL | BL_FIX);
7509 if (*eap->arg == '/')
7510 {
7511 /* ":open /pattern/": put cursor in column found with pattern */
7512 ++eap->arg;
7513 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7514 *p = NUL;
7515 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7516 if (regmatch.regprog != NULL)
7517 {
7518 regmatch.rm_ic = p_ic;
7519 p = ml_get_curline();
7520 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007521 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007522 else
7523 EMSG(_(e_nomatch));
7524 vim_free(regmatch.regprog);
7525 }
7526 /* Move to the NUL, ignore any other arguments. */
7527 eap->arg += STRLEN(eap->arg);
7528 }
7529 check_cursor();
7530
7531 eap->cmdidx = CMD_visual;
7532 do_exedit(eap, NULL);
7533}
7534
7535/*
7536 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 */
7538 static void
7539ex_edit(eap)
7540 exarg_T *eap;
7541{
7542 do_exedit(eap, NULL);
7543}
7544
7545/*
7546 * ":edit <file>" command and alikes.
7547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 void
7549do_exedit(eap, old_curwin)
7550 exarg_T *eap;
7551 win_T *old_curwin; /* curwin before doing a split or NULL */
7552{
7553 int n;
7554#ifdef FEAT_WINDOWS
7555 int need_hide;
7556#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007557 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
7559 /*
7560 * ":vi" command ends Ex mode.
7561 */
7562 if (exmode_active && (eap->cmdidx == CMD_visual
7563 || eap->cmdidx == CMD_view))
7564 {
7565 exmode_active = FALSE;
7566 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007567 {
7568 /* Special case: ":global/pat/visual\NLvi-commands" */
7569 if (global_busy)
7570 {
7571 int rd = RedrawingDisabled;
7572 int nwr = no_wait_return;
7573 int ms = msg_scroll;
7574#ifdef FEAT_GUI
7575 int he = hold_gui_events;
7576#endif
7577
7578 if (eap->nextcmd != NULL)
7579 {
7580 stuffReadbuff(eap->nextcmd);
7581 eap->nextcmd = NULL;
7582 }
7583
7584 if (exmode_was != EXMODE_VIM)
7585 settmode(TMODE_RAW);
7586 RedrawingDisabled = 0;
7587 no_wait_return = 0;
7588 need_wait_return = FALSE;
7589 msg_scroll = 0;
7590#ifdef FEAT_GUI
7591 hold_gui_events = 0;
7592#endif
7593 must_redraw = CLEAR;
7594
7595 main_loop(FALSE, TRUE);
7596
7597 RedrawingDisabled = rd;
7598 no_wait_return = nwr;
7599 msg_scroll = ms;
7600#ifdef FEAT_GUI
7601 hold_gui_events = he;
7602#endif
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607
7608 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007609 || eap->cmdidx == CMD_tabnew
7610 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611#ifdef FEAT_VERTSPLIT
7612 || eap->cmdidx == CMD_vnew
7613#endif
7614 ) && *eap->arg == NUL)
7615 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007616 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 setpcmark();
7618 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007619 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7620 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 else if ((eap->cmdidx != CMD_split
7623#ifdef FEAT_VERTSPLIT
7624 && eap->cmdidx != CMD_vsplit
7625#endif
7626 )
7627 || *eap->arg != NUL
7628#ifdef FEAT_BROWSE
7629 || cmdmod.browse
7630#endif
7631 )
7632 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007633#ifdef FEAT_AUTOCMD
7634 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7635 * can bring us here, others are stopped earlier. */
7636 if (*eap->arg != NUL && curbuf_locked())
7637 return;
7638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 n = readonlymode;
7640 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7641 readonlymode = TRUE;
7642 else if (eap->cmdidx == CMD_enew)
7643 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7644 empty buffer */
7645 setpcmark();
7646 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7647 NULL, eap,
7648 /* ":edit" goes to first line if Vi compatible */
7649 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7650 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7651 ? ECMD_ONE : eap->do_ecmd_lnum,
7652 (P_HID(curbuf) ? ECMD_HIDE : 0)
7653 + (eap->forceit ? ECMD_FORCEIT : 0)
7654#ifdef FEAT_LISTCMDS
7655 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7656#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007657 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
7659 /* Editing the file failed. If the window was split, close it. */
7660#ifdef FEAT_WINDOWS
7661 if (old_curwin != NULL)
7662 {
7663 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7664 if (!need_hide || P_HID(curbuf))
7665 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007666# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7667 cleanup_T cs;
7668
7669 /* Reset the error/interrupt/exception state here so that
7670 * aborting() returns FALSE when closing a window. */
7671 enter_cleanup(&cs);
7672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673# ifdef FEAT_GUI
7674 need_mouse_correct = TRUE;
7675# endif
7676 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007677
7678# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7679 /* Restore the error/interrupt/exception state if not
7680 * discarded by a new aborting error, interrupt, or
7681 * uncaught exception. */
7682 leave_cleanup(&cs);
7683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685 }
7686#endif
7687 }
7688 else if (readonlymode && curbuf->b_nwindows == 1)
7689 {
7690 /* When editing an already visited buffer, 'readonly' won't be set
7691 * but the previous value is kept. With ":view" and ":sview" we
7692 * want the file to be readonly, except when another window is
7693 * editing the same buffer. */
7694 curbuf->b_p_ro = TRUE;
7695 }
7696 readonlymode = n;
7697 }
7698 else
7699 {
7700 if (eap->do_ecmd_cmd != NULL)
7701 do_cmdline_cmd(eap->do_ecmd_cmd);
7702#ifdef FEAT_TITLE
7703 n = curwin->w_arg_idx_invalid;
7704#endif
7705 check_arg_idx(curwin);
7706#ifdef FEAT_TITLE
7707 if (n != curwin->w_arg_idx_invalid)
7708 maketitle();
7709#endif
7710 }
7711
7712#ifdef FEAT_WINDOWS
7713 /*
7714 * if ":split file" worked, set alternate file name in old window to new
7715 * file
7716 */
7717 if (old_curwin != NULL
7718 && *eap->arg != NUL
7719 && curwin != old_curwin
7720 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007721 && old_curwin->w_buffer != curbuf
7722 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 old_curwin->w_alt_fnum = curbuf->b_fnum;
7724#endif
7725
7726 ex_no_reprint = TRUE;
7727}
7728
7729#ifndef FEAT_GUI
7730/*
7731 * ":gui" and ":gvim" when there is no GUI.
7732 */
7733 static void
7734ex_nogui(eap)
7735 exarg_T *eap;
7736{
7737 eap->errmsg = e_nogvim;
7738}
7739#endif
7740
7741#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7742 static void
7743ex_tearoff(eap)
7744 exarg_T *eap;
7745{
7746 gui_make_tearoff(eap->arg);
7747}
7748#endif
7749
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007750#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 static void
7752ex_popup(eap)
7753 exarg_T *eap;
7754{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007755 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756}
7757#endif
7758
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 static void
7760ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007761 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7764 MSG(_("No swap file"));
7765 else
7766 msg(curbuf->b_ml.ml_mfp->mf_fname);
7767}
7768
7769/*
7770 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7771 * offset.
7772 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 static void
7775ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007776 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778#ifdef FEAT_SCROLLBIND
7779 win_T *wp;
7780 long topline;
7781 long y;
7782 linenr_T old_linenr = curwin->w_cursor.lnum;
7783
7784 setpcmark();
7785
7786 /*
7787 * determine max topline
7788 */
7789 if (curwin->w_p_scb)
7790 {
7791 topline = curwin->w_topline;
7792 for (wp = firstwin; wp; wp = wp->w_next)
7793 {
7794 if (wp->w_p_scb && wp->w_buffer)
7795 {
7796 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7797 if (topline > y)
7798 topline = y;
7799 }
7800 }
7801 if (topline < 1)
7802 topline = 1;
7803 }
7804 else
7805 {
7806 topline = 1;
7807 }
7808
7809
7810 /*
7811 * set all scrollbind windows to the same topline
7812 */
7813 wp = curwin;
7814 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7815 {
7816 if (curwin->w_p_scb)
7817 {
7818 y = topline - curwin->w_topline;
7819 if (y > 0)
7820 scrollup(y, TRUE);
7821 else
7822 scrolldown(-y, TRUE);
7823 curwin->w_scbind_pos = topline;
7824 redraw_later(VALID);
7825 cursor_correct();
7826#ifdef FEAT_WINDOWS
7827 curwin->w_redr_status = TRUE;
7828#endif
7829 }
7830 }
7831 curwin = wp;
7832 if (curwin->w_p_scb)
7833 {
7834 did_syncbind = TRUE;
7835 checkpcmark();
7836 if (old_linenr != curwin->w_cursor.lnum)
7837 {
7838 char_u ctrl_o[2];
7839
7840 ctrl_o[0] = Ctrl_O;
7841 ctrl_o[1] = 0;
7842 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7843 }
7844 }
7845#endif
7846}
7847
7848
7849 static void
7850ex_read(eap)
7851 exarg_T *eap;
7852{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007853 int i;
7854 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7855 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
7857 if (eap->usefilter) /* :r!cmd */
7858 do_bang(1, eap, FALSE, FALSE, TRUE);
7859 else
7860 {
7861 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7862 return;
7863
7864#ifdef FEAT_BROWSE
7865 if (cmdmod.browse)
7866 {
7867 char_u *browseFile;
7868
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007869 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 NULL, NULL, NULL, curbuf);
7871 if (browseFile != NULL)
7872 {
7873 i = readfile(browseFile, NULL,
7874 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7875 vim_free(browseFile);
7876 }
7877 else
7878 i = OK;
7879 }
7880 else
7881#endif
7882 if (*eap->arg == NUL)
7883 {
7884 if (check_fname() == FAIL) /* check for no file name */
7885 return;
7886 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7887 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7888 }
7889 else
7890 {
7891 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7892 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7893 i = readfile(eap->arg, NULL,
7894 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7895
7896 }
7897 if (i == FAIL)
7898 {
7899#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7900 if (!aborting())
7901#endif
7902 EMSG2(_(e_notopen), eap->arg);
7903 }
7904 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007905 {
7906 if (empty && exmode_active)
7907 {
7908 /* Delete the empty line that remains. Historically ex does
7909 * this but vi doesn't. */
7910 if (eap->line2 == 0)
7911 lnum = curbuf->b_ml.ml_line_count;
7912 else
7913 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007914 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007915 {
7916 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007917 if (curwin->w_cursor.lnum > 1
7918 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007919 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007920 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007921 }
7922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926}
7927
Bram Moolenaarea408852005-06-25 22:49:46 +00007928static char_u *prev_dir = NULL;
7929
7930#if defined(EXITFREE) || defined(PROTO)
7931 void
7932free_cd_dir()
7933{
7934 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007935 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007936
7937 vim_free(globaldir);
7938 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007939}
7940#endif
7941
7942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943/*
7944 * ":cd", ":lcd", ":chdir" and ":lchdir".
7945 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007946 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947ex_cd(eap)
7948 exarg_T *eap;
7949{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 char_u *new_dir;
7951 char_u *tofree;
7952
7953 new_dir = eap->arg;
7954#if !defined(UNIX) && !defined(VMS)
7955 /* for non-UNIX ":cd" means: print current directory */
7956 if (*new_dir == NUL)
7957 ex_pwd(NULL);
7958 else
7959#endif
7960 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007961#ifdef FEAT_AUTOCMD
7962 if (allbuf_locked())
7963 return;
7964#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007965 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7966 && !eap->forceit)
7967 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007968 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 return;
7970 }
7971
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 /* ":cd -": Change to previous directory */
7973 if (STRCMP(new_dir, "-") == 0)
7974 {
7975 if (prev_dir == NULL)
7976 {
7977 EMSG(_("E186: No previous directory"));
7978 return;
7979 }
7980 new_dir = prev_dir;
7981 }
7982
7983 /* Save current directory for next ":cd -" */
7984 tofree = prev_dir;
7985 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7986 prev_dir = vim_strsave(NameBuff);
7987 else
7988 prev_dir = NULL;
7989
7990#if defined(UNIX) || defined(VMS)
7991 /* for UNIX ":cd" means: go to home directory */
7992 if (*new_dir == NUL)
7993 {
7994 /* use NameBuff for home directory name */
7995# ifdef VMS
7996 char_u *p;
7997
7998 p = mch_getenv((char_u *)"SYS$LOGIN");
7999 if (p == NULL || *p == NUL) /* empty is the same as not set */
8000 NameBuff[0] = NUL;
8001 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008002 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003# else
8004 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8005# endif
8006 new_dir = NameBuff;
8007 }
8008#endif
8009 if (new_dir == NULL || vim_chdir(new_dir))
8010 EMSG(_(e_failed));
8011 else
8012 {
8013 vim_free(curwin->w_localdir);
8014 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8015 {
8016 /* If still in global directory, need to remember current
8017 * directory as global directory. */
8018 if (globaldir == NULL && prev_dir != NULL)
8019 globaldir = vim_strsave(prev_dir);
8020 /* Remember this local directory for the window. */
8021 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8022 curwin->w_localdir = vim_strsave(NameBuff);
8023 }
8024 else
8025 {
8026 /* We are now in the global directory, no need to remember its
8027 * name. */
8028 vim_free(globaldir);
8029 globaldir = NULL;
8030 curwin->w_localdir = NULL;
8031 }
8032
8033 shorten_fnames(TRUE);
8034
8035 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008036 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 ex_pwd(eap);
8038 }
8039 vim_free(tofree);
8040 }
8041}
8042
8043/*
8044 * ":pwd".
8045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 static void
8047ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008048 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8051 {
8052#ifdef BACKSLASH_IN_FILENAME
8053 slash_adjust(NameBuff);
8054#endif
8055 msg(NameBuff);
8056 }
8057 else
8058 EMSG(_("E187: Unknown"));
8059}
8060
8061/*
8062 * ":=".
8063 */
8064 static void
8065ex_equal(eap)
8066 exarg_T *eap;
8067{
8068 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008069 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070}
8071
8072 static void
8073ex_sleep(eap)
8074 exarg_T *eap;
8075{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008076 int n;
8077 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
8079 if (cursor_valid())
8080 {
8081 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8082 if (n >= 0)
8083 windgoto((int)n, curwin->w_wcol);
8084 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008085
8086 len = eap->line2;
8087 switch (*eap->arg)
8088 {
8089 case 'm': break;
8090 case NUL: len *= 1000L; break;
8091 default: EMSG2(_(e_invarg2), eap->arg); return;
8092 }
8093 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094}
8095
8096/*
8097 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8098 */
8099 void
8100do_sleep(msec)
8101 long msec;
8102{
8103 long done;
8104
8105 cursor_on();
8106 out_flush();
8107 for (done = 0; !got_int && done < msec; done += 1000L)
8108 {
8109 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8110 ui_breakcheck();
8111 }
8112}
8113
8114 static void
8115do_exmap(eap, isabbrev)
8116 exarg_T *eap;
8117 int isabbrev;
8118{
8119 int mode;
8120 char_u *cmdp;
8121
8122 cmdp = eap->cmd;
8123 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8124
8125 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8126 eap->arg, mode, isabbrev))
8127 {
8128 case 1: EMSG(_(e_invarg));
8129 break;
8130 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8131 break;
8132 }
8133}
8134
8135/*
8136 * ":winsize" command (obsolete).
8137 */
8138 static void
8139ex_winsize(eap)
8140 exarg_T *eap;
8141{
8142 int w, h;
8143 char_u *arg = eap->arg;
8144 char_u *p;
8145
8146 w = getdigits(&arg);
8147 arg = skipwhite(arg);
8148 p = arg;
8149 h = getdigits(&arg);
8150 if (*p != NUL && *arg == NUL)
8151 set_shellsize(w, h, TRUE);
8152 else
8153 EMSG(_("E465: :winsize requires two number arguments"));
8154}
8155
8156#ifdef FEAT_WINDOWS
8157 static void
8158ex_wincmd(eap)
8159 exarg_T *eap;
8160{
8161 int xchar = NUL;
8162 char_u *p;
8163
8164 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8165 {
8166 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8167 if (eap->arg[1] == NUL)
8168 {
8169 EMSG(_(e_invarg));
8170 return;
8171 }
8172 xchar = eap->arg[1];
8173 p = eap->arg + 2;
8174 }
8175 else
8176 p = eap->arg + 1;
8177
8178 eap->nextcmd = check_nextcmd(p);
8179 p = skipwhite(p);
8180 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8181 EMSG(_(e_invarg));
8182 else
8183 {
8184 /* Pass flags on for ":vertical wincmd ]". */
8185 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008186 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8188 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008189 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 }
8191}
8192#endif
8193
Bram Moolenaar843ee412004-06-30 16:16:41 +00008194#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195/*
8196 * ":winpos".
8197 */
8198 static void
8199ex_winpos(eap)
8200 exarg_T *eap;
8201{
8202 int x, y;
8203 char_u *arg = eap->arg;
8204 char_u *p;
8205
8206 if (*arg == NUL)
8207 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008208# if defined(FEAT_GUI) || defined(MSWIN)
8209# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008211# else
8212 if (mch_get_winpos(&x, &y) != FAIL)
8213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
8215 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8216 msg(IObuff);
8217 }
8218 else
8219# endif
8220 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8221 }
8222 else
8223 {
8224 x = getdigits(&arg);
8225 arg = skipwhite(arg);
8226 p = arg;
8227 y = getdigits(&arg);
8228 if (*p == NUL || *arg != NUL)
8229 {
8230 EMSG(_("E466: :winpos requires two number arguments"));
8231 return;
8232 }
8233# ifdef FEAT_GUI
8234 if (gui.in_use)
8235 gui_mch_set_winpos(x, y);
8236 else if (gui.starting)
8237 {
8238 /* Remember the coordinates for when the window is opened. */
8239 gui_win_x = x;
8240 gui_win_y = y;
8241 }
8242# ifdef HAVE_TGETENT
8243 else
8244# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008245# else
8246# ifdef MSWIN
8247 mch_set_winpos(x, y);
8248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249# endif
8250# ifdef HAVE_TGETENT
8251 if (*T_CWP)
8252 term_set_winpos(x, y);
8253# endif
8254 }
8255}
8256#endif
8257
8258/*
8259 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8260 */
8261 static void
8262ex_operators(eap)
8263 exarg_T *eap;
8264{
8265 oparg_T oa;
8266
8267 clear_oparg(&oa);
8268 oa.regname = eap->regname;
8269 oa.start.lnum = eap->line1;
8270 oa.end.lnum = eap->line2;
8271 oa.line_count = eap->line2 - eap->line1 + 1;
8272 oa.motion_type = MLINE;
8273#ifdef FEAT_VIRTUALEDIT
8274 virtual_op = FALSE;
8275#endif
8276 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8277 {
8278 setpcmark();
8279 curwin->w_cursor.lnum = eap->line1;
8280 beginline(BL_SOL | BL_FIX);
8281 }
8282
8283 switch (eap->cmdidx)
8284 {
8285 case CMD_delete:
8286 oa.op_type = OP_DELETE;
8287 op_delete(&oa);
8288 break;
8289
8290 case CMD_yank:
8291 oa.op_type = OP_YANK;
8292 (void)op_yank(&oa, FALSE, TRUE);
8293 break;
8294
8295 default: /* CMD_rshift or CMD_lshift */
8296 if ((eap->cmdidx == CMD_rshift)
8297#ifdef FEAT_RIGHTLEFT
8298 ^ curwin->w_p_rl
8299#endif
8300 )
8301 oa.op_type = OP_RSHIFT;
8302 else
8303 oa.op_type = OP_LSHIFT;
8304 op_shift(&oa, FALSE, eap->amount);
8305 break;
8306 }
8307#ifdef FEAT_VIRTUALEDIT
8308 virtual_op = MAYBE;
8309#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008310 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
8313/*
8314 * ":put".
8315 */
8316 static void
8317ex_put(eap)
8318 exarg_T *eap;
8319{
8320 /* ":0put" works like ":1put!". */
8321 if (eap->line2 == 0)
8322 {
8323 eap->line2 = 1;
8324 eap->forceit = TRUE;
8325 }
8326 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008327 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8328 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329}
8330
8331/*
8332 * Handle ":copy" and ":move".
8333 */
8334 static void
8335ex_copymove(eap)
8336 exarg_T *eap;
8337{
8338 long n;
8339
8340 n = get_address(&eap->arg, FALSE, FALSE);
8341 if (eap->arg == NULL) /* error detected */
8342 {
8343 eap->nextcmd = NULL;
8344 return;
8345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008346 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 /*
8349 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8350 */
8351 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8352 {
8353 EMSG(_(e_invaddr));
8354 return;
8355 }
8356
8357 if (eap->cmdidx == CMD_move)
8358 {
8359 if (do_move(eap->line1, eap->line2, n) == FAIL)
8360 return;
8361 }
8362 else
8363 ex_copy(eap->line1, eap->line2, n);
8364 u_clearline();
8365 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008366 ex_may_print(eap);
8367}
8368
8369/*
8370 * Print the current line if flags were given to the Ex command.
8371 */
8372 static void
8373ex_may_print(eap)
8374 exarg_T *eap;
8375{
8376 if (eap->flags != 0)
8377 {
8378 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8379 (eap->flags & EXFLAG_LIST));
8380 ex_no_reprint = TRUE;
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382}
8383
8384/*
8385 * ":smagic" and ":snomagic".
8386 */
8387 static void
8388ex_submagic(eap)
8389 exarg_T *eap;
8390{
8391 int magic_save = p_magic;
8392
8393 p_magic = (eap->cmdidx == CMD_smagic);
8394 do_sub(eap);
8395 p_magic = magic_save;
8396}
8397
8398/*
8399 * ":join".
8400 */
8401 static void
8402ex_join(eap)
8403 exarg_T *eap;
8404{
8405 curwin->w_cursor.lnum = eap->line1;
8406 if (eap->line1 == eap->line2)
8407 {
8408 if (eap->addr_count >= 2) /* :2,2join does nothing */
8409 return;
8410 if (eap->line2 == curbuf->b_ml.ml_line_count)
8411 {
8412 beep_flush();
8413 return;
8414 }
8415 ++eap->line2;
8416 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008417 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008419 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420}
8421
8422/*
8423 * ":[addr]@r" or ":[addr]*r": execute register
8424 */
8425 static void
8426ex_at(eap)
8427 exarg_T *eap;
8428{
8429 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008430 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
8432 curwin->w_cursor.lnum = eap->line2;
8433
8434#ifdef USE_ON_FLY_SCROLL
8435 dont_scroll = TRUE; /* disallow scrolling here */
8436#endif
8437
8438 /* get the register name. No name means to use the previous one */
8439 c = *eap->arg;
8440 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8441 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008442 /* Put the register in the typeahead buffer with the "silent" flag. */
8443 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8444 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 else
8449 {
8450 int save_efr = exec_from_reg;
8451
8452 exec_from_reg = TRUE;
8453
8454 /*
8455 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008456 * Continue until the stuff buffer is empty and all added characters
8457 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008459 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8461
8462 exec_from_reg = save_efr;
8463 }
8464}
8465
8466/*
8467 * ":!".
8468 */
8469 static void
8470ex_bang(eap)
8471 exarg_T *eap;
8472{
8473 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8474}
8475
8476/*
8477 * ":undo".
8478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 static void
8480ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008481 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008483 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008484 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008485 else
8486 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487}
8488
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008489#ifdef FEAT_PERSISTENT_UNDO
8490 void
8491ex_wundo(eap)
8492 exarg_T *eap;
8493{
8494 char_u hash[UNDO_HASH_SIZE];
8495
8496 u_compute_hash(hash);
8497 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8498}
8499
8500 void
8501ex_rundo(eap)
8502 exarg_T *eap;
8503{
8504 char_u hash[UNDO_HASH_SIZE];
8505
8506 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008507 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008508}
8509#endif
8510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511/*
8512 * ":redo".
8513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 static void
8515ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008516 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 u_redo(1);
8519}
8520
8521/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008522 * ":earlier" and ":later".
8523 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008524 static void
8525ex_later(eap)
8526 exarg_T *eap;
8527{
8528 long count = 0;
8529 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008530 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008531 char_u *p = eap->arg;
8532
8533 if (*p == NUL)
8534 count = 1;
8535 else if (isdigit(*p))
8536 {
8537 count = getdigits(&p);
8538 switch (*p)
8539 {
8540 case 's': ++p; sec = TRUE; break;
8541 case 'm': ++p; sec = TRUE; count *= 60; break;
8542 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008543 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8544 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008545 }
8546 }
8547
8548 if (*p != NUL)
8549 EMSG2(_(e_invarg2), eap->arg);
8550 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008551 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8552 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008553}
8554
8555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 * ":redir": start/stop redirection.
8557 */
8558 static void
8559ex_redir(eap)
8560 exarg_T *eap;
8561{
8562 char *mode;
8563 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008564 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566 if (STRICMP(eap->arg, "END") == 0)
8567 close_redir();
8568 else
8569 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008570 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008572 ++arg;
8573 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008575 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 mode = "a";
8577 }
8578 else
8579 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008580 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
8582 close_redir();
8583
8584 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008585 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (fname == NULL)
8587 return;
8588#ifdef FEAT_BROWSE
8589 if (cmdmod.browse)
8590 {
8591 char_u *browseFile;
8592
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008593 browseFile = do_browse(BROWSE_SAVE,
8594 (char_u *)_("Save Redirection"),
8595 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 if (browseFile == NULL)
8597 return; /* operation cancelled */
8598 vim_free(fname);
8599 fname = browseFile;
8600 eap->forceit = TRUE; /* since dialog already asked */
8601 }
8602#endif
8603
8604 redir_fd = open_exfile(fname, eap->forceit, mode);
8605 vim_free(fname);
8606 }
8607#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008608 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {
8610 /* redirect to a register a-z (resp. A-Z for appending) */
8611 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008612 ++arg;
8613 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008615 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008616 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008618 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008620 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008621 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008622 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008623 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008625 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008626 if (*arg == '>')
8627 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008628 /* Make register empty when not using @A-@Z and the
8629 * command is valid. */
8630 if (*arg == NUL && !isupper(redir_reg))
8631 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008633 }
8634 if (*arg != NUL)
8635 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008636 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008637 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008638 }
8639 }
8640 else if (*arg == '=' && arg[1] == '>')
8641 {
8642 int append;
8643
8644 /* redirect to a variable */
8645 close_redir();
8646 arg += 2;
8647
8648 if (*arg == '>')
8649 {
8650 ++arg;
8651 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008654 append = FALSE;
8655
8656 if (var_redir_start(skipwhite(arg), append) == OK)
8657 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 }
8659#endif
8660
8661 /* TODO: redirect to a buffer */
8662
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008664 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008666
8667 /* Make sure redirection is not off. Can happen for cmdline completion
8668 * that indirectly invokes a command to catch its output. */
8669 if (redir_fd != NULL
8670#ifdef FEAT_EVAL
8671 || redir_reg || redir_vname
8672#endif
8673 )
8674 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675}
8676
8677/*
8678 * ":redraw": force redraw
8679 */
8680 static void
8681ex_redraw(eap)
8682 exarg_T *eap;
8683{
8684 int r = RedrawingDisabled;
8685 int p = p_lz;
8686
8687 RedrawingDisabled = 0;
8688 p_lz = FALSE;
8689 update_topline();
8690 update_screen(eap->forceit ? CLEAR :
8691#ifdef FEAT_VISUAL
8692 VIsual_active ? INVERTED :
8693#endif
8694 0);
8695#ifdef FEAT_TITLE
8696 if (need_maketitle)
8697 maketitle();
8698#endif
8699 RedrawingDisabled = r;
8700 p_lz = p;
8701
8702 /* Reset msg_didout, so that a message that's there is overwritten. */
8703 msg_didout = FALSE;
8704 msg_col = 0;
8705
8706 out_flush();
8707}
8708
8709/*
8710 * ":redrawstatus": force redraw of status line(s)
8711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 static void
8713ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008714 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716#if defined(FEAT_WINDOWS)
8717 int r = RedrawingDisabled;
8718 int p = p_lz;
8719
8720 RedrawingDisabled = 0;
8721 p_lz = FALSE;
8722 if (eap->forceit)
8723 status_redraw_all();
8724 else
8725 status_redraw_curbuf();
8726 update_screen(
8727# ifdef FEAT_VISUAL
8728 VIsual_active ? INVERTED :
8729# endif
8730 0);
8731 RedrawingDisabled = r;
8732 p_lz = p;
8733 out_flush();
8734#endif
8735}
8736
8737 static void
8738close_redir()
8739{
8740 if (redir_fd != NULL)
8741 {
8742 fclose(redir_fd);
8743 redir_fd = NULL;
8744 }
8745#ifdef FEAT_EVAL
8746 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008747 if (redir_vname)
8748 {
8749 var_redir_stop();
8750 redir_vname = 0;
8751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752#endif
8753}
8754
8755#if defined(FEAT_SESSION) && defined(USE_CRNL)
8756# define MKSESSION_NL
8757static int mksession_nl = FALSE; /* use NL only in put_eol() */
8758#endif
8759
8760/*
8761 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8762 */
8763 static void
8764ex_mkrc(eap)
8765 exarg_T *eap;
8766{
8767 FILE *fd;
8768 int failed = FALSE;
8769 char_u *fname;
8770#ifdef FEAT_BROWSE
8771 char_u *browseFile = NULL;
8772#endif
8773#ifdef FEAT_SESSION
8774 int view_session = FALSE;
8775 int using_vdir = FALSE; /* using 'viewdir'? */
8776 char_u *viewFile = NULL;
8777 unsigned *flagp;
8778#endif
8779
8780 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8781 {
8782#ifdef FEAT_SESSION
8783 view_session = TRUE;
8784#else
8785 ex_ni(eap);
8786 return;
8787#endif
8788 }
8789
8790#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008791 /* Use the short file name until ":lcd" is used. We also don't use the
8792 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008793 did_lcd = FALSE;
8794
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8796 if (eap->cmdidx == CMD_mkview
8797 && (*eap->arg == NUL
8798 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8799 {
8800 eap->forceit = TRUE;
8801 fname = get_view_file(*eap->arg);
8802 if (fname == NULL)
8803 return;
8804 viewFile = fname;
8805 using_vdir = TRUE;
8806 }
8807 else
8808#endif
8809 if (*eap->arg != NUL)
8810 fname = eap->arg;
8811 else if (eap->cmdidx == CMD_mkvimrc)
8812 fname = (char_u *)VIMRC_FILE;
8813#ifdef FEAT_SESSION
8814 else if (eap->cmdidx == CMD_mksession)
8815 fname = (char_u *)SESSION_FILE;
8816#endif
8817 else
8818 fname = (char_u *)EXRC_FILE;
8819
8820#ifdef FEAT_BROWSE
8821 if (cmdmod.browse)
8822 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008823 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824# ifdef FEAT_SESSION
8825 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8826 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8827# endif
8828 (char_u *)_("Save Setup"),
8829 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8830 if (browseFile == NULL)
8831 goto theend;
8832 fname = browseFile;
8833 eap->forceit = TRUE; /* since dialog already asked */
8834 }
8835#endif
8836
8837#if defined(FEAT_SESSION) && defined(vim_mkdir)
8838 /* When using 'viewdir' may have to create the directory. */
8839 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008840 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841#endif
8842
8843 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8844 if (fd != NULL)
8845 {
8846#ifdef FEAT_SESSION
8847 if (eap->cmdidx == CMD_mkview)
8848 flagp = &vop_flags;
8849 else
8850 flagp = &ssop_flags;
8851#endif
8852
8853#ifdef MKSESSION_NL
8854 /* "unix" in 'sessionoptions': use NL line separator */
8855 if (view_session && (*flagp & SSOP_UNIX))
8856 mksession_nl = TRUE;
8857#endif
8858
8859 /* Write the version command for :mkvimrc */
8860 if (eap->cmdidx == CMD_mkvimrc)
8861 (void)put_line(fd, "version 6.0");
8862
8863#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008864 if (eap->cmdidx == CMD_mksession)
8865 {
8866 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8867 failed = TRUE;
8868 }
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 if (eap->cmdidx != CMD_mkview)
8871#endif
8872 {
8873 /* Write setting 'compatible' first, because it has side effects.
8874 * For that same reason only do it when needed. */
8875 if (p_cp)
8876 (void)put_line(fd, "if !&cp | set cp | endif");
8877 else
8878 (void)put_line(fd, "if &cp | set nocp | endif");
8879 }
8880
8881#ifdef FEAT_SESSION
8882 if (!view_session
8883 || (eap->cmdidx == CMD_mksession
8884 && (*flagp & SSOP_OPTIONS)))
8885#endif
8886 failed |= (makemap(fd, NULL) == FAIL
8887 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8888
8889#ifdef FEAT_SESSION
8890 if (!failed && view_session)
8891 {
8892 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8893 failed = TRUE;
8894 if (eap->cmdidx == CMD_mksession)
8895 {
8896 char_u dirnow[MAXPATHL]; /* current directory */
8897
8898 /*
8899 * Change to session file's dir.
8900 */
8901 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8902 || mch_chdir((char *)dirnow) != 0)
8903 *dirnow = NUL;
8904 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8905 {
8906 if (vim_chdirfile(fname) == OK)
8907 shorten_fnames(TRUE);
8908 }
8909 else if (*dirnow != NUL
8910 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8911 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008912 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008913 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915
8916 failed |= (makeopens(fd, dirnow) == FAIL);
8917
8918 /* restore original dir */
8919 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8920 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8921 {
8922 if (mch_chdir((char *)dirnow) != 0)
8923 EMSG(_(e_prev_dir));
8924 shorten_fnames(TRUE);
8925 }
8926 }
8927 else
8928 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008929 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8930 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
8932 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8933 == FAIL)
8934 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008935 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8936 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008937 if (eap->cmdidx == CMD_mksession)
8938 {
8939 if (put_line(fd, "unlet SessionLoad") == FAIL)
8940 failed = TRUE;
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 }
8943#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008944 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8945 failed = TRUE;
8946
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 failed |= fclose(fd);
8948
8949 if (failed)
8950 EMSG(_(e_write));
8951#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8952 else if (eap->cmdidx == CMD_mksession)
8953 {
8954 /* successful session write - set this_session var */
8955 char_u tbuf[MAXPATHL];
8956
8957 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8958 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8959 }
8960#endif
8961#ifdef MKSESSION_NL
8962 mksession_nl = FALSE;
8963#endif
8964 }
8965
8966#ifdef FEAT_BROWSE
8967theend:
8968 vim_free(browseFile);
8969#endif
8970#ifdef FEAT_SESSION
8971 vim_free(viewFile);
8972#endif
8973}
8974
Bram Moolenaardf177f62005-02-22 08:39:57 +00008975#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8976 || defined(PROTO)
8977 int
8978vim_mkdir_emsg(name, prot)
8979 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008980 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008981{
8982 if (vim_mkdir(name, prot) != 0)
8983 {
8984 EMSG2(_("E739: Cannot create directory: %s"), name);
8985 return FAIL;
8986 }
8987 return OK;
8988}
8989#endif
8990
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991/*
8992 * Open a file for writing for an Ex command, with some checks.
8993 * Return file descriptor, or NULL on failure.
8994 */
8995 FILE *
8996open_exfile(fname, forceit, mode)
8997 char_u *fname;
8998 int forceit;
8999 char *mode; /* "w" for create new file or "a" for append */
9000{
9001 FILE *fd;
9002
9003#ifdef UNIX
9004 /* with Unix it is possible to open a directory */
9005 if (mch_isdir(fname))
9006 {
9007 EMSG2(_(e_isadir2), fname);
9008 return NULL;
9009 }
9010#endif
9011 if (!forceit && *mode != 'a' && vim_fexists(fname))
9012 {
9013 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9014 return NULL;
9015 }
9016
9017 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9018 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9019
9020 return fd;
9021}
9022
9023/*
9024 * ":mark" and ":k".
9025 */
9026 static void
9027ex_mark(eap)
9028 exarg_T *eap;
9029{
9030 pos_T pos;
9031
9032 if (*eap->arg == NUL) /* No argument? */
9033 EMSG(_(e_argreq));
9034 else if (eap->arg[1] != NUL) /* more than one character? */
9035 EMSG(_(e_trailing));
9036 else
9037 {
9038 pos = curwin->w_cursor; /* save curwin->w_cursor */
9039 curwin->w_cursor.lnum = eap->line2;
9040 beginline(BL_WHITE | BL_FIX);
9041 if (setmark(*eap->arg) == FAIL) /* set mark */
9042 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9043 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9044 }
9045}
9046
9047/*
9048 * Update w_topline, w_leftcol and the cursor position.
9049 */
9050 void
9051update_topline_cursor()
9052{
9053 check_cursor(); /* put cursor on valid line */
9054 update_topline();
9055 if (!curwin->w_p_wrap)
9056 validate_cursor();
9057 update_curswant();
9058}
9059
9060#ifdef FEAT_EX_EXTRA
9061/*
9062 * ":normal[!] {commands}": Execute normal mode commands.
9063 */
9064 static void
9065ex_normal(eap)
9066 exarg_T *eap;
9067{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int save_msg_scroll = msg_scroll;
9069 int save_restart_edit = restart_edit;
9070 int save_msg_didout = msg_didout;
9071 int save_State = State;
9072 tasave_T tabuf;
9073 int save_insertmode = p_im;
9074 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009075 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076#ifdef FEAT_MBYTE
9077 char_u *arg = NULL;
9078 int l;
9079 char_u *p;
9080#endif
9081
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009082 if (ex_normal_lock > 0)
9083 {
9084 EMSG(_(e_secure));
9085 return;
9086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (ex_normal_busy >= p_mmd)
9088 {
9089 EMSG(_("E192: Recursive use of :normal too deep"));
9090 return;
9091 }
9092 ++ex_normal_busy;
9093
9094 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9095 restart_edit = 0; /* don't go to Insert mode */
9096 p_im = FALSE; /* don't use 'insertmode' */
9097
9098#ifdef FEAT_MBYTE
9099 /*
9100 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9101 * this for the K_SPECIAL leading byte, otherwise special keys will not
9102 * work.
9103 */
9104 if (has_mbyte)
9105 {
9106 int len = 0;
9107
9108 /* Count the number of characters to be escaped. */
9109 for (p = eap->arg; *p != NUL; ++p)
9110 {
9111# ifdef FEAT_GUI
9112 if (*p == CSI) /* leadbyte CSI */
9113 len += 2;
9114# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009115 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9117# ifdef FEAT_GUI
9118 || *p == CSI
9119# endif
9120 )
9121 len += 2;
9122 }
9123 if (len > 0)
9124 {
9125 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9126 if (arg != NULL)
9127 {
9128 len = 0;
9129 for (p = eap->arg; *p != NUL; ++p)
9130 {
9131 arg[len++] = *p;
9132# ifdef FEAT_GUI
9133 if (*p == CSI)
9134 {
9135 arg[len++] = KS_EXTRA;
9136 arg[len++] = (int)KE_CSI;
9137 }
9138# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009139 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 {
9141 arg[len++] = *++p;
9142 if (*p == K_SPECIAL)
9143 {
9144 arg[len++] = KS_SPECIAL;
9145 arg[len++] = KE_FILLER;
9146 }
9147# ifdef FEAT_GUI
9148 else if (*p == CSI)
9149 {
9150 arg[len++] = KS_EXTRA;
9151 arg[len++] = (int)KE_CSI;
9152 }
9153# endif
9154 }
9155 arg[len] = NUL;
9156 }
9157 }
9158 }
9159 }
9160#endif
9161
9162 /*
9163 * Save the current typeahead. This is required to allow using ":normal"
9164 * from an event handler and makes sure we don't hang when the argument
9165 * ends with half a command.
9166 */
9167 save_typeahead(&tabuf);
9168 if (tabuf.typebuf_valid)
9169 {
9170 /*
9171 * Repeat the :normal command for each line in the range. When no
9172 * range given, execute it just once, without positioning the cursor
9173 * first.
9174 */
9175 do
9176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 if (eap->addr_count != 0)
9178 {
9179 curwin->w_cursor.lnum = eap->line1++;
9180 curwin->w_cursor.col = 0;
9181 }
9182
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009183 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#ifdef FEAT_MBYTE
9185 arg != NULL ? arg :
9186#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009187 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9190 }
9191
9192 /* Might not return to the main loop when in an event handler. */
9193 update_topline_cursor();
9194
9195 /* Restore the previous typeahead. */
9196 restore_typeahead(&tabuf);
9197
9198 --ex_normal_busy;
9199 msg_scroll = save_msg_scroll;
9200 restart_edit = save_restart_edit;
9201 p_im = save_insertmode;
9202 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009203 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9205
9206 /* Restore the state (needed when called from a function executed for
9207 * 'indentexpr'). */
9208 State = save_State;
9209#ifdef FEAT_MBYTE
9210 vim_free(arg);
9211#endif
9212}
9213
9214/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009215 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 */
9217 static void
9218ex_startinsert(eap)
9219 exarg_T *eap;
9220{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009221 if (eap->forceit)
9222 {
9223 coladvance((colnr_T)MAXCOL);
9224 curwin->w_curswant = MAXCOL;
9225 curwin->w_set_curswant = FALSE;
9226 }
9227
Bram Moolenaara40c5002005-01-09 21:16:21 +00009228 /* Ignore the command when already in Insert mode. Inserting an
9229 * expression register that invokes a function can do this. */
9230 if (State & INSERT)
9231 return;
9232
Bram Moolenaar64969662005-12-14 21:59:55 +00009233 if (eap->cmdidx == CMD_startinsert)
9234 restart_edit = 'a';
9235 else if (eap->cmdidx == CMD_startreplace)
9236 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009238 restart_edit = 'V';
9239
9240 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009242 if (eap->cmdidx == CMD_startinsert)
9243 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 curwin->w_curswant = 0; /* avoid MAXCOL */
9245 }
9246}
9247
9248/*
9249 * ":stopinsert"
9250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 static void
9252ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009253 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255 restart_edit = 0;
9256 stop_insert_mode = TRUE;
9257}
9258#endif
9259
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009260#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9261/*
9262 * Execute normal mode command "cmd".
9263 * "remap" can be REMAP_NONE or REMAP_YES.
9264 */
9265 void
9266exec_normal_cmd(cmd, remap, silent)
9267 char_u *cmd;
9268 int remap;
9269 int silent;
9270{
9271 oparg_T oa;
9272
9273 /*
9274 * Stuff the argument into the typeahead buffer.
9275 * Execute normal_cmd() until there is no typeahead left.
9276 */
9277 clear_oparg(&oa);
9278 finish_op = FALSE;
9279 ins_typebuf(cmd, remap, 0, TRUE, silent);
9280 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9281 && !got_int)
9282 {
9283 update_topline_cursor();
9284 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9285 }
9286}
9287#endif
9288
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289#ifdef FEAT_FIND_ID
9290 static void
9291ex_checkpath(eap)
9292 exarg_T *eap;
9293{
9294 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9295 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9296 (linenr_T)1, (linenr_T)MAXLNUM);
9297}
9298
9299#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9300/*
9301 * ":psearch"
9302 */
9303 static void
9304ex_psearch(eap)
9305 exarg_T *eap;
9306{
9307 g_do_tagpreview = p_pvh;
9308 ex_findpat(eap);
9309 g_do_tagpreview = 0;
9310}
9311#endif
9312
9313 static void
9314ex_findpat(eap)
9315 exarg_T *eap;
9316{
9317 int whole = TRUE;
9318 long n;
9319 char_u *p;
9320 int action;
9321
9322 switch (cmdnames[eap->cmdidx].cmd_name[2])
9323 {
9324 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9325 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9326 action = ACTION_GOTO;
9327 else
9328 action = ACTION_SHOW;
9329 break;
9330 case 'i': /* ":ilist" and ":dlist" */
9331 action = ACTION_SHOW_ALL;
9332 break;
9333 case 'u': /* ":ijump" and ":djump" */
9334 action = ACTION_GOTO;
9335 break;
9336 default: /* ":isplit" and ":dsplit" */
9337 action = ACTION_SPLIT;
9338 break;
9339 }
9340
9341 n = 1;
9342 if (vim_isdigit(*eap->arg)) /* get count */
9343 {
9344 n = getdigits(&eap->arg);
9345 eap->arg = skipwhite(eap->arg);
9346 }
9347 if (*eap->arg == '/') /* Match regexp, not just whole words */
9348 {
9349 whole = FALSE;
9350 ++eap->arg;
9351 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9352 if (*p)
9353 {
9354 *p++ = NUL;
9355 p = skipwhite(p);
9356
9357 /* Check for trailing illegal characters */
9358 if (!ends_excmd(*p))
9359 eap->errmsg = e_trailing;
9360 else
9361 eap->nextcmd = check_nextcmd(p);
9362 }
9363 }
9364 if (!eap->skip)
9365 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9366 whole, !eap->forceit,
9367 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9368 n, action, eap->line1, eap->line2);
9369}
9370#endif
9371
9372#ifdef FEAT_WINDOWS
9373
9374# ifdef FEAT_QUICKFIX
9375/*
9376 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9377 */
9378 static void
9379ex_ptag(eap)
9380 exarg_T *eap;
9381{
9382 g_do_tagpreview = p_pvh;
9383 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9384}
9385
9386/*
9387 * ":pedit"
9388 */
9389 static void
9390ex_pedit(eap)
9391 exarg_T *eap;
9392{
9393 win_T *curwin_save = curwin;
9394
9395 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009396 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 keep_help_flag = curwin_save->w_buffer->b_help;
9398 do_exedit(eap, NULL);
9399 keep_help_flag = FALSE;
9400 if (curwin != curwin_save && win_valid(curwin_save))
9401 {
9402 /* Return cursor to where we were */
9403 validate_cursor();
9404 redraw_later(VALID);
9405 win_enter(curwin_save, TRUE);
9406 }
9407 g_do_tagpreview = 0;
9408}
9409# endif
9410
9411/*
9412 * ":stag", ":stselect" and ":stjump".
9413 */
9414 static void
9415ex_stag(eap)
9416 exarg_T *eap;
9417{
9418 postponed_split = -1;
9419 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009420 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9422 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009423 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425#endif
9426
9427/*
9428 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9429 */
9430 static void
9431ex_tag(eap)
9432 exarg_T *eap;
9433{
9434 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9435}
9436
9437 static void
9438ex_tag_cmd(eap, name)
9439 exarg_T *eap;
9440 char_u *name;
9441{
9442 int cmd;
9443
9444 switch (name[1])
9445 {
9446 case 'j': cmd = DT_JUMP; /* ":tjump" */
9447 break;
9448 case 's': cmd = DT_SELECT; /* ":tselect" */
9449 break;
9450 case 'p': cmd = DT_PREV; /* ":tprevious" */
9451 break;
9452 case 'N': cmd = DT_PREV; /* ":tNext" */
9453 break;
9454 case 'n': cmd = DT_NEXT; /* ":tnext" */
9455 break;
9456 case 'o': cmd = DT_POP; /* ":pop" */
9457 break;
9458 case 'f': /* ":tfirst" */
9459 case 'r': cmd = DT_FIRST; /* ":trewind" */
9460 break;
9461 case 'l': cmd = DT_LAST; /* ":tlast" */
9462 break;
9463 default: /* ":tag" */
9464#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009465 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 do_cstag(eap);
9468 return;
9469 }
9470#endif
9471 cmd = DT_TAG;
9472 break;
9473 }
9474
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009475 if (name[0] == 'l')
9476 {
9477#ifndef FEAT_QUICKFIX
9478 ex_ni(eap);
9479 return;
9480#else
9481 cmd = DT_LTAG;
9482#endif
9483 }
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9486 eap->forceit, TRUE);
9487}
9488
9489/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009490 * Check "str" for starting with a special cmdline variable.
9491 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9492 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9493 */
9494 int
9495find_cmdline_var(src, usedlen)
9496 char_u *src;
9497 int *usedlen;
9498{
9499 int len;
9500 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009501 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009502 "%",
9503#define SPEC_PERC 0
9504 "#",
9505#define SPEC_HASH 1
9506 "<cword>", /* cursor word */
9507#define SPEC_CWORD 2
9508 "<cWORD>", /* cursor WORD */
9509#define SPEC_CCWORD 3
9510 "<cfile>", /* cursor path name */
9511#define SPEC_CFILE 4
9512 "<sfile>", /* ":so" file name */
9513#define SPEC_SFILE 5
9514#ifdef FEAT_AUTOCMD
9515 "<afile>", /* autocommand file name */
9516# define SPEC_AFILE 6
9517 "<abuf>", /* autocommand buffer number */
9518# define SPEC_ABUF 7
9519 "<amatch>", /* autocommand match name */
9520# define SPEC_AMATCH 8
9521#endif
9522#ifdef FEAT_CLIENTSERVER
9523 "<client>"
9524# define SPEC_CLIENT 9
9525#endif
9526 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009527
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009528 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009529 {
9530 len = (int)STRLEN(spec_str[i]);
9531 if (STRNCMP(src, spec_str[i], len) == 0)
9532 {
9533 *usedlen = len;
9534 return i;
9535 }
9536 }
9537 return -1;
9538}
9539
9540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 * Evaluate cmdline variables.
9542 *
9543 * change '%' to curbuf->b_ffname
9544 * '#' to curwin->w_altfile
9545 * '<cword>' to word under the cursor
9546 * '<cWORD>' to WORD under the cursor
9547 * '<cfile>' to path name under the cursor
9548 * '<sfile>' to sourced file name
9549 * '<afile>' to file name for autocommand
9550 * '<abuf>' to buffer number for autocommand
9551 * '<amatch>' to matching name for autocommand
9552 *
9553 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9554 * "" for error without a message) and NULL is returned.
9555 * Returns an allocated string if a valid match was found.
9556 * Returns NULL if no match was found. "usedlen" then still contains the
9557 * number of characters to skip.
9558 */
9559 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009560eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009562 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 int *usedlen; /* characters after src that are used */
9564 linenr_T *lnump; /* line number for :e command, or NULL */
9565 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009566 int *escaped; /* return value has escaped white space (can
9567 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 int i;
9570 char_u *s;
9571 char_u *result;
9572 char_u *resultbuf = NULL;
9573 int resultlen;
9574 buf_T *buf;
9575 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9576 int spec_idx;
9577#ifdef FEAT_MODIFY_FNAME
9578 int skip_mod = FALSE;
9579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580
9581#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9582 char_u strbuf[30];
9583#endif
9584
9585 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009586 if (escaped != NULL)
9587 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588
9589 /*
9590 * Check if there is something to do.
9591 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009592 spec_idx = find_cmdline_var(src, usedlen);
9593 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
9595 *usedlen = 1;
9596 return NULL;
9597 }
9598
9599 /*
9600 * Skip when preceded with a backslash "\%" and "\#".
9601 * Note: In "\\%" the % is also not recognized!
9602 */
9603 if (src > srcstart && src[-1] == '\\')
9604 {
9605 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009606 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 return NULL;
9608 }
9609
9610 /*
9611 * word or WORD under cursor
9612 */
9613 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9614 {
9615 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9616 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9617 if (resultlen == 0)
9618 {
9619 *errormsg = (char_u *)"";
9620 return NULL;
9621 }
9622 }
9623
9624 /*
9625 * '#': Alternate file name
9626 * '%': Current file name
9627 * File name under the cursor
9628 * File name for autocommand
9629 * and following modifiers
9630 */
9631 else
9632 {
9633 switch (spec_idx)
9634 {
9635 case SPEC_PERC: /* '%': current file */
9636 if (curbuf->b_fname == NULL)
9637 {
9638 result = (char_u *)"";
9639 valid = 0; /* Must have ":p:h" to be valid */
9640 }
9641 else
9642#ifdef RISCOS
9643 /* Always use the full path for RISC OS if possible. */
9644 result = curbuf->b_ffname;
9645 if (result == NULL)
9646 result = curbuf->b_fname;
9647#else
9648 result = curbuf->b_fname;
9649#endif
9650 break;
9651
9652 case SPEC_HASH: /* '#' or "#99": alternate file */
9653 if (src[1] == '#') /* "##": the argument list */
9654 {
9655 result = arg_all();
9656 resultbuf = result;
9657 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009658 if (escaped != NULL)
9659 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660#ifdef FEAT_MODIFY_FNAME
9661 skip_mod = TRUE;
9662#endif
9663 break;
9664 }
9665 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009666 if (*s == '<') /* "#<99" uses v:oldfiles */
9667 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 i = (int)getdigits(&s);
9669 *usedlen = (int)(s - src); /* length of what we expand */
9670
Bram Moolenaard812df62008-11-09 12:46:09 +00009671 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009673 if (*usedlen < 2)
9674 {
9675 /* Should we give an error message for #<text? */
9676 *usedlen = 1;
9677 return NULL;
9678 }
9679#ifdef FEAT_EVAL
9680 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9681 (long)i);
9682 if (result == NULL)
9683 {
9684 *errormsg = (char_u *)"";
9685 return NULL;
9686 }
9687#else
9688 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 }
9692 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009693 {
9694 buf = buflist_findnr(i);
9695 if (buf == NULL)
9696 {
9697 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9698 return NULL;
9699 }
9700 if (lnump != NULL)
9701 *lnump = ECMD_LAST;
9702 if (buf->b_fname == NULL)
9703 {
9704 result = (char_u *)"";
9705 valid = 0; /* Must have ":p:h" to be valid */
9706 }
9707 else
9708 result = buf->b_fname;
9709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 break;
9711
9712#ifdef FEAT_SEARCHPATH
9713 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009714 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 if (result == NULL)
9716 {
9717 *errormsg = (char_u *)"";
9718 return NULL;
9719 }
9720 resultbuf = result; /* remember allocated string */
9721 break;
9722#endif
9723
9724#ifdef FEAT_AUTOCMD
9725 case SPEC_AFILE: /* file name for autocommand */
9726 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009727 if (result != NULL && !autocmd_fname_full)
9728 {
9729 /* Still need to turn the fname into a full path. It is
9730 * postponed to avoid a delay when <afile> is not used. */
9731 autocmd_fname_full = TRUE;
9732 result = FullName_save(autocmd_fname, FALSE);
9733 vim_free(autocmd_fname);
9734 autocmd_fname = result;
9735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 if (result == NULL)
9737 {
9738 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9739 return NULL;
9740 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009741 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 break;
9743
9744 case SPEC_ABUF: /* buffer number for autocommand */
9745 if (autocmd_bufnr <= 0)
9746 {
9747 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9748 return NULL;
9749 }
9750 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9751 result = strbuf;
9752 break;
9753
9754 case SPEC_AMATCH: /* match name for autocommand */
9755 result = autocmd_match;
9756 if (result == NULL)
9757 {
9758 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9759 return NULL;
9760 }
9761 break;
9762
9763#endif
9764 case SPEC_SFILE: /* file name for ":so" command */
9765 result = sourcing_name;
9766 if (result == NULL)
9767 {
9768 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9769 return NULL;
9770 }
9771 break;
9772#if defined(FEAT_CLIENTSERVER)
9773 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009774 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9775 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 result = strbuf;
9777 break;
9778#endif
9779 }
9780
9781 resultlen = (int)STRLEN(result); /* length of new string */
9782 if (src[*usedlen] == '<') /* remove the file name extension */
9783 {
9784 ++*usedlen;
9785#ifdef RISCOS
9786 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9787#else
9788 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9789#endif
9790 resultlen = (int)(s - result);
9791 }
9792#ifdef FEAT_MODIFY_FNAME
9793 else if (!skip_mod)
9794 {
9795 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9796 &resultlen);
9797 if (result == NULL)
9798 {
9799 *errormsg = (char_u *)"";
9800 return NULL;
9801 }
9802 }
9803#endif
9804 }
9805
9806 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9807 {
9808 if (valid != VALID_HEAD + VALID_PATH)
9809 /* xgettext:no-c-format */
9810 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9811 else
9812 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9813 result = NULL;
9814 }
9815 else
9816 result = vim_strnsave(result, resultlen);
9817 vim_free(resultbuf);
9818 return result;
9819}
9820
9821/*
9822 * Concatenate all files in the argument list, separated by spaces, and return
9823 * it in one allocated string.
9824 * Spaces and backslashes in the file names are escaped with a backslash.
9825 * Returns NULL when out of memory.
9826 */
9827 static char_u *
9828arg_all()
9829{
9830 int len;
9831 int idx;
9832 char_u *retval = NULL;
9833 char_u *p;
9834
9835 /*
9836 * Do this loop two times:
9837 * first time: compute the total length
9838 * second time: concatenate the names
9839 */
9840 for (;;)
9841 {
9842 len = 0;
9843 for (idx = 0; idx < ARGCOUNT; ++idx)
9844 {
9845 p = alist_name(&ARGLIST[idx]);
9846 if (p != NULL)
9847 {
9848 if (len > 0)
9849 {
9850 /* insert a space in between names */
9851 if (retval != NULL)
9852 retval[len] = ' ';
9853 ++len;
9854 }
9855 for ( ; *p != NUL; ++p)
9856 {
9857 if (*p == ' ' || *p == '\\')
9858 {
9859 /* insert a backslash */
9860 if (retval != NULL)
9861 retval[len] = '\\';
9862 ++len;
9863 }
9864 if (retval != NULL)
9865 retval[len] = *p;
9866 ++len;
9867 }
9868 }
9869 }
9870
9871 /* second time: break here */
9872 if (retval != NULL)
9873 {
9874 retval[len] = NUL;
9875 break;
9876 }
9877
9878 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009879 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 if (retval == NULL)
9881 break;
9882 }
9883
9884 return retval;
9885}
9886
9887#if defined(FEAT_AUTOCMD) || defined(PROTO)
9888/*
9889 * Expand the <sfile> string in "arg".
9890 *
9891 * Returns an allocated string, or NULL for any error.
9892 */
9893 char_u *
9894expand_sfile(arg)
9895 char_u *arg;
9896{
9897 char_u *errormsg;
9898 int len;
9899 char_u *result;
9900 char_u *newres;
9901 char_u *repl;
9902 int srclen;
9903 char_u *p;
9904
9905 result = vim_strsave(arg);
9906 if (result == NULL)
9907 return NULL;
9908
9909 for (p = result; *p; )
9910 {
9911 if (STRNCMP(p, "<sfile>", 7) != 0)
9912 ++p;
9913 else
9914 {
9915 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009916 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (errormsg != NULL)
9918 {
9919 if (*errormsg)
9920 emsg(errormsg);
9921 vim_free(result);
9922 return NULL;
9923 }
9924 if (repl == NULL) /* no match (cannot happen) */
9925 {
9926 p += srclen;
9927 continue;
9928 }
9929 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9930 newres = alloc(len);
9931 if (newres == NULL)
9932 {
9933 vim_free(repl);
9934 vim_free(result);
9935 return NULL;
9936 }
9937 mch_memmove(newres, result, (size_t)(p - result));
9938 STRCPY(newres + (p - result), repl);
9939 len = (int)STRLEN(newres);
9940 STRCAT(newres, p + srclen);
9941 vim_free(repl);
9942 vim_free(result);
9943 result = newres;
9944 p = newres + len; /* continue after the match */
9945 }
9946 }
9947
9948 return result;
9949}
9950#endif
9951
9952#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009953static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9954 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9956static frame_T *ses_skipframe __ARGS((frame_T *fr));
9957static int ses_do_frame __ARGS((frame_T *fr));
9958static int ses_do_win __ARGS((win_T *wp));
9959static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9960static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9961static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9962
9963/*
9964 * Write openfile commands for the current buffers to an .exrc file.
9965 * Return FAIL on error, OK otherwise.
9966 */
9967 static int
9968makeopens(fd, dirnow)
9969 FILE *fd;
9970 char_u *dirnow; /* Current directory name */
9971{
9972 buf_T *buf;
9973 int only_save_windows = TRUE;
9974 int nr;
9975 int cnr = 1;
9976 int restore_size = TRUE;
9977 win_T *wp;
9978 char_u *sname;
9979 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009980 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009981 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009982 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009983 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009984 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985
9986 if (ssop_flags & SSOP_BUFFERS)
9987 only_save_windows = FALSE; /* Save ALL buffers */
9988
9989 /*
9990 * Begin by setting the this_session variable, and then other
9991 * sessionable variables.
9992 */
9993#ifdef FEAT_EVAL
9994 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9995 return FAIL;
9996 if (ssop_flags & SSOP_GLOBALS)
9997 if (store_session_globals(fd) == FAIL)
9998 return FAIL;
9999#endif
10000
10001 /*
10002 * Close all windows but one.
10003 */
10004 if (put_line(fd, "silent only") == FAIL)
10005 return FAIL;
10006
10007 /*
10008 * Now a :cd command to the session directory or the current directory
10009 */
10010 if (ssop_flags & SSOP_SESDIR)
10011 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010012 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10013 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 return FAIL;
10015 }
10016 else if (ssop_flags & SSOP_CURDIR)
10017 {
10018 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10019 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010020 || fputs("cd ", fd) < 0
10021 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10022 || put_eol(fd) == FAIL)
10023 {
10024 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 vim_free(sname);
10028 }
10029
10030 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010031 * If there is an empty, unnamed buffer we will wipe it out later.
10032 * Remember the buffer number.
10033 */
10034 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10035 return FAIL;
10036 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10037 return FAIL;
10038 if (put_line(fd, "endif") == FAIL)
10039 return FAIL;
10040
10041 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 * Now save the current files, current buffer first.
10043 */
10044 if (put_line(fd, "set shortmess=aoO") == FAIL)
10045 return FAIL;
10046
10047 /* Now put the other buffers into the buffer list */
10048 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10049 {
10050 if (!(only_save_windows && buf->b_nwindows == 0)
10051 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10052 && buf->b_fname != NULL
10053 && buf->b_p_bl)
10054 {
10055 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10056 : buf->b_wininfo->wi_fpos.lnum) < 0
10057 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10058 return FAIL;
10059 }
10060 }
10061
10062 /* the global argument list */
10063 if (ses_arglist(fd, "args", &global_alist.al_ga,
10064 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10065 return FAIL;
10066
10067 if (ssop_flags & SSOP_RESIZE)
10068 {
10069 /* Note: after the restore we still check it worked!*/
10070 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10071 || put_eol(fd) == FAIL)
10072 return FAIL;
10073 }
10074
10075#ifdef FEAT_GUI
10076 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10077 {
10078 int x, y;
10079
10080 if (gui_mch_get_winpos(&x, &y) == OK)
10081 {
10082 /* Note: after the restore we still check it worked!*/
10083 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10084 return FAIL;
10085 }
10086 }
10087#endif
10088
10089 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010090 * May repeat putting Windows for each tab, when "tabpages" is in
10091 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010092 * Don't use goto_tabpage(), it may change directory and trigger
10093 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010095 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010096 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010097 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010099 int need_tabnew = FALSE;
10100
Bram Moolenaar18144c82006-04-12 21:52:12 +000010101 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010103 tabpage_T *tp = find_tabpage(tabnr);
10104
10105 if (tp == NULL)
10106 break; /* done all tab pages */
10107 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010108 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010109 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010110 tab_topframe = topframe;
10111 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010112 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010113 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010114 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010115 tab_topframe = tp->tp_topframe;
10116 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010117 if (tabnr > 1)
10118 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaar18144c82006-04-12 21:52:12 +000010121 /*
10122 * Before creating the window layout, try loading one file. If this
10123 * is aborted we don't end up with a number of useless windows.
10124 * This may have side effects! (e.g., compressed or network file).
10125 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010126 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010127 {
10128 if (ses_do_win(wp)
10129 && wp->w_buffer->b_ffname != NULL
10130 && !wp->w_buffer->b_help
10131#ifdef FEAT_QUICKFIX
10132 && !bt_nofile(wp->w_buffer)
10133#endif
10134 )
10135 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010136 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010137 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10138 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010139 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010140 if (!wp->w_arg_idx_invalid)
10141 edited_win = wp;
10142 break;
10143 }
10144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010146 /* If no file got edited create an empty tab page. */
10147 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10148 return FAIL;
10149
Bram Moolenaar18144c82006-04-12 21:52:12 +000010150 /*
10151 * Save current window layout.
10152 */
10153 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010155 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010157 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10158 return FAIL;
10159 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaar18144c82006-04-12 21:52:12 +000010162 /*
10163 * Check if window sizes can be restored (no windows omitted).
10164 * Remember the window number of the current window after restoring.
10165 */
10166 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010167 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010168 {
10169 if (ses_do_win(wp))
10170 ++nr;
10171 else
10172 restore_size = FALSE;
10173 if (curwin == wp)
10174 cnr = nr;
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176
Bram Moolenaar18144c82006-04-12 21:52:12 +000010177 /* Go to the first window. */
10178 if (put_line(fd, "wincmd t") == FAIL)
10179 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010180
Bram Moolenaar18144c82006-04-12 21:52:12 +000010181 /*
10182 * If more than one window, see if sizes can be restored.
10183 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10184 * resized when moving between windows.
10185 * Do this before restoring the view, so that the topline and the
10186 * cursor can be set. This is done again below.
10187 */
10188 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10189 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010190 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaar18144c82006-04-12 21:52:12 +000010193 /*
10194 * Restore the view of the window (options, file, cursor, etc.).
10195 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010196 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010197 {
10198 if (!ses_do_win(wp))
10199 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010200 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10201 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010202 return FAIL;
10203 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10204 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010205 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010206 }
10207
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010208 /* The argument index in the first tab page is zero, need to set it in
10209 * each window. For further tab pages it's the window where we do
10210 * "tabedit". */
10211 cur_arg_idx = next_arg_idx;
10212
Bram Moolenaar18144c82006-04-12 21:52:12 +000010213 /*
10214 * Restore cursor to the current window if it's not the first one.
10215 */
10216 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10217 || put_eol(fd) == FAIL))
10218 return FAIL;
10219
10220 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010221 * Restore window sizes again after jumping around in windows, because
10222 * the current window has a minimum size while others may not.
10223 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010224 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010225 return FAIL;
10226
Bram Moolenaar18144c82006-04-12 21:52:12 +000010227 /* Don't continue in another tab page when doing only the current one
10228 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010229 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010230 break;
10231 }
10232
10233 if (ssop_flags & SSOP_TABPAGES)
10234 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010235 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10236 || put_eol(fd) == FAIL)
10237 return FAIL;
10238 }
10239
Bram Moolenaar9c102382006-05-03 21:26:49 +000010240 /*
10241 * Wipe out an empty unnamed buffer we started in.
10242 */
10243 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10244 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010245 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010246 return FAIL;
10247 if (put_line(fd, "endif") == FAIL)
10248 return FAIL;
10249 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10250 return FAIL;
10251
10252 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10253 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10254 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256
10257 /*
10258 * Lastly, execute the x.vim file if it exists.
10259 */
10260 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10261 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010262 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 || put_line(fd, "endif") == FAIL)
10264 return FAIL;
10265
10266 return OK;
10267}
10268
10269 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010270ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 FILE *fd;
10272 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010273 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274{
10275 int n = 0;
10276 win_T *wp;
10277
10278 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10279 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010280 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 {
10282 if (!ses_do_win(wp))
10283 continue;
10284 ++n;
10285
10286 /* restore height when not full height */
10287 if (wp->w_height + wp->w_status_height < topframe->fr_height
10288 && (fprintf(fd,
10289 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10290 n, (long)wp->w_height, Rows / 2, Rows) < 0
10291 || put_eol(fd) == FAIL))
10292 return FAIL;
10293
10294 /* restore width when not full width */
10295 if (wp->w_width < Columns && (fprintf(fd,
10296 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10297 n, (long)wp->w_width, Columns / 2, Columns) < 0
10298 || put_eol(fd) == FAIL))
10299 return FAIL;
10300 }
10301 }
10302 else
10303 {
10304 /* Just equalise window sizes */
10305 if (put_line(fd, "wincmd =") == FAIL)
10306 return FAIL;
10307 }
10308 return OK;
10309}
10310
10311/*
10312 * Write commands to "fd" to recursively create windows for frame "fr",
10313 * horizontally and vertically split.
10314 * After the commands the last window in the frame is the current window.
10315 * Returns FAIL when writing the commands to "fd" fails.
10316 */
10317 static int
10318ses_win_rec(fd, fr)
10319 FILE *fd;
10320 frame_T *fr;
10321{
10322 frame_T *frc;
10323 int count = 0;
10324
10325 if (fr->fr_layout != FR_LEAF)
10326 {
10327 /* Find first frame that's not skipped and then create a window for
10328 * each following one (first frame is already there). */
10329 frc = ses_skipframe(fr->fr_child);
10330 if (frc != NULL)
10331 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10332 {
10333 /* Make window as big as possible so that we have lots of room
10334 * to split. */
10335 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10336 || put_line(fd, fr->fr_layout == FR_COL
10337 ? "split" : "vsplit") == FAIL)
10338 return FAIL;
10339 ++count;
10340 }
10341
10342 /* Go back to the first window. */
10343 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10344 ? "%dwincmd k" : "%dwincmd h", count) < 0
10345 || put_eol(fd) == FAIL))
10346 return FAIL;
10347
10348 /* Recursively create frames/windows in each window of this column or
10349 * row. */
10350 frc = ses_skipframe(fr->fr_child);
10351 while (frc != NULL)
10352 {
10353 ses_win_rec(fd, frc);
10354 frc = ses_skipframe(frc->fr_next);
10355 /* Go to next window. */
10356 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10357 return FAIL;
10358 }
10359 }
10360 return OK;
10361}
10362
10363/*
10364 * Skip frames that don't contain windows we want to save in the Session.
10365 * Returns NULL when there none.
10366 */
10367 static frame_T *
10368ses_skipframe(fr)
10369 frame_T *fr;
10370{
10371 frame_T *frc;
10372
10373 for (frc = fr; frc != NULL; frc = frc->fr_next)
10374 if (ses_do_frame(frc))
10375 break;
10376 return frc;
10377}
10378
10379/*
10380 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10381 * the Session.
10382 */
10383 static int
10384ses_do_frame(fr)
10385 frame_T *fr;
10386{
10387 frame_T *frc;
10388
10389 if (fr->fr_layout == FR_LEAF)
10390 return ses_do_win(fr->fr_win);
10391 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10392 if (ses_do_frame(frc))
10393 return TRUE;
10394 return FALSE;
10395}
10396
10397/*
10398 * Return non-zero if window "wp" is to be stored in the Session.
10399 */
10400 static int
10401ses_do_win(wp)
10402 win_T *wp;
10403{
10404 if (wp->w_buffer->b_fname == NULL
10405#ifdef FEAT_QUICKFIX
10406 /* When 'buftype' is "nofile" can't restore the window contents. */
10407 || bt_nofile(wp->w_buffer)
10408#endif
10409 )
10410 return (ssop_flags & SSOP_BLANK);
10411 if (wp->w_buffer->b_help)
10412 return (ssop_flags & SSOP_HELP);
10413 return TRUE;
10414}
10415
10416/*
10417 * Write commands to "fd" to restore the view of a window.
10418 * Caller must make sure 'scrolloff' is zero.
10419 */
10420 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010421put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 FILE *fd;
10423 win_T *wp;
10424 int add_edit; /* add ":edit" command to view */
10425 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010426 int current_arg_idx; /* current argument index of the window, use
10427 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
10429 win_T *save_curwin;
10430 int f;
10431 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010432 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
10434 /* Always restore cursor position for ":mksession". For ":mkview" only
10435 * when 'viewoptions' contains "cursor". */
10436 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10437
10438 /*
10439 * Local argument list.
10440 */
10441 if (wp->w_alist == &global_alist)
10442 {
10443 if (put_line(fd, "argglobal") == FAIL)
10444 return FAIL;
10445 }
10446 else
10447 {
10448 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10449 flagp == &vop_flags
10450 || !(*flagp & SSOP_CURDIR)
10451 || wp->w_localdir != NULL, flagp) == FAIL)
10452 return FAIL;
10453 }
10454
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010455 /* Only when part of a session: restore the argument index. Some
10456 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010457 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010458 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010460 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 || put_eol(fd) == FAIL)
10462 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010463 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 }
10465
10466 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010467 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 {
10469 /*
10470 * Load the file.
10471 */
10472 if (wp->w_buffer->b_ffname != NULL
10473#ifdef FEAT_QUICKFIX
10474 && !bt_nofile(wp->w_buffer)
10475#endif
10476 )
10477 {
10478 /*
10479 * Editing a file in this buffer: use ":edit file".
10480 * This may have side effects! (e.g., compressed or network file).
10481 */
10482 if (fputs("edit ", fd) < 0
10483 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10484 return FAIL;
10485 }
10486 else
10487 {
10488 /* No file in this buffer, just make it empty. */
10489 if (put_line(fd, "enew") == FAIL)
10490 return FAIL;
10491#ifdef FEAT_QUICKFIX
10492 if (wp->w_buffer->b_ffname != NULL)
10493 {
10494 /* The buffer does have a name, but it's not a file name. */
10495 if (fputs("file ", fd) < 0
10496 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10497 return FAIL;
10498 }
10499#endif
10500 do_cursor = FALSE;
10501 }
10502 }
10503
10504 /*
10505 * Local mappings and abbreviations.
10506 */
10507 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10508 && makemap(fd, wp->w_buffer) == FAIL)
10509 return FAIL;
10510
10511 /*
10512 * Local options. Need to go to the window temporarily.
10513 * Store only local values when using ":mkview" and when ":mksession" is
10514 * used and 'sessionoptions' doesn't include "options".
10515 * Some folding options are always stored when "folds" is included,
10516 * otherwise the folds would not be restored correctly.
10517 */
10518 save_curwin = curwin;
10519 curwin = wp;
10520 curbuf = curwin->w_buffer;
10521 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10522 f = makeset(fd, OPT_LOCAL,
10523 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10524#ifdef FEAT_FOLDING
10525 else if (*flagp & SSOP_FOLDS)
10526 f = makefoldset(fd);
10527#endif
10528 else
10529 f = OK;
10530 curwin = save_curwin;
10531 curbuf = curwin->w_buffer;
10532 if (f == FAIL)
10533 return FAIL;
10534
10535#ifdef FEAT_FOLDING
10536 /*
10537 * Save Folds when 'buftype' is empty and for help files.
10538 */
10539 if ((*flagp & SSOP_FOLDS)
10540 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010541# ifdef FEAT_QUICKFIX
10542 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10543# endif
10544 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 {
10546 if (put_folds(fd, wp) == FAIL)
10547 return FAIL;
10548 }
10549#endif
10550
10551 /*
10552 * Set the cursor after creating folds, since that moves the cursor.
10553 */
10554 if (do_cursor)
10555 {
10556
10557 /* Restore the cursor line in the file and relatively in the
10558 * window. Don't use "G", it changes the jumplist. */
10559 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10560 (long)wp->w_cursor.lnum,
10561 (long)(wp->w_cursor.lnum - wp->w_topline),
10562 (long)wp->w_height / 2, (long)wp->w_height) < 0
10563 || put_eol(fd) == FAIL
10564 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10565 || put_line(fd, "exe s:l") == FAIL
10566 || put_line(fd, "normal! zt") == FAIL
10567 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10568 || put_eol(fd) == FAIL)
10569 return FAIL;
10570 /* Restore the cursor column and left offset when not wrapping. */
10571 if (wp->w_cursor.col == 0)
10572 {
10573 if (put_line(fd, "normal! 0") == FAIL)
10574 return FAIL;
10575 }
10576 else
10577 {
10578 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10579 {
10580 if (fprintf(fd,
10581 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10582 (long)wp->w_cursor.col,
10583 (long)(wp->w_cursor.col - wp->w_leftcol),
10584 (long)wp->w_width / 2, (long)wp->w_width) < 0
10585 || put_eol(fd) == FAIL
10586 || put_line(fd, "if s:c > 0") == FAIL
10587 || fprintf(fd,
10588 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10589 (long)wp->w_cursor.col) < 0
10590 || put_eol(fd) == FAIL
10591 || put_line(fd, "else") == FAIL
10592 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10593 || put_eol(fd) == FAIL
10594 || put_line(fd, "endif") == FAIL)
10595 return FAIL;
10596 }
10597 else
10598 {
10599 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10600 || put_eol(fd) == FAIL)
10601 return FAIL;
10602 }
10603 }
10604 }
10605
10606 /*
10607 * Local directory.
10608 */
10609 if (wp->w_localdir != NULL)
10610 {
10611 if (fputs("lcd ", fd) < 0
10612 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10613 || put_eol(fd) == FAIL)
10614 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010615 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 }
10617
10618 return OK;
10619}
10620
10621/*
10622 * Write an argument list to the session file.
10623 * Returns FAIL if writing fails.
10624 */
10625 static int
10626ses_arglist(fd, cmd, gap, fullname, flagp)
10627 FILE *fd;
10628 char *cmd;
10629 garray_T *gap;
10630 int fullname; /* TRUE: use full path name */
10631 unsigned *flagp;
10632{
10633 int i;
10634 char_u buf[MAXPATHL];
10635 char_u *s;
10636
10637 if (gap->ga_len == 0)
10638 return put_line(fd, "silent! argdel *");
10639 if (fputs(cmd, fd) < 0)
10640 return FAIL;
10641 for (i = 0; i < gap->ga_len; ++i)
10642 {
10643 /* NULL file names are skipped (only happens when out of memory). */
10644 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10645 if (s != NULL)
10646 {
10647 if (fullname)
10648 {
10649 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10650 s = buf;
10651 }
10652 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10653 return FAIL;
10654 }
10655 }
10656 return put_eol(fd);
10657}
10658
10659/*
10660 * Write a buffer name to the session file.
10661 * Also ends the line.
10662 * Returns FAIL if writing fails.
10663 */
10664 static int
10665ses_fname(fd, buf, flagp)
10666 FILE *fd;
10667 buf_T *buf;
10668 unsigned *flagp;
10669{
10670 char_u *name;
10671
10672 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010673 * the session file will be sourced.
10674 * Don't do this for ":mkview", we don't know the current directory.
10675 * Don't do this after ":lcd", we don't keep track of what the current
10676 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 if (buf->b_sfname != NULL
10678 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010679 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010680#ifdef FEAT_AUTOCHDIR
10681 && !p_acd
10682#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010683 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 name = buf->b_sfname;
10685 else
10686 name = buf->b_ffname;
10687 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10688 return FAIL;
10689 return OK;
10690}
10691
10692/*
10693 * Write a file name to the session file.
10694 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10695 * characters.
10696 * Returns FAIL if writing fails.
10697 */
10698 static int
10699ses_put_fname(fd, name, flagp)
10700 FILE *fd;
10701 char_u *name;
10702 unsigned *flagp;
10703{
10704 char_u *sname;
10705 int retval = OK;
10706 int c;
10707
10708 sname = home_replace_save(NULL, name);
10709 if (sname != NULL)
10710 name = sname;
10711 while (*name != NUL)
10712 {
10713#ifdef FEAT_MBYTE
10714 {
10715 int l;
10716
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010717 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 {
10719 /* copy a multibyte char */
10720 while (--l >= 0)
10721 {
10722 if (putc(*name, fd) != *name)
10723 retval = FAIL;
10724 ++name;
10725 }
10726 continue;
10727 }
10728 }
10729#endif
10730 c = *name++;
10731 if (c == '\\' && (*flagp & SSOP_SLASH))
10732 /* change a backslash to a forward slash */
10733 c = '/';
10734 else if ((vim_strchr(escape_chars, c) != NULL
10735#ifdef BACKSLASH_IN_FILENAME
10736 && c != '\\'
10737#endif
10738 ) || c == '#' || c == '%')
10739 {
10740 /* escape a special character with a backslash */
10741 if (putc('\\', fd) != '\\')
10742 retval = FAIL;
10743 }
10744 if (putc(c, fd) != c)
10745 retval = FAIL;
10746 }
10747 vim_free(sname);
10748 return retval;
10749}
10750
10751/*
10752 * ":loadview [nr]"
10753 */
10754 static void
10755ex_loadview(eap)
10756 exarg_T *eap;
10757{
10758 char_u *fname;
10759
10760 fname = get_view_file(*eap->arg);
10761 if (fname != NULL)
10762 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010763 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 vim_free(fname);
10765 }
10766}
10767
10768/*
10769 * Get the name of the view file for the current buffer.
10770 */
10771 static char_u *
10772get_view_file(c)
10773 int c;
10774{
10775 int len = 0;
10776 char_u *p, *s;
10777 char_u *retval;
10778 char_u *sname;
10779
10780 if (curbuf->b_ffname == NULL)
10781 {
10782 EMSG(_(e_noname));
10783 return NULL;
10784 }
10785 sname = home_replace_save(NULL, curbuf->b_ffname);
10786 if (sname == NULL)
10787 return NULL;
10788
10789 /*
10790 * We want a file name without separators, because we're not going to make
10791 * a directory.
10792 * "normal" path separator -> "=+"
10793 * "=" -> "=="
10794 * ":" path separator -> "=-"
10795 */
10796 for (p = sname; *p; ++p)
10797 if (*p == '=' || vim_ispathsep(*p))
10798 ++len;
10799 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10800 if (retval != NULL)
10801 {
10802 STRCPY(retval, p_vdir);
10803 add_pathsep(retval);
10804 s = retval + STRLEN(retval);
10805 for (p = sname; *p; ++p)
10806 {
10807 if (*p == '=')
10808 {
10809 *s++ = '=';
10810 *s++ = '=';
10811 }
10812 else if (vim_ispathsep(*p))
10813 {
10814 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010815#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 || defined(VMS)
10817 if (*p == ':')
10818 *s++ = '-';
10819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010821 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 }
10823 else
10824 *s++ = *p;
10825 }
10826 *s++ = '=';
10827 *s++ = c;
10828 STRCPY(s, ".vim");
10829 }
10830
10831 vim_free(sname);
10832 return retval;
10833}
10834
10835#endif /* FEAT_SESSION */
10836
10837/*
10838 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10839 * Return FAIL for a write error.
10840 */
10841 int
10842put_eol(fd)
10843 FILE *fd;
10844{
10845 if (
10846#ifdef USE_CRNL
10847 (
10848# ifdef MKSESSION_NL
10849 !mksession_nl &&
10850# endif
10851 (putc('\r', fd) < 0)) ||
10852#endif
10853 (putc('\n', fd) < 0))
10854 return FAIL;
10855 return OK;
10856}
10857
10858/*
10859 * Write a line to "fd".
10860 * Return FAIL for a write error.
10861 */
10862 int
10863put_line(fd, s)
10864 FILE *fd;
10865 char *s;
10866{
10867 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10868 return FAIL;
10869 return OK;
10870}
10871
10872#ifdef FEAT_VIMINFO
10873/*
10874 * ":rviminfo" and ":wviminfo".
10875 */
10876 static void
10877ex_viminfo(eap)
10878 exarg_T *eap;
10879{
10880 char_u *save_viminfo;
10881
10882 save_viminfo = p_viminfo;
10883 if (*p_viminfo == NUL)
10884 p_viminfo = (char_u *)"'100";
10885 if (eap->cmdidx == CMD_rviminfo)
10886 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010887 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10888 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 EMSG(_("E195: Cannot open viminfo file for reading"));
10890 }
10891 else
10892 write_viminfo(eap->arg, eap->forceit);
10893 p_viminfo = save_viminfo;
10894}
10895#endif
10896
10897#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010898/*
10899 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010900 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 void
10903dialog_msg(buff, format, fname)
10904 char_u *buff;
10905 char *format;
10906 char_u *fname;
10907{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 if (fname == NULL)
10909 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010910 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
10912#endif
10913
10914/*
10915 * ":behave {mswin,xterm}"
10916 */
10917 static void
10918ex_behave(eap)
10919 exarg_T *eap;
10920{
10921 if (STRCMP(eap->arg, "mswin") == 0)
10922 {
10923 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10924 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10925 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10926 set_option_value((char_u *)"keymodel", 0L,
10927 (char_u *)"startsel,stopsel", 0);
10928 }
10929 else if (STRCMP(eap->arg, "xterm") == 0)
10930 {
10931 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10932 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10933 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10934 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10935 }
10936 else
10937 EMSG2(_(e_invarg2), eap->arg);
10938}
10939
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10941/*
10942 * Function given to ExpandGeneric() to obtain the possible arguments of the
10943 * ":behave {mswin,xterm}" command.
10944 */
10945 char_u *
10946get_behave_arg(xp, idx)
10947 expand_T *xp UNUSED;
10948 int idx;
10949{
10950 if (idx == 0)
10951 return (char_u *)"mswin";
10952 if (idx == 1)
10953 return (char_u *)"xterm";
10954 return NULL;
10955}
10956#endif
10957
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958#ifdef FEAT_AUTOCMD
10959static int filetype_detect = FALSE;
10960static int filetype_plugin = FALSE;
10961static int filetype_indent = FALSE;
10962
10963/*
10964 * ":filetype [plugin] [indent] {on,off,detect}"
10965 * on: Load the filetype.vim file to install autocommands for file types.
10966 * off: Load the ftoff.vim file to remove all autocommands for file types.
10967 * plugin on: load filetype.vim and ftplugin.vim
10968 * plugin off: load ftplugof.vim
10969 * indent on: load filetype.vim and indent.vim
10970 * indent off: load indoff.vim
10971 */
10972 static void
10973ex_filetype(eap)
10974 exarg_T *eap;
10975{
10976 char_u *arg = eap->arg;
10977 int plugin = FALSE;
10978 int indent = FALSE;
10979
10980 if (*eap->arg == NUL)
10981 {
10982 /* Print current status. */
10983 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10984 filetype_detect ? "ON" : "OFF",
10985 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10986 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10987 return;
10988 }
10989
10990 /* Accept "plugin" and "indent" in any order. */
10991 for (;;)
10992 {
10993 if (STRNCMP(arg, "plugin", 6) == 0)
10994 {
10995 plugin = TRUE;
10996 arg = skipwhite(arg + 6);
10997 continue;
10998 }
10999 if (STRNCMP(arg, "indent", 6) == 0)
11000 {
11001 indent = TRUE;
11002 arg = skipwhite(arg + 6);
11003 continue;
11004 }
11005 break;
11006 }
11007 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11008 {
11009 if (*arg == 'o' || !filetype_detect)
11010 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011011 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 filetype_detect = TRUE;
11013 if (plugin)
11014 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011015 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 filetype_plugin = TRUE;
11017 }
11018 if (indent)
11019 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011020 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 filetype_indent = TRUE;
11022 }
11023 }
11024 if (*arg == 'd')
11025 {
11026 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011027 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 }
11029 }
11030 else if (STRCMP(arg, "off") == 0)
11031 {
11032 if (plugin || indent)
11033 {
11034 if (plugin)
11035 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011036 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 filetype_plugin = FALSE;
11038 }
11039 if (indent)
11040 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011041 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 filetype_indent = FALSE;
11043 }
11044 }
11045 else
11046 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011047 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 filetype_detect = FALSE;
11049 }
11050 }
11051 else
11052 EMSG2(_(e_invarg2), arg);
11053}
11054
11055/*
11056 * ":setfiletype {name}"
11057 */
11058 static void
11059ex_setfiletype(eap)
11060 exarg_T *eap;
11061{
11062 if (!did_filetype)
11063 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11064}
11065#endif
11066
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 static void
11068ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011069 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
11071#ifdef FEAT_DIGRAPHS
11072 if (*eap->arg != NUL)
11073 putdigraph(eap->arg);
11074 else
11075 listdigraphs();
11076#else
11077 EMSG(_("E196: No digraphs in this version"));
11078#endif
11079}
11080
11081 static void
11082ex_set(eap)
11083 exarg_T *eap;
11084{
11085 int flags = 0;
11086
11087 if (eap->cmdidx == CMD_setlocal)
11088 flags = OPT_LOCAL;
11089 else if (eap->cmdidx == CMD_setglobal)
11090 flags = OPT_GLOBAL;
11091#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11092 if (cmdmod.browse && flags == 0)
11093 ex_options(eap);
11094 else
11095#endif
11096 (void)do_set(eap->arg, flags);
11097}
11098
11099#ifdef FEAT_SEARCH_EXTRA
11100/*
11101 * ":nohlsearch"
11102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 static void
11104ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011105 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106{
11107 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011108 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109}
11110
11111/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011112 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 * Sets nextcmd to the start of the next command, if any. Also called when
11114 * skipping commands to find the next command.
11115 */
11116 static void
11117ex_match(eap)
11118 exarg_T *eap;
11119{
11120 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011121 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 char_u *end;
11123 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011124 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011125
11126 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011127 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011128 else
11129 {
11130 EMSG(e_invcmd);
11131 return;
11132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
11134 /* First clear any old pattern. */
11135 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011136 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137
11138 if (ends_excmd(*eap->arg))
11139 end = eap->arg;
11140 else if ((STRNICMP(eap->arg, "none", 4) == 0
11141 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11142 end = eap->arg + 4;
11143 else
11144 {
11145 p = skiptowhite(eap->arg);
11146 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011147 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 p = skipwhite(p);
11149 if (*p == NUL)
11150 {
11151 /* There must be two arguments. */
11152 EMSG2(_(e_invarg2), eap->arg);
11153 return;
11154 }
11155 end = skip_regexp(p + 1, *p, TRUE, NULL);
11156 if (!eap->skip)
11157 {
11158 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11159 {
11160 eap->errmsg = e_trailing;
11161 return;
11162 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011163 if (*end != *p)
11164 {
11165 EMSG2(_(e_invarg2), p);
11166 return;
11167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168
11169 c = *end;
11170 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011171 match_add(curwin, g, p + 1, 10, id);
11172 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011173 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 }
11175 }
11176 eap->nextcmd = find_nextcmd(end);
11177}
11178#endif
11179
11180#ifdef FEAT_CRYPT
11181/*
11182 * ":X": Get crypt key
11183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 static void
11185ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011186 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011188 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11189 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190}
11191#endif
11192
11193#ifdef FEAT_FOLDING
11194 static void
11195ex_fold(eap)
11196 exarg_T *eap;
11197{
11198 if (foldManualAllowed(TRUE))
11199 foldCreate(eap->line1, eap->line2);
11200}
11201
11202 static void
11203ex_foldopen(eap)
11204 exarg_T *eap;
11205{
11206 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11207 eap->forceit, FALSE);
11208}
11209
11210 static void
11211ex_folddo(eap)
11212 exarg_T *eap;
11213{
11214 linenr_T lnum;
11215
11216 /* First set the marks for all lines closed/open. */
11217 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11218 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11219 ml_setmarked(lnum);
11220
11221 /* Execute the command on the marked lines. */
11222 global_exe(eap->arg);
11223 ml_clearmarked(); /* clear rest of the marks */
11224}
11225#endif