blob: 499ff1626f02096780673b617e4286cf41e70981 [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 Moolenaar071d4272004-06-13 20:20:40 +0000132#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000133 || !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
268#ifndef FEAT_TCL
269# define ex_tcl ex_script_ni
270# define ex_tcldo ex_ni
271# define ex_tclfile ex_ni
272#endif
273#ifndef FEAT_RUBY
274# define ex_ruby ex_script_ni
275# define ex_rubydo ex_ni
276# define ex_rubyfile ex_ni
277#endif
278#ifndef FEAT_SNIFF
279# define ex_sniff ex_ni
280#endif
281#ifndef FEAT_KEYMAP
282# define ex_loadkeymap ex_ni
283#endif
284static void ex_swapname __ARGS((exarg_T *eap));
285static void ex_syncbind __ARGS((exarg_T *eap));
286static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287static void ex_pwd __ARGS((exarg_T *eap));
288static void ex_equal __ARGS((exarg_T *eap));
289static void ex_sleep __ARGS((exarg_T *eap));
290static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
291static void ex_winsize __ARGS((exarg_T *eap));
292#ifdef FEAT_WINDOWS
293static void ex_wincmd __ARGS((exarg_T *eap));
294#else
295# define ex_wincmd ex_ni
296#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000297#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static void ex_winpos __ARGS((exarg_T *eap));
299#else
300# define ex_winpos ex_ni
301#endif
302static void ex_operators __ARGS((exarg_T *eap));
303static void ex_put __ARGS((exarg_T *eap));
304static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000305static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_submagic __ARGS((exarg_T *eap));
307static void ex_join __ARGS((exarg_T *eap));
308static void ex_at __ARGS((exarg_T *eap));
309static void ex_bang __ARGS((exarg_T *eap));
310static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200311#ifdef FEAT_PERSISTENT_UNDO
312static void ex_wundo __ARGS((exarg_T *eap));
313static void ex_rundo __ARGS((exarg_T *eap));
314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000316static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static void ex_redir __ARGS((exarg_T *eap));
318static void ex_redraw __ARGS((exarg_T *eap));
319static void ex_redrawstatus __ARGS((exarg_T *eap));
320static void close_redir __ARGS((void));
321static void ex_mkrc __ARGS((exarg_T *eap));
322static void ex_mark __ARGS((exarg_T *eap));
323#ifdef FEAT_USR_CMDS
324static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000325static 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 +0000326#endif
327#ifdef FEAT_EX_EXTRA
328static void ex_normal __ARGS((exarg_T *eap));
329static void ex_startinsert __ARGS((exarg_T *eap));
330static void ex_stopinsert __ARGS((exarg_T *eap));
331#else
332# define ex_normal ex_ni
333# define ex_align ex_ni
334# define ex_retab ex_ni
335# define ex_startinsert ex_ni
336# define ex_stopinsert ex_ni
337# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000338# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#endif
340#ifdef FEAT_FIND_ID
341static void ex_checkpath __ARGS((exarg_T *eap));
342static void ex_findpat __ARGS((exarg_T *eap));
343#else
344# define ex_findpat ex_ni
345# define ex_checkpath ex_ni
346#endif
347#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
348static void ex_psearch __ARGS((exarg_T *eap));
349#else
350# define ex_psearch ex_ni
351#endif
352static void ex_tag __ARGS((exarg_T *eap));
353static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
354#ifndef FEAT_EVAL
355# define ex_scriptnames ex_ni
356# define ex_finish ex_ni
357# define ex_echo ex_ni
358# define ex_echohl ex_ni
359# define ex_execute ex_ni
360# define ex_call ex_ni
361# define ex_if ex_ni
362# define ex_endif ex_ni
363# define ex_else ex_ni
364# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000365# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define ex_continue ex_ni
367# define ex_break ex_ni
368# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000369# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370# define ex_throw ex_ni
371# define ex_try ex_ni
372# define ex_catch ex_ni
373# define ex_finally ex_ni
374# define ex_endtry ex_ni
375# define ex_endfunction ex_ni
376# define ex_let ex_ni
377# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000378# define ex_lockvar ex_ni
379# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380# define ex_function ex_ni
381# define ex_delfunction ex_ni
382# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000383# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#endif
385static char_u *arg_all __ARGS((void));
386#ifdef FEAT_SESSION
387static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000388static 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 +0000389static void ex_loadview __ARGS((exarg_T *eap));
390static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000391static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#else
393# define ex_loadview ex_ni
394#endif
395#ifndef FEAT_EVAL
396# define ex_compiler ex_ni
397#endif
398#ifdef FEAT_VIMINFO
399static void ex_viminfo __ARGS((exarg_T *eap));
400#else
401# define ex_viminfo ex_ni
402#endif
403static void ex_behave __ARGS((exarg_T *eap));
404#ifdef FEAT_AUTOCMD
405static void ex_filetype __ARGS((exarg_T *eap));
406static void ex_setfiletype __ARGS((exarg_T *eap));
407#else
408# define ex_filetype ex_ni
409# define ex_setfiletype ex_ni
410#endif
411#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000412# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413# define ex_diffpatch ex_ni
414# define ex_diffgetput ex_ni
415# define ex_diffsplit ex_ni
416# define ex_diffthis ex_ni
417# define ex_diffupdate ex_ni
418#endif
419static void ex_digraphs __ARGS((exarg_T *eap));
420static void ex_set __ARGS((exarg_T *eap));
421#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
422# define ex_options ex_ni
423#endif
424#ifdef FEAT_SEARCH_EXTRA
425static void ex_nohlsearch __ARGS((exarg_T *eap));
426static void ex_match __ARGS((exarg_T *eap));
427#else
428# define ex_nohlsearch ex_ni
429# define ex_match ex_ni
430#endif
431#ifdef FEAT_CRYPT
432static void ex_X __ARGS((exarg_T *eap));
433#else
434# define ex_X ex_ni
435#endif
436#ifdef FEAT_FOLDING
437static void ex_fold __ARGS((exarg_T *eap));
438static void ex_foldopen __ARGS((exarg_T *eap));
439static void ex_folddo __ARGS((exarg_T *eap));
440#else
441# define ex_fold ex_ni
442# define ex_foldopen ex_ni
443# define ex_folddo ex_ni
444#endif
445#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
446 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
447# define ex_language ex_ni
448#endif
449#ifndef FEAT_SIGNS
450# define ex_sign ex_ni
451#endif
452#ifndef FEAT_SUN_WORKSHOP
453# define ex_wsverb ex_ni
454#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000455#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200456# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000457# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200458# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461#ifndef FEAT_EVAL
462# define ex_debug ex_ni
463# define ex_breakadd ex_ni
464# define ex_debuggreedy ex_ni
465# define ex_breakdel ex_ni
466# define ex_breaklist ex_ni
467#endif
468
469#ifndef FEAT_CMDHIST
470# define ex_history ex_ni
471#endif
472#ifndef FEAT_JUMPLIST
473# define ex_jumps ex_ni
474# define ex_changes ex_ni
475#endif
476
Bram Moolenaar05159a02005-02-26 23:04:13 +0000477#ifndef FEAT_PROFILE
478# define ex_profile ex_ni
479#endif
480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481/*
482 * Declare cmdnames[].
483 */
484#define DO_DECLARE_EXCMD
485#include "ex_cmds.h"
486
487/*
488 * Table used to quickly search for a command, based on its first character.
489 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000490static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 CMD_append,
493 CMD_buffer,
494 CMD_change,
495 CMD_delete,
496 CMD_edit,
497 CMD_file,
498 CMD_global,
499 CMD_help,
500 CMD_insert,
501 CMD_join,
502 CMD_k,
503 CMD_list,
504 CMD_move,
505 CMD_next,
506 CMD_open,
507 CMD_print,
508 CMD_quit,
509 CMD_read,
510 CMD_substitute,
511 CMD_t,
512 CMD_undo,
513 CMD_vglobal,
514 CMD_write,
515 CMD_xit,
516 CMD_yank,
517 CMD_z,
518 CMD_bang
519};
520
521static char_u dollar_command[2] = {'$', 0};
522
523
524#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000525/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526typedef struct
527{
528 char_u *line; /* command line */
529 linenr_T lnum; /* sourcing_lnum of the line */
530} wcmd_T;
531
532/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000535 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000537struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 garray_T *lines_gap; /* growarray with line info */
540 int current_line; /* last read line from growarray */
541 int repeating; /* TRUE when looping a second time */
542 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
543 char_u *(*getline) __ARGS((int, void *, int));
544 void *cookie;
545};
546
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000547static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
548static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000550
551/* Struct to save a few things while debugging. Used in do_cmdline() only. */
552struct dbg_stuff
553{
554 int trylevel;
555 int force_abort;
556 except_T *caught_stack;
557 char_u *vv_exception;
558 char_u *vv_throwpoint;
559 int did_emsg;
560 int got_int;
561 int did_throw;
562 int need_rethrow;
563 int check_cstack;
564 except_T *current_exception;
565};
566
567static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
568static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
569
570 static void
571save_dbg_stuff(dsp)
572 struct dbg_stuff *dsp;
573{
574 dsp->trylevel = trylevel; trylevel = 0;
575 dsp->force_abort = force_abort; force_abort = FALSE;
576 dsp->caught_stack = caught_stack; caught_stack = NULL;
577 dsp->vv_exception = v_exception(NULL);
578 dsp->vv_throwpoint = v_throwpoint(NULL);
579
580 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
581 dsp->did_emsg = did_emsg; did_emsg = FALSE;
582 dsp->got_int = got_int; got_int = FALSE;
583 dsp->did_throw = did_throw; did_throw = FALSE;
584 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
585 dsp->check_cstack = check_cstack; check_cstack = FALSE;
586 dsp->current_exception = current_exception; current_exception = NULL;
587}
588
589 static void
590restore_dbg_stuff(dsp)
591 struct dbg_stuff *dsp;
592{
593 suppress_errthrow = FALSE;
594 trylevel = dsp->trylevel;
595 force_abort = dsp->force_abort;
596 caught_stack = dsp->caught_stack;
597 (void)v_exception(dsp->vv_exception);
598 (void)v_throwpoint(dsp->vv_throwpoint);
599 did_emsg = dsp->did_emsg;
600 got_int = dsp->got_int;
601 did_throw = dsp->did_throw;
602 need_rethrow = dsp->need_rethrow;
603 check_cstack = dsp->check_cstack;
604 current_exception = dsp->current_exception;
605}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#endif
607
608
609/*
610 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
611 * command is given.
612 */
613 void
614do_exmode(improved)
615 int improved; /* TRUE for "improved Ex" mode */
616{
617 int save_msg_scroll;
618 int prev_msg_row;
619 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000620 int changedtick;
621
622 if (improved)
623 exmode_active = EXMODE_VIM;
624 else
625 exmode_active = EXMODE_NORMAL;
626 State = NORMAL;
627
628 /* When using ":global /pat/ visual" and then "Q" we return to continue
629 * the :global command. */
630 if (global_busy)
631 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633 save_msg_scroll = msg_scroll;
634 ++RedrawingDisabled; /* don't redisplay the window */
635 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#ifdef FEAT_GUI
637 /* Ignore scrollbar and mouse events in Ex mode */
638 ++hold_gui_events;
639#endif
640#ifdef FEAT_SNIFF
641 want_sniff_request = 0; /* No K_SNIFF wanted */
642#endif
643
644 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
645 while (exmode_active)
646 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000647#ifdef FEAT_EX_EXTRA
648 /* Check for a ":normal" command and no more characters left. */
649 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
650 {
651 exmode_active = FALSE;
652 break;
653 }
654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 msg_scroll = TRUE;
656 need_wait_return = FALSE;
657 ex_pressedreturn = FALSE;
658 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000659 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 prev_msg_row = msg_row;
661 prev_line = curwin->w_cursor.lnum;
662#ifdef FEAT_SNIFF
663 ProcessSniffRequests();
664#endif
665 if (improved)
666 {
667 cmdline_row = msg_row;
668 do_cmdline(NULL, getexline, NULL, 0);
669 }
670 else
671 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
672 lines_left = Rows - 1;
673
Bram Moolenaardf177f62005-02-22 08:39:57 +0000674 if ((prev_line != curwin->w_cursor.lnum
675 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 if (curbuf->b_ml.ml_flags & ML_EMPTY)
678 EMSG(_(e_emptybuf));
679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if (ex_pressedreturn)
682 {
683 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000684 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 msg_row = prev_msg_row;
686 if (prev_msg_row == Rows - 1)
687 msg_row--;
688 }
689 msg_col = 0;
690 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
691 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000694 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
695 {
696 if (curbuf->b_ml.ml_flags & ML_EMPTY)
697 EMSG(_(e_emptybuf));
698 else
699 EMSG(_("E501: At end-of-file"));
700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
702
703#ifdef FEAT_GUI
704 --hold_gui_events;
705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 --RedrawingDisabled;
707 --no_wait_return;
708 update_screen(CLEAR);
709 need_wait_return = FALSE;
710 msg_scroll = save_msg_scroll;
711}
712
713/*
714 * Execute a simple command line. Used for translated commands like "*".
715 */
716 int
717do_cmdline_cmd(cmd)
718 char_u *cmd;
719{
720 return do_cmdline(cmd, NULL, NULL,
721 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
722}
723
724/*
725 * do_cmdline(): execute one Ex command line
726 *
727 * 1. Execute "cmdline" when it is not NULL.
728 * If "cmdline" is NULL, or more lines are needed, getline() is used.
729 * 2. Split up in parts separated with '|'.
730 *
731 * This function can be called recursively!
732 *
733 * flags:
734 * DOCMD_VERBOSE - The command will be included in the error message.
735 * DOCMD_NOWAIT - Don't call wait_return() and friends.
736 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
737 * DOCMD_KEYTYPED - Don't reset KeyTyped.
738 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
739 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
740 *
741 * return FAIL if cmdline could not be executed, OK otherwise
742 */
743 int
744do_cmdline(cmdline, getline, cookie, flags)
745 char_u *cmdline;
746 char_u *(*getline) __ARGS((int, void *, int));
747 void *cookie; /* argument for getline() */
748 int flags;
749{
750 char_u *next_cmdline; /* next cmd to execute */
751 char_u *cmdline_copy = NULL; /* copy of cmd line */
752 int used_getline = FALSE; /* used "getline" to obtain command */
753 static int recursive = 0; /* recursive depth */
754 int msg_didout_before_start = 0;
755 int count = 0; /* line number count */
756 int did_inc = FALSE; /* incremented RedrawingDisabled */
757 int retval = OK;
758#ifdef FEAT_EVAL
759 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int current_line = 0; /* active line in lines_ga */
762 char_u *fname = NULL; /* function or script name */
763 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
764 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000765 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 int initial_trylevel;
767 struct msglist **saved_msg_list = NULL;
768 struct msglist *private_msg_list;
769
770 /* "getline" and "cookie" passed to do_one_cmd() */
771 char_u *(*cmd_getline) __ARGS((int, void *, int));
772 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000773 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#else
777# define cmd_getline getline
778# define cmd_cookie cookie
779#endif
780 static int call_depth = 0; /* recursiveness */
781
782#ifdef FEAT_EVAL
783 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
784 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000785 * This ensures that the do_errthrow() call in do_one_cmd() does not
786 * combine the messages stored by an earlier invocation of do_one_cmd()
787 * with the command name of the later one. This would happen when
788 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 saved_msg_list = msg_list;
790 msg_list = &private_msg_list;
791 private_msg_list = NULL;
792#endif
793
794 /* It's possible to create an endless loop with ":execute", catch that
795 * here. The value of 200 allows nested function calls, ":source", etc. */
796 if (call_depth == 200)
797 {
798 EMSG(_("E169: Command too recursive"));
799#ifdef FEAT_EVAL
800 /* When converting to an exception, we do not include the command name
801 * since this is not an error of the specific command. */
802 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
803 msg_list = saved_msg_list;
804#endif
805 return FAIL;
806 }
807 ++call_depth;
808
809#ifdef FEAT_EVAL
810 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000811 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 cstack.cs_trylevel = 0;
813 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000814 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
816
817 real_cookie = getline_cookie(getline, cookie);
818
819 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 getline_is_func = getline_equal(getline, cookie, get_func_line);
821 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ++ex_nesting_level;
823
824 /* Get the function or script name and the address where the next breakpoint
825 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 fname = func_name(real_cookie);
829 breakpoint = func_breakpoint(real_cookie);
830 dbg_tick = func_dbg_tick(real_cookie);
831 }
832 else if (getline_equal(getline, cookie, getsourceline))
833 {
834 fname = sourcing_name;
835 breakpoint = source_breakpoint(real_cookie);
836 dbg_tick = source_dbg_tick(real_cookie);
837 }
838
839 /*
840 * Initialize "force_abort" and "suppress_errthrow" at the top level.
841 */
842 if (!recursive)
843 {
844 force_abort = FALSE;
845 suppress_errthrow = FALSE;
846 }
847
848 /*
849 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 * exception handling (used when debugging). Otherwise clear it to avoid
851 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000853 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000854 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000855 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200856 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858 initial_trylevel = trylevel;
859
860 /*
861 * "did_throw" will be set to TRUE when an exception is being thrown.
862 */
863 did_throw = FALSE;
864#endif
865 /*
866 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * If force_abort is set, we cancel everything.
869 */
870 did_emsg = FALSE;
871
872 /*
873 * KeyTyped is only set when calling vgetc(). Reset it here when not
874 * calling vgetc() (sourced command lines).
875 */
876 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
877 KeyTyped = FALSE;
878
879 /*
880 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000881 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 * - for multiple commands on one line, separated with '|'
883 * - when repeating until there are no more lines (for ":source")
884 */
885 next_cmdline = cmdline;
886 do
887 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#ifdef FEAT_EVAL
889 getline_is_func = getline_equal(getline, cookie, get_func_line);
890#endif
891
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000892 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (next_cmdline == NULL
894#ifdef FEAT_EVAL
895 && !force_abort
896 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#endif
899 )
900 did_emsg = FALSE;
901
902 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000903 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 * 2. If no line given: Get an allocated line with getline().
905 * 3. If a line is given: Make a copy, so we can mess with it.
906 */
907
908#ifdef FEAT_EVAL
909 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000910 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
912 /* Each '|' separated command is stored separately in lines_ga, to
913 * be able to jump to it. Don't use next_cmdline now. */
914 vim_free(cmdline_copy);
915 cmdline_copy = NULL;
916
917 /* Check if a function has returned or, unless it has an unclosed
918 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000921# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000922 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923 func_line_end(real_cookie);
924# endif
925 if (func_has_ended(real_cookie))
926 {
927 retval = FAIL;
928 break;
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000932 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933 && getline_equal(getline, cookie, getsourceline))
934 script_line_end();
935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937 /* Check if a sourced file hit a ":finish" command. */
938 if (source_finished(getline, cookie))
939 {
940 retval = FAIL;
941 break;
942 }
943
944 /* If breakpoints have been added/deleted need to check for it. */
945 if (breakpoint != NULL && dbg_tick != NULL
946 && *dbg_tick != debug_tick)
947 {
948 *breakpoint = dbg_find_breakpoint(
949 getline_equal(getline, cookie, getsourceline),
950 fname, sourcing_lnum);
951 *dbg_tick = debug_tick;
952 }
953
954 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
955 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
956
957 /* Did we encounter a breakpoint? */
958 if (breakpoint != NULL && *breakpoint != 0
959 && *breakpoint <= sourcing_lnum)
960 {
961 dbg_breakpoint(fname, sourcing_lnum);
962 /* Find next breakpoint. */
963 *breakpoint = dbg_find_breakpoint(
964 getline_equal(getline, cookie, getsourceline),
965 fname, sourcing_lnum);
966 *dbg_tick = debug_tick;
967 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000968# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000969 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970 {
971 if (getline_is_func)
972 func_line_start(real_cookie);
973 else if (getline_equal(getline, cookie, getsourceline))
974 script_line_start();
975 }
976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
978
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000979 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 * again. Pass a different "getline" function to do_one_cmd()
983 * below, so that it stores lines in or reads them from
984 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 * while/for loop. */
986 cmd_getline = get_loop_line;
987 cmd_cookie = (void *)&cmd_loop_cookie;
988 cmd_loop_cookie.lines_gap = &lines_ga;
989 cmd_loop_cookie.current_line = current_line;
990 cmd_loop_cookie.getline = getline;
991 cmd_loop_cookie.cookie = cookie;
992 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994 else
995 {
996 cmd_getline = getline;
997 cmd_cookie = cookie;
998 }
999#endif
1000
1001 /* 2. If no line given, get an allocated line with getline(). */
1002 if (next_cmdline == NULL)
1003 {
1004 /*
1005 * Need to set msg_didout for the first line after an ":if",
1006 * otherwise the ":if" will be overwritten.
1007 */
1008 if (count == 1 && getline_equal(getline, cookie, getexline))
1009 msg_didout = TRUE;
1010 if (getline == NULL || (next_cmdline = getline(':', cookie,
1011#ifdef FEAT_EVAL
1012 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1013#else
1014 0
1015#endif
1016 )) == NULL)
1017 {
1018 /* Don't call wait_return for aborted command line. The NULL
1019 * returned for the end of a sourced file or executed function
1020 * doesn't do this. */
1021 if (KeyTyped && !(flags & DOCMD_REPEAT))
1022 need_wait_return = FALSE;
1023 retval = FAIL;
1024 break;
1025 }
1026 used_getline = TRUE;
1027
1028 /*
1029 * Keep the first typed line. Clear it when more lines are typed.
1030 */
1031 if (flags & DOCMD_KEEPLINE)
1032 {
1033 vim_free(repeat_cmdline);
1034 if (count == 0)
1035 repeat_cmdline = vim_strsave(next_cmdline);
1036 else
1037 repeat_cmdline = NULL;
1038 }
1039 }
1040
1041 /* 3. Make a copy of the command so we can mess with it. */
1042 else if (cmdline_copy == NULL)
1043 {
1044 next_cmdline = vim_strsave(next_cmdline);
1045 if (next_cmdline == NULL)
1046 {
1047 EMSG(_(e_outofmem));
1048 retval = FAIL;
1049 break;
1050 }
1051 }
1052 cmdline_copy = next_cmdline;
1053
1054#ifdef FEAT_EVAL
1055 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 * Save the current line when inside a ":while" or ":for", and when
1057 * the command looks like a ":while" or ":for", because we may need it
1058 * later. When there is a '|' and another command, it is stored
1059 * separately, because we need to be able to jump back to it from an
1060 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 if (current_line == lines_ga.ga_len
1063 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
1067 retval = FAIL;
1068 break;
1069 }
1070 }
1071 did_endif = FALSE;
1072#endif
1073
1074 if (count++ == 0)
1075 {
1076 /*
1077 * All output from the commands is put below each other, without
1078 * waiting for a return. Don't do this when executing commands
1079 * from a script or when being called recursive (e.g. for ":e
1080 * +command file").
1081 */
1082 if (!(flags & DOCMD_NOWAIT) && !recursive)
1083 {
1084 msg_didout_before_start = msg_didout;
1085 msg_didany = FALSE; /* no output yet */
1086 msg_start();
1087 msg_scroll = TRUE; /* put messages below each other */
1088 ++no_wait_return; /* dont wait for return until finished */
1089 ++RedrawingDisabled;
1090 did_inc = TRUE;
1091 }
1092 }
1093
1094 if (p_verbose >= 15 && sourcing_name != NULL)
1095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001097 verbose_enter_scroll();
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 smsg((char_u *)_("line %ld: %s"),
1100 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001101 if (msg_silent == 0)
1102 msg_puts((char_u *)"\n"); /* don't overwrite this */
1103
1104 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 --no_wait_return;
1106 }
1107
1108 /*
1109 * 2. Execute one '|' separated command.
1110 * do_one_cmd() will return NULL if there is no trailing '|'.
1111 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1112 */
1113 ++recursive;
1114 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1115#ifdef FEAT_EVAL
1116 &cstack,
1117#endif
1118 cmd_getline, cmd_cookie);
1119 --recursive;
1120
1121#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 if (cmd_cookie == (void *)&cmd_loop_cookie)
1123 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#endif
1127
1128 if (next_cmdline == NULL)
1129 {
1130 vim_free(cmdline_copy);
1131 cmdline_copy = NULL;
1132#ifdef FEAT_CMDHIST
1133 /*
1134 * If the command was typed, remember it for the ':' register.
1135 * Do this AFTER executing the command to make :@: work.
1136 */
1137 if (getline_equal(getline, cookie, getexline)
1138 && new_last_cmdline != NULL)
1139 {
1140 vim_free(last_cmdline);
1141 last_cmdline = new_last_cmdline;
1142 new_last_cmdline = NULL;
1143 }
1144#endif
1145 }
1146 else
1147 {
1148 /* need to copy the command after the '|' to cmdline_copy, for the
1149 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001150 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 next_cmdline = cmdline_copy;
1152 }
1153
1154
1155#ifdef FEAT_EVAL
1156 /* reset did_emsg for a function that is not aborted by an error */
1157 if (did_emsg && !force_abort
1158 && getline_equal(getline, cookie, get_func_line)
1159 && !func_has_abort(real_cookie))
1160 did_emsg = FALSE;
1161
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
1164 ++current_line;
1165
1166 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 * An ":endwhile", ":endfor" and ":continue" is handled here.
1168 * If we were executing commands, jump back to the ":while" or
1169 * ":for".
1170 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 /* Jump back to the matching ":while" or ":for". Be careful
1177 * not to use a cs_line[] from an entry that isn't a ":while"
1178 * or ":for": It would make "current_line" invalid and can
1179 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (!did_emsg && !got_int && !did_throw
1181 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 && (cstack.cs_flags[cstack.cs_idx]
1183 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 && cstack.cs_line[cstack.cs_idx] >= 0
1185 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1186 {
1187 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* remember we jumped there */
1189 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 line_breakcheck(); /* check if CTRL-C typed */
1191
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 /* Check for the next breakpoint at or after the ":while"
1193 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (breakpoint != NULL)
1195 {
1196 *breakpoint = dbg_find_breakpoint(
1197 getline_equal(getline, cookie, getsourceline),
1198 fname,
1199 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1200 *dbg_tick = debug_tick;
1201 }
1202 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001207 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1208 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 }
1211
1212 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1219 }
1220 }
1221
1222 /*
1223 * When not inside any ":while" loop, clear remembered lines.
1224 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
1227 if (lines_ga.ga_len > 0)
1228 {
1229 sourcing_lnum =
1230 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1231 free_cmdlines(&lines_ga);
1232 }
1233 current_line = 0;
1234 }
1235
1236 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1238 * being restored at the ":endtry". Reset them here and set the
1239 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1240 * This includes the case where a missing ":endif", ":endwhile" or
1241 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 cstack.cs_lflags &= ~CSL_HAD_FINA;
1246 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1247 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 did_throw ? (void *)current_exception : NULL);
1249 did_emsg = got_int = did_throw = FALSE;
1250 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1251 }
1252
1253 /* Update global "trylevel" for recursive calls to do_cmdline() from
1254 * within this loop. */
1255 trylevel = initial_trylevel + cstack.cs_trylevel;
1256
1257 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001258 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 * files) is aborted because of an error, an interrupt, or an uncaught
1260 * exception, cancel everything. If it is left normally, reset
1261 * force_abort to get the non-EH compatible abortion behavior for
1262 * the rest of the script.
1263 */
1264 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1265 force_abort = FALSE;
1266
1267 /* Convert an interrupt to an exception if appropriate. */
1268 (void)do_intthrow(&cstack);
1269#endif /* FEAT_EVAL */
1270
1271 }
1272 /*
1273 * Continue executing command lines when:
1274 * - no CTRL-C typed, no aborting error, no exception thrown or try
1275 * conditionals need to be checked for executing finally clauses or
1276 * catching an interrupt exception
1277 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001278 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 * looping for ":source" command or function call.
1280 */
1281 while (!((got_int
1282#ifdef FEAT_EVAL
1283 || (did_emsg && force_abort) || did_throw
1284#endif
1285 )
1286#ifdef FEAT_EVAL
1287 && cstack.cs_trylevel == 0
1288#endif
1289 )
1290 && !(did_emsg && used_getline
1291 && (getline_equal(getline, cookie, getexmodeline)
1292 || getline_equal(getline, cookie, getexline)))
1293 && (next_cmdline != NULL
1294#ifdef FEAT_EVAL
1295 || cstack.cs_idx >= 0
1296#endif
1297 || (flags & DOCMD_REPEAT)));
1298
1299 vim_free(cmdline_copy);
1300#ifdef FEAT_EVAL
1301 free_cmdlines(&lines_ga);
1302 ga_clear(&lines_ga);
1303
1304 if (cstack.cs_idx >= 0)
1305 {
1306 /*
1307 * If a sourced file or executed function ran to its end, report the
1308 * unclosed conditional.
1309 */
1310 if (!got_int && !did_throw
1311 && ((getline_equal(getline, cookie, getsourceline)
1312 && !source_finished(getline, cookie))
1313 || (getline_equal(getline, cookie, get_func_line)
1314 && !func_has_ended(real_cookie))))
1315 {
1316 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1317 EMSG(_(e_endtry));
1318 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1319 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001320 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1321 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 else
1323 EMSG(_(e_endif));
1324 }
1325
1326 /*
1327 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1328 * ":endtry" in a sourced file or executed function. If the try
1329 * conditional is in its finally clause, ignore anything pending.
1330 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001331 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 */
1333 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001334 {
1335 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1336
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001337 if (idx >= 0)
1338 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001339 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1340 &cstack.cs_looplevel);
1341 }
1342 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 trylevel = initial_trylevel;
1344 }
1345
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001346 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1347 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 * exception, do this now after rewinding the cstack. */
1349 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1350 ? (char_u *)"endfunction" : (char_u *)NULL);
1351
1352 if (trylevel == 0)
1353 {
1354 /*
1355 * When an exception is being thrown out of the outermost try
1356 * conditional, discard the uncaught exception, disable the conversion
1357 * of interrupts or errors to exceptions, and ensure that no more
1358 * commands are executed.
1359 */
1360 if (did_throw)
1361 {
1362 void *p = NULL;
1363 char_u *saved_sourcing_name;
1364 int saved_sourcing_lnum;
1365 struct msglist *messages = NULL, *next;
1366
1367 /*
1368 * If the uncaught exception is a user exception, report it as an
1369 * error. If it is an error exception, display the saved error
1370 * message now. For an interrupt exception, do nothing; the
1371 * interrupt message is given elsewhere.
1372 */
1373 switch (current_exception->type)
1374 {
1375 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001376 vim_snprintf((char *)IObuff, IOSIZE,
1377 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 current_exception->value);
1379 p = vim_strsave(IObuff);
1380 break;
1381 case ET_ERROR:
1382 messages = current_exception->messages;
1383 current_exception->messages = NULL;
1384 break;
1385 case ET_INTERRUPT:
1386 break;
1387 default:
1388 p = vim_strsave((char_u *)_(e_internal));
1389 }
1390
1391 saved_sourcing_name = sourcing_name;
1392 saved_sourcing_lnum = sourcing_lnum;
1393 sourcing_name = current_exception->throw_name;
1394 sourcing_lnum = current_exception->throw_lnum;
1395 current_exception->throw_name = NULL;
1396
1397 discard_current_exception(); /* uses IObuff if 'verbose' */
1398 suppress_errthrow = TRUE;
1399 force_abort = TRUE;
1400
1401 if (messages != NULL)
1402 {
1403 do
1404 {
1405 next = messages->next;
1406 emsg(messages->msg);
1407 vim_free(messages->msg);
1408 vim_free(messages);
1409 messages = next;
1410 }
1411 while (messages != NULL);
1412 }
1413 else if (p != NULL)
1414 {
1415 emsg(p);
1416 vim_free(p);
1417 }
1418 vim_free(sourcing_name);
1419 sourcing_name = saved_sourcing_name;
1420 sourcing_lnum = saved_sourcing_lnum;
1421 }
1422
1423 /*
1424 * On an interrupt or an aborting error not converted to an exception,
1425 * disable the conversion of errors to exceptions. (Interrupts are not
1426 * converted any more, here.) This enables also the interrupt message
1427 * when force_abort is set and did_emsg unset in case of an interrupt
1428 * from a finally clause after an error.
1429 */
1430 else if (got_int || (did_emsg && force_abort))
1431 suppress_errthrow = TRUE;
1432 }
1433
1434 /*
1435 * The current cstack will be freed when do_cmdline() returns. An uncaught
1436 * exception will have to be rethrown in the previous cstack. If a function
1437 * has just returned or a script file was just finished and the previous
1438 * cstack belongs to the same function or, respectively, script file, it
1439 * will have to be checked for finally clauses to be executed due to the
1440 * ":return" or ":finish". This is done in do_one_cmd().
1441 */
1442 if (did_throw)
1443 need_rethrow = TRUE;
1444 if ((getline_equal(getline, cookie, getsourceline)
1445 && ex_nesting_level > source_level(real_cookie))
1446 || (getline_equal(getline, cookie, get_func_line)
1447 && ex_nesting_level > func_level(real_cookie) + 1))
1448 {
1449 if (!did_throw)
1450 check_cstack = TRUE;
1451 }
1452 else
1453 {
1454 /* When leaving a function, reduce nesting level. */
1455 if (getline_equal(getline, cookie, get_func_line))
1456 --ex_nesting_level;
1457 /*
1458 * Go to debug mode when returning from a function in which we are
1459 * single-stepping.
1460 */
1461 if ((getline_equal(getline, cookie, getsourceline)
1462 || getline_equal(getline, cookie, get_func_line))
1463 && ex_nesting_level + 1 <= debug_break_level)
1464 do_debug(getline_equal(getline, cookie, getsourceline)
1465 ? (char_u *)_("End of sourced file")
1466 : (char_u *)_("End of function"));
1467 }
1468
1469 /*
1470 * Restore the exception environment (done after returning from the
1471 * debugger).
1472 */
1473 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001474 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 msg_list = saved_msg_list;
1477#endif /* FEAT_EVAL */
1478
1479 /*
1480 * If there was too much output to fit on the command line, ask the user to
1481 * hit return before redrawing the screen. With the ":global" command we do
1482 * this only once after the command is finished.
1483 */
1484 if (did_inc)
1485 {
1486 --RedrawingDisabled;
1487 --no_wait_return;
1488 msg_scroll = FALSE;
1489
1490 /*
1491 * When just finished an ":if"-":else" which was typed, no need to
1492 * wait for hit-return. Also for an error situation.
1493 */
1494 if (retval == FAIL
1495#ifdef FEAT_EVAL
1496 || (did_endif && KeyTyped && !did_emsg)
1497#endif
1498 )
1499 {
1500 need_wait_return = FALSE;
1501 msg_didany = FALSE; /* don't wait when restarting edit */
1502 }
1503 else if (need_wait_return)
1504 {
1505 /*
1506 * The msg_start() above clears msg_didout. The wait_return we do
1507 * here should not overwrite the command that may be shown before
1508 * doing that.
1509 */
1510 msg_didout |= msg_didout_before_start;
1511 wait_return(FALSE);
1512 }
1513 }
1514
1515#ifndef FEAT_EVAL
1516 /*
1517 * Reset if_level, in case a sourced script file contains more ":if" than
1518 * ":endif" (could be ":if x | foo | endif").
1519 */
1520 if_level = 0;
1521#endif
1522
1523 --call_depth;
1524 return retval;
1525}
1526
1527#ifdef FEAT_EVAL
1528/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 */
1531 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 int c;
1534 void *cookie;
1535 int indent;
1536{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 wcmd_T *wp;
1539 char_u *line;
1540
1541 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1542 {
1543 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001544 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (cp->getline == NULL)
1548 line = getcmdline(c, 0L, indent);
1549 else
1550 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001551 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 ++cp->current_line;
1553
1554 return line;
1555 }
1556
1557 KeyTyped = FALSE;
1558 ++cp->current_line;
1559 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1560 sourcing_lnum = wp->lnum;
1561 return vim_strsave(wp->line);
1562}
1563
1564/*
1565 * Store a line in "gap" so that a ":while" loop can execute it again.
1566 */
1567 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 garray_T *gap;
1570 char_u *line;
1571{
1572 if (ga_grow(gap, 1) == FAIL)
1573 return FAIL;
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1575 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1576 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return OK;
1578}
1579
1580/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 */
1583 static void
1584free_cmdlines(gap)
1585 garray_T *gap;
1586{
1587 while (gap->ga_len > 0)
1588 {
1589 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1590 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592}
1593#endif
1594
1595/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001596 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1597 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001600getline_equal(fgetline, cookie, func)
1601 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001602 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 char_u *(*func) __ARGS((int, void *, int));
1604{
1605#ifdef FEAT_EVAL
1606 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001610 * function that's originally used to obtain the lines. This may be
1611 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001612 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001613 cp = (struct loop_cookie *)cookie;
1614 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 gp = cp->getline;
1617 cp = cp->cookie;
1618 }
1619 return gp == func;
1620#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#endif
1623}
1624
1625#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1626/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 * getline function. Otherwise return "cookie".
1629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001632 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001633 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635# ifdef FEAT_EVAL
1636 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001637 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001640 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001643 cp = (struct loop_cookie *)cookie;
1644 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 gp = cp->getline;
1647 cp = cp->cookie;
1648 }
1649 return cp;
1650# else
1651 return cookie;
1652# endif
1653}
1654#endif
1655
1656/*
1657 * Execute one Ex command.
1658 *
1659 * If 'sourcing' is TRUE, the command will be included in the error message.
1660 *
1661 * 1. skip comment lines and leading space
1662 * 2. handle command modifiers
1663 * 3. parse range
1664 * 4. parse command
1665 * 5. parse arguments
1666 * 6. switch on command name
1667 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001668 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 *
1670 * This function may be called recursively!
1671 */
1672#if (_MSC_VER == 1200)
1673/*
Bram Moolenaared203462004-06-16 11:19:22 +00001674 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001676 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
1678 static char_u *
1679do_one_cmd(cmdlinep, sourcing,
1680#ifdef FEAT_EVAL
1681 cstack,
1682#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001683 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 char_u **cmdlinep;
1685 int sourcing;
1686#ifdef FEAT_EVAL
1687 struct condstack *cstack;
1688#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001689 char_u *(*fgetline) __ARGS((int, void *, int));
1690 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 char_u *p;
1693 linenr_T lnum;
1694 long n;
1695 char_u *errormsg = NULL; /* error message */
1696 exarg_T ea; /* Ex command arguments */
1697 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001698 int save_msg_scroll = msg_scroll;
1699 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001701#ifdef HAVE_SANDBOX
1702 int did_sandbox = FALSE;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 cmdmod_T save_cmdmod;
1705 int ni; /* set when Not Implemented */
1706
1707 vim_memset(&ea, 0, sizeof(ea));
1708 ea.line1 = 1;
1709 ea.line2 = 1;
1710#ifdef FEAT_EVAL
1711 ++ex_nesting_level;
1712#endif
1713
1714 /* when not editing the last file :q has to be typed twice */
1715 if (quitmore
1716#ifdef FEAT_EVAL
1717 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001718 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720 )
1721 --quitmore;
1722
1723 /*
1724 * Reset browse, confirm, etc.. They are restored when returning, for
1725 * recursive calls.
1726 */
1727 save_cmdmod = cmdmod;
1728 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1729
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001730 /* "#!anything" is handled like a comment. */
1731 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1732 goto doend;
1733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * Repeat until no more command modifiers are found.
1736 */
1737 ea.cmd = *cmdlinep;
1738 for (;;)
1739 {
1740/*
1741 * 1. skip comment lines and leading white space and colons
1742 */
1743 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1744 ++ea.cmd;
1745
1746 /* in ex mode, an empty line works like :+ */
1747 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001748 && (getline_equal(fgetline, cookie, getexmodeline)
1749 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001750 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752 ea.cmd = (char_u *)"+";
1753 ex_pressedreturn = TRUE;
1754 }
1755
1756 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001757 if (*ea.cmd == '"')
1758 goto doend;
1759 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001760 {
1761 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765/*
1766 * 2. handle command modifiers.
1767 */
1768 p = ea.cmd;
1769 if (VIM_ISDIGIT(*ea.cmd))
1770 p = skipwhite(skipdigits(ea.cmd));
1771 switch (*p)
1772 {
1773 /* When adding an entry, also modify cmd_exists(). */
1774 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1775 break;
1776#ifdef FEAT_WINDOWS
1777 cmdmod.split |= WSP_ABOVE;
1778#endif
1779 continue;
1780
1781 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1782 {
1783#ifdef FEAT_WINDOWS
1784 cmdmod.split |= WSP_BELOW;
1785#endif
1786 continue;
1787 }
1788 if (checkforcmd(&ea.cmd, "browse", 3))
1789 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001790#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 cmdmod.browse = TRUE;
1792#endif
1793 continue;
1794 }
1795 if (!checkforcmd(&ea.cmd, "botright", 2))
1796 break;
1797#ifdef FEAT_WINDOWS
1798 cmdmod.split |= WSP_BOT;
1799#endif
1800 continue;
1801
1802 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1803 break;
1804#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1805 cmdmod.confirm = TRUE;
1806#endif
1807 continue;
1808
1809 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1810 {
1811 cmdmod.keepmarks = TRUE;
1812 continue;
1813 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001814 if (checkforcmd(&ea.cmd, "keepalt", 5))
1815 {
1816 cmdmod.keepalt = TRUE;
1817 continue;
1818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1820 break;
1821 cmdmod.keepjumps = TRUE;
1822 continue;
1823
1824 /* ":hide" and ":hide | cmd" are not modifiers */
1825 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1826 || *p == NUL || ends_excmd(*p))
1827 break;
1828 ea.cmd = p;
1829 cmdmod.hide = TRUE;
1830 continue;
1831
1832 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1833 {
1834 cmdmod.lockmarks = TRUE;
1835 continue;
1836 }
1837
1838 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1839 break;
1840#ifdef FEAT_WINDOWS
1841 cmdmod.split |= WSP_ABOVE;
1842#endif
1843 continue;
1844
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001845 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1846 break;
1847#ifdef FEAT_AUTOCMD
1848 if (cmdmod.save_ei == NULL)
1849 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001850 /* Set 'eventignore' to "all". Restore the
1851 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001852 cmdmod.save_ei = vim_strsave(p_ei);
1853 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001854 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001855 }
1856#endif
1857 continue;
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1860 break;
1861#ifdef FEAT_WINDOWS
1862 cmdmod.split |= WSP_BELOW;
1863#endif
1864 continue;
1865
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001866 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1867 {
1868#ifdef HAVE_SANDBOX
1869 if (!did_sandbox)
1870 ++sandbox;
1871 did_sandbox = TRUE;
1872#endif
1873 continue;
1874 }
1875 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001877 if (save_msg_silent == -1)
1878 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1881 {
1882 /* ":silent!", but not "silent !cmd" */
1883 ea.cmd = skipwhite(ea.cmd + 1);
1884 ++emsg_silent;
1885 ++did_esilent;
1886 }
1887 continue;
1888
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001889 case 't': if (checkforcmd(&p, "tab", 3))
1890 {
1891#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001892 if (vim_isdigit(*ea.cmd))
1893 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1894 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001895 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001896 ea.cmd = p;
1897#endif
1898 continue;
1899 }
1900 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 break;
1902#ifdef FEAT_WINDOWS
1903 cmdmod.split |= WSP_TOP;
1904#endif
1905 continue;
1906
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001907 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1908 break;
1909 if (save_msg_silent == -1)
1910 save_msg_silent = msg_silent;
1911 msg_silent = 0;
1912 continue;
1913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1915 {
1916#ifdef FEAT_VERTSPLIT
1917 cmdmod.split |= WSP_VERT;
1918#endif
1919 continue;
1920 }
1921 if (!checkforcmd(&p, "verbose", 4))
1922 break;
1923 if (verbose_save < 0)
1924 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001925 if (vim_isdigit(*ea.cmd))
1926 p_verbose = atoi((char *)ea.cmd);
1927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 p_verbose = 1;
1929 ea.cmd = p;
1930 continue;
1931 }
1932 break;
1933 }
1934
1935#ifdef FEAT_EVAL
1936 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1937 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1938#else
1939 ea.skip = (if_level > 0);
1940#endif
1941
1942#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001943# ifdef FEAT_PROFILE
1944 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001945 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001946 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001947 if (getline_equal(fgetline, cookie, get_func_line))
1948 func_line_exec(getline_cookie(fgetline, cookie));
1949 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001950 script_line_exec();
1951 }
1952#endif
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 /* May go to debug mode. If this happens and the ">quit" debug command is
1955 * used, throw an interrupt exception and skip the next command. */
1956 dbg_check_breakpoint(&ea);
1957 if (!ea.skip && got_int)
1958 {
1959 ea.skip = TRUE;
1960 (void)do_intthrow(cstack);
1961 }
1962#endif
1963
1964/*
1965 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1966 *
1967 * where 'addr' is:
1968 *
1969 * % (entire file)
1970 * $ [+-NUM]
1971 * 'x [+-NUM] (where x denotes a currently defined mark)
1972 * . [+-NUM]
1973 * [+-NUM]..
1974 * NUM
1975 *
1976 * The ea.cmd pointer is updated to point to the first character following the
1977 * range spec. If an initial address is found, but no second, the upper bound
1978 * is equal to the lower.
1979 */
1980
1981 /* repeat for all ',' or ';' separated addresses */
1982 for (;;)
1983 {
1984 ea.line1 = ea.line2;
1985 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1986 ea.cmd = skipwhite(ea.cmd);
1987 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1988 if (ea.cmd == NULL) /* error detected */
1989 goto doend;
1990 if (lnum == MAXLNUM)
1991 {
1992 if (*ea.cmd == '%') /* '%' - all lines */
1993 {
1994 ++ea.cmd;
1995 ea.line1 = 1;
1996 ea.line2 = curbuf->b_ml.ml_line_count;
1997 ++ea.addr_count;
1998 }
1999 /* '*' - visual area */
2000 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2001 {
2002 pos_T *fp;
2003
2004 ++ea.cmd;
2005 if (!ea.skip)
2006 {
2007 fp = getmark('<', FALSE);
2008 if (check_mark(fp) == FAIL)
2009 goto doend;
2010 ea.line1 = fp->lnum;
2011 fp = getmark('>', FALSE);
2012 if (check_mark(fp) == FAIL)
2013 goto doend;
2014 ea.line2 = fp->lnum;
2015 ++ea.addr_count;
2016 }
2017 }
2018 }
2019 else
2020 ea.line2 = lnum;
2021 ea.addr_count++;
2022
2023 if (*ea.cmd == ';')
2024 {
2025 if (!ea.skip)
2026 curwin->w_cursor.lnum = ea.line2;
2027 }
2028 else if (*ea.cmd != ',')
2029 break;
2030 ++ea.cmd;
2031 }
2032
2033 /* One address given: set start and end lines */
2034 if (ea.addr_count == 1)
2035 {
2036 ea.line1 = ea.line2;
2037 /* ... but only implicit: really no address given */
2038 if (lnum == MAXLNUM)
2039 ea.addr_count = 0;
2040 }
2041
2042 /* Don't leave the cursor on an illegal line (caused by ';') */
2043 check_cursor_lnum();
2044
2045/*
2046 * 4. parse command
2047 */
2048
2049 /*
2050 * Skip ':' and any white space
2051 */
2052 ea.cmd = skipwhite(ea.cmd);
2053 while (*ea.cmd == ':')
2054 ea.cmd = skipwhite(ea.cmd + 1);
2055
2056 /*
2057 * If we got a line, but no command, then go to the line.
2058 * If we find a '|' or '\n' we set ea.nextcmd.
2059 */
2060 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2061 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2062 {
2063 /*
2064 * strange vi behaviour:
2065 * ":3" jumps to line 3
2066 * ":3|..." prints line 3
2067 * ":|" prints current line
2068 */
2069 if (ea.skip) /* skip this if inside :if */
2070 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002071 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 ea.cmdidx = CMD_print;
2074 ea.argt = RANGE+COUNT+TRLBAR;
2075 if ((errormsg = invalid_range(&ea)) == NULL)
2076 {
2077 correct_range(&ea);
2078 ex_print(&ea);
2079 }
2080 }
2081 else if (ea.addr_count != 0)
2082 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002083 if (ea.line2 > curbuf->b_ml.ml_line_count)
2084 {
2085 /* With '-' in 'cpoptions' a line number past the file is an
2086 * error, otherwise put it at the end of the file. */
2087 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2088 ea.line2 = -1;
2089 else
2090 ea.line2 = curbuf->b_ml.ml_line_count;
2091 }
2092
2093 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002094 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else
2096 {
2097 if (ea.line2 == 0)
2098 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 else
2100 curwin->w_cursor.lnum = ea.line2;
2101 beginline(BL_SOL | BL_FIX);
2102 }
2103 }
2104 goto doend;
2105 }
2106
2107 /* Find the command and let "p" point to after it. */
2108 p = find_command(&ea, NULL);
2109
2110#ifdef FEAT_USR_CMDS
2111 if (p == NULL)
2112 {
2113 if (!ea.skip)
2114 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2115 goto doend;
2116 }
2117 /* Check for wrong commands. */
2118 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2119 {
2120 errormsg = uc_fun_cmd();
2121 goto doend;
2122 }
2123#endif
2124 if (ea.cmdidx == CMD_SIZE)
2125 {
2126 if (!ea.skip)
2127 {
2128 STRCPY(IObuff, _("E492: Not an editor command"));
2129 if (!sourcing)
2130 {
2131 STRCAT(IObuff, ": ");
2132 STRNCAT(IObuff, *cmdlinep, 40);
2133 }
2134 errormsg = IObuff;
2135 }
2136 goto doend;
2137 }
2138
2139 ni = (
2140#ifdef FEAT_USR_CMDS
2141 !USER_CMDIDX(ea.cmdidx) &&
2142#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002143 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002144#ifdef HAVE_EX_SCRIPT_NI
2145 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2146#endif
2147 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
2149#ifndef FEAT_EVAL
2150 /*
2151 * When the expression evaluation is disabled, recognize the ":if" and
2152 * ":endif" commands and ignore everything in between it.
2153 */
2154 if (ea.cmdidx == CMD_if)
2155 ++if_level;
2156 if (if_level)
2157 {
2158 if (ea.cmdidx == CMD_endif)
2159 --if_level;
2160 goto doend;
2161 }
2162
2163#endif
2164
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002165 /* forced commands */
2166 if (*p == '!' && ea.cmdidx != CMD_substitute
2167 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 ++p;
2170 ea.forceit = TRUE;
2171 }
2172 else
2173 ea.forceit = FALSE;
2174
2175/*
2176 * 5. parse arguments
2177 */
2178#ifdef FEAT_USR_CMDS
2179 if (!USER_CMDIDX(ea.cmdidx))
2180#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002181 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 if (!ea.skip)
2184 {
2185#ifdef HAVE_SANDBOX
2186 if (sandbox != 0 && !(ea.argt & SBOXOK))
2187 {
2188 /* Command not allowed in sandbox. */
2189 errormsg = (char_u *)_(e_sandbox);
2190 goto doend;
2191 }
2192#endif
2193 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2194 {
2195 /* Command not allowed in non-'modifiable' buffer */
2196 errormsg = (char_u *)_(e_modifiable);
2197 goto doend;
2198 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002199
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002200 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201# ifdef FEAT_USR_CMDS
2202 && !USER_CMDIDX(ea.cmdidx)
2203# endif
2204 )
2205 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002206 /* Command not allowed when editing the command line. */
2207#ifdef FEAT_CMDWIN
2208 if (cmdwin_type != 0)
2209 errormsg = (char_u *)_(e_cmdwin);
2210 else
2211#endif
2212 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 goto doend;
2214 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002215#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002216 /* Disallow editing another buffer when "curbuf_lock" is set.
2217 * Do allow ":edit" (check for argument later).
2218 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002220 && ea.cmdidx != CMD_edit
2221 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002222# ifdef FEAT_USR_CMDS
2223 && !USER_CMDIDX(ea.cmdidx)
2224# endif
2225 && curbuf_locked())
2226 goto doend;
2227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2230 {
2231 /* no range allowed */
2232 errormsg = (char_u *)_(e_norange);
2233 goto doend;
2234 }
2235 }
2236
2237 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2238 {
2239 errormsg = (char_u *)_(e_nobang);
2240 goto doend;
2241 }
2242
2243 /*
2244 * Don't complain about the range if it is not used
2245 * (could happen if line_count is accidentally set to 0).
2246 */
2247 if (!ea.skip && !ni)
2248 {
2249 /*
2250 * If the range is backwards, ask for confirmation and, if given, swap
2251 * ea.line1 & ea.line2 so it's forwards again.
2252 * When global command is busy, don't ask, will fail below.
2253 */
2254 if (!global_busy && ea.line1 > ea.line2)
2255 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002256 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002258 if (sourcing || exmode_active)
2259 {
2260 errormsg = (char_u *)_("E493: Backwards range given");
2261 goto doend;
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (ask_yesno((char_u *)
2264 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002265 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
2267 lnum = ea.line1;
2268 ea.line1 = ea.line2;
2269 ea.line2 = lnum;
2270 }
2271 if ((errormsg = invalid_range(&ea)) != NULL)
2272 goto doend;
2273 }
2274
2275 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2276 ea.line2 = 1;
2277
2278 correct_range(&ea);
2279
2280#ifdef FEAT_FOLDING
2281 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2282 {
2283 /* Put the first line at the start of a closed fold, put the last line
2284 * at the end of a closed fold. */
2285 (void)hasFolding(ea.line1, &ea.line1, NULL);
2286 (void)hasFolding(ea.line2, NULL, &ea.line2);
2287 }
2288#endif
2289
2290#ifdef FEAT_QUICKFIX
2291 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002292 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 * option here, so things like % get expanded.
2294 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002295 p = replace_makeprg(&ea, p, cmdlinep);
2296 if (p == NULL)
2297 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298#endif
2299
2300 /*
2301 * Skip to start of argument.
2302 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2303 */
2304 if (ea.cmdidx == CMD_bang)
2305 ea.arg = p;
2306 else
2307 ea.arg = skipwhite(p);
2308
2309 /*
2310 * Check for "++opt=val" argument.
2311 * Must be first, allow ":w ++enc=utf8 !cmd"
2312 */
2313 if (ea.argt & ARGOPT)
2314 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2315 if (getargopt(&ea) == FAIL && !ni)
2316 {
2317 errormsg = (char_u *)_(e_invarg);
2318 goto doend;
2319 }
2320
2321 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2322 {
2323 if (*ea.arg == '>') /* append */
2324 {
2325 if (*++ea.arg != '>') /* typed wrong */
2326 {
2327 errormsg = (char_u *)_("E494: Use w or w>>");
2328 goto doend;
2329 }
2330 ea.arg = skipwhite(ea.arg + 1);
2331 ea.append = TRUE;
2332 }
2333 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2334 {
2335 ++ea.arg;
2336 ea.usefilter = TRUE;
2337 }
2338 }
2339
2340 if (ea.cmdidx == CMD_read)
2341 {
2342 if (ea.forceit)
2343 {
2344 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2345 ea.forceit = FALSE;
2346 }
2347 else if (*ea.arg == '!') /* :r !filter */
2348 {
2349 ++ea.arg;
2350 ea.usefilter = TRUE;
2351 }
2352 }
2353
2354 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2355 {
2356 ea.amount = 1;
2357 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2358 {
2359 ++ea.arg;
2360 ++ea.amount;
2361 }
2362 ea.arg = skipwhite(ea.arg);
2363 }
2364
2365 /*
2366 * Check for "+command" argument, before checking for next command.
2367 * Don't do this for ":read !cmd" and ":write !cmd".
2368 */
2369 if ((ea.argt & EDITCMD) && !ea.usefilter)
2370 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2371
2372 /*
2373 * Check for '|' to separate commands and '"' to start comments.
2374 * Don't do this for ":read !cmd" and ":write !cmd".
2375 */
2376 if ((ea.argt & TRLBAR) && !ea.usefilter)
2377 separate_nextcmd(&ea);
2378
2379 /*
2380 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002381 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2382 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002384 else if (ea.cmdidx == CMD_bang
2385 || ea.cmdidx == CMD_global
2386 || ea.cmdidx == CMD_vglobal
2387 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 for (p = ea.arg; *p; ++p)
2390 {
2391 /* Remove one backslash before a newline, so that it's possible to
2392 * pass a newline to the shell and also a newline that is preceded
2393 * with a backslash. This makes it impossible to end a shell
2394 * command in a backslash, but that doesn't appear useful.
2395 * Halving the number of backslashes is incompatible with previous
2396 * versions. */
2397 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002398 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 else if (*p == '\n')
2400 {
2401 ea.nextcmd = p + 1;
2402 *p = NUL;
2403 break;
2404 }
2405 }
2406 }
2407
2408 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2409 {
2410 ea.line1 = 1;
2411 ea.line2 = curbuf->b_ml.ml_line_count;
2412 }
2413
2414 /* accept numbered register only when no count allowed (:put) */
2415 if ( (ea.argt & REGSTR)
2416 && *ea.arg != NUL
2417#ifdef FEAT_USR_CMDS
2418 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2419 && USER_CMDIDX(ea.cmdidx)))
2420 /* Do not allow register = for user commands */
2421 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2422#else
2423 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2424#endif
2425 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2426 {
2427 ea.regname = *ea.arg++;
2428#ifdef FEAT_EVAL
2429 /* for '=' register: accept the rest of the line as an expression */
2430 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2431 {
2432 set_expr_line(vim_strsave(ea.arg));
2433 ea.arg += STRLEN(ea.arg);
2434 }
2435#endif
2436 ea.arg = skipwhite(ea.arg);
2437 }
2438
2439 /*
2440 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2441 * count, it's a buffer name.
2442 */
2443 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2444 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2445 || vim_iswhite(*p)))
2446 {
2447 n = getdigits(&ea.arg);
2448 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002449 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451 errormsg = (char_u *)_(e_zerocount);
2452 goto doend;
2453 }
2454 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2455 {
2456 ea.line2 = n;
2457 if (ea.addr_count == 0)
2458 ea.addr_count = 1;
2459 }
2460 else
2461 {
2462 ea.line1 = ea.line2;
2463 ea.line2 += n - 1;
2464 ++ea.addr_count;
2465 /*
2466 * Be vi compatible: no error message for out of range.
2467 */
2468 if (ea.line2 > curbuf->b_ml.ml_line_count)
2469 ea.line2 = curbuf->b_ml.ml_line_count;
2470 }
2471 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002472
2473 /*
2474 * Check for flags: 'l', 'p' and '#'.
2475 */
2476 if (ea.argt & EXFLAGS)
2477 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002479 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002480 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 errormsg = (char_u *)_(e_trailing);
2483 goto doend;
2484 }
2485
2486 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2487 {
2488 errormsg = (char_u *)_(e_argreq);
2489 goto doend;
2490 }
2491
2492#ifdef FEAT_EVAL
2493 /*
2494 * Skip the command when it's not going to be executed.
2495 * The commands like :if, :endif, etc. always need to be executed.
2496 * Also make an exception for commands that handle a trailing command
2497 * themselves.
2498 */
2499 if (ea.skip)
2500 {
2501 switch (ea.cmdidx)
2502 {
2503 /* commands that need evaluation */
2504 case CMD_while:
2505 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002506 case CMD_for:
2507 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 case CMD_if:
2509 case CMD_elseif:
2510 case CMD_else:
2511 case CMD_endif:
2512 case CMD_try:
2513 case CMD_catch:
2514 case CMD_finally:
2515 case CMD_endtry:
2516 case CMD_function:
2517 break;
2518
2519 /* Commands that handle '|' themselves. Check: A command should
2520 * either have the TRLBAR flag, appear in this list or appear in
2521 * the list at ":help :bar". */
2522 case CMD_aboveleft:
2523 case CMD_and:
2524 case CMD_belowright:
2525 case CMD_botright:
2526 case CMD_browse:
2527 case CMD_call:
2528 case CMD_confirm:
2529 case CMD_delfunction:
2530 case CMD_djump:
2531 case CMD_dlist:
2532 case CMD_dsearch:
2533 case CMD_dsplit:
2534 case CMD_echo:
2535 case CMD_echoerr:
2536 case CMD_echomsg:
2537 case CMD_echon:
2538 case CMD_execute:
2539 case CMD_help:
2540 case CMD_hide:
2541 case CMD_ijump:
2542 case CMD_ilist:
2543 case CMD_isearch:
2544 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002545 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 case CMD_keepjumps:
2547 case CMD_keepmarks:
2548 case CMD_leftabove:
2549 case CMD_let:
2550 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002551 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002553 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 case CMD_perl:
2555 case CMD_psearch:
2556 case CMD_python:
2557 case CMD_return:
2558 case CMD_rightbelow:
2559 case CMD_ruby:
2560 case CMD_silent:
2561 case CMD_smagic:
2562 case CMD_snomagic:
2563 case CMD_substitute:
2564 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002565 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 case CMD_tcl:
2567 case CMD_throw:
2568 case CMD_tilde:
2569 case CMD_topleft:
2570 case CMD_unlet:
2571 case CMD_verbose:
2572 case CMD_vertical:
2573 break;
2574
2575 default: goto doend;
2576 }
2577 }
2578#endif
2579
2580 if (ea.argt & XFILE)
2581 {
2582 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2583 goto doend;
2584 }
2585
2586#ifdef FEAT_LISTCMDS
2587 /*
2588 * Accept buffer name. Cannot be used at the same time with a buffer
2589 * number. Don't do this for a user command.
2590 */
2591 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2592# ifdef FEAT_USR_CMDS
2593 && !USER_CMDIDX(ea.cmdidx)
2594# endif
2595 )
2596 {
2597 /*
2598 * :bdelete, :bwipeout and :bunload take several arguments, separated
2599 * by spaces: find next space (skipping over escaped characters).
2600 * The others take one argument: ignore trailing spaces.
2601 */
2602 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2603 || ea.cmdidx == CMD_bunload)
2604 p = skiptowhite_esc(ea.arg);
2605 else
2606 {
2607 p = ea.arg + STRLEN(ea.arg);
2608 while (p > ea.arg && vim_iswhite(p[-1]))
2609 --p;
2610 }
2611 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2612 if (ea.line2 < 0) /* failed */
2613 goto doend;
2614 ea.addr_count = 1;
2615 ea.arg = skipwhite(p);
2616 }
2617#endif
2618
2619/*
2620 * 6. switch on command name
2621 *
2622 * The "ea" structure holds the arguments that can be used.
2623 */
2624 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002625 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 ea.cookie = cookie;
2627#ifdef FEAT_EVAL
2628 ea.cstack = cstack;
2629#endif
2630
2631#ifdef FEAT_USR_CMDS
2632 if (USER_CMDIDX(ea.cmdidx))
2633 {
2634 /*
2635 * Execute a user-defined command.
2636 */
2637 do_ucmd(&ea);
2638 }
2639 else
2640#endif
2641 {
2642 /*
2643 * Call the function to execute the command.
2644 */
2645 ea.errmsg = NULL;
2646 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2647 if (ea.errmsg != NULL)
2648 errormsg = (char_u *)_(ea.errmsg);
2649 }
2650
2651#ifdef FEAT_EVAL
2652 /*
2653 * If the command just executed called do_cmdline(), any throw or ":return"
2654 * or ":finish" encountered there must also check the cstack of the still
2655 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2656 * exception, or reanimate a returned function or finished script file and
2657 * return or finish it again.
2658 */
2659 if (need_rethrow)
2660 do_throw(cstack);
2661 else if (check_cstack)
2662 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 && current_func_returned())
2667 do_return(&ea, TRUE, FALSE, NULL);
2668 }
2669 need_rethrow = check_cstack = FALSE;
2670#endif
2671
2672doend:
2673 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2674 curwin->w_cursor.lnum = 1;
2675
2676 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2677 {
2678 if (sourcing)
2679 {
2680 if (errormsg != IObuff)
2681 {
2682 STRCPY(IObuff, errormsg);
2683 errormsg = IObuff;
2684 }
2685 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002686 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688 emsg(errormsg);
2689 }
2690#ifdef FEAT_EVAL
2691 do_errthrow(cstack,
2692 (ea.cmdidx != CMD_SIZE
2693# ifdef FEAT_USR_CMDS
2694 && !USER_CMDIDX(ea.cmdidx)
2695# endif
2696 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2697#endif
2698
2699 if (verbose_save >= 0)
2700 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002701#ifdef FEAT_AUTOCMD
2702 if (cmdmod.save_ei != NULL)
2703 {
2704 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002705 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2706 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002707 free_string_option(cmdmod.save_ei);
2708 }
2709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 cmdmod = save_cmdmod;
2712
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002713 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
2715 /* messages could be enabled for a serious error, need to check if the
2716 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002717 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002718 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 emsg_silent -= did_esilent;
2720 if (emsg_silent < 0)
2721 emsg_silent = 0;
2722 /* Restore msg_scroll, it's set by file I/O commands, even when no
2723 * message is actually displayed. */
2724 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002725
2726 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2727 * somewhere in the line. Put it back in the first column. */
2728 if (redirecting())
2729 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002732#ifdef HAVE_SANDBOX
2733 if (did_sandbox)
2734 --sandbox;
2735#endif
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2738 ea.nextcmd = NULL;
2739
2740#ifdef FEAT_EVAL
2741 --ex_nesting_level;
2742#endif
2743
2744 return ea.nextcmd;
2745}
2746#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002747 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748#endif
2749
2750/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002751 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 * If there is a match advance "pp" to the argument and return TRUE.
2753 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002754 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002756 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 char *cmd; /* name of command */
2758 int len; /* required length */
2759{
2760 int i;
2761
2762 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002763 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 break;
2765 if (i >= len && !isalpha((*pp)[i]))
2766 {
2767 *pp = skipwhite(*pp + i);
2768 return TRUE;
2769 }
2770 return FALSE;
2771}
2772
2773/*
2774 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002775 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002777 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 * Returns NULL for an ambiguous user command.
2779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 static char_u *
2781find_command(eap, full)
2782 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002783 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 int len;
2786 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002787 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 /*
2790 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002791 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 * - the 'k' command can directly be followed by any character.
2793 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2794 * but :sre[wind] is another command, as are :scrip[tnames],
2795 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002796 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 */
2798 p = eap->cmd;
2799 if (*p == 'k')
2800 {
2801 eap->cmdidx = CMD_k;
2802 ++p;
2803 }
2804 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002805 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2806 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 || p[1] == 'g'
2808 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2809 || p[1] == 'I'
2810 || (p[1] == 'r' && p[2] != 'e')))
2811 {
2812 eap->cmdidx = CMD_substitute;
2813 ++p;
2814 }
2815 else
2816 {
2817 while (ASCII_ISALPHA(*p))
2818 ++p;
2819 /* check for non-alpha command */
2820 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2821 ++p;
2822 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002823 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2824 {
2825 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2826 * :delete with the 'l' flag. Same for 'p'. */
2827 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002828 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002829 break;
2830 if (i == len - 1)
2831 {
2832 --len;
2833 if (p[-1] == 'l')
2834 eap->flags |= EXFLAG_LIST;
2835 else
2836 eap->flags |= EXFLAG_PRINT;
2837 }
2838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840 if (ASCII_ISLOWER(*eap->cmd))
2841 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2842 else
2843 eap->cmdidx = cmdidxs[26];
2844
2845 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2846 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2847 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2848 (size_t)len) == 0)
2849 {
2850#ifdef FEAT_EVAL
2851 if (full != NULL
2852 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2853 *full = TRUE;
2854#endif
2855 break;
2856 }
2857
2858#ifdef FEAT_USR_CMDS
2859 /* Look for a user defined command as a last resort */
2860 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2861 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002862 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 while (ASCII_ISALNUM(*p))
2864 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002865 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002868 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 eap->cmdidx = CMD_SIZE;
2870 }
2871
2872 return p;
2873}
2874
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002875#ifdef FEAT_USR_CMDS
2876/*
2877 * Search for a user command that matches "eap->cmd".
2878 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2879 * Return a pointer to just after the command.
2880 * Return NULL if there is no matching command.
2881 */
2882 static char_u *
2883find_ucmd(eap, p, full, xp, compl)
2884 exarg_T *eap;
2885 char_u *p; /* end of the command (possibly including count) */
2886 int *full; /* set to TRUE for a full match */
2887 expand_T *xp; /* used for completion, NULL otherwise */
2888 int *compl; /* completion flags or NULL */
2889{
2890 int len = (int)(p - eap->cmd);
2891 int j, k, matchlen = 0;
2892 ucmd_T *uc;
2893 int found = FALSE;
2894 int possible = FALSE;
2895 char_u *cp, *np; /* Point into typed cmd and test name */
2896 garray_T *gap;
2897 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2898 only full match global is accepted. */
2899
2900 /*
2901 * Look for buffer-local user commands first, then global ones.
2902 */
2903 gap = &curbuf->b_ucmds;
2904 for (;;)
2905 {
2906 for (j = 0; j < gap->ga_len; ++j)
2907 {
2908 uc = USER_CMD_GA(gap, j);
2909 cp = eap->cmd;
2910 np = uc->uc_name;
2911 k = 0;
2912 while (k < len && *np != NUL && *cp++ == *np++)
2913 k++;
2914 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2915 {
2916 /* If finding a second match, the command is ambiguous. But
2917 * not if a buffer-local command wasn't a full match and a
2918 * global command is a full match. */
2919 if (k == len && found && *np != NUL)
2920 {
2921 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002922 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002923 amb_local = TRUE;
2924 }
2925
2926 if (!found || (k == len && *np == NUL))
2927 {
2928 /* If we matched up to a digit, then there could
2929 * be another command including the digit that we
2930 * should use instead.
2931 */
2932 if (k == len)
2933 found = TRUE;
2934 else
2935 possible = TRUE;
2936
2937 if (gap == &ucmds)
2938 eap->cmdidx = CMD_USER;
2939 else
2940 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002941 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002942 eap->useridx = j;
2943
2944# ifdef FEAT_CMDL_COMPL
2945 if (compl != NULL)
2946 *compl = uc->uc_compl;
2947# ifdef FEAT_EVAL
2948 if (xp != NULL)
2949 {
2950 xp->xp_arg = uc->uc_compl_arg;
2951 xp->xp_scriptID = uc->uc_scriptID;
2952 }
2953# endif
2954# endif
2955 /* Do not search for further abbreviations
2956 * if this is an exact match. */
2957 matchlen = k;
2958 if (k == len && *np == NUL)
2959 {
2960 if (full != NULL)
2961 *full = TRUE;
2962 amb_local = FALSE;
2963 break;
2964 }
2965 }
2966 }
2967 }
2968
2969 /* Stop if we found a full match or searched all. */
2970 if (j < gap->ga_len || gap == &ucmds)
2971 break;
2972 gap = &ucmds;
2973 }
2974
2975 /* Only found ambiguous matches. */
2976 if (amb_local)
2977 {
2978 if (xp != NULL)
2979 xp->xp_context = EXPAND_UNSUCCESSFUL;
2980 return NULL;
2981 }
2982
2983 /* The match we found may be followed immediately by a number. Move "p"
2984 * back to point to it. */
2985 if (found || possible)
2986 return p + (matchlen - len);
2987 return p;
2988}
2989#endif
2990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00002992static struct cmdmod
2993{
2994 char *name;
2995 int minlen;
2996 int has_count; /* :123verbose :3tab */
2997} cmdmods[] = {
2998 {"aboveleft", 3, FALSE},
2999 {"belowright", 3, FALSE},
3000 {"botright", 2, FALSE},
3001 {"browse", 3, FALSE},
3002 {"confirm", 4, FALSE},
3003 {"hide", 3, FALSE},
3004 {"keepalt", 5, FALSE},
3005 {"keepjumps", 5, FALSE},
3006 {"keepmarks", 3, FALSE},
3007 {"leftabove", 5, FALSE},
3008 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003009 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003010 {"rightbelow", 6, FALSE},
3011 {"sandbox", 3, FALSE},
3012 {"silent", 3, FALSE},
3013 {"tab", 3, TRUE},
3014 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003015 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003016 {"verbose", 4, TRUE},
3017 {"vertical", 4, FALSE},
3018};
3019
3020/*
3021 * Return length of a command modifier (including optional count).
3022 * Return zero when it's not a modifier.
3023 */
3024 int
3025modifier_len(cmd)
3026 char_u *cmd;
3027{
3028 int i, j;
3029 char_u *p = cmd;
3030
3031 if (VIM_ISDIGIT(*cmd))
3032 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003033 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003034 {
3035 for (j = 0; p[j] != NUL; ++j)
3036 if (p[j] != cmdmods[i].name[j])
3037 break;
3038 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3039 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003040 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003041 }
3042 return 0;
3043}
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045/*
3046 * Return > 0 if an Ex command "name" exists.
3047 * Return 2 if there is an exact match.
3048 * Return 3 if there is an ambiguous match.
3049 */
3050 int
3051cmd_exists(name)
3052 char_u *name;
3053{
3054 exarg_T ea;
3055 int full = FALSE;
3056 int i;
3057 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003058 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003061 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 {
3063 for (j = 0; name[j] != NUL; ++j)
3064 if (name[j] != cmdmods[i].name[j])
3065 break;
3066 if (name[j] == NUL && j >= cmdmods[i].minlen)
3067 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3068 }
3069
Bram Moolenaara9587612006-05-04 21:47:50 +00003070 /* Check built-in commands and user defined commands.
3071 * For ":2match" and ":3match" we need to skip the number. */
3072 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003074 p = find_command(&ea, &full);
3075 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003077 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3078 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003079 if (*skipwhite(p) != NUL)
3080 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3082}
3083#endif
3084
3085/*
3086 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3087 * we don't need/want deleted. Maybe this could be done better if we didn't
3088 * repeat all this stuff. The only problem is that they may not stay
3089 * perfectly compatible with each other, but then the command line syntax
3090 * probably won't change that much -- webb.
3091 */
3092 char_u *
3093set_one_cmd_context(xp, buff)
3094 expand_T *xp;
3095 char_u *buff; /* buffer for command string */
3096{
3097 char_u *p;
3098 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003099 int len = 0;
3100 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3102 int compl = EXPAND_NOTHING;
3103#endif
3104#ifdef FEAT_CMDL_COMPL
3105 int delim;
3106#endif
3107 int forceit = FALSE;
3108 int usefilter = FALSE; /* filter instead of file name */
3109
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003110 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 xp->xp_pattern = buff;
3112 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003113 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115/*
3116 * 2. skip comment lines and leading space, colons or bars
3117 */
3118 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3119 ;
3120 xp->xp_pattern = cmd;
3121
3122 if (*cmd == NUL)
3123 return NULL;
3124 if (*cmd == '"') /* ignore comment lines */
3125 {
3126 xp->xp_context = EXPAND_NOTHING;
3127 return NULL;
3128 }
3129
3130/*
3131 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3132 */
3133 cmd = skip_range(cmd, &xp->xp_context);
3134
3135/*
3136 * 4. parse command
3137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 xp->xp_pattern = cmd;
3139 if (*cmd == NUL)
3140 return NULL;
3141 if (*cmd == '"')
3142 {
3143 xp->xp_context = EXPAND_NOTHING;
3144 return NULL;
3145 }
3146
3147 if (*cmd == '|' || *cmd == '\n')
3148 return cmd + 1; /* There's another command */
3149
3150 /*
3151 * Isolate the command and search for it in the command table.
3152 * Exceptions:
3153 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003154 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3156 */
3157 if (*cmd == 'k' && cmd[1] != 'e')
3158 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003159 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 p = cmd + 1;
3161 }
3162 else
3163 {
3164 p = cmd;
3165 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3166 ++p;
3167 /* check for non-alpha command */
3168 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3169 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003170 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003172 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 xp->xp_context = EXPAND_UNSUCCESSFUL;
3175 return NULL;
3176 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003178 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3179 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3180 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182
3183#ifdef FEAT_USR_CMDS
3184 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3186 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187#endif
3188 }
3189
3190 /*
3191 * If the cursor is touching the command, and it ends in an alpha-numeric
3192 * character, complete the command name.
3193 */
3194 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3195 return NULL;
3196
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003197 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
3199 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3200 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 p = cmd + 1;
3203 }
3204#ifdef FEAT_USR_CMDS
3205 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3206 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 ea.cmd = cmd;
3208 p = find_ucmd(&ea, p, NULL, xp,
3209# if defined(FEAT_CMDL_COMPL)
3210 &compl
3211# else
3212 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003215 if (p == NULL)
3216 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218#endif
3219 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
3222 /* Not still touching the command and it was an illegal one */
3223 xp->xp_context = EXPAND_UNSUCCESSFUL;
3224 return NULL;
3225 }
3226
3227 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3228
3229 if (*p == '!') /* forced commands */
3230 {
3231 forceit = TRUE;
3232 ++p;
3233 }
3234
3235/*
3236 * 5. parse arguments
3237 */
3238#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003241 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 arg = skipwhite(p);
3244
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 if (*arg == '>') /* append */
3248 {
3249 if (*++arg == '>')
3250 ++arg;
3251 arg = skipwhite(arg);
3252 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
3255 ++arg;
3256 usefilter = TRUE;
3257 }
3258 }
3259
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 usefilter = forceit; /* :r! filter if forced */
3263 if (*arg == '!') /* :r !filter */
3264 {
3265 ++arg;
3266 usefilter = TRUE;
3267 }
3268 }
3269
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 while (*arg == *cmd) /* allow any number of '>' or '<' */
3273 ++arg;
3274 arg = skipwhite(arg);
3275 }
3276
3277 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 /* Check if we're in the +command */
3281 p = arg + 1;
3282 arg = skip_cmd_arg(arg, FALSE);
3283
3284 /* Still touching the command after '+'? */
3285 if (*arg == NUL)
3286 return p;
3287
3288 /* Skip space(s) after +command to get to the real argument */
3289 arg = skipwhite(arg);
3290 }
3291
3292 /*
3293 * Check for '|' to separate commands and '"' to start comments.
3294 * Don't do this for ":read !cmd" and ":write !cmd".
3295 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 p = arg;
3299 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003300 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 p += 2;
3302 while (*p)
3303 {
3304 if (*p == Ctrl_V)
3305 {
3306 if (p[1] != NUL)
3307 ++p;
3308 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 || *p == '|' || *p == '\n')
3311 {
3312 if (*(p - 1) != '\\')
3313 {
3314 if (*p == '|' || *p == '\n')
3315 return p + 1;
3316 return NULL; /* It's a comment */
3317 }
3318 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003319 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 }
3322
3323 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 vim_strchr((char_u *)"|\"", *arg) == NULL)
3326 return NULL;
3327
3328 /* Find start of last argument (argument just before cursor): */
3329 p = buff + STRLEN(buff);
3330 while (p != arg && *p != ' ' && *p != TAB)
3331 p--;
3332 if (*p == ' ' || *p == TAB)
3333 p++;
3334 xp->xp_pattern = p;
3335
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003336 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003338 int c;
3339 int in_quote = FALSE;
3340 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342 /*
3343 * Allow spaces within back-quotes to count as part of the argument
3344 * being expanded.
3345 */
3346 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003347 p = xp->xp_pattern;
3348 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003350#ifdef FEAT_MBYTE
3351 if (has_mbyte)
3352 c = mb_ptr2char(p);
3353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003355 c = *p;
3356 if (c == '\\' && p[1] != NUL)
3357 ++p;
3358 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 if (!in_quote)
3361 {
3362 xp->xp_pattern = p;
3363 bow = p + 1;
3364 }
3365 in_quote = !in_quote;
3366 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003367 /* An argument can contain just about everything, except
3368 * characters that end the command and white space. */
3369 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003371 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003372#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003373 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003374 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003375 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003376 while (*p != NUL)
3377 {
3378#ifdef FEAT_MBYTE
3379 if (has_mbyte)
3380 c = mb_ptr2char(p);
3381 else
3382#endif
3383 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003384 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003385 break;
3386#ifdef FEAT_MBYTE
3387 if (has_mbyte)
3388 len = (*mb_ptr2len)(p);
3389 else
3390#endif
3391 len = 1;
3392 mb_ptr_adv(p);
3393 }
3394 if (in_quote)
3395 bow = p;
3396 else
3397 xp->xp_pattern = p;
3398 p -= len;
3399 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003400 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402
3403 /*
3404 * If we are still inside the quotes, and we passed a space, just
3405 * expand from there.
3406 */
3407 if (bow != NULL && in_quote)
3408 xp->xp_pattern = bow;
3409 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003410
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003411#ifndef BACKSLASH_IN_FILENAME
3412 /* For a shell command more chars need to be escaped. */
3413 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003414 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003415 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003416
3417 /* When still after the command name expand executables. */
3418 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003419 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003420 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /* Check for environment variable */
3424 if (*xp->xp_pattern == '$'
3425#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3426 || *xp->xp_pattern == '%'
3427#endif
3428 )
3429 {
3430 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3431 if (!vim_isIDc(*p))
3432 break;
3433 if (*p == NUL)
3434 {
3435 xp->xp_context = EXPAND_ENV_VARS;
3436 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003437#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3438 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003439 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003440 compl = EXPAND_ENV_VARS;
3441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443 }
3444 }
3445
3446/*
3447 * 6. switch on command name
3448 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003449 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003451 case CMD_find:
3452 case CMD_sfind:
3453 case CMD_tabfind:
3454 xp->xp_context = EXPAND_FILES_IN_PATH;
3455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 case CMD_cd:
3457 case CMD_chdir:
3458 case CMD_lcd:
3459 case CMD_lchdir:
3460 if (xp->xp_context == EXPAND_FILES)
3461 xp->xp_context = EXPAND_DIRECTORIES;
3462 break;
3463 case CMD_help:
3464 xp->xp_context = EXPAND_HELP;
3465 xp->xp_pattern = arg;
3466 break;
3467
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003468 /* Command modifiers: return the argument.
3469 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003471 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 case CMD_belowright:
3473 case CMD_botright:
3474 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003475 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003477 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case CMD_folddoclosed:
3479 case CMD_folddoopen:
3480 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003481 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 case CMD_keepjumps:
3483 case CMD_keepmarks:
3484 case CMD_leftabove:
3485 case CMD_lockmarks:
3486 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003487 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003489 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 case CMD_topleft:
3491 case CMD_verbose:
3492 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003493 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 return arg;
3495
Bram Moolenaar4f688582007-07-24 12:34:30 +00003496#ifdef FEAT_CMDL_COMPL
3497# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 case CMD_match:
3499 if (*arg == NUL || !ends_excmd(*arg))
3500 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003501 /* also complete "None" */
3502 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 arg = skipwhite(skiptowhite(arg));
3504 if (*arg != NUL)
3505 {
3506 xp->xp_context = EXPAND_NOTHING;
3507 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3508 }
3509 }
3510 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513/*
3514 * All completion for the +cmdline_compl feature goes here.
3515 */
3516
3517# ifdef FEAT_USR_CMDS
3518 case CMD_command:
3519 /* Check for attributes */
3520 while (*arg == '-')
3521 {
3522 arg++; /* Skip "-" */
3523 p = skiptowhite(arg);
3524 if (*p == NUL)
3525 {
3526 /* Cursor is still in the attribute */
3527 p = vim_strchr(arg, '=');
3528 if (p == NULL)
3529 {
3530 /* No "=", so complete attribute names */
3531 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3532 xp->xp_pattern = arg;
3533 return NULL;
3534 }
3535
3536 /* For the -complete and -nargs attributes, we complete
3537 * their arguments as well.
3538 */
3539 if (STRNICMP(arg, "complete", p - arg) == 0)
3540 {
3541 xp->xp_context = EXPAND_USER_COMPLETE;
3542 xp->xp_pattern = p + 1;
3543 return NULL;
3544 }
3545 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3546 {
3547 xp->xp_context = EXPAND_USER_NARGS;
3548 xp->xp_pattern = p + 1;
3549 return NULL;
3550 }
3551 return NULL;
3552 }
3553 arg = skipwhite(p);
3554 }
3555
3556 /* After the attributes comes the new command name */
3557 p = skiptowhite(arg);
3558 if (*p == NUL)
3559 {
3560 xp->xp_context = EXPAND_USER_COMMANDS;
3561 xp->xp_pattern = arg;
3562 break;
3563 }
3564
3565 /* And finally comes a normal command */
3566 return skipwhite(p);
3567
3568 case CMD_delcommand:
3569 xp->xp_context = EXPAND_USER_COMMANDS;
3570 xp->xp_pattern = arg;
3571 break;
3572# endif
3573
3574 case CMD_global:
3575 case CMD_vglobal:
3576 delim = *arg; /* get the delimiter */
3577 if (delim)
3578 ++arg; /* skip delimiter if there is one */
3579
3580 while (arg[0] != NUL && arg[0] != delim)
3581 {
3582 if (arg[0] == '\\' && arg[1] != NUL)
3583 ++arg;
3584 ++arg;
3585 }
3586 if (arg[0] != NUL)
3587 return arg + 1;
3588 break;
3589 case CMD_and:
3590 case CMD_substitute:
3591 delim = *arg;
3592 if (delim)
3593 {
3594 /* skip "from" part */
3595 ++arg;
3596 arg = skip_regexp(arg, delim, p_magic, NULL);
3597 }
3598 /* skip "to" part */
3599 while (arg[0] != NUL && arg[0] != delim)
3600 {
3601 if (arg[0] == '\\' && arg[1] != NUL)
3602 ++arg;
3603 ++arg;
3604 }
3605 if (arg[0] != NUL) /* skip delimiter */
3606 ++arg;
3607 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3608 ++arg;
3609 if (arg[0] != NUL)
3610 return arg;
3611 break;
3612 case CMD_isearch:
3613 case CMD_dsearch:
3614 case CMD_ilist:
3615 case CMD_dlist:
3616 case CMD_ijump:
3617 case CMD_psearch:
3618 case CMD_djump:
3619 case CMD_isplit:
3620 case CMD_dsplit:
3621 arg = skipwhite(skipdigits(arg)); /* skip count */
3622 if (*arg == '/') /* Match regexp, not just whole words */
3623 {
3624 for (++arg; *arg && *arg != '/'; arg++)
3625 if (*arg == '\\' && arg[1] != NUL)
3626 arg++;
3627 if (*arg)
3628 {
3629 arg = skipwhite(arg + 1);
3630
3631 /* Check for trailing illegal characters */
3632 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3633 xp->xp_context = EXPAND_NOTHING;
3634 else
3635 return arg;
3636 }
3637 }
3638 break;
3639#ifdef FEAT_AUTOCMD
3640 case CMD_autocmd:
3641 return set_context_in_autocmd(xp, arg, FALSE);
3642
3643 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003644 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 return set_context_in_autocmd(xp, arg, TRUE);
3646#endif
3647 case CMD_set:
3648 set_context_in_set_cmd(xp, arg, 0);
3649 break;
3650 case CMD_setglobal:
3651 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3652 break;
3653 case CMD_setlocal:
3654 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3655 break;
3656 case CMD_tag:
3657 case CMD_stag:
3658 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003659 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 case CMD_tselect:
3661 case CMD_stselect:
3662 case CMD_ptselect:
3663 case CMD_tjump:
3664 case CMD_stjump:
3665 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003666 if (*p_wop != NUL)
3667 xp->xp_context = EXPAND_TAGS_LISTFILES;
3668 else
3669 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 xp->xp_pattern = arg;
3671 break;
3672 case CMD_augroup:
3673 xp->xp_context = EXPAND_AUGROUP;
3674 xp->xp_pattern = arg;
3675 break;
3676#ifdef FEAT_SYN_HL
3677 case CMD_syntax:
3678 set_context_in_syntax_cmd(xp, arg);
3679 break;
3680#endif
3681#ifdef FEAT_EVAL
3682 case CMD_let:
3683 case CMD_if:
3684 case CMD_elseif:
3685 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003686 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 case CMD_echo:
3688 case CMD_echon:
3689 case CMD_execute:
3690 case CMD_echomsg:
3691 case CMD_echoerr:
3692 case CMD_call:
3693 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003694 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 break;
3696
3697 case CMD_unlet:
3698 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3699 arg = xp->xp_pattern + 1;
3700 xp->xp_context = EXPAND_USER_VARS;
3701 xp->xp_pattern = arg;
3702 break;
3703
3704 case CMD_function:
3705 case CMD_delfunction:
3706 xp->xp_context = EXPAND_USER_FUNC;
3707 xp->xp_pattern = arg;
3708 break;
3709
3710 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003711 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 break;
3713#endif
3714 case CMD_highlight:
3715 set_context_in_highlight_cmd(xp, arg);
3716 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003717#ifdef FEAT_CSCOPE
3718 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003719 case CMD_lcscope:
3720 case CMD_scscope:
3721 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003722 break;
3723#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003724#ifdef FEAT_SIGNS
3725 case CMD_sign:
3726 set_context_in_sign_cmd(xp, arg);
3727 break;
3728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729#ifdef FEAT_LISTCMDS
3730 case CMD_bdelete:
3731 case CMD_bwipeout:
3732 case CMD_bunload:
3733 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3734 arg = xp->xp_pattern + 1;
3735 /*FALLTHROUGH*/
3736 case CMD_buffer:
3737 case CMD_sbuffer:
3738 case CMD_checktime:
3739 xp->xp_context = EXPAND_BUFFERS;
3740 xp->xp_pattern = arg;
3741 break;
3742#endif
3743#ifdef FEAT_USR_CMDS
3744 case CMD_USER:
3745 case CMD_USER_BUF:
3746 if (compl != EXPAND_NOTHING)
3747 {
3748 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003749 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751# ifdef FEAT_MENU
3752 if (compl == EXPAND_MENUS)
3753 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3754# endif
3755 if (compl == EXPAND_COMMANDS)
3756 return arg;
3757 if (compl == EXPAND_MAPPINGS)
3758 return set_context_in_map_cmd(xp, (char_u *)"map",
3759 arg, forceit, FALSE, FALSE, CMD_map);
3760 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3761 arg = xp->xp_pattern + 1;
3762 xp->xp_pattern = arg;
3763 }
3764 xp->xp_context = compl;
3765 }
3766 break;
3767#endif
3768 case CMD_map: case CMD_noremap:
3769 case CMD_nmap: case CMD_nnoremap:
3770 case CMD_vmap: case CMD_vnoremap:
3771 case CMD_omap: case CMD_onoremap:
3772 case CMD_imap: case CMD_inoremap:
3773 case CMD_cmap: case CMD_cnoremap:
3774 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003775 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 case CMD_unmap:
3777 case CMD_nunmap:
3778 case CMD_vunmap:
3779 case CMD_ounmap:
3780 case CMD_iunmap:
3781 case CMD_cunmap:
3782 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003783 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 case CMD_abbreviate: case CMD_noreabbrev:
3785 case CMD_cabbrev: case CMD_cnoreabbrev:
3786 case CMD_iabbrev: case CMD_inoreabbrev:
3787 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003788 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 case CMD_unabbreviate:
3790 case CMD_cunabbrev:
3791 case CMD_iunabbrev:
3792 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003793 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794#ifdef FEAT_MENU
3795 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3796 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3797 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3798 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3799 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3800 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3801 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3802 case CMD_tmenu: case CMD_tunmenu:
3803 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3804 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3805#endif
3806
3807 case CMD_colorscheme:
3808 xp->xp_context = EXPAND_COLORS;
3809 xp->xp_pattern = arg;
3810 break;
3811
3812 case CMD_compiler:
3813 xp->xp_context = EXPAND_COMPILER;
3814 xp->xp_pattern = arg;
3815 break;
3816
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003817 case CMD_ownsyntax:
3818 xp->xp_context = EXPAND_FILETYPE;
3819 xp->xp_pattern = arg;
3820 break;
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3823 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3824 case CMD_language:
3825 if (*skiptowhite(arg) == NUL)
3826 {
3827 xp->xp_context = EXPAND_LANGUAGE;
3828 xp->xp_pattern = arg;
3829 }
3830 else
3831 xp->xp_context = EXPAND_NOTHING;
3832 break;
3833#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003834#if defined(FEAT_PROFILE)
3835 case CMD_profile:
3836 set_context_in_profile_cmd(xp, arg);
3837 break;
3838#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003839 case CMD_behave:
3840 xp->xp_context = EXPAND_BEHAVE;
3841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843#endif /* FEAT_CMDL_COMPL */
3844
3845 default:
3846 break;
3847 }
3848 return NULL;
3849}
3850
3851/*
3852 * skip a range specifier of the form: addr [,addr] [;addr] ..
3853 *
3854 * Backslashed delimiters after / or ? will be skipped, and commands will
3855 * not be expanded between /'s and ?'s or after "'".
3856 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003857 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * Returns the "cmd" pointer advanced to beyond the range.
3859 */
3860 char_u *
3861skip_range(cmd, ctx)
3862 char_u *cmd;
3863 int *ctx; /* pointer to xp_context or NULL */
3864{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003865 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 if (*cmd == '\'')
3870 {
3871 if (*++cmd == NUL && ctx != NULL)
3872 *ctx = EXPAND_NOTHING;
3873 }
3874 else if (*cmd == '/' || *cmd == '?')
3875 {
3876 delim = *cmd++;
3877 while (*cmd != NUL && *cmd != delim)
3878 if (*cmd++ == '\\' && *cmd != NUL)
3879 ++cmd;
3880 if (*cmd == NUL && ctx != NULL)
3881 *ctx = EXPAND_NOTHING;
3882 }
3883 if (*cmd != NUL)
3884 ++cmd;
3885 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003886
3887 /* Skip ":" and white space. */
3888 while (*cmd == ':')
3889 cmd = skipwhite(cmd + 1);
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 return cmd;
3892}
3893
3894/*
3895 * get a single EX address
3896 *
3897 * Set ptr to the next character after the part that was interpreted.
3898 * Set ptr to NULL when an error is encountered.
3899 *
3900 * Return MAXLNUM when no Ex address was found.
3901 */
3902 static linenr_T
3903get_address(ptr, skip, to_other_file)
3904 char_u **ptr;
3905 int skip; /* only skip the address, don't use it */
3906 int to_other_file; /* flag: may jump to other file */
3907{
3908 int c;
3909 int i;
3910 long n;
3911 char_u *cmd;
3912 pos_T pos;
3913 pos_T *fp;
3914 linenr_T lnum;
3915
3916 cmd = skipwhite(*ptr);
3917 lnum = MAXLNUM;
3918 do
3919 {
3920 switch (*cmd)
3921 {
3922 case '.': /* '.' - Cursor position */
3923 ++cmd;
3924 lnum = curwin->w_cursor.lnum;
3925 break;
3926
3927 case '$': /* '$' - last line */
3928 ++cmd;
3929 lnum = curbuf->b_ml.ml_line_count;
3930 break;
3931
3932 case '\'': /* ''' - mark */
3933 if (*++cmd == NUL)
3934 {
3935 cmd = NULL;
3936 goto error;
3937 }
3938 if (skip)
3939 ++cmd;
3940 else
3941 {
3942 /* Only accept a mark in another file when it is
3943 * used by itself: ":'M". */
3944 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3945 ++cmd;
3946 if (fp == (pos_T *)-1)
3947 /* Jumped to another file. */
3948 lnum = curwin->w_cursor.lnum;
3949 else
3950 {
3951 if (check_mark(fp) == FAIL)
3952 {
3953 cmd = NULL;
3954 goto error;
3955 }
3956 lnum = fp->lnum;
3957 }
3958 }
3959 break;
3960
3961 case '/':
3962 case '?': /* '/' or '?' - search */
3963 c = *cmd++;
3964 if (skip) /* skip "/pat/" */
3965 {
3966 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3967 if (*cmd == c)
3968 ++cmd;
3969 }
3970 else
3971 {
3972 pos = curwin->w_cursor; /* save curwin->w_cursor */
3973 /*
3974 * When '/' or '?' follows another address, start
3975 * from there.
3976 */
3977 if (lnum != MAXLNUM)
3978 curwin->w_cursor.lnum = lnum;
3979 /*
3980 * Start a forward search at the end of the line.
3981 * Start a backward search at the start of the line.
3982 * This makes sure we never match in the current
3983 * line, and can match anywhere in the
3984 * next/previous line.
3985 */
3986 if (c == '/')
3987 curwin->w_cursor.col = MAXCOL;
3988 else
3989 curwin->w_cursor.col = 0;
3990 searchcmdlen = 0;
3991 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003992 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
3994 curwin->w_cursor = pos;
3995 cmd = NULL;
3996 goto error;
3997 }
3998 lnum = curwin->w_cursor.lnum;
3999 curwin->w_cursor = pos;
4000 /* adjust command string pointer */
4001 cmd += searchcmdlen;
4002 }
4003 break;
4004
4005 case '\\': /* "\?", "\/" or "\&", repeat search */
4006 ++cmd;
4007 if (*cmd == '&')
4008 i = RE_SUBST;
4009 else if (*cmd == '?' || *cmd == '/')
4010 i = RE_SEARCH;
4011 else
4012 {
4013 EMSG(_(e_backslash));
4014 cmd = NULL;
4015 goto error;
4016 }
4017
4018 if (!skip)
4019 {
4020 /*
4021 * When search follows another address, start from
4022 * there.
4023 */
4024 if (lnum != MAXLNUM)
4025 pos.lnum = lnum;
4026 else
4027 pos.lnum = curwin->w_cursor.lnum;
4028
4029 /*
4030 * Start the search just like for the above
4031 * do_search().
4032 */
4033 if (*cmd != '?')
4034 pos.col = MAXCOL;
4035 else
4036 pos.col = 0;
4037 if (searchit(curwin, curbuf, &pos,
4038 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004039 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004040 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 lnum = pos.lnum;
4042 else
4043 {
4044 cmd = NULL;
4045 goto error;
4046 }
4047 }
4048 ++cmd;
4049 break;
4050
4051 default:
4052 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4053 lnum = getdigits(&cmd);
4054 }
4055
4056 for (;;)
4057 {
4058 cmd = skipwhite(cmd);
4059 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4060 break;
4061
4062 if (lnum == MAXLNUM)
4063 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4064 if (VIM_ISDIGIT(*cmd))
4065 i = '+'; /* "number" is same as "+number" */
4066 else
4067 i = *cmd++;
4068 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4069 n = 1;
4070 else
4071 n = getdigits(&cmd);
4072 if (i == '-')
4073 lnum -= n;
4074 else
4075 lnum += n;
4076 }
4077 } while (*cmd == '/' || *cmd == '?');
4078
4079error:
4080 *ptr = cmd;
4081 return lnum;
4082}
4083
4084/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004085 * Get flags from an Ex command argument.
4086 */
4087 static void
4088get_flags(eap)
4089 exarg_T *eap;
4090{
4091 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4092 {
4093 if (*eap->arg == 'l')
4094 eap->flags |= EXFLAG_LIST;
4095 else if (*eap->arg == 'p')
4096 eap->flags |= EXFLAG_PRINT;
4097 else
4098 eap->flags |= EXFLAG_NR;
4099 eap->arg = skipwhite(eap->arg + 1);
4100 }
4101}
4102
4103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * Function called for command which is Not Implemented. NI!
4105 */
4106 void
4107ex_ni(eap)
4108 exarg_T *eap;
4109{
4110 if (!eap->skip)
4111 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4112}
4113
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004114#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115/*
4116 * Function called for script command which is Not Implemented. NI!
4117 * Skips over ":perl <<EOF" constructs.
4118 */
4119 static void
4120ex_script_ni(eap)
4121 exarg_T *eap;
4122{
4123 if (!eap->skip)
4124 ex_ni(eap);
4125 else
4126 vim_free(script_get(eap, eap->arg));
4127}
4128#endif
4129
4130/*
4131 * Check range in Ex command for validity.
4132 * Return NULL when valid, error message when invalid.
4133 */
4134 static char_u *
4135invalid_range(eap)
4136 exarg_T *eap;
4137{
4138 if ( eap->line1 < 0
4139 || eap->line2 < 0
4140 || eap->line1 > eap->line2
4141 || ((eap->argt & RANGE)
4142 && !(eap->argt & NOTADR)
4143 && eap->line2 > curbuf->b_ml.ml_line_count
4144#ifdef FEAT_DIFF
4145 + (eap->cmdidx == CMD_diffget)
4146#endif
4147 ))
4148 return (char_u *)_(e_invrange);
4149 return NULL;
4150}
4151
4152/*
4153 * Correct the range for zero line number, if required.
4154 */
4155 static void
4156correct_range(eap)
4157 exarg_T *eap;
4158{
4159 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4160 {
4161 if (eap->line1 == 0)
4162 eap->line1 = 1;
4163 if (eap->line2 == 0)
4164 eap->line2 = 1;
4165 }
4166}
4167
Bram Moolenaar748bf032005-02-02 23:04:36 +00004168#ifdef FEAT_QUICKFIX
4169static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4170
4171/*
4172 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4173 * pattern. Otherwise return eap->arg.
4174 */
4175 static char_u *
4176skip_grep_pat(eap)
4177 exarg_T *eap;
4178{
4179 char_u *p = eap->arg;
4180
Bram Moolenaara37420f2006-02-04 22:37:47 +00004181 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4182 || eap->cmdidx == CMD_vimgrepadd
4183 || eap->cmdidx == CMD_lvimgrepadd
4184 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004185 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004187 if (p == NULL)
4188 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004189 }
4190 return p;
4191}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004192
4193/*
4194 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4195 * in the command line, so that things like % get expanded.
4196 */
4197 static char_u *
4198replace_makeprg(eap, p, cmdlinep)
4199 exarg_T *eap;
4200 char_u *p;
4201 char_u **cmdlinep;
4202{
4203 char_u *new_cmdline;
4204 char_u *program;
4205 char_u *pos;
4206 char_u *ptr;
4207 int len;
4208 int i;
4209
4210 /*
4211 * Don't do it when ":vimgrep" is used for ":grep".
4212 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004213 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4214 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4215 || eap->cmdidx == CMD_grepadd
4216 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004217 && !grep_internal(eap->cmdidx))
4218 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004219 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4220 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004221 {
4222 if (*curbuf->b_p_gp == NUL)
4223 program = p_gp;
4224 else
4225 program = curbuf->b_p_gp;
4226 }
4227 else
4228 {
4229 if (*curbuf->b_p_mp == NUL)
4230 program = p_mp;
4231 else
4232 program = curbuf->b_p_mp;
4233 }
4234
4235 p = skipwhite(p);
4236
4237 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4238 {
4239 /* replace $* by given arguments */
4240 i = 1;
4241 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4242 ++i;
4243 len = (int)STRLEN(p);
4244 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4245 if (new_cmdline == NULL)
4246 return NULL; /* out of memory */
4247 ptr = new_cmdline;
4248 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4249 {
4250 i = (int)(pos - program);
4251 STRNCPY(ptr, program, i);
4252 STRCPY(ptr += i, p);
4253 ptr += len;
4254 program = pos + 2;
4255 }
4256 STRCPY(ptr, program);
4257 }
4258 else
4259 {
4260 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4261 if (new_cmdline == NULL)
4262 return NULL; /* out of memory */
4263 STRCPY(new_cmdline, program);
4264 STRCAT(new_cmdline, " ");
4265 STRCAT(new_cmdline, p);
4266 }
4267 msg_make(p);
4268
4269 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4270 vim_free(*cmdlinep);
4271 *cmdlinep = new_cmdline;
4272 p = new_cmdline;
4273 }
4274 return p;
4275}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004276#endif
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278/*
4279 * Expand file name in Ex command argument.
4280 * Return FAIL for failure, OK otherwise.
4281 */
4282 int
4283expand_filename(eap, cmdlinep, errormsgp)
4284 exarg_T *eap;
4285 char_u **cmdlinep;
4286 char_u **errormsgp;
4287{
4288 int has_wildcards; /* need to expand wildcards */
4289 char_u *repl;
4290 int srclen;
4291 char_u *p;
4292 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004293 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
Bram Moolenaar748bf032005-02-02 23:04:36 +00004295#ifdef FEAT_QUICKFIX
4296 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4297 p = skip_grep_pat(eap);
4298#else
4299 p = eap->arg;
4300#endif
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 /*
4303 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4304 * the file name contains a wildcard it should not cause expanding.
4305 * (it will be expanded anyway if there is a wildcard before replacing).
4306 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004307 has_wildcards = mch_has_wildcard(p);
4308 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004310#ifdef FEAT_EVAL
4311 /* Skip over `=expr`, wildcards in it are not expanded. */
4312 if (p[0] == '`' && p[1] == '=')
4313 {
4314 p += 2;
4315 (void)skip_expr(&p);
4316 if (*p == '`')
4317 ++p;
4318 continue;
4319 }
4320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 /*
4322 * Quick check if this cannot be the start of a special string.
4323 * Also removes backslash before '%', '#' and '<'.
4324 */
4325 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4326 {
4327 ++p;
4328 continue;
4329 }
4330
4331 /*
4332 * Try to find a match at this position.
4333 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004334 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4335 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (*errormsgp != NULL) /* error detected */
4337 return FAIL;
4338 if (repl == NULL) /* no match found */
4339 {
4340 p += srclen;
4341 continue;
4342 }
4343
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004344 /* Wildcards won't be expanded below, the replacement is taken
4345 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4346 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4347 {
4348 char_u *l = repl;
4349
4350 repl = expand_env_save(repl);
4351 vim_free(l);
4352 }
4353
Bram Moolenaar63b92542007-03-27 14:57:09 +00004354 /* Need to escape white space et al. with a backslash.
4355 * Don't do this for:
4356 * - replacement that already has been escaped: "##"
4357 * - shell commands (may have to use quotes instead).
4358 * - non-unix systems when there is a single argument (spaces don't
4359 * separate arguments then).
4360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004362 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 && eap->cmdidx != CMD_bang
4364 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004365 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004367 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004369 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#ifndef UNIX
4371 && !(eap->argt & NOSPC)
4372#endif
4373 )
4374 {
4375 char_u *l;
4376#ifdef BACKSLASH_IN_FILENAME
4377 /* Don't escape a backslash here, because rem_backslash() doesn't
4378 * remove it later. */
4379 static char_u *nobslash = (char_u *)" \t\"|";
4380# define ESCAPE_CHARS nobslash
4381#else
4382# define ESCAPE_CHARS escape_chars
4383#endif
4384
4385 for (l = repl; *l; ++l)
4386 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4387 {
4388 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4389 if (l != NULL)
4390 {
4391 vim_free(repl);
4392 repl = l;
4393 }
4394 break;
4395 }
4396 }
4397
4398 /* For a shell command a '!' must be escaped. */
4399 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004400 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 char_u *l;
4403
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004404 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (l != NULL)
4406 {
4407 vim_free(repl);
4408 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004409 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (strstr((char *)p_sh, "sh") != NULL)
4411 {
4412 l = vim_strsave_escaped(repl, (char_u *)"!");
4413 if (l != NULL)
4414 {
4415 vim_free(repl);
4416 repl = l;
4417 }
4418 }
4419 }
4420 }
4421
4422 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4423 vim_free(repl);
4424 if (p == NULL)
4425 return FAIL;
4426 }
4427
4428 /*
4429 * One file argument: Expand wildcards.
4430 * Don't do this with ":r !command" or ":w !command".
4431 */
4432 if ((eap->argt & NOSPC) && !eap->usefilter)
4433 {
4434 /*
4435 * May do this twice:
4436 * 1. Replace environment variables.
4437 * 2. Replace any other wildcards, remove backslashes.
4438 */
4439 for (n = 1; n <= 2; ++n)
4440 {
4441 if (n == 2)
4442 {
4443#ifdef UNIX
4444 /*
4445 * Only for Unix we check for more than one file name.
4446 * For other systems spaces are considered to be part
4447 * of the file name.
4448 * Only check here if there is no wildcard, otherwise
4449 * ExpandOne() will check for errors. This allows
4450 * ":e `ls ve*.c`" on Unix.
4451 */
4452 if (!has_wildcards)
4453 for (p = eap->arg; *p; ++p)
4454 {
4455 /* skip escaped characters */
4456 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4457 ++p;
4458 else if (vim_iswhite(*p))
4459 {
4460 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4461 return FAIL;
4462 }
4463 }
4464#endif
4465
4466 /*
4467 * Halve the number of backslashes (this is Vi compatible).
4468 * For Unix and OS/2, when wildcards are expanded, this is
4469 * done by ExpandOne() below.
4470 */
4471#if defined(UNIX) || defined(OS2)
4472 if (!has_wildcards)
4473#endif
4474 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476
4477 if (has_wildcards)
4478 {
4479 if (n == 1)
4480 {
4481 /*
4482 * First loop: May expand environment variables. This
4483 * can be done much faster with expand_env() than with
4484 * something else (e.g., calling a shell).
4485 * After expanding environment variables, check again
4486 * if there are still wildcards present.
4487 */
4488 if (vim_strchr(eap->arg, '$') != NULL
4489 || vim_strchr(eap->arg, '~') != NULL)
4490 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004491 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004492 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 has_wildcards = mch_has_wildcard(NameBuff);
4494 p = NameBuff;
4495 }
4496 else
4497 p = NULL;
4498 }
4499 else /* n == 2 */
4500 {
4501 expand_T xpc;
4502
4503 ExpandInit(&xpc);
4504 xpc.xp_context = EXPAND_FILES;
4505 p = ExpandOne(&xpc, eap->arg, NULL,
4506 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4507 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (p == NULL)
4509 return FAIL;
4510 }
4511 if (p != NULL)
4512 {
4513 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4514 p, cmdlinep);
4515 if (n == 2) /* p came from ExpandOne() */
4516 vim_free(p);
4517 }
4518 }
4519 }
4520 }
4521 return OK;
4522}
4523
4524/*
4525 * Replace part of the command line, keeping eap->cmd, eap->arg and
4526 * eap->nextcmd correct.
4527 * "src" points to the part that is to be replaced, of length "srclen".
4528 * "repl" is the replacement string.
4529 * Returns a pointer to the character after the replaced string.
4530 * Returns NULL for failure.
4531 */
4532 static char_u *
4533repl_cmdline(eap, src, srclen, repl, cmdlinep)
4534 exarg_T *eap;
4535 char_u *src;
4536 int srclen;
4537 char_u *repl;
4538 char_u **cmdlinep;
4539{
4540 int len;
4541 int i;
4542 char_u *new_cmdline;
4543
4544 /*
4545 * The new command line is build in new_cmdline[].
4546 * First allocate it.
4547 * Careful: a "+cmd" argument may have been NUL terminated.
4548 */
4549 len = (int)STRLEN(repl);
4550 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004551 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4553 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4554 return NULL; /* out of memory! */
4555
4556 /*
4557 * Copy the stuff before the expanded part.
4558 * Copy the expanded stuff.
4559 * Copy what came after the expanded part.
4560 * Copy the next commands, if there are any.
4561 */
4562 i = (int)(src - *cmdlinep); /* length of part before match */
4563 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 mch_memmove(new_cmdline + i, repl, (size_t)len);
4566 i += len; /* remember the end of the string */
4567 STRCPY(new_cmdline + i, src + srclen);
4568 src = new_cmdline + i; /* remember where to continue */
4569
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004570 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 i = (int)STRLEN(new_cmdline) + 1;
4573 STRCPY(new_cmdline + i, eap->nextcmd);
4574 eap->nextcmd = new_cmdline + i;
4575 }
4576 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4577 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4578 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4579 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4580 vim_free(*cmdlinep);
4581 *cmdlinep = new_cmdline;
4582
4583 return src;
4584}
4585
4586/*
4587 * Check for '|' to separate commands and '"' to start comments.
4588 */
4589 void
4590separate_nextcmd(eap)
4591 exarg_T *eap;
4592{
4593 char_u *p;
4594
Bram Moolenaar86b68352004-12-27 21:59:20 +00004595#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004596 p = skip_grep_pat(eap);
4597#else
4598 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004599#endif
4600
4601 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
4603 if (*p == Ctrl_V)
4604 {
4605 if (eap->argt & (USECTRLV | XFILE))
4606 ++p; /* skip CTRL-V and next char */
4607 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004608 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004609 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (*p == NUL) /* stop at NUL after CTRL-V */
4611 break;
4612 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004613
4614#ifdef FEAT_EVAL
4615 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004616 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004617 {
4618 p += 2;
4619 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004620 }
4621#endif
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 /* Check for '"': start of comment or '|': next command */
4624 /* :@" and :*" do not start a comment!
4625 * :redir @" doesn't either. */
4626 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4627 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4628 || p != eap->arg)
4629 && (eap->cmdidx != CMD_redir
4630 || p != eap->arg + 1 || p[-1] != '@'))
4631 || *p == '|' || *p == '\n')
4632 {
4633 /*
4634 * We remove the '\' before the '|', unless USECTRLV is used
4635 * AND 'b' is present in 'cpoptions'.
4636 */
4637 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4638 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4639 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004640 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 --p;
4642 }
4643 else
4644 {
4645 eap->nextcmd = check_nextcmd(p);
4646 *p = NUL;
4647 break;
4648 }
4649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4653 del_trailing_spaces(eap->arg);
4654}
4655
4656/*
4657 * get + command from ex argument
4658 */
4659 static char_u *
4660getargcmd(argp)
4661 char_u **argp;
4662{
4663 char_u *arg = *argp;
4664 char_u *command = NULL;
4665
4666 if (*arg == '+') /* +[command] */
4667 {
4668 ++arg;
4669 if (vim_isspace(*arg))
4670 command = dollar_command;
4671 else
4672 {
4673 command = arg;
4674 arg = skip_cmd_arg(command, TRUE);
4675 if (*arg != NUL)
4676 *arg++ = NUL; /* terminate command with NUL */
4677 }
4678
4679 arg = skipwhite(arg); /* skip over spaces */
4680 *argp = arg;
4681 }
4682 return command;
4683}
4684
4685/*
4686 * Find end of "+command" argument. Skip over "\ " and "\\".
4687 */
4688 static char_u *
4689skip_cmd_arg(p, rembs)
4690 char_u *p;
4691 int rembs; /* TRUE to halve the number of backslashes */
4692{
4693 while (*p && !vim_isspace(*p))
4694 {
4695 if (*p == '\\' && p[1] != NUL)
4696 {
4697 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004698 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 else
4700 ++p;
4701 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004702 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 return p;
4705}
4706
4707/*
4708 * Get "++opt=arg" argument.
4709 * Return FAIL or OK.
4710 */
4711 static int
4712getargopt(eap)
4713 exarg_T *eap;
4714{
4715 char_u *arg = eap->arg + 2;
4716 int *pp = NULL;
4717#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004718 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 char_u *p;
4720#endif
4721
4722 /* ":edit ++[no]bin[ary] file" */
4723 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4724 {
4725 if (*arg == 'n')
4726 {
4727 arg += 2;
4728 eap->force_bin = FORCE_NOBIN;
4729 }
4730 else
4731 eap->force_bin = FORCE_BIN;
4732 if (!checkforcmd(&arg, "binary", 3))
4733 return FAIL;
4734 eap->arg = skipwhite(arg);
4735 return OK;
4736 }
4737
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004738 /* ":read ++edit file" */
4739 if (STRNCMP(arg, "edit", 4) == 0)
4740 {
4741 eap->read_edit = TRUE;
4742 eap->arg = skipwhite(arg + 4);
4743 return OK;
4744 }
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (STRNCMP(arg, "ff", 2) == 0)
4747 {
4748 arg += 2;
4749 pp = &eap->force_ff;
4750 }
4751 else if (STRNCMP(arg, "fileformat", 10) == 0)
4752 {
4753 arg += 10;
4754 pp = &eap->force_ff;
4755 }
4756#ifdef FEAT_MBYTE
4757 else if (STRNCMP(arg, "enc", 3) == 0)
4758 {
4759 arg += 3;
4760 pp = &eap->force_enc;
4761 }
4762 else if (STRNCMP(arg, "encoding", 8) == 0)
4763 {
4764 arg += 8;
4765 pp = &eap->force_enc;
4766 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004767 else if (STRNCMP(arg, "bad", 3) == 0)
4768 {
4769 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004770 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772#endif
4773
4774 if (pp == NULL || *arg != '=')
4775 return FAIL;
4776
4777 ++arg;
4778 *pp = (int)(arg - eap->cmd);
4779 arg = skip_cmd_arg(arg, FALSE);
4780 eap->arg = skipwhite(arg);
4781 *arg = NUL;
4782
4783#ifdef FEAT_MBYTE
4784 if (pp == &eap->force_ff)
4785 {
4786#endif
4787 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4788 return FAIL;
4789#ifdef FEAT_MBYTE
4790 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004791 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793 /* Make 'fileencoding' lower case. */
4794 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4795 *p = TOLOWER_ASC(*p);
4796 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004797 else
4798 {
4799 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4800 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004801 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004802 if (STRICMP(p, "keep") == 0)
4803 eap->bad_char = BAD_KEEP;
4804 else if (STRICMP(p, "drop") == 0)
4805 eap->bad_char = BAD_DROP;
4806 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4807 eap->bad_char = *p;
4808 else
4809 return FAIL;
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#endif
4812
4813 return OK;
4814}
4815
4816/*
4817 * ":abbreviate" and friends.
4818 */
4819 static void
4820ex_abbreviate(eap)
4821 exarg_T *eap;
4822{
4823 do_exmap(eap, TRUE); /* almost the same as mapping */
4824}
4825
4826/*
4827 * ":map" and friends.
4828 */
4829 static void
4830ex_map(eap)
4831 exarg_T *eap;
4832{
4833 /*
4834 * If we are sourcing .exrc or .vimrc in current directory we
4835 * print the mappings for security reasons.
4836 */
4837 if (secure)
4838 {
4839 secure = 2;
4840 msg_outtrans(eap->cmd);
4841 msg_putchar('\n');
4842 }
4843 do_exmap(eap, FALSE);
4844}
4845
4846/*
4847 * ":unmap" and friends.
4848 */
4849 static void
4850ex_unmap(eap)
4851 exarg_T *eap;
4852{
4853 do_exmap(eap, FALSE);
4854}
4855
4856/*
4857 * ":mapclear" and friends.
4858 */
4859 static void
4860ex_mapclear(eap)
4861 exarg_T *eap;
4862{
4863 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4864}
4865
4866/*
4867 * ":abclear" and friends.
4868 */
4869 static void
4870ex_abclear(eap)
4871 exarg_T *eap;
4872{
4873 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4874}
4875
4876#ifdef FEAT_AUTOCMD
4877 static void
4878ex_autocmd(eap)
4879 exarg_T *eap;
4880{
4881 /*
4882 * Disallow auto commands from .exrc and .vimrc in current
4883 * directory for security reasons.
4884 */
4885 if (secure)
4886 {
4887 secure = 2;
4888 eap->errmsg = e_curdir;
4889 }
4890 else if (eap->cmdidx == CMD_autocmd)
4891 do_autocmd(eap->arg, eap->forceit);
4892 else
4893 do_augroup(eap->arg, eap->forceit);
4894}
4895
4896/*
4897 * ":doautocmd": Apply the automatic commands to the current buffer.
4898 */
4899 static void
4900ex_doautocmd(eap)
4901 exarg_T *eap;
4902{
4903 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004904 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905}
4906#endif
4907
4908#ifdef FEAT_LISTCMDS
4909/*
4910 * :[N]bunload[!] [N] [bufname] unload buffer
4911 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4912 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4913 */
4914 static void
4915ex_bunload(eap)
4916 exarg_T *eap;
4917{
4918 eap->errmsg = do_bufdel(
4919 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4920 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4921 : DOBUF_UNLOAD, eap->arg,
4922 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4923}
4924
4925/*
4926 * :[N]buffer [N] to buffer N
4927 * :[N]sbuffer [N] to buffer N
4928 */
4929 static void
4930ex_buffer(eap)
4931 exarg_T *eap;
4932{
4933 if (*eap->arg)
4934 eap->errmsg = e_trailing;
4935 else
4936 {
4937 if (eap->addr_count == 0) /* default is current buffer */
4938 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4939 else
4940 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4941 }
4942}
4943
4944/*
4945 * :[N]bmodified [N] to next mod. buffer
4946 * :[N]sbmodified [N] to next mod. buffer
4947 */
4948 static void
4949ex_bmodified(eap)
4950 exarg_T *eap;
4951{
4952 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4953}
4954
4955/*
4956 * :[N]bnext [N] to next buffer
4957 * :[N]sbnext [N] split and to next buffer
4958 */
4959 static void
4960ex_bnext(eap)
4961 exarg_T *eap;
4962{
4963 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4964}
4965
4966/*
4967 * :[N]bNext [N] to previous buffer
4968 * :[N]bprevious [N] to previous buffer
4969 * :[N]sbNext [N] split and to previous buffer
4970 * :[N]sbprevious [N] split and to previous buffer
4971 */
4972 static void
4973ex_bprevious(eap)
4974 exarg_T *eap;
4975{
4976 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4977}
4978
4979/*
4980 * :brewind to first buffer
4981 * :bfirst to first buffer
4982 * :sbrewind split and to first buffer
4983 * :sbfirst split and to first buffer
4984 */
4985 static void
4986ex_brewind(eap)
4987 exarg_T *eap;
4988{
4989 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4990}
4991
4992/*
4993 * :blast to last buffer
4994 * :sblast split and to last buffer
4995 */
4996 static void
4997ex_blast(eap)
4998 exarg_T *eap;
4999{
5000 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5001}
5002#endif
5003
5004 int
5005ends_excmd(c)
5006 int c;
5007{
5008 return (c == NUL || c == '|' || c == '"' || c == '\n');
5009}
5010
5011#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5012 || defined(PROTO)
5013/*
5014 * Return the next command, after the first '|' or '\n'.
5015 * Return NULL if not found.
5016 */
5017 char_u *
5018find_nextcmd(p)
5019 char_u *p;
5020{
5021 while (*p != '|' && *p != '\n')
5022 {
5023 if (*p == NUL)
5024 return NULL;
5025 ++p;
5026 }
5027 return (p + 1);
5028}
5029#endif
5030
5031/*
5032 * Check if *p is a separator between Ex commands.
5033 * Return NULL if it isn't, (p + 1) if it is.
5034 */
5035 char_u *
5036check_nextcmd(p)
5037 char_u *p;
5038{
5039 p = skipwhite(p);
5040 if (*p == '|' || *p == '\n')
5041 return (p + 1);
5042 else
5043 return NULL;
5044}
5045
5046/*
5047 * - if there are more files to edit
5048 * - and this is the last window
5049 * - and forceit not used
5050 * - and not repeated twice on a row
5051 * return FAIL and give error message if 'message' TRUE
5052 * return OK otherwise
5053 */
5054 static int
5055check_more(message, forceit)
5056 int message; /* when FALSE check only, no messages */
5057 int forceit;
5058{
5059 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5060
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005061 if (!forceit && only_one_window()
5062 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 if (message)
5065 {
5066#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5067 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5068 {
5069 char_u buff[IOSIZE];
5070
5071 if (n == 1)
5072 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5073 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005074 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 _("%d more files to edit. Quit anyway?"), n);
5076 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5077 return OK;
5078 return FAIL;
5079 }
5080#endif
5081 if (n == 1)
5082 EMSG(_("E173: 1 more file to edit"));
5083 else
5084 EMSGN(_("E173: %ld more files to edit"), n);
5085 quitmore = 2; /* next try to quit is allowed */
5086 }
5087 return FAIL;
5088 }
5089 return OK;
5090}
5091
5092#ifdef FEAT_CMDL_COMPL
5093/*
5094 * Function given to ExpandGeneric() to obtain the list of command names.
5095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 char_u *
5097get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005098 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 int idx;
5100{
5101 if (idx >= (int)CMD_SIZE)
5102# ifdef FEAT_USR_CMDS
5103 return get_user_command_name(idx);
5104# else
5105 return NULL;
5106# endif
5107 return cmdnames[idx].cmd_name;
5108}
5109#endif
5110
5111#if defined(FEAT_USR_CMDS) || defined(PROTO)
5112static 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));
5113static void uc_list __ARGS((char_u *name, size_t name_len));
5114static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5115static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5116static 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));
5117
5118 static int
5119uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5120 char_u *name;
5121 size_t name_len;
5122 char_u *rep;
5123 long argt;
5124 long def;
5125 int flags;
5126 int compl;
5127 char_u *compl_arg;
5128 int force;
5129{
5130 ucmd_T *cmd = NULL;
5131 char_u *p;
5132 int i;
5133 int cmp = 1;
5134 char_u *rep_buf = NULL;
5135 garray_T *gap;
5136
Bram Moolenaar9c102382006-05-03 21:26:49 +00005137 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (rep_buf == NULL)
5139 {
5140 /* Can't replace termcodes - try using the string as is */
5141 rep_buf = vim_strsave(rep);
5142
5143 /* Give up if out of memory */
5144 if (rep_buf == NULL)
5145 return FAIL;
5146 }
5147
5148 /* get address of growarray: global or in curbuf */
5149 if (flags & UC_BUFFER)
5150 {
5151 gap = &curbuf->b_ucmds;
5152 if (gap->ga_itemsize == 0)
5153 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5154 }
5155 else
5156 gap = &ucmds;
5157
5158 /* Search for the command in the already defined commands. */
5159 for (i = 0; i < gap->ga_len; ++i)
5160 {
5161 size_t len;
5162
5163 cmd = USER_CMD_GA(gap, i);
5164 len = STRLEN(cmd->uc_name);
5165 cmp = STRNCMP(name, cmd->uc_name, name_len);
5166 if (cmp == 0)
5167 {
5168 if (name_len < len)
5169 cmp = -1;
5170 else if (name_len > len)
5171 cmp = 1;
5172 }
5173
5174 if (cmp == 0)
5175 {
5176 if (!force)
5177 {
5178 EMSG(_("E174: Command already exists: add ! to replace it"));
5179 goto fail;
5180 }
5181
5182 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005183 cmd->uc_rep = NULL;
5184#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5185 vim_free(cmd->uc_compl_arg);
5186 cmd->uc_compl_arg = NULL;
5187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189 }
5190
5191 /* Stop as soon as we pass the name to add */
5192 if (cmp < 0)
5193 break;
5194 }
5195
5196 /* Extend the array unless we're replacing an existing command */
5197 if (cmp != 0)
5198 {
5199 if (ga_grow(gap, 1) != OK)
5200 goto fail;
5201 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5202 goto fail;
5203
5204 cmd = USER_CMD_GA(gap, i);
5205 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5206
5207 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 cmd->uc_name = p;
5210 }
5211
5212 cmd->uc_rep = rep_buf;
5213 cmd->uc_argt = argt;
5214 cmd->uc_def = def;
5215 cmd->uc_compl = compl;
5216#ifdef FEAT_EVAL
5217 cmd->uc_scriptID = current_SID;
5218# ifdef FEAT_CMDL_COMPL
5219 cmd->uc_compl_arg = compl_arg;
5220# endif
5221#endif
5222
5223 return OK;
5224
5225fail:
5226 vim_free(rep_buf);
5227#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5228 vim_free(compl_arg);
5229#endif
5230 return FAIL;
5231}
5232
5233/*
5234 * List of names for completion for ":command" with the EXPAND_ flag.
5235 * Must be alphabetical for completion.
5236 */
5237static struct
5238{
5239 int expand;
5240 char *name;
5241} command_complete[] =
5242{
5243 {EXPAND_AUGROUP, "augroup"},
5244 {EXPAND_BUFFERS, "buffer"},
5245 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005246#if defined(FEAT_CSCOPE)
5247 {EXPAND_CSCOPE, "cscope"},
5248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5250 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005251 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
5253 {EXPAND_DIRECTORIES, "dir"},
5254 {EXPAND_ENV_VARS, "environment"},
5255 {EXPAND_EVENTS, "event"},
5256 {EXPAND_EXPRESSION, "expression"},
5257 {EXPAND_FILES, "file"},
5258 {EXPAND_FUNCTIONS, "function"},
5259 {EXPAND_HELP, "help"},
5260 {EXPAND_HIGHLIGHT, "highlight"},
5261 {EXPAND_MAPPINGS, "mapping"},
5262 {EXPAND_MENUS, "menu"},
5263 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005264 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005265#if defined(FEAT_SIGNS)
5266 {EXPAND_SIGN, "sign"},
5267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {EXPAND_TAGS, "tag"},
5269 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5270 {EXPAND_USER_VARS, "var"},
5271 {0, NULL}
5272};
5273
5274 static void
5275uc_list(name, name_len)
5276 char_u *name;
5277 size_t name_len;
5278{
5279 int i, j;
5280 int found = FALSE;
5281 ucmd_T *cmd;
5282 int len;
5283 long a;
5284 garray_T *gap;
5285
5286 gap = &curbuf->b_ucmds;
5287 for (;;)
5288 {
5289 for (i = 0; i < gap->ga_len; ++i)
5290 {
5291 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005292 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 /* Skip commands which don't match the requested prefix */
5295 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5296 continue;
5297
5298 /* Put out the title first time */
5299 if (!found)
5300 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5301 found = TRUE;
5302 msg_putchar('\n');
5303 if (got_int)
5304 break;
5305
5306 /* Special cases */
5307 msg_putchar(a & BANG ? '!' : ' ');
5308 msg_putchar(a & REGSTR ? '"' : ' ');
5309 msg_putchar(gap != &ucmds ? 'b' : ' ');
5310 msg_putchar(' ');
5311
5312 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5313 len = (int)STRLEN(cmd->uc_name) + 4;
5314
5315 do {
5316 msg_putchar(' ');
5317 ++len;
5318 } while (len < 16);
5319
5320 len = 0;
5321
5322 /* Arguments */
5323 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5324 {
5325 case 0: IObuff[len++] = '0'; break;
5326 case (EXTRA): IObuff[len++] = '*'; break;
5327 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5328 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5329 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5330 }
5331
5332 do {
5333 IObuff[len++] = ' ';
5334 } while (len < 5);
5335
5336 /* Range */
5337 if (a & (RANGE|COUNT))
5338 {
5339 if (a & COUNT)
5340 {
5341 /* -count=N */
5342 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5343 len += (int)STRLEN(IObuff + len);
5344 }
5345 else if (a & DFLALL)
5346 IObuff[len++] = '%';
5347 else if (cmd->uc_def >= 0)
5348 {
5349 /* -range=N */
5350 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5351 len += (int)STRLEN(IObuff + len);
5352 }
5353 else
5354 IObuff[len++] = '.';
5355 }
5356
5357 do {
5358 IObuff[len++] = ' ';
5359 } while (len < 11);
5360
5361 /* Completion */
5362 for (j = 0; command_complete[j].expand != 0; ++j)
5363 if (command_complete[j].expand == cmd->uc_compl)
5364 {
5365 STRCPY(IObuff + len, command_complete[j].name);
5366 len += (int)STRLEN(IObuff + len);
5367 break;
5368 }
5369
5370 do {
5371 IObuff[len++] = ' ';
5372 } while (len < 21);
5373
5374 IObuff[len] = '\0';
5375 msg_outtrans(IObuff);
5376
5377 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005378#ifdef FEAT_EVAL
5379 if (p_verbose > 0)
5380 last_set_msg(cmd->uc_scriptID);
5381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 out_flush();
5383 ui_breakcheck();
5384 if (got_int)
5385 break;
5386 }
5387 if (gap == &ucmds || i < gap->ga_len)
5388 break;
5389 gap = &ucmds;
5390 }
5391
5392 if (!found)
5393 MSG(_("No user-defined commands found"));
5394}
5395
5396 static char_u *
5397uc_fun_cmd()
5398{
5399 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5400 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5401 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5402 0xb9, 0x7f, 0};
5403 int i;
5404
5405 for (i = 0; fcmd[i]; ++i)
5406 IObuff[i] = fcmd[i] - 0x40;
5407 IObuff[i] = 0;
5408 return IObuff;
5409}
5410
5411 static int
5412uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5413 char_u *attr;
5414 size_t len;
5415 long *argt;
5416 long *def;
5417 int *flags;
5418 int *compl;
5419 char_u **compl_arg;
5420{
5421 char_u *p;
5422
5423 if (len == 0)
5424 {
5425 EMSG(_("E175: No attribute specified"));
5426 return FAIL;
5427 }
5428
5429 /* First, try the simple attributes (no arguments) */
5430 if (STRNICMP(attr, "bang", len) == 0)
5431 *argt |= BANG;
5432 else if (STRNICMP(attr, "buffer", len) == 0)
5433 *flags |= UC_BUFFER;
5434 else if (STRNICMP(attr, "register", len) == 0)
5435 *argt |= REGSTR;
5436 else if (STRNICMP(attr, "bar", len) == 0)
5437 *argt |= TRLBAR;
5438 else
5439 {
5440 int i;
5441 char_u *val = NULL;
5442 size_t vallen = 0;
5443 size_t attrlen = len;
5444
5445 /* Look for the attribute name - which is the part before any '=' */
5446 for (i = 0; i < (int)len; ++i)
5447 {
5448 if (attr[i] == '=')
5449 {
5450 val = &attr[i + 1];
5451 vallen = len - i - 1;
5452 attrlen = i;
5453 break;
5454 }
5455 }
5456
5457 if (STRNICMP(attr, "nargs", attrlen) == 0)
5458 {
5459 if (vallen == 1)
5460 {
5461 if (*val == '0')
5462 /* Do nothing - this is the default */;
5463 else if (*val == '1')
5464 *argt |= (EXTRA | NOSPC | NEEDARG);
5465 else if (*val == '*')
5466 *argt |= EXTRA;
5467 else if (*val == '?')
5468 *argt |= (EXTRA | NOSPC);
5469 else if (*val == '+')
5470 *argt |= (EXTRA | NEEDARG);
5471 else
5472 goto wrong_nargs;
5473 }
5474 else
5475 {
5476wrong_nargs:
5477 EMSG(_("E176: Invalid number of arguments"));
5478 return FAIL;
5479 }
5480 }
5481 else if (STRNICMP(attr, "range", attrlen) == 0)
5482 {
5483 *argt |= RANGE;
5484 if (vallen == 1 && *val == '%')
5485 *argt |= DFLALL;
5486 else if (val != NULL)
5487 {
5488 p = val;
5489 if (*def >= 0)
5490 {
5491two_count:
5492 EMSG(_("E177: Count cannot be specified twice"));
5493 return FAIL;
5494 }
5495
5496 *def = getdigits(&p);
5497 *argt |= (ZEROR | NOTADR);
5498
5499 if (p != val + vallen || vallen == 0)
5500 {
5501invalid_count:
5502 EMSG(_("E178: Invalid default value for count"));
5503 return FAIL;
5504 }
5505 }
5506 }
5507 else if (STRNICMP(attr, "count", attrlen) == 0)
5508 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005509 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
5511 if (val != NULL)
5512 {
5513 p = val;
5514 if (*def >= 0)
5515 goto two_count;
5516
5517 *def = getdigits(&p);
5518
5519 if (p != val + vallen)
5520 goto invalid_count;
5521 }
5522
5523 if (*def < 0)
5524 *def = 0;
5525 }
5526 else if (STRNICMP(attr, "complete", attrlen) == 0)
5527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (val == NULL)
5529 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005530 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 return FAIL;
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005534 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5535 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538 else
5539 {
5540 char_u ch = attr[len];
5541 attr[len] = '\0';
5542 EMSG2(_("E181: Invalid attribute: %s"), attr);
5543 attr[len] = ch;
5544 return FAIL;
5545 }
5546 }
5547
5548 return OK;
5549}
5550
Bram Moolenaara850a712009-01-28 14:42:59 +00005551/*
5552 * ":command ..."
5553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 static void
5555ex_command(eap)
5556 exarg_T *eap;
5557{
5558 char_u *name;
5559 char_u *end;
5560 char_u *p;
5561 long argt = 0;
5562 long def = -1;
5563 int flags = 0;
5564 int compl = EXPAND_NOTHING;
5565 char_u *compl_arg = NULL;
5566 int has_attr = (eap->arg[0] == '-');
5567
5568 p = eap->arg;
5569
5570 /* Check for attributes */
5571 while (*p == '-')
5572 {
5573 ++p;
5574 end = skiptowhite(p);
5575 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5576 == FAIL)
5577 return;
5578 p = skipwhite(end);
5579 }
5580
5581 /* Get the name (if any) and skip to the following argument */
5582 name = p;
5583 if (ASCII_ISALPHA(*p))
5584 while (ASCII_ISALNUM(*p))
5585 ++p;
5586 if (!ends_excmd(*p) && !vim_iswhite(*p))
5587 {
5588 EMSG(_("E182: Invalid command name"));
5589 return;
5590 }
5591 end = p;
5592
5593 /* If there is nothing after the name, and no attributes were specified,
5594 * we are listing commands
5595 */
5596 p = skipwhite(end);
5597 if (!has_attr && ends_excmd(*p))
5598 {
5599 uc_list(name, end - name);
5600 }
5601 else if (!ASCII_ISUPPER(*name))
5602 {
5603 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5604 return;
5605 }
5606 else
5607 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5608 eap->forceit);
5609}
5610
5611/*
5612 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 * Clear all user commands, global and for current buffer.
5614 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005615 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005617 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 uc_clear(&ucmds);
5620 uc_clear(&curbuf->b_ucmds);
5621}
5622
5623/*
5624 * Clear all user commands for "gap".
5625 */
5626 void
5627uc_clear(gap)
5628 garray_T *gap;
5629{
5630 int i;
5631 ucmd_T *cmd;
5632
5633 for (i = 0; i < gap->ga_len; ++i)
5634 {
5635 cmd = USER_CMD_GA(gap, i);
5636 vim_free(cmd->uc_name);
5637 vim_free(cmd->uc_rep);
5638# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5639 vim_free(cmd->uc_compl_arg);
5640# endif
5641 }
5642 ga_clear(gap);
5643}
5644
5645 static void
5646ex_delcommand(eap)
5647 exarg_T *eap;
5648{
5649 int i = 0;
5650 ucmd_T *cmd = NULL;
5651 int cmp = -1;
5652 garray_T *gap;
5653
5654 gap = &curbuf->b_ucmds;
5655 for (;;)
5656 {
5657 for (i = 0; i < gap->ga_len; ++i)
5658 {
5659 cmd = USER_CMD_GA(gap, i);
5660 cmp = STRCMP(eap->arg, cmd->uc_name);
5661 if (cmp <= 0)
5662 break;
5663 }
5664 if (gap == &ucmds || cmp == 0)
5665 break;
5666 gap = &ucmds;
5667 }
5668
5669 if (cmp != 0)
5670 {
5671 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5672 return;
5673 }
5674
5675 vim_free(cmd->uc_name);
5676 vim_free(cmd->uc_rep);
5677# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5678 vim_free(cmd->uc_compl_arg);
5679# endif
5680
5681 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 if (i < gap->ga_len)
5684 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5685}
5686
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005687/*
5688 * split and quote args for <f-args>
5689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 static char_u *
5691uc_split_args(arg, lenp)
5692 char_u *arg;
5693 size_t *lenp;
5694{
5695 char_u *buf;
5696 char_u *p;
5697 char_u *q;
5698 int len;
5699
5700 /* Precalculate length */
5701 p = arg;
5702 len = 2; /* Initial and final quotes */
5703
5704 while (*p)
5705 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005706 if (p[0] == '\\' && p[1] == '\\')
5707 {
5708 len += 2;
5709 p += 2;
5710 }
5711 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 len += 1;
5714 p += 2;
5715 }
5716 else if (*p == '\\' || *p == '"')
5717 {
5718 len += 2;
5719 p += 1;
5720 }
5721 else if (vim_iswhite(*p))
5722 {
5723 p = skipwhite(p);
5724 if (*p == NUL)
5725 break;
5726 len += 3; /* "," */
5727 }
5728 else
5729 {
5730 ++len;
5731 ++p;
5732 }
5733 }
5734
5735 buf = alloc(len + 1);
5736 if (buf == NULL)
5737 {
5738 *lenp = 0;
5739 return buf;
5740 }
5741
5742 p = arg;
5743 q = buf;
5744 *q++ = '"';
5745 while (*p)
5746 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005747 if (p[0] == '\\' && p[1] == '\\')
5748 {
5749 *q++ = '\\';
5750 *q++ = '\\';
5751 p += 2;
5752 }
5753 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 *q++ = p[1];
5756 p += 2;
5757 }
5758 else if (*p == '\\' || *p == '"')
5759 {
5760 *q++ = '\\';
5761 *q++ = *p++;
5762 }
5763 else if (vim_iswhite(*p))
5764 {
5765 p = skipwhite(p);
5766 if (*p == NUL)
5767 break;
5768 *q++ = '"';
5769 *q++ = ',';
5770 *q++ = '"';
5771 }
5772 else
5773 {
5774 *q++ = *p++;
5775 }
5776 }
5777 *q++ = '"';
5778 *q = 0;
5779
5780 *lenp = len;
5781 return buf;
5782}
5783
5784/*
5785 * Check for a <> code in a user command.
5786 * "code" points to the '<'. "len" the length of the <> (inclusive).
5787 * "buf" is where the result is to be added.
5788 * "split_buf" points to a buffer used for splitting, caller should free it.
5789 * "split_len" is the length of what "split_buf" contains.
5790 * Returns the length of the replacement, which has been added to "buf".
5791 * Returns -1 if there was no match, and only the "<" has been copied.
5792 */
5793 static size_t
5794uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5795 char_u *code;
5796 size_t len;
5797 char_u *buf;
5798 ucmd_T *cmd; /* the user command we're expanding */
5799 exarg_T *eap; /* ex arguments */
5800 char_u **split_buf;
5801 size_t *split_len;
5802{
5803 size_t result = 0;
5804 char_u *p = code + 1;
5805 size_t l = len - 2;
5806 int quote = 0;
5807 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5808 ct_LT, ct_NONE } type = ct_NONE;
5809
5810 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5811 {
5812 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5813 p += 2;
5814 l -= 2;
5815 }
5816
Bram Moolenaar371d5402006-03-20 21:47:49 +00005817 ++l;
5818 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005820 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005822 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005824 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005826 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005828 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005830 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005832 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 type = ct_REGISTER;
5834
5835 switch (type)
5836 {
5837 case ct_ARGS:
5838 /* Simple case first */
5839 if (*eap->arg == NUL)
5840 {
5841 if (quote == 1)
5842 {
5843 result = 2;
5844 if (buf != NULL)
5845 STRCPY(buf, "''");
5846 }
5847 else
5848 result = 0;
5849 break;
5850 }
5851
5852 /* When specified there is a single argument don't split it.
5853 * Works for ":Cmd %" when % is "a b c". */
5854 if ((eap->argt & NOSPC) && quote == 2)
5855 quote = 1;
5856
5857 switch (quote)
5858 {
5859 case 0: /* No quoting, no splitting */
5860 result = STRLEN(eap->arg);
5861 if (buf != NULL)
5862 STRCPY(buf, eap->arg);
5863 break;
5864 case 1: /* Quote, but don't split */
5865 result = STRLEN(eap->arg) + 2;
5866 for (p = eap->arg; *p; ++p)
5867 {
5868 if (*p == '\\' || *p == '"')
5869 ++result;
5870 }
5871
5872 if (buf != NULL)
5873 {
5874 *buf++ = '"';
5875 for (p = eap->arg; *p; ++p)
5876 {
5877 if (*p == '\\' || *p == '"')
5878 *buf++ = '\\';
5879 *buf++ = *p;
5880 }
5881 *buf = '"';
5882 }
5883
5884 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005885 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 /* This is hard, so only do it once, and cache the result */
5887 if (*split_buf == NULL)
5888 *split_buf = uc_split_args(eap->arg, split_len);
5889
5890 result = *split_len;
5891 if (buf != NULL && result != 0)
5892 STRCPY(buf, *split_buf);
5893
5894 break;
5895 }
5896 break;
5897
5898 case ct_BANG:
5899 result = eap->forceit ? 1 : 0;
5900 if (quote)
5901 result += 2;
5902 if (buf != NULL)
5903 {
5904 if (quote)
5905 *buf++ = '"';
5906 if (eap->forceit)
5907 *buf++ = '!';
5908 if (quote)
5909 *buf = '"';
5910 }
5911 break;
5912
5913 case ct_LINE1:
5914 case ct_LINE2:
5915 case ct_COUNT:
5916 {
5917 char num_buf[20];
5918 long num = (type == ct_LINE1) ? eap->line1 :
5919 (type == ct_LINE2) ? eap->line2 :
5920 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5921 size_t num_len;
5922
5923 sprintf(num_buf, "%ld", num);
5924 num_len = STRLEN(num_buf);
5925 result = num_len;
5926
5927 if (quote)
5928 result += 2;
5929
5930 if (buf != NULL)
5931 {
5932 if (quote)
5933 *buf++ = '"';
5934 STRCPY(buf, num_buf);
5935 buf += num_len;
5936 if (quote)
5937 *buf = '"';
5938 }
5939
5940 break;
5941 }
5942
5943 case ct_REGISTER:
5944 result = eap->regname ? 1 : 0;
5945 if (quote)
5946 result += 2;
5947 if (buf != NULL)
5948 {
5949 if (quote)
5950 *buf++ = '\'';
5951 if (eap->regname)
5952 *buf++ = eap->regname;
5953 if (quote)
5954 *buf = '\'';
5955 }
5956 break;
5957
5958 case ct_LT:
5959 result = 1;
5960 if (buf != NULL)
5961 *buf = '<';
5962 break;
5963
5964 default:
5965 /* Not recognized: just copy the '<' and return -1. */
5966 result = (size_t)-1;
5967 if (buf != NULL)
5968 *buf = '<';
5969 break;
5970 }
5971
5972 return result;
5973}
5974
5975 static void
5976do_ucmd(eap)
5977 exarg_T *eap;
5978{
5979 char_u *buf;
5980 char_u *p;
5981 char_u *q;
5982
5983 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005984 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005985 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 size_t len, totlen;
5987
5988 size_t split_len = 0;
5989 char_u *split_buf = NULL;
5990 ucmd_T *cmd;
5991#ifdef FEAT_EVAL
5992 scid_T save_current_SID = current_SID;
5993#endif
5994
5995 if (eap->cmdidx == CMD_USER)
5996 cmd = USER_CMD(eap->useridx);
5997 else
5998 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5999
6000 /*
6001 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006002 * First round: "buf" is NULL, compute length, allocate "buf".
6003 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 buf = NULL;
6006 for (;;)
6007 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006008 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006009 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006011
6012 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006014 start = vim_strchr(p, '<');
6015 if (start != NULL)
6016 end = vim_strchr(start + 1, '>');
6017 if (buf != NULL)
6018 {
6019 ksp = vim_strchr(p, K_SPECIAL);
6020 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6021 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6022# ifdef FEAT_GUI
6023 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6024# endif
6025 ))
6026 {
6027 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6028 * KS_SPECIAL KE_FILLER, like for mappings, but
6029 * do_cmdline() doesn't handle that, so convert it back.
6030 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6031 len = ksp - p;
6032 if (len > 0)
6033 {
6034 mch_memmove(q, p, len);
6035 q += len;
6036 }
6037 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6038 p = ksp + 3;
6039 continue;
6040 }
6041 }
6042
6043 /* break if there no <item> is found */
6044 if (start == NULL || end == NULL)
6045 break;
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 /* Include the '>' */
6048 ++end;
6049
6050 /* Take everything up to the '<' */
6051 len = start - p;
6052 if (buf == NULL)
6053 totlen += len;
6054 else
6055 {
6056 mch_memmove(q, p, len);
6057 q += len;
6058 }
6059
6060 len = uc_check_code(start, end - start, q, cmd, eap,
6061 &split_buf, &split_len);
6062 if (len == (size_t)-1)
6063 {
6064 /* no match, continue after '<' */
6065 p = start + 1;
6066 len = 1;
6067 }
6068 else
6069 p = end;
6070 if (buf == NULL)
6071 totlen += len;
6072 else
6073 q += len;
6074 }
6075 if (buf != NULL) /* second time here, finished */
6076 {
6077 STRCPY(q, p);
6078 break;
6079 }
6080
6081 totlen += STRLEN(p); /* Add on the trailing characters */
6082 buf = alloc((unsigned)(totlen + 1));
6083 if (buf == NULL)
6084 {
6085 vim_free(split_buf);
6086 return;
6087 }
6088 }
6089
6090#ifdef FEAT_EVAL
6091 current_SID = cmd->uc_scriptID;
6092#endif
6093 (void)do_cmdline(buf, eap->getline, eap->cookie,
6094 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6095#ifdef FEAT_EVAL
6096 current_SID = save_current_SID;
6097#endif
6098 vim_free(buf);
6099 vim_free(split_buf);
6100}
6101
6102# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6103 static char_u *
6104get_user_command_name(idx)
6105 int idx;
6106{
6107 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6108}
6109
6110/*
6111 * Function given to ExpandGeneric() to obtain the list of user command names.
6112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 char_u *
6114get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006115 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int idx;
6117{
6118 if (idx < curbuf->b_ucmds.ga_len)
6119 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6120 idx -= curbuf->b_ucmds.ga_len;
6121 if (idx < ucmds.ga_len)
6122 return USER_CMD(idx)->uc_name;
6123 return NULL;
6124}
6125
6126/*
6127 * Function given to ExpandGeneric() to obtain the list of user command
6128 * attributes.
6129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 char_u *
6131get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006132 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 int idx;
6134{
6135 static char *user_cmd_flags[] =
6136 {"bang", "bar", "buffer", "complete", "count",
6137 "nargs", "range", "register"};
6138
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006139 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 return NULL;
6141 return (char_u *)user_cmd_flags[idx];
6142}
6143
6144/*
6145 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 char_u *
6148get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006149 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 int idx;
6151{
6152 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6153
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006154 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 return NULL;
6156 return (char_u *)user_cmd_nargs[idx];
6157}
6158
6159/*
6160 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 char_u *
6163get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006164 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 int idx;
6166{
6167 return (char_u *)command_complete[idx].name;
6168}
6169# endif /* FEAT_CMDL_COMPL */
6170
6171#endif /* FEAT_USR_CMDS */
6172
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006173#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6174/*
6175 * Parse a completion argument "value[vallen]".
6176 * The detected completion goes in "*complp", argument type in "*argt".
6177 * When there is an argument, for function and user defined completion, it's
6178 * copied to allocated memory and stored in "*compl_arg".
6179 * Returns FAIL if something is wrong.
6180 */
6181 int
6182parse_compl_arg(value, vallen, complp, argt, compl_arg)
6183 char_u *value;
6184 int vallen;
6185 int *complp;
6186 long *argt;
6187 char_u **compl_arg;
6188{
6189 char_u *arg = NULL;
6190 size_t arglen = 0;
6191 int i;
6192 int valend = vallen;
6193
6194 /* Look for any argument part - which is the part after any ',' */
6195 for (i = 0; i < vallen; ++i)
6196 {
6197 if (value[i] == ',')
6198 {
6199 arg = &value[i + 1];
6200 arglen = vallen - i - 1;
6201 valend = i;
6202 break;
6203 }
6204 }
6205
6206 for (i = 0; command_complete[i].expand != 0; ++i)
6207 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006208 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006209 && STRNCMP(value, command_complete[i].name, valend) == 0)
6210 {
6211 *complp = command_complete[i].expand;
6212 if (command_complete[i].expand == EXPAND_BUFFERS)
6213 *argt |= BUFNAME;
6214 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6215 || command_complete[i].expand == EXPAND_FILES)
6216 *argt |= XFILE;
6217 break;
6218 }
6219 }
6220
6221 if (command_complete[i].expand == 0)
6222 {
6223 EMSG2(_("E180: Invalid complete value: %s"), value);
6224 return FAIL;
6225 }
6226
6227# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6228 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6229 && arg != NULL)
6230# else
6231 if (arg != NULL)
6232# endif
6233 {
6234 EMSG(_("E468: Completion argument only allowed for custom completion"));
6235 return FAIL;
6236 }
6237
6238# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6239 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6240 && arg == NULL)
6241 {
6242 EMSG(_("E467: Custom completion requires a function argument"));
6243 return FAIL;
6244 }
6245
6246 if (arg != NULL)
6247 *compl_arg = vim_strnsave(arg, (int)arglen);
6248# endif
6249 return OK;
6250}
6251#endif
6252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 static void
6254ex_colorscheme(eap)
6255 exarg_T *eap;
6256{
Bram Moolenaare6850792010-05-14 15:28:44 +02006257 if (*eap->arg == NUL)
6258 {
6259#ifdef FEAT_EVAL
6260 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6261 char_u *p = NULL;
6262
6263 if (expr != NULL)
6264 {
6265 ++emsg_off;
6266 p = eval_to_string(expr, NULL, FALSE);
6267 --emsg_off;
6268 vim_free(expr);
6269 }
6270 if (p != NULL)
6271 {
6272 MSG(p);
6273 vim_free(p);
6274 }
6275 else
6276 MSG("default");
6277#else
6278 MSG(_("unknown"));
6279#endif
6280 }
6281 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6283}
6284
6285 static void
6286ex_highlight(eap)
6287 exarg_T *eap;
6288{
6289 if (*eap->arg == NUL && eap->cmd[2] == '!')
6290 MSG(_("Greetings, Vim user!"));
6291 do_highlight(eap->arg, eap->forceit, FALSE);
6292}
6293
6294
6295/*
6296 * Call this function if we thought we were going to exit, but we won't
6297 * (because of an error). May need to restore the terminal mode.
6298 */
6299 void
6300not_exiting()
6301{
6302 exiting = FALSE;
6303 settmode(TMODE_RAW);
6304}
6305
6306/*
6307 * ":quit": quit current window, quit Vim if closed the last window.
6308 */
6309 static void
6310ex_quit(eap)
6311 exarg_T *eap;
6312{
6313#ifdef FEAT_CMDWIN
6314 if (cmdwin_type != 0)
6315 {
6316 cmdwin_result = Ctrl_C;
6317 return;
6318 }
6319#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006320 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006321 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006322 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006323 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006324 return;
6325 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006326#ifdef FEAT_AUTOCMD
6327 if (curbuf_locked())
6328 return;
6329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331#ifdef FEAT_NETBEANS_INTG
6332 netbeansForcedQuit = eap->forceit;
6333#endif
6334
6335 /*
6336 * If there are more files or windows we won't exit.
6337 */
6338 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6339 exiting = TRUE;
6340 if ((!P_HID(curbuf)
6341 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6342 || check_more(TRUE, eap->forceit) == FAIL
6343 || (only_one_window() && check_changed_any(eap->forceit)))
6344 {
6345 not_exiting();
6346 }
6347 else
6348 {
6349#ifdef FEAT_WINDOWS
6350 if (only_one_window()) /* quit last window */
6351#endif
6352 getout(0);
6353#ifdef FEAT_WINDOWS
6354# ifdef FEAT_GUI
6355 need_mouse_correct = TRUE;
6356# endif
6357 /* close window; may free buffer */
6358 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6359#endif
6360 }
6361}
6362
6363/*
6364 * ":cquit".
6365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 static void
6367ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006368 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 getout(1); /* this does not always pass on the exit code to the Manx
6371 compiler. why? */
6372}
6373
6374/*
6375 * ":qall": try to quit all windows
6376 */
6377 static void
6378ex_quit_all(eap)
6379 exarg_T *eap;
6380{
6381# ifdef FEAT_CMDWIN
6382 if (cmdwin_type != 0)
6383 {
6384 if (eap->forceit)
6385 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6386 else
6387 cmdwin_result = K_XF2;
6388 return;
6389 }
6390# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006391
6392 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006393 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006394 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006395 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006396 return;
6397 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006398#ifdef FEAT_AUTOCMD
6399 if (curbuf_locked())
6400 return;
6401#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 exiting = TRUE;
6404 if (eap->forceit || !check_changed_any(FALSE))
6405 getout(0);
6406 not_exiting();
6407}
6408
Bram Moolenaard9967712006-03-11 21:18:15 +00006409#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410/*
6411 * ":close": close current window, unless it is the last one
6412 */
6413 static void
6414ex_close(eap)
6415 exarg_T *eap;
6416{
6417# ifdef FEAT_CMDWIN
6418 if (cmdwin_type != 0)
6419 cmdwin_result = K_IGNORE;
6420 else
6421# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006422 if (!text_locked()
6423#ifdef FEAT_AUTOCMD
6424 && !curbuf_locked()
6425#endif
6426 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006427 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428}
6429
Bram Moolenaard9967712006-03-11 21:18:15 +00006430# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431/*
6432 * ":pclose": Close any preview window.
6433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006435ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437{
6438 win_T *win;
6439
6440 for (win = firstwin; win != NULL; win = win->w_next)
6441 if (win->w_p_pvw)
6442 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006443 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 break;
6445 }
6446}
Bram Moolenaard9967712006-03-11 21:18:15 +00006447# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448
Bram Moolenaarf740b292006-02-16 22:11:02 +00006449/*
6450 * Close window "win" and take care of handling closing the last window for a
6451 * modified buffer.
6452 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006454ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006455 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006457 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 int need_hide;
6460 buf_T *buf = win->w_buffer;
6461
6462 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006465# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if ((p_confirm || cmdmod.confirm) && p_write)
6467 {
6468 dialog_changed(buf, FALSE);
6469 if (buf_valid(buf) && bufIsChanged(buf))
6470 return;
6471 need_hide = FALSE;
6472 }
6473 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 {
6476 EMSG(_(e_nowrtmsg));
6477 return;
6478 }
6479 }
6480
Bram Moolenaard9967712006-03-11 21:18:15 +00006481# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006483# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006484
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006486 if (tp == NULL)
6487 win_close(win, !need_hide && !P_HID(buf));
6488 else
6489 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490}
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006493 * ":tabclose": close current tab page, unless it is the last one.
6494 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 */
6496 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 exarg_T *eap;
6499{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006500 tabpage_T *tp;
6501
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006502# ifdef FEAT_CMDWIN
6503 if (cmdwin_type != 0)
6504 cmdwin_result = K_IGNORE;
6505 else
6506# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006507 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006508 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006511 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006513 tp = find_tabpage((int)eap->line2);
6514 if (tp == NULL)
6515 {
6516 beep_flush();
6517 return;
6518 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006519 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006520 {
6521 tabpage_close_other(tp, eap->forceit);
6522 return;
6523 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006524 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006525 if (!text_locked()
6526#ifdef FEAT_AUTOCMD
6527 && !curbuf_locked()
6528#endif
6529 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006530 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006532}
6533
6534/*
6535 * ":tabonly": close all tab pages except the current one
6536 */
6537 static void
6538ex_tabonly(eap)
6539 exarg_T *eap;
6540{
6541 tabpage_T *tp;
6542 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006543
6544# ifdef FEAT_CMDWIN
6545 if (cmdwin_type != 0)
6546 cmdwin_result = K_IGNORE;
6547 else
6548# endif
6549 if (first_tabpage->tp_next == NULL)
6550 MSG(_("Already only one tab page"));
6551 else
6552 {
6553 /* Repeat this up to a 1000 times, because autocommands may mess
6554 * up the lists. */
6555 for (done = 0; done < 1000; ++done)
6556 {
6557 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6558 if (tp->tp_topframe != topframe)
6559 {
6560 tabpage_close_other(tp, eap->forceit);
6561 /* if we failed to close it quit */
6562 if (valid_tabpage(tp))
6563 done = 1000;
6564 /* start over, "tp" is now invalid */
6565 break;
6566 }
6567 if (first_tabpage->tp_next == NULL)
6568 break;
6569 }
6570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574 * Close the current tab page.
6575 */
6576 void
6577tabpage_close(forceit)
6578 int forceit;
6579{
6580 /* First close all the windows but the current one. If that worked then
6581 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006582 if (lastwin != firstwin)
6583 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006584 if (lastwin == firstwin)
6585 ex_win_close(forceit, curwin, NULL);
6586# ifdef FEAT_GUI
6587 need_mouse_correct = TRUE;
6588# endif
6589}
6590
6591/*
6592 * Close tab page "tp", which is not the current tab page.
6593 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006594 * Also takes care of the tab pages line disappearing when closing the
6595 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006596 */
6597 void
6598tabpage_close_other(tp, forceit)
6599 tabpage_T *tp;
6600 int forceit;
6601{
6602 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006603 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006604 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006605
6606 /* Limit to 1000 windows, autocommands may add a window while we close
6607 * one. OK, so I'm paranoid... */
6608 while (++done < 1000)
6609 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006610 wp = tp->tp_firstwin;
6611 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006612
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006613 /* Autocommands may delete the tab page under our fingers and we may
6614 * fail to close a window with a modified buffer. */
6615 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006616 break;
6617 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006618
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006619 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006620 if (h != tabline_height())
6621 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006622}
6623
6624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 * ":only".
6626 */
6627 static void
6628ex_only(eap)
6629 exarg_T *eap;
6630{
6631# ifdef FEAT_GUI
6632 need_mouse_correct = TRUE;
6633# endif
6634 close_others(TRUE, eap->forceit);
6635}
6636
6637/*
6638 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006639 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006641 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642ex_all(eap)
6643 exarg_T *eap;
6644{
6645 if (eap->addr_count == 0)
6646 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006647 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649#endif /* FEAT_WINDOWS */
6650
6651 static void
6652ex_hide(eap)
6653 exarg_T *eap;
6654{
6655 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6656 eap->errmsg = e_invarg;
6657 else
6658 {
6659 /* ":hide" or ":hide | cmd": hide current window */
6660 eap->nextcmd = check_nextcmd(eap->arg);
6661#ifdef FEAT_WINDOWS
6662 if (!eap->skip)
6663 {
6664# ifdef FEAT_GUI
6665 need_mouse_correct = TRUE;
6666# endif
6667 win_close(curwin, FALSE); /* don't free buffer */
6668 }
6669#endif
6670 }
6671}
6672
6673/*
6674 * ":stop" and ":suspend": Suspend Vim.
6675 */
6676 static void
6677ex_stop(eap)
6678 exarg_T *eap;
6679{
6680 /*
6681 * Disallow suspending for "rvim".
6682 */
6683 if (!check_restricted()
6684#ifdef WIN3264
6685 /*
6686 * Check if external commands are allowed now.
6687 */
6688 && can_end_termcap_mode(TRUE)
6689#endif
6690 )
6691 {
6692 if (!eap->forceit)
6693 autowrite_all();
6694 windgoto((int)Rows - 1, 0);
6695 out_char('\n');
6696 out_flush();
6697 stoptermcap();
6698 out_flush(); /* needed for SUN to restore xterm buffer */
6699#ifdef FEAT_TITLE
6700 mch_restore_title(3); /* restore window titles */
6701#endif
6702 ui_suspend(); /* call machine specific function */
6703#ifdef FEAT_TITLE
6704 maketitle();
6705 resettitle(); /* force updating the title */
6706#endif
6707 starttermcap();
6708 scroll_start(); /* scroll screen before redrawing */
6709 redraw_later_clear();
6710 shell_resized(); /* may have resized window */
6711 }
6712}
6713
6714/*
6715 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6716 */
6717 static void
6718ex_exit(eap)
6719 exarg_T *eap;
6720{
6721#ifdef FEAT_CMDWIN
6722 if (cmdwin_type != 0)
6723 {
6724 cmdwin_result = Ctrl_C;
6725 return;
6726 }
6727#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006728 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006729 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006730 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006731 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006732 return;
6733 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006734#ifdef FEAT_AUTOCMD
6735 if (curbuf_locked())
6736 return;
6737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
6739 /*
6740 * if more files or windows we won't exit
6741 */
6742 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6743 exiting = TRUE;
6744 if ( ((eap->cmdidx == CMD_wq
6745 || curbufIsChanged())
6746 && do_write(eap) == FAIL)
6747 || check_more(TRUE, eap->forceit) == FAIL
6748 || (only_one_window() && check_changed_any(eap->forceit)))
6749 {
6750 not_exiting();
6751 }
6752 else
6753 {
6754#ifdef FEAT_WINDOWS
6755 if (only_one_window()) /* quit last window, exit Vim */
6756#endif
6757 getout(0);
6758#ifdef FEAT_WINDOWS
6759# ifdef FEAT_GUI
6760 need_mouse_correct = TRUE;
6761# endif
6762 /* quit current window, may free buffer */
6763 win_close(curwin, !P_HID(curwin->w_buffer));
6764#endif
6765 }
6766}
6767
6768/*
6769 * ":print", ":list", ":number".
6770 */
6771 static void
6772ex_print(eap)
6773 exarg_T *eap;
6774{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006775 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6776 EMSG(_(e_emptybuf));
6777 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006779 for ( ;!got_int; ui_breakcheck())
6780 {
6781 print_line(eap->line1,
6782 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6783 || (eap->flags & EXFLAG_NR)),
6784 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6785 if (++eap->line1 > eap->line2)
6786 break;
6787 out_flush(); /* show one line at a time */
6788 }
6789 setpcmark();
6790 /* put cursor at last line */
6791 curwin->w_cursor.lnum = eap->line2;
6792 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796}
6797
6798#ifdef FEAT_BYTEOFF
6799 static void
6800ex_goto(eap)
6801 exarg_T *eap;
6802{
6803 goto_byte(eap->line2);
6804}
6805#endif
6806
6807/*
6808 * ":shell".
6809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 static void
6811ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006812 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814 do_shell(NULL, 0);
6815}
6816
6817#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6818 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006819 || defined(FEAT_GUI_MSWIN) \
6820 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 || defined(PROTO)
6822
6823/*
6824 * Handle a file drop. The code is here because a drop is *nearly* like an
6825 * :args command, but not quite (we have a list of exact filenames, so we
6826 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6827 * code is very similar to :args and hence needs access to a lot of the static
6828 * functions in this file.
6829 *
6830 * The list should be allocated using alloc(), as should each item in the
6831 * list. This function takes over responsibility for freeing the list.
6832 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006833 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6835 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6836 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6837 * file functionality is (currently) not in EMX this is not presently a
6838 * problem.
6839 */
6840 void
6841handle_drop(filec, filev, split)
6842 int filec; /* the number of files dropped */
6843 char_u **filev; /* the list of files dropped */
6844 int split; /* force splitting the window */
6845{
6846 exarg_T ea;
6847 int save_msg_scroll = msg_scroll;
6848
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006849 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006850 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006852#ifdef FEAT_AUTOCMD
6853 if (curbuf_locked())
6854 return;
6855#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006856 /* When the screen is being updated we should not change buffers and
6857 * windows structures, it may cause freed memory to be used. */
6858 if (updating_screen)
6859 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
6861 /* Check whether the current buffer is changed. If so, we will need
6862 * to split the current window or data could be lost.
6863 * We don't need to check if the 'hidden' option is set, as in this
6864 * case the buffer won't be lost.
6865 */
6866 if (!P_HID(curbuf) && !split)
6867 {
6868 ++emsg_off;
6869 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6870 --emsg_off;
6871 }
6872 if (split)
6873 {
6874# ifdef FEAT_WINDOWS
6875 if (win_split(0, 0) == FAIL)
6876 return;
6877# ifdef FEAT_SCROLLBIND
6878 curwin->w_p_scb = FALSE;
6879# endif
6880
6881 /* When splitting the window, create a new alist. Otherwise the
6882 * existing one is overwritten. */
6883 alist_unlink(curwin->w_alist);
6884 alist_new();
6885# else
6886 return; /* can't split, always fail */
6887# endif
6888 }
6889
6890 /*
6891 * Set up the new argument list.
6892 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006893 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
6895 /*
6896 * Move to the first file.
6897 */
6898 /* Fake up a minimal "next" command for do_argfile() */
6899 vim_memset(&ea, 0, sizeof(ea));
6900 ea.cmd = (char_u *)"next";
6901 do_argfile(&ea, 0);
6902
6903 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6904 * mode that is not needed here. */
6905 need_start_insertmode = FALSE;
6906
6907 /* Restore msg_scroll, otherwise a following command may cause scrolling
6908 * unexpectedly. The screen will be redrawn by the caller, thus
6909 * msg_scroll being set by displaying a message is irrelevant. */
6910 msg_scroll = save_msg_scroll;
6911}
6912#endif
6913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914/*
6915 * Clear an argument list: free all file names and reset it to zero entries.
6916 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006917 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918alist_clear(al)
6919 alist_T *al;
6920{
6921 while (--al->al_ga.ga_len >= 0)
6922 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6923 ga_clear(&al->al_ga);
6924}
6925
6926/*
6927 * Init an argument list.
6928 */
6929 void
6930alist_init(al)
6931 alist_T *al;
6932{
6933 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6934}
6935
6936#if defined(FEAT_WINDOWS) || defined(PROTO)
6937
6938/*
6939 * Remove a reference from an argument list.
6940 * Ignored when the argument list is the global one.
6941 * If the argument list is no longer used by any window, free it.
6942 */
6943 void
6944alist_unlink(al)
6945 alist_T *al;
6946{
6947 if (al != &global_alist && --al->al_refcount <= 0)
6948 {
6949 alist_clear(al);
6950 vim_free(al);
6951 }
6952}
6953
6954# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6955/*
6956 * Create a new argument list and use it for the current window.
6957 */
6958 void
6959alist_new()
6960{
6961 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6962 if (curwin->w_alist == NULL)
6963 {
6964 curwin->w_alist = &global_alist;
6965 ++global_alist.al_refcount;
6966 }
6967 else
6968 {
6969 curwin->w_alist->al_refcount = 1;
6970 alist_init(curwin->w_alist);
6971 }
6972}
6973# endif
6974#endif
6975
6976#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6977/*
6978 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006979 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6980 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 */
6982 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006983alist_expand(fnum_list, fnum_len)
6984 int *fnum_list;
6985 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006988 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 char_u **new_arg_files;
6990 int new_arg_file_count;
6991 char_u *save_p_su = p_su;
6992 int i;
6993
6994 /* Don't use 'suffixes' here. This should work like the shell did the
6995 * expansion. Also, the vimrc file isn't read yet, thus the user
6996 * can't set the options. */
6997 p_su = empty_option;
6998 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6999 if (old_arg_files != NULL)
7000 {
7001 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007002 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7003 old_arg_count = GARGCOUNT;
7004 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 &new_arg_file_count, &new_arg_files,
7006 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7007 && new_arg_file_count > 0)
7008 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007009 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7010 TRUE, fnum_list, fnum_len);
7011 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014 p_su = save_p_su;
7015}
7016#endif
7017
7018/*
7019 * Set the argument list for the current window.
7020 * Takes over the allocated files[] and the allocated fnames in it.
7021 */
7022 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007023alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 alist_T *al;
7025 int count;
7026 char_u **files;
7027 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007028 int *fnum_list;
7029 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 int i;
7032
7033 alist_clear(al);
7034 if (ga_grow(&al->al_ga, count) == OK)
7035 {
7036 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007037 {
7038 if (got_int)
7039 {
7040 /* When adding many buffers this can take a long time. Allow
7041 * interrupting here. */
7042 while (i < count)
7043 vim_free(files[i++]);
7044 break;
7045 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007046
7047 /* May set buffer name of a buffer previously used for the
7048 * argument list, so that it's re-used by alist_add. */
7049 if (fnum_list != NULL && i < fnum_len)
7050 buf_set_name(fnum_list[i], files[i]);
7051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007053 ui_breakcheck();
7054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 vim_free(files);
7056 }
7057 else
7058 FreeWild(count, files);
7059#ifdef FEAT_WINDOWS
7060 if (al == &global_alist)
7061#endif
7062 arg_had_last = FALSE;
7063}
7064
7065/*
7066 * Add file "fname" to argument list "al".
7067 * "fname" must have been allocated and "al" must have been checked for room.
7068 */
7069 void
7070alist_add(al, fname, set_fnum)
7071 alist_T *al;
7072 char_u *fname;
7073 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7074{
7075 if (fname == NULL) /* don't add NULL file names */
7076 return;
7077#ifdef BACKSLASH_IN_FILENAME
7078 slash_adjust(fname);
7079#endif
7080 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7081 if (set_fnum > 0)
7082 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7083 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7084 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
7086
7087#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7088/*
7089 * Adjust slashes in file names. Called after 'shellslash' was set.
7090 */
7091 void
7092alist_slash_adjust()
7093{
7094 int i;
7095# ifdef FEAT_WINDOWS
7096 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007097 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098# endif
7099
7100 for (i = 0; i < GARGCOUNT; ++i)
7101 if (GARGLIST[i].ae_fname != NULL)
7102 slash_adjust(GARGLIST[i].ae_fname);
7103# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007104 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (wp->w_alist != &global_alist)
7106 for (i = 0; i < WARGCOUNT(wp); ++i)
7107 if (WARGLIST(wp)[i].ae_fname != NULL)
7108 slash_adjust(WARGLIST(wp)[i].ae_fname);
7109# endif
7110}
7111#endif
7112
7113/*
7114 * ":preserve".
7115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 static void
7117ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007118 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007120 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 ml_preserve(curbuf, TRUE);
7122}
7123
7124/*
7125 * ":recover".
7126 */
7127 static void
7128ex_recover(eap)
7129 exarg_T *eap;
7130{
7131 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7132 recoverymode = TRUE;
7133 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7134 && (*eap->arg == NUL
7135 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7136 ml_recover();
7137 recoverymode = FALSE;
7138}
7139
7140/*
7141 * Command modifier used in a wrong way.
7142 */
7143 static void
7144ex_wrongmodifier(eap)
7145 exarg_T *eap;
7146{
7147 eap->errmsg = e_invcmd;
7148}
7149
7150#ifdef FEAT_WINDOWS
7151/*
7152 * :sview [+command] file split window with new file, read-only
7153 * :split [[+command] file] split window with current or new file
7154 * :vsplit [[+command] file] split window vertically with current or new file
7155 * :new [[+command] file] split window with no or new file
7156 * :vnew [[+command] file] split vertically window with no or new file
7157 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007158 *
7159 * :tabedit open new Tab page with empty window
7160 * :tabedit [+command] file open new Tab page and edit "file"
7161 * :tabnew [[+command] file] just like :tabedit
7162 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 */
7164 void
7165ex_splitview(eap)
7166 exarg_T *eap;
7167{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007168 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007169# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171# endif
7172# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007176# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7178 {
7179 ex_ni(eap);
7180 return;
7181 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007184# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007188# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007190 * quickfix windows. But it's OK when doing ":tab split". */
7191 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
7193 if (eap->cmdidx == CMD_split)
7194 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (eap->cmdidx == CMD_vsplit)
7197 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007203 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {
7205 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7206 FNAME_MESS, TRUE, curbuf->b_ffname);
7207 if (fname == NULL)
7208 goto theend;
7209 eap->arg = fname;
7210 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007211# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007215# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007217# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 && eap->cmdidx != CMD_new)
7221 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007222# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007223 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007224# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007225 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007226# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007227 au_has_group((char_u *)"FileExplorer"))
7228 {
7229 /* No browsing supported but we do have the file explorer:
7230 * Edit the directory. */
7231 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7232 eap->arg = (char_u *)".";
7233 }
7234 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007235# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007236 {
7237 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007239 if (fname == NULL)
7240 goto theend;
7241 eap->arg = fname;
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
7244 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007247 /*
7248 * Either open new tab page or split the window.
7249 */
7250 if (eap->cmdidx == CMD_tabedit
7251 || eap->cmdidx == CMD_tabfind
7252 || eap->cmdidx == CMD_tabnew)
7253 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007254 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7255 : eap->addr_count == 0 ? 0
7256 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007257 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007258 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007259
7260 /* set the alternate buffer for the window we came from */
7261 if (curwin != old_curwin
7262 && win_valid(old_curwin)
7263 && old_curwin->w_buffer != curbuf
7264 && !cmdmod.keepalt)
7265 old_curwin->w_alt_fnum = curbuf->b_fnum;
7266 }
7267 }
7268 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7270 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 /* Reset 'scrollbind' when editing another file, but keep it when
7273 * doing ":split" without arguments. */
7274 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007275# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 )
7279 curwin->w_p_scb = FALSE;
7280 else
7281 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 do_exedit(eap, old_curwin);
7284 }
7285
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007290# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291theend:
7292 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295
7296/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007297 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007298 */
7299 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007300tabpage_new()
7301{
7302 exarg_T ea;
7303
7304 vim_memset(&ea, 0, sizeof(ea));
7305 ea.cmdidx = CMD_tabnew;
7306 ea.cmd = (char_u *)"tabn";
7307 ea.arg = (char_u *)"";
7308 ex_splitview(&ea);
7309}
7310
7311/*
7312 * :tabnext command
7313 */
7314 static void
7315ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007316 exarg_T *eap;
7317{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007318 switch (eap->cmdidx)
7319 {
7320 case CMD_tabfirst:
7321 case CMD_tabrewind:
7322 goto_tabpage(1);
7323 break;
7324 case CMD_tablast:
7325 goto_tabpage(9999);
7326 break;
7327 case CMD_tabprevious:
7328 case CMD_tabNext:
7329 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7330 break;
7331 default: /* CMD_tabnext */
7332 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7333 break;
7334 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007335}
7336
7337/*
7338 * :tabmove command
7339 */
7340 static void
7341ex_tabmove(eap)
7342 exarg_T *eap;
7343{
7344 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007345}
7346
7347/*
7348 * :tabs command: List tabs and their contents.
7349 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350 static void
7351ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007352 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007353{
7354 tabpage_T *tp;
7355 win_T *wp;
7356 int tabcount = 1;
7357
7358 msg_start();
7359 msg_scroll = TRUE;
7360 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7361 {
7362 msg_putchar('\n');
7363 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7364 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7365 out_flush(); /* output one line at a time */
7366 ui_breakcheck();
7367
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007368 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007369 wp = firstwin;
7370 else
7371 wp = tp->tp_firstwin;
7372 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7373 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007374 msg_putchar('\n');
7375 msg_putchar(wp == curwin ? '>' : ' ');
7376 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007377 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7378 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007379 if (buf_spname(wp->w_buffer) != NULL)
7380 STRCPY(IObuff, buf_spname(wp->w_buffer));
7381 else
7382 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7383 IObuff, IOSIZE, TRUE);
7384 msg_outtrans(IObuff);
7385 out_flush(); /* output one line at a time */
7386 ui_breakcheck();
7387 }
7388 }
7389}
7390
7391#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
7393/*
7394 * ":mode": Set screen mode.
7395 * If no argument given, just get the screen size and redraw.
7396 */
7397 static void
7398ex_mode(eap)
7399 exarg_T *eap;
7400{
7401 if (*eap->arg == NUL)
7402 shell_resized();
7403 else
7404 mch_screenmode(eap->arg);
7405}
7406
7407#ifdef FEAT_WINDOWS
7408/*
7409 * ":resize".
7410 * set, increment or decrement current window height
7411 */
7412 static void
7413ex_resize(eap)
7414 exarg_T *eap;
7415{
7416 int n;
7417 win_T *wp = curwin;
7418
7419 if (eap->addr_count > 0)
7420 {
7421 n = eap->line2;
7422 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7423 ;
7424 }
7425
7426#ifdef FEAT_GUI
7427 need_mouse_correct = TRUE;
7428#endif
7429 n = atol((char *)eap->arg);
7430#ifdef FEAT_VERTSPLIT
7431 if (cmdmod.split & WSP_VERT)
7432 {
7433 if (*eap->arg == '-' || *eap->arg == '+')
7434 n += W_WIDTH(curwin);
7435 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7436 n = 9999;
7437 win_setwidth_win((int)n, wp);
7438 }
7439 else
7440#endif
7441 {
7442 if (*eap->arg == '-' || *eap->arg == '+')
7443 n += curwin->w_height;
7444 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7445 n = 9999;
7446 win_setheight_win((int)n, wp);
7447 }
7448}
7449#endif
7450
7451/*
7452 * ":find [+command] <file>" command.
7453 */
7454 static void
7455ex_find(eap)
7456 exarg_T *eap;
7457{
7458#ifdef FEAT_SEARCHPATH
7459 char_u *fname;
7460 int count;
7461
7462 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7463 TRUE, curbuf->b_ffname);
7464 if (eap->addr_count > 0)
7465 {
7466 /* Repeat finding the file "count" times. This matters when it
7467 * appears several times in the path. */
7468 count = eap->line2;
7469 while (fname != NULL && --count > 0)
7470 {
7471 vim_free(fname);
7472 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7473 FALSE, curbuf->b_ffname);
7474 }
7475 }
7476
7477 if (fname != NULL)
7478 {
7479 eap->arg = fname;
7480#endif
7481 do_exedit(eap, NULL);
7482#ifdef FEAT_SEARCHPATH
7483 vim_free(fname);
7484 }
7485#endif
7486}
7487
7488/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007489 * ":open" simulation: for now just work like ":visual".
7490 */
7491 static void
7492ex_open(eap)
7493 exarg_T *eap;
7494{
7495 regmatch_T regmatch;
7496 char_u *p;
7497
7498 curwin->w_cursor.lnum = eap->line2;
7499 beginline(BL_SOL | BL_FIX);
7500 if (*eap->arg == '/')
7501 {
7502 /* ":open /pattern/": put cursor in column found with pattern */
7503 ++eap->arg;
7504 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7505 *p = NUL;
7506 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7507 if (regmatch.regprog != NULL)
7508 {
7509 regmatch.rm_ic = p_ic;
7510 p = ml_get_curline();
7511 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007512 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007513 else
7514 EMSG(_(e_nomatch));
7515 vim_free(regmatch.regprog);
7516 }
7517 /* Move to the NUL, ignore any other arguments. */
7518 eap->arg += STRLEN(eap->arg);
7519 }
7520 check_cursor();
7521
7522 eap->cmdidx = CMD_visual;
7523 do_exedit(eap, NULL);
7524}
7525
7526/*
7527 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
7529 static void
7530ex_edit(eap)
7531 exarg_T *eap;
7532{
7533 do_exedit(eap, NULL);
7534}
7535
7536/*
7537 * ":edit <file>" command and alikes.
7538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 void
7540do_exedit(eap, old_curwin)
7541 exarg_T *eap;
7542 win_T *old_curwin; /* curwin before doing a split or NULL */
7543{
7544 int n;
7545#ifdef FEAT_WINDOWS
7546 int need_hide;
7547#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007548 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 /*
7551 * ":vi" command ends Ex mode.
7552 */
7553 if (exmode_active && (eap->cmdidx == CMD_visual
7554 || eap->cmdidx == CMD_view))
7555 {
7556 exmode_active = FALSE;
7557 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007558 {
7559 /* Special case: ":global/pat/visual\NLvi-commands" */
7560 if (global_busy)
7561 {
7562 int rd = RedrawingDisabled;
7563 int nwr = no_wait_return;
7564 int ms = msg_scroll;
7565#ifdef FEAT_GUI
7566 int he = hold_gui_events;
7567#endif
7568
7569 if (eap->nextcmd != NULL)
7570 {
7571 stuffReadbuff(eap->nextcmd);
7572 eap->nextcmd = NULL;
7573 }
7574
7575 if (exmode_was != EXMODE_VIM)
7576 settmode(TMODE_RAW);
7577 RedrawingDisabled = 0;
7578 no_wait_return = 0;
7579 need_wait_return = FALSE;
7580 msg_scroll = 0;
7581#ifdef FEAT_GUI
7582 hold_gui_events = 0;
7583#endif
7584 must_redraw = CLEAR;
7585
7586 main_loop(FALSE, TRUE);
7587
7588 RedrawingDisabled = rd;
7589 no_wait_return = nwr;
7590 msg_scroll = ms;
7591#ifdef FEAT_GUI
7592 hold_gui_events = he;
7593#endif
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
7598
7599 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007600 || eap->cmdidx == CMD_tabnew
7601 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602#ifdef FEAT_VERTSPLIT
7603 || eap->cmdidx == CMD_vnew
7604#endif
7605 ) && *eap->arg == NUL)
7606 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007607 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 setpcmark();
7609 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007610 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7611 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 else if ((eap->cmdidx != CMD_split
7614#ifdef FEAT_VERTSPLIT
7615 && eap->cmdidx != CMD_vsplit
7616#endif
7617 )
7618 || *eap->arg != NUL
7619#ifdef FEAT_BROWSE
7620 || cmdmod.browse
7621#endif
7622 )
7623 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007624#ifdef FEAT_AUTOCMD
7625 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7626 * can bring us here, others are stopped earlier. */
7627 if (*eap->arg != NUL && curbuf_locked())
7628 return;
7629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 n = readonlymode;
7631 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7632 readonlymode = TRUE;
7633 else if (eap->cmdidx == CMD_enew)
7634 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7635 empty buffer */
7636 setpcmark();
7637 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7638 NULL, eap,
7639 /* ":edit" goes to first line if Vi compatible */
7640 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7641 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7642 ? ECMD_ONE : eap->do_ecmd_lnum,
7643 (P_HID(curbuf) ? ECMD_HIDE : 0)
7644 + (eap->forceit ? ECMD_FORCEIT : 0)
7645#ifdef FEAT_LISTCMDS
7646 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7647#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007648 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {
7650 /* Editing the file failed. If the window was split, close it. */
7651#ifdef FEAT_WINDOWS
7652 if (old_curwin != NULL)
7653 {
7654 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7655 if (!need_hide || P_HID(curbuf))
7656 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007657# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7658 cleanup_T cs;
7659
7660 /* Reset the error/interrupt/exception state here so that
7661 * aborting() returns FALSE when closing a window. */
7662 enter_cleanup(&cs);
7663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664# ifdef FEAT_GUI
7665 need_mouse_correct = TRUE;
7666# endif
7667 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007668
7669# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7670 /* Restore the error/interrupt/exception state if not
7671 * discarded by a new aborting error, interrupt, or
7672 * uncaught exception. */
7673 leave_cleanup(&cs);
7674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
7676 }
7677#endif
7678 }
7679 else if (readonlymode && curbuf->b_nwindows == 1)
7680 {
7681 /* When editing an already visited buffer, 'readonly' won't be set
7682 * but the previous value is kept. With ":view" and ":sview" we
7683 * want the file to be readonly, except when another window is
7684 * editing the same buffer. */
7685 curbuf->b_p_ro = TRUE;
7686 }
7687 readonlymode = n;
7688 }
7689 else
7690 {
7691 if (eap->do_ecmd_cmd != NULL)
7692 do_cmdline_cmd(eap->do_ecmd_cmd);
7693#ifdef FEAT_TITLE
7694 n = curwin->w_arg_idx_invalid;
7695#endif
7696 check_arg_idx(curwin);
7697#ifdef FEAT_TITLE
7698 if (n != curwin->w_arg_idx_invalid)
7699 maketitle();
7700#endif
7701 }
7702
7703#ifdef FEAT_WINDOWS
7704 /*
7705 * if ":split file" worked, set alternate file name in old window to new
7706 * file
7707 */
7708 if (old_curwin != NULL
7709 && *eap->arg != NUL
7710 && curwin != old_curwin
7711 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007712 && old_curwin->w_buffer != curbuf
7713 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 old_curwin->w_alt_fnum = curbuf->b_fnum;
7715#endif
7716
7717 ex_no_reprint = TRUE;
7718}
7719
7720#ifndef FEAT_GUI
7721/*
7722 * ":gui" and ":gvim" when there is no GUI.
7723 */
7724 static void
7725ex_nogui(eap)
7726 exarg_T *eap;
7727{
7728 eap->errmsg = e_nogvim;
7729}
7730#endif
7731
7732#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7733 static void
7734ex_tearoff(eap)
7735 exarg_T *eap;
7736{
7737 gui_make_tearoff(eap->arg);
7738}
7739#endif
7740
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007741#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 static void
7743ex_popup(eap)
7744 exarg_T *eap;
7745{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007746 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747}
7748#endif
7749
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 static void
7751ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007752 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7755 MSG(_("No swap file"));
7756 else
7757 msg(curbuf->b_ml.ml_mfp->mf_fname);
7758}
7759
7760/*
7761 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7762 * offset.
7763 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 static void
7766ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007767 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769#ifdef FEAT_SCROLLBIND
7770 win_T *wp;
7771 long topline;
7772 long y;
7773 linenr_T old_linenr = curwin->w_cursor.lnum;
7774
7775 setpcmark();
7776
7777 /*
7778 * determine max topline
7779 */
7780 if (curwin->w_p_scb)
7781 {
7782 topline = curwin->w_topline;
7783 for (wp = firstwin; wp; wp = wp->w_next)
7784 {
7785 if (wp->w_p_scb && wp->w_buffer)
7786 {
7787 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7788 if (topline > y)
7789 topline = y;
7790 }
7791 }
7792 if (topline < 1)
7793 topline = 1;
7794 }
7795 else
7796 {
7797 topline = 1;
7798 }
7799
7800
7801 /*
7802 * set all scrollbind windows to the same topline
7803 */
7804 wp = curwin;
7805 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7806 {
7807 if (curwin->w_p_scb)
7808 {
7809 y = topline - curwin->w_topline;
7810 if (y > 0)
7811 scrollup(y, TRUE);
7812 else
7813 scrolldown(-y, TRUE);
7814 curwin->w_scbind_pos = topline;
7815 redraw_later(VALID);
7816 cursor_correct();
7817#ifdef FEAT_WINDOWS
7818 curwin->w_redr_status = TRUE;
7819#endif
7820 }
7821 }
7822 curwin = wp;
7823 if (curwin->w_p_scb)
7824 {
7825 did_syncbind = TRUE;
7826 checkpcmark();
7827 if (old_linenr != curwin->w_cursor.lnum)
7828 {
7829 char_u ctrl_o[2];
7830
7831 ctrl_o[0] = Ctrl_O;
7832 ctrl_o[1] = 0;
7833 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7834 }
7835 }
7836#endif
7837}
7838
7839
7840 static void
7841ex_read(eap)
7842 exarg_T *eap;
7843{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007844 int i;
7845 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7846 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
7848 if (eap->usefilter) /* :r!cmd */
7849 do_bang(1, eap, FALSE, FALSE, TRUE);
7850 else
7851 {
7852 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7853 return;
7854
7855#ifdef FEAT_BROWSE
7856 if (cmdmod.browse)
7857 {
7858 char_u *browseFile;
7859
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007860 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 NULL, NULL, NULL, curbuf);
7862 if (browseFile != NULL)
7863 {
7864 i = readfile(browseFile, NULL,
7865 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7866 vim_free(browseFile);
7867 }
7868 else
7869 i = OK;
7870 }
7871 else
7872#endif
7873 if (*eap->arg == NUL)
7874 {
7875 if (check_fname() == FAIL) /* check for no file name */
7876 return;
7877 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7878 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7879 }
7880 else
7881 {
7882 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7883 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7884 i = readfile(eap->arg, NULL,
7885 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7886
7887 }
7888 if (i == FAIL)
7889 {
7890#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7891 if (!aborting())
7892#endif
7893 EMSG2(_(e_notopen), eap->arg);
7894 }
7895 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007896 {
7897 if (empty && exmode_active)
7898 {
7899 /* Delete the empty line that remains. Historically ex does
7900 * this but vi doesn't. */
7901 if (eap->line2 == 0)
7902 lnum = curbuf->b_ml.ml_line_count;
7903 else
7904 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007905 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007906 {
7907 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007908 if (curwin->w_cursor.lnum > 1
7909 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007910 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007911 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007912 }
7913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 }
7917}
7918
Bram Moolenaarea408852005-06-25 22:49:46 +00007919static char_u *prev_dir = NULL;
7920
7921#if defined(EXITFREE) || defined(PROTO)
7922 void
7923free_cd_dir()
7924{
7925 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007926 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007927
7928 vim_free(globaldir);
7929 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007930}
7931#endif
7932
7933
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934/*
7935 * ":cd", ":lcd", ":chdir" and ":lchdir".
7936 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007937 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938ex_cd(eap)
7939 exarg_T *eap;
7940{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 char_u *new_dir;
7942 char_u *tofree;
7943
7944 new_dir = eap->arg;
7945#if !defined(UNIX) && !defined(VMS)
7946 /* for non-UNIX ":cd" means: print current directory */
7947 if (*new_dir == NUL)
7948 ex_pwd(NULL);
7949 else
7950#endif
7951 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007952#ifdef FEAT_AUTOCMD
7953 if (allbuf_locked())
7954 return;
7955#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007956 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7957 && !eap->forceit)
7958 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007959 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007960 return;
7961 }
7962
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 /* ":cd -": Change to previous directory */
7964 if (STRCMP(new_dir, "-") == 0)
7965 {
7966 if (prev_dir == NULL)
7967 {
7968 EMSG(_("E186: No previous directory"));
7969 return;
7970 }
7971 new_dir = prev_dir;
7972 }
7973
7974 /* Save current directory for next ":cd -" */
7975 tofree = prev_dir;
7976 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7977 prev_dir = vim_strsave(NameBuff);
7978 else
7979 prev_dir = NULL;
7980
7981#if defined(UNIX) || defined(VMS)
7982 /* for UNIX ":cd" means: go to home directory */
7983 if (*new_dir == NUL)
7984 {
7985 /* use NameBuff for home directory name */
7986# ifdef VMS
7987 char_u *p;
7988
7989 p = mch_getenv((char_u *)"SYS$LOGIN");
7990 if (p == NULL || *p == NUL) /* empty is the same as not set */
7991 NameBuff[0] = NUL;
7992 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007993 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994# else
7995 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7996# endif
7997 new_dir = NameBuff;
7998 }
7999#endif
8000 if (new_dir == NULL || vim_chdir(new_dir))
8001 EMSG(_(e_failed));
8002 else
8003 {
8004 vim_free(curwin->w_localdir);
8005 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8006 {
8007 /* If still in global directory, need to remember current
8008 * directory as global directory. */
8009 if (globaldir == NULL && prev_dir != NULL)
8010 globaldir = vim_strsave(prev_dir);
8011 /* Remember this local directory for the window. */
8012 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8013 curwin->w_localdir = vim_strsave(NameBuff);
8014 }
8015 else
8016 {
8017 /* We are now in the global directory, no need to remember its
8018 * name. */
8019 vim_free(globaldir);
8020 globaldir = NULL;
8021 curwin->w_localdir = NULL;
8022 }
8023
8024 shorten_fnames(TRUE);
8025
8026 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008027 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 ex_pwd(eap);
8029 }
8030 vim_free(tofree);
8031 }
8032}
8033
8034/*
8035 * ":pwd".
8036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 static void
8038ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008039 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8042 {
8043#ifdef BACKSLASH_IN_FILENAME
8044 slash_adjust(NameBuff);
8045#endif
8046 msg(NameBuff);
8047 }
8048 else
8049 EMSG(_("E187: Unknown"));
8050}
8051
8052/*
8053 * ":=".
8054 */
8055 static void
8056ex_equal(eap)
8057 exarg_T *eap;
8058{
8059 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008060 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061}
8062
8063 static void
8064ex_sleep(eap)
8065 exarg_T *eap;
8066{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008067 int n;
8068 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
8070 if (cursor_valid())
8071 {
8072 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8073 if (n >= 0)
8074 windgoto((int)n, curwin->w_wcol);
8075 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008076
8077 len = eap->line2;
8078 switch (*eap->arg)
8079 {
8080 case 'm': break;
8081 case NUL: len *= 1000L; break;
8082 default: EMSG2(_(e_invarg2), eap->arg); return;
8083 }
8084 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085}
8086
8087/*
8088 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8089 */
8090 void
8091do_sleep(msec)
8092 long msec;
8093{
8094 long done;
8095
8096 cursor_on();
8097 out_flush();
8098 for (done = 0; !got_int && done < msec; done += 1000L)
8099 {
8100 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8101 ui_breakcheck();
8102 }
8103}
8104
8105 static void
8106do_exmap(eap, isabbrev)
8107 exarg_T *eap;
8108 int isabbrev;
8109{
8110 int mode;
8111 char_u *cmdp;
8112
8113 cmdp = eap->cmd;
8114 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8115
8116 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8117 eap->arg, mode, isabbrev))
8118 {
8119 case 1: EMSG(_(e_invarg));
8120 break;
8121 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8122 break;
8123 }
8124}
8125
8126/*
8127 * ":winsize" command (obsolete).
8128 */
8129 static void
8130ex_winsize(eap)
8131 exarg_T *eap;
8132{
8133 int w, h;
8134 char_u *arg = eap->arg;
8135 char_u *p;
8136
8137 w = getdigits(&arg);
8138 arg = skipwhite(arg);
8139 p = arg;
8140 h = getdigits(&arg);
8141 if (*p != NUL && *arg == NUL)
8142 set_shellsize(w, h, TRUE);
8143 else
8144 EMSG(_("E465: :winsize requires two number arguments"));
8145}
8146
8147#ifdef FEAT_WINDOWS
8148 static void
8149ex_wincmd(eap)
8150 exarg_T *eap;
8151{
8152 int xchar = NUL;
8153 char_u *p;
8154
8155 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8156 {
8157 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8158 if (eap->arg[1] == NUL)
8159 {
8160 EMSG(_(e_invarg));
8161 return;
8162 }
8163 xchar = eap->arg[1];
8164 p = eap->arg + 2;
8165 }
8166 else
8167 p = eap->arg + 1;
8168
8169 eap->nextcmd = check_nextcmd(p);
8170 p = skipwhite(p);
8171 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8172 EMSG(_(e_invarg));
8173 else
8174 {
8175 /* Pass flags on for ":vertical wincmd ]". */
8176 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008177 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8179 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008180 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 }
8182}
8183#endif
8184
Bram Moolenaar843ee412004-06-30 16:16:41 +00008185#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186/*
8187 * ":winpos".
8188 */
8189 static void
8190ex_winpos(eap)
8191 exarg_T *eap;
8192{
8193 int x, y;
8194 char_u *arg = eap->arg;
8195 char_u *p;
8196
8197 if (*arg == NUL)
8198 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008199# if defined(FEAT_GUI) || defined(MSWIN)
8200# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008202# else
8203 if (mch_get_winpos(&x, &y) != FAIL)
8204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {
8206 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8207 msg(IObuff);
8208 }
8209 else
8210# endif
8211 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8212 }
8213 else
8214 {
8215 x = getdigits(&arg);
8216 arg = skipwhite(arg);
8217 p = arg;
8218 y = getdigits(&arg);
8219 if (*p == NUL || *arg != NUL)
8220 {
8221 EMSG(_("E466: :winpos requires two number arguments"));
8222 return;
8223 }
8224# ifdef FEAT_GUI
8225 if (gui.in_use)
8226 gui_mch_set_winpos(x, y);
8227 else if (gui.starting)
8228 {
8229 /* Remember the coordinates for when the window is opened. */
8230 gui_win_x = x;
8231 gui_win_y = y;
8232 }
8233# ifdef HAVE_TGETENT
8234 else
8235# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008236# else
8237# ifdef MSWIN
8238 mch_set_winpos(x, y);
8239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240# endif
8241# ifdef HAVE_TGETENT
8242 if (*T_CWP)
8243 term_set_winpos(x, y);
8244# endif
8245 }
8246}
8247#endif
8248
8249/*
8250 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8251 */
8252 static void
8253ex_operators(eap)
8254 exarg_T *eap;
8255{
8256 oparg_T oa;
8257
8258 clear_oparg(&oa);
8259 oa.regname = eap->regname;
8260 oa.start.lnum = eap->line1;
8261 oa.end.lnum = eap->line2;
8262 oa.line_count = eap->line2 - eap->line1 + 1;
8263 oa.motion_type = MLINE;
8264#ifdef FEAT_VIRTUALEDIT
8265 virtual_op = FALSE;
8266#endif
8267 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8268 {
8269 setpcmark();
8270 curwin->w_cursor.lnum = eap->line1;
8271 beginline(BL_SOL | BL_FIX);
8272 }
8273
8274 switch (eap->cmdidx)
8275 {
8276 case CMD_delete:
8277 oa.op_type = OP_DELETE;
8278 op_delete(&oa);
8279 break;
8280
8281 case CMD_yank:
8282 oa.op_type = OP_YANK;
8283 (void)op_yank(&oa, FALSE, TRUE);
8284 break;
8285
8286 default: /* CMD_rshift or CMD_lshift */
8287 if ((eap->cmdidx == CMD_rshift)
8288#ifdef FEAT_RIGHTLEFT
8289 ^ curwin->w_p_rl
8290#endif
8291 )
8292 oa.op_type = OP_RSHIFT;
8293 else
8294 oa.op_type = OP_LSHIFT;
8295 op_shift(&oa, FALSE, eap->amount);
8296 break;
8297 }
8298#ifdef FEAT_VIRTUALEDIT
8299 virtual_op = MAYBE;
8300#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008301 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302}
8303
8304/*
8305 * ":put".
8306 */
8307 static void
8308ex_put(eap)
8309 exarg_T *eap;
8310{
8311 /* ":0put" works like ":1put!". */
8312 if (eap->line2 == 0)
8313 {
8314 eap->line2 = 1;
8315 eap->forceit = TRUE;
8316 }
8317 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008318 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8319 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320}
8321
8322/*
8323 * Handle ":copy" and ":move".
8324 */
8325 static void
8326ex_copymove(eap)
8327 exarg_T *eap;
8328{
8329 long n;
8330
8331 n = get_address(&eap->arg, FALSE, FALSE);
8332 if (eap->arg == NULL) /* error detected */
8333 {
8334 eap->nextcmd = NULL;
8335 return;
8336 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008337 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
8339 /*
8340 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8341 */
8342 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8343 {
8344 EMSG(_(e_invaddr));
8345 return;
8346 }
8347
8348 if (eap->cmdidx == CMD_move)
8349 {
8350 if (do_move(eap->line1, eap->line2, n) == FAIL)
8351 return;
8352 }
8353 else
8354 ex_copy(eap->line1, eap->line2, n);
8355 u_clearline();
8356 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008357 ex_may_print(eap);
8358}
8359
8360/*
8361 * Print the current line if flags were given to the Ex command.
8362 */
8363 static void
8364ex_may_print(eap)
8365 exarg_T *eap;
8366{
8367 if (eap->flags != 0)
8368 {
8369 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8370 (eap->flags & EXFLAG_LIST));
8371 ex_no_reprint = TRUE;
8372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373}
8374
8375/*
8376 * ":smagic" and ":snomagic".
8377 */
8378 static void
8379ex_submagic(eap)
8380 exarg_T *eap;
8381{
8382 int magic_save = p_magic;
8383
8384 p_magic = (eap->cmdidx == CMD_smagic);
8385 do_sub(eap);
8386 p_magic = magic_save;
8387}
8388
8389/*
8390 * ":join".
8391 */
8392 static void
8393ex_join(eap)
8394 exarg_T *eap;
8395{
8396 curwin->w_cursor.lnum = eap->line1;
8397 if (eap->line1 == eap->line2)
8398 {
8399 if (eap->addr_count >= 2) /* :2,2join does nothing */
8400 return;
8401 if (eap->line2 == curbuf->b_ml.ml_line_count)
8402 {
8403 beep_flush();
8404 return;
8405 }
8406 ++eap->line2;
8407 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008408 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008410 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413/*
8414 * ":[addr]@r" or ":[addr]*r": execute register
8415 */
8416 static void
8417ex_at(eap)
8418 exarg_T *eap;
8419{
8420 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008421 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422
8423 curwin->w_cursor.lnum = eap->line2;
8424
8425#ifdef USE_ON_FLY_SCROLL
8426 dont_scroll = TRUE; /* disallow scrolling here */
8427#endif
8428
8429 /* get the register name. No name means to use the previous one */
8430 c = *eap->arg;
8431 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8432 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008433 /* Put the register in the typeahead buffer with the "silent" flag. */
8434 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8435 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 else
8440 {
8441 int save_efr = exec_from_reg;
8442
8443 exec_from_reg = TRUE;
8444
8445 /*
8446 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008447 * Continue until the stuff buffer is empty and all added characters
8448 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008450 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8452
8453 exec_from_reg = save_efr;
8454 }
8455}
8456
8457/*
8458 * ":!".
8459 */
8460 static void
8461ex_bang(eap)
8462 exarg_T *eap;
8463{
8464 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8465}
8466
8467/*
8468 * ":undo".
8469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 static void
8471ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008472 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008474 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008475 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008476 else
8477 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478}
8479
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008480#ifdef FEAT_PERSISTENT_UNDO
8481 void
8482ex_wundo(eap)
8483 exarg_T *eap;
8484{
8485 char_u hash[UNDO_HASH_SIZE];
8486
8487 u_compute_hash(hash);
8488 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8489}
8490
8491 void
8492ex_rundo(eap)
8493 exarg_T *eap;
8494{
8495 char_u hash[UNDO_HASH_SIZE];
8496
8497 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008498 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008499}
8500#endif
8501
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502/*
8503 * ":redo".
8504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 static void
8506ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008507 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 u_redo(1);
8510}
8511
8512/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008513 * ":earlier" and ":later".
8514 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008515 static void
8516ex_later(eap)
8517 exarg_T *eap;
8518{
8519 long count = 0;
8520 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008521 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008522 char_u *p = eap->arg;
8523
8524 if (*p == NUL)
8525 count = 1;
8526 else if (isdigit(*p))
8527 {
8528 count = getdigits(&p);
8529 switch (*p)
8530 {
8531 case 's': ++p; sec = TRUE; break;
8532 case 'm': ++p; sec = TRUE; count *= 60; break;
8533 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008534 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8535 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008536 }
8537 }
8538
8539 if (*p != NUL)
8540 EMSG2(_(e_invarg2), eap->arg);
8541 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008542 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8543 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008544}
8545
8546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 * ":redir": start/stop redirection.
8548 */
8549 static void
8550ex_redir(eap)
8551 exarg_T *eap;
8552{
8553 char *mode;
8554 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008555 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
8557 if (STRICMP(eap->arg, "END") == 0)
8558 close_redir();
8559 else
8560 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008561 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008563 ++arg;
8564 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008566 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 mode = "a";
8568 }
8569 else
8570 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008571 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
8573 close_redir();
8574
8575 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008576 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (fname == NULL)
8578 return;
8579#ifdef FEAT_BROWSE
8580 if (cmdmod.browse)
8581 {
8582 char_u *browseFile;
8583
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008584 browseFile = do_browse(BROWSE_SAVE,
8585 (char_u *)_("Save Redirection"),
8586 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 if (browseFile == NULL)
8588 return; /* operation cancelled */
8589 vim_free(fname);
8590 fname = browseFile;
8591 eap->forceit = TRUE; /* since dialog already asked */
8592 }
8593#endif
8594
8595 redir_fd = open_exfile(fname, eap->forceit, mode);
8596 vim_free(fname);
8597 }
8598#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008599 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {
8601 /* redirect to a register a-z (resp. A-Z for appending) */
8602 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008603 ++arg;
8604 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008606 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008607 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008609 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008611 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008612 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008613 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008616 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008617 if (*arg == '>')
8618 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008619 /* Make register empty when not using @A-@Z and the
8620 * command is valid. */
8621 if (*arg == NUL && !isupper(redir_reg))
8622 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008624 }
8625 if (*arg != NUL)
8626 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008627 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008628 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008629 }
8630 }
8631 else if (*arg == '=' && arg[1] == '>')
8632 {
8633 int append;
8634
8635 /* redirect to a variable */
8636 close_redir();
8637 arg += 2;
8638
8639 if (*arg == '>')
8640 {
8641 ++arg;
8642 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 }
8644 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008645 append = FALSE;
8646
8647 if (var_redir_start(skipwhite(arg), append) == OK)
8648 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 }
8650#endif
8651
8652 /* TODO: redirect to a buffer */
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008655 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008657
8658 /* Make sure redirection is not off. Can happen for cmdline completion
8659 * that indirectly invokes a command to catch its output. */
8660 if (redir_fd != NULL
8661#ifdef FEAT_EVAL
8662 || redir_reg || redir_vname
8663#endif
8664 )
8665 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666}
8667
8668/*
8669 * ":redraw": force redraw
8670 */
8671 static void
8672ex_redraw(eap)
8673 exarg_T *eap;
8674{
8675 int r = RedrawingDisabled;
8676 int p = p_lz;
8677
8678 RedrawingDisabled = 0;
8679 p_lz = FALSE;
8680 update_topline();
8681 update_screen(eap->forceit ? CLEAR :
8682#ifdef FEAT_VISUAL
8683 VIsual_active ? INVERTED :
8684#endif
8685 0);
8686#ifdef FEAT_TITLE
8687 if (need_maketitle)
8688 maketitle();
8689#endif
8690 RedrawingDisabled = r;
8691 p_lz = p;
8692
8693 /* Reset msg_didout, so that a message that's there is overwritten. */
8694 msg_didout = FALSE;
8695 msg_col = 0;
8696
8697 out_flush();
8698}
8699
8700/*
8701 * ":redrawstatus": force redraw of status line(s)
8702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 static void
8704ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008705 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707#if defined(FEAT_WINDOWS)
8708 int r = RedrawingDisabled;
8709 int p = p_lz;
8710
8711 RedrawingDisabled = 0;
8712 p_lz = FALSE;
8713 if (eap->forceit)
8714 status_redraw_all();
8715 else
8716 status_redraw_curbuf();
8717 update_screen(
8718# ifdef FEAT_VISUAL
8719 VIsual_active ? INVERTED :
8720# endif
8721 0);
8722 RedrawingDisabled = r;
8723 p_lz = p;
8724 out_flush();
8725#endif
8726}
8727
8728 static void
8729close_redir()
8730{
8731 if (redir_fd != NULL)
8732 {
8733 fclose(redir_fd);
8734 redir_fd = NULL;
8735 }
8736#ifdef FEAT_EVAL
8737 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008738 if (redir_vname)
8739 {
8740 var_redir_stop();
8741 redir_vname = 0;
8742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#endif
8744}
8745
8746#if defined(FEAT_SESSION) && defined(USE_CRNL)
8747# define MKSESSION_NL
8748static int mksession_nl = FALSE; /* use NL only in put_eol() */
8749#endif
8750
8751/*
8752 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8753 */
8754 static void
8755ex_mkrc(eap)
8756 exarg_T *eap;
8757{
8758 FILE *fd;
8759 int failed = FALSE;
8760 char_u *fname;
8761#ifdef FEAT_BROWSE
8762 char_u *browseFile = NULL;
8763#endif
8764#ifdef FEAT_SESSION
8765 int view_session = FALSE;
8766 int using_vdir = FALSE; /* using 'viewdir'? */
8767 char_u *viewFile = NULL;
8768 unsigned *flagp;
8769#endif
8770
8771 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8772 {
8773#ifdef FEAT_SESSION
8774 view_session = TRUE;
8775#else
8776 ex_ni(eap);
8777 return;
8778#endif
8779 }
8780
8781#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008782 /* Use the short file name until ":lcd" is used. We also don't use the
8783 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008784 did_lcd = FALSE;
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8787 if (eap->cmdidx == CMD_mkview
8788 && (*eap->arg == NUL
8789 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8790 {
8791 eap->forceit = TRUE;
8792 fname = get_view_file(*eap->arg);
8793 if (fname == NULL)
8794 return;
8795 viewFile = fname;
8796 using_vdir = TRUE;
8797 }
8798 else
8799#endif
8800 if (*eap->arg != NUL)
8801 fname = eap->arg;
8802 else if (eap->cmdidx == CMD_mkvimrc)
8803 fname = (char_u *)VIMRC_FILE;
8804#ifdef FEAT_SESSION
8805 else if (eap->cmdidx == CMD_mksession)
8806 fname = (char_u *)SESSION_FILE;
8807#endif
8808 else
8809 fname = (char_u *)EXRC_FILE;
8810
8811#ifdef FEAT_BROWSE
8812 if (cmdmod.browse)
8813 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008814 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815# ifdef FEAT_SESSION
8816 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8817 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8818# endif
8819 (char_u *)_("Save Setup"),
8820 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8821 if (browseFile == NULL)
8822 goto theend;
8823 fname = browseFile;
8824 eap->forceit = TRUE; /* since dialog already asked */
8825 }
8826#endif
8827
8828#if defined(FEAT_SESSION) && defined(vim_mkdir)
8829 /* When using 'viewdir' may have to create the directory. */
8830 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008831 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832#endif
8833
8834 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8835 if (fd != NULL)
8836 {
8837#ifdef FEAT_SESSION
8838 if (eap->cmdidx == CMD_mkview)
8839 flagp = &vop_flags;
8840 else
8841 flagp = &ssop_flags;
8842#endif
8843
8844#ifdef MKSESSION_NL
8845 /* "unix" in 'sessionoptions': use NL line separator */
8846 if (view_session && (*flagp & SSOP_UNIX))
8847 mksession_nl = TRUE;
8848#endif
8849
8850 /* Write the version command for :mkvimrc */
8851 if (eap->cmdidx == CMD_mkvimrc)
8852 (void)put_line(fd, "version 6.0");
8853
8854#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008855 if (eap->cmdidx == CMD_mksession)
8856 {
8857 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8858 failed = TRUE;
8859 }
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (eap->cmdidx != CMD_mkview)
8862#endif
8863 {
8864 /* Write setting 'compatible' first, because it has side effects.
8865 * For that same reason only do it when needed. */
8866 if (p_cp)
8867 (void)put_line(fd, "if !&cp | set cp | endif");
8868 else
8869 (void)put_line(fd, "if &cp | set nocp | endif");
8870 }
8871
8872#ifdef FEAT_SESSION
8873 if (!view_session
8874 || (eap->cmdidx == CMD_mksession
8875 && (*flagp & SSOP_OPTIONS)))
8876#endif
8877 failed |= (makemap(fd, NULL) == FAIL
8878 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8879
8880#ifdef FEAT_SESSION
8881 if (!failed && view_session)
8882 {
8883 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8884 failed = TRUE;
8885 if (eap->cmdidx == CMD_mksession)
8886 {
8887 char_u dirnow[MAXPATHL]; /* current directory */
8888
8889 /*
8890 * Change to session file's dir.
8891 */
8892 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8893 || mch_chdir((char *)dirnow) != 0)
8894 *dirnow = NUL;
8895 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8896 {
8897 if (vim_chdirfile(fname) == OK)
8898 shorten_fnames(TRUE);
8899 }
8900 else if (*dirnow != NUL
8901 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8902 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008903 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008904 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906
8907 failed |= (makeopens(fd, dirnow) == FAIL);
8908
8909 /* restore original dir */
8910 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8911 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8912 {
8913 if (mch_chdir((char *)dirnow) != 0)
8914 EMSG(_(e_prev_dir));
8915 shorten_fnames(TRUE);
8916 }
8917 }
8918 else
8919 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008920 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8921 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 }
8923 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8924 == FAIL)
8925 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008926 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8927 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008928 if (eap->cmdidx == CMD_mksession)
8929 {
8930 if (put_line(fd, "unlet SessionLoad") == FAIL)
8931 failed = TRUE;
8932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008935 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8936 failed = TRUE;
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 failed |= fclose(fd);
8939
8940 if (failed)
8941 EMSG(_(e_write));
8942#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8943 else if (eap->cmdidx == CMD_mksession)
8944 {
8945 /* successful session write - set this_session var */
8946 char_u tbuf[MAXPATHL];
8947
8948 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8949 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8950 }
8951#endif
8952#ifdef MKSESSION_NL
8953 mksession_nl = FALSE;
8954#endif
8955 }
8956
8957#ifdef FEAT_BROWSE
8958theend:
8959 vim_free(browseFile);
8960#endif
8961#ifdef FEAT_SESSION
8962 vim_free(viewFile);
8963#endif
8964}
8965
Bram Moolenaardf177f62005-02-22 08:39:57 +00008966#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8967 || defined(PROTO)
8968 int
8969vim_mkdir_emsg(name, prot)
8970 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008971 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008972{
8973 if (vim_mkdir(name, prot) != 0)
8974 {
8975 EMSG2(_("E739: Cannot create directory: %s"), name);
8976 return FAIL;
8977 }
8978 return OK;
8979}
8980#endif
8981
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982/*
8983 * Open a file for writing for an Ex command, with some checks.
8984 * Return file descriptor, or NULL on failure.
8985 */
8986 FILE *
8987open_exfile(fname, forceit, mode)
8988 char_u *fname;
8989 int forceit;
8990 char *mode; /* "w" for create new file or "a" for append */
8991{
8992 FILE *fd;
8993
8994#ifdef UNIX
8995 /* with Unix it is possible to open a directory */
8996 if (mch_isdir(fname))
8997 {
8998 EMSG2(_(e_isadir2), fname);
8999 return NULL;
9000 }
9001#endif
9002 if (!forceit && *mode != 'a' && vim_fexists(fname))
9003 {
9004 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9005 return NULL;
9006 }
9007
9008 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9009 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9010
9011 return fd;
9012}
9013
9014/*
9015 * ":mark" and ":k".
9016 */
9017 static void
9018ex_mark(eap)
9019 exarg_T *eap;
9020{
9021 pos_T pos;
9022
9023 if (*eap->arg == NUL) /* No argument? */
9024 EMSG(_(e_argreq));
9025 else if (eap->arg[1] != NUL) /* more than one character? */
9026 EMSG(_(e_trailing));
9027 else
9028 {
9029 pos = curwin->w_cursor; /* save curwin->w_cursor */
9030 curwin->w_cursor.lnum = eap->line2;
9031 beginline(BL_WHITE | BL_FIX);
9032 if (setmark(*eap->arg) == FAIL) /* set mark */
9033 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9034 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9035 }
9036}
9037
9038/*
9039 * Update w_topline, w_leftcol and the cursor position.
9040 */
9041 void
9042update_topline_cursor()
9043{
9044 check_cursor(); /* put cursor on valid line */
9045 update_topline();
9046 if (!curwin->w_p_wrap)
9047 validate_cursor();
9048 update_curswant();
9049}
9050
9051#ifdef FEAT_EX_EXTRA
9052/*
9053 * ":normal[!] {commands}": Execute normal mode commands.
9054 */
9055 static void
9056ex_normal(eap)
9057 exarg_T *eap;
9058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 int save_msg_scroll = msg_scroll;
9060 int save_restart_edit = restart_edit;
9061 int save_msg_didout = msg_didout;
9062 int save_State = State;
9063 tasave_T tabuf;
9064 int save_insertmode = p_im;
9065 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009066 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067#ifdef FEAT_MBYTE
9068 char_u *arg = NULL;
9069 int l;
9070 char_u *p;
9071#endif
9072
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009073 if (ex_normal_lock > 0)
9074 {
9075 EMSG(_(e_secure));
9076 return;
9077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 if (ex_normal_busy >= p_mmd)
9079 {
9080 EMSG(_("E192: Recursive use of :normal too deep"));
9081 return;
9082 }
9083 ++ex_normal_busy;
9084
9085 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9086 restart_edit = 0; /* don't go to Insert mode */
9087 p_im = FALSE; /* don't use 'insertmode' */
9088
9089#ifdef FEAT_MBYTE
9090 /*
9091 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9092 * this for the K_SPECIAL leading byte, otherwise special keys will not
9093 * work.
9094 */
9095 if (has_mbyte)
9096 {
9097 int len = 0;
9098
9099 /* Count the number of characters to be escaped. */
9100 for (p = eap->arg; *p != NUL; ++p)
9101 {
9102# ifdef FEAT_GUI
9103 if (*p == CSI) /* leadbyte CSI */
9104 len += 2;
9105# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009106 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9108# ifdef FEAT_GUI
9109 || *p == CSI
9110# endif
9111 )
9112 len += 2;
9113 }
9114 if (len > 0)
9115 {
9116 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9117 if (arg != NULL)
9118 {
9119 len = 0;
9120 for (p = eap->arg; *p != NUL; ++p)
9121 {
9122 arg[len++] = *p;
9123# ifdef FEAT_GUI
9124 if (*p == CSI)
9125 {
9126 arg[len++] = KS_EXTRA;
9127 arg[len++] = (int)KE_CSI;
9128 }
9129# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009130 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 {
9132 arg[len++] = *++p;
9133 if (*p == K_SPECIAL)
9134 {
9135 arg[len++] = KS_SPECIAL;
9136 arg[len++] = KE_FILLER;
9137 }
9138# ifdef FEAT_GUI
9139 else if (*p == CSI)
9140 {
9141 arg[len++] = KS_EXTRA;
9142 arg[len++] = (int)KE_CSI;
9143 }
9144# endif
9145 }
9146 arg[len] = NUL;
9147 }
9148 }
9149 }
9150 }
9151#endif
9152
9153 /*
9154 * Save the current typeahead. This is required to allow using ":normal"
9155 * from an event handler and makes sure we don't hang when the argument
9156 * ends with half a command.
9157 */
9158 save_typeahead(&tabuf);
9159 if (tabuf.typebuf_valid)
9160 {
9161 /*
9162 * Repeat the :normal command for each line in the range. When no
9163 * range given, execute it just once, without positioning the cursor
9164 * first.
9165 */
9166 do
9167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (eap->addr_count != 0)
9169 {
9170 curwin->w_cursor.lnum = eap->line1++;
9171 curwin->w_cursor.col = 0;
9172 }
9173
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009174 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#ifdef FEAT_MBYTE
9176 arg != NULL ? arg :
9177#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009178 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9181 }
9182
9183 /* Might not return to the main loop when in an event handler. */
9184 update_topline_cursor();
9185
9186 /* Restore the previous typeahead. */
9187 restore_typeahead(&tabuf);
9188
9189 --ex_normal_busy;
9190 msg_scroll = save_msg_scroll;
9191 restart_edit = save_restart_edit;
9192 p_im = save_insertmode;
9193 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009194 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9196
9197 /* Restore the state (needed when called from a function executed for
9198 * 'indentexpr'). */
9199 State = save_State;
9200#ifdef FEAT_MBYTE
9201 vim_free(arg);
9202#endif
9203}
9204
9205/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009206 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 */
9208 static void
9209ex_startinsert(eap)
9210 exarg_T *eap;
9211{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009212 if (eap->forceit)
9213 {
9214 coladvance((colnr_T)MAXCOL);
9215 curwin->w_curswant = MAXCOL;
9216 curwin->w_set_curswant = FALSE;
9217 }
9218
Bram Moolenaara40c5002005-01-09 21:16:21 +00009219 /* Ignore the command when already in Insert mode. Inserting an
9220 * expression register that invokes a function can do this. */
9221 if (State & INSERT)
9222 return;
9223
Bram Moolenaar64969662005-12-14 21:59:55 +00009224 if (eap->cmdidx == CMD_startinsert)
9225 restart_edit = 'a';
9226 else if (eap->cmdidx == CMD_startreplace)
9227 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009229 restart_edit = 'V';
9230
9231 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009233 if (eap->cmdidx == CMD_startinsert)
9234 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 curwin->w_curswant = 0; /* avoid MAXCOL */
9236 }
9237}
9238
9239/*
9240 * ":stopinsert"
9241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 static void
9243ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009244 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245{
9246 restart_edit = 0;
9247 stop_insert_mode = TRUE;
9248}
9249#endif
9250
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009251#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9252/*
9253 * Execute normal mode command "cmd".
9254 * "remap" can be REMAP_NONE or REMAP_YES.
9255 */
9256 void
9257exec_normal_cmd(cmd, remap, silent)
9258 char_u *cmd;
9259 int remap;
9260 int silent;
9261{
9262 oparg_T oa;
9263
9264 /*
9265 * Stuff the argument into the typeahead buffer.
9266 * Execute normal_cmd() until there is no typeahead left.
9267 */
9268 clear_oparg(&oa);
9269 finish_op = FALSE;
9270 ins_typebuf(cmd, remap, 0, TRUE, silent);
9271 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9272 && !got_int)
9273 {
9274 update_topline_cursor();
9275 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9276 }
9277}
9278#endif
9279
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280#ifdef FEAT_FIND_ID
9281 static void
9282ex_checkpath(eap)
9283 exarg_T *eap;
9284{
9285 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9286 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9287 (linenr_T)1, (linenr_T)MAXLNUM);
9288}
9289
9290#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9291/*
9292 * ":psearch"
9293 */
9294 static void
9295ex_psearch(eap)
9296 exarg_T *eap;
9297{
9298 g_do_tagpreview = p_pvh;
9299 ex_findpat(eap);
9300 g_do_tagpreview = 0;
9301}
9302#endif
9303
9304 static void
9305ex_findpat(eap)
9306 exarg_T *eap;
9307{
9308 int whole = TRUE;
9309 long n;
9310 char_u *p;
9311 int action;
9312
9313 switch (cmdnames[eap->cmdidx].cmd_name[2])
9314 {
9315 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9316 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9317 action = ACTION_GOTO;
9318 else
9319 action = ACTION_SHOW;
9320 break;
9321 case 'i': /* ":ilist" and ":dlist" */
9322 action = ACTION_SHOW_ALL;
9323 break;
9324 case 'u': /* ":ijump" and ":djump" */
9325 action = ACTION_GOTO;
9326 break;
9327 default: /* ":isplit" and ":dsplit" */
9328 action = ACTION_SPLIT;
9329 break;
9330 }
9331
9332 n = 1;
9333 if (vim_isdigit(*eap->arg)) /* get count */
9334 {
9335 n = getdigits(&eap->arg);
9336 eap->arg = skipwhite(eap->arg);
9337 }
9338 if (*eap->arg == '/') /* Match regexp, not just whole words */
9339 {
9340 whole = FALSE;
9341 ++eap->arg;
9342 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9343 if (*p)
9344 {
9345 *p++ = NUL;
9346 p = skipwhite(p);
9347
9348 /* Check for trailing illegal characters */
9349 if (!ends_excmd(*p))
9350 eap->errmsg = e_trailing;
9351 else
9352 eap->nextcmd = check_nextcmd(p);
9353 }
9354 }
9355 if (!eap->skip)
9356 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9357 whole, !eap->forceit,
9358 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9359 n, action, eap->line1, eap->line2);
9360}
9361#endif
9362
9363#ifdef FEAT_WINDOWS
9364
9365# ifdef FEAT_QUICKFIX
9366/*
9367 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9368 */
9369 static void
9370ex_ptag(eap)
9371 exarg_T *eap;
9372{
9373 g_do_tagpreview = p_pvh;
9374 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9375}
9376
9377/*
9378 * ":pedit"
9379 */
9380 static void
9381ex_pedit(eap)
9382 exarg_T *eap;
9383{
9384 win_T *curwin_save = curwin;
9385
9386 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009387 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 keep_help_flag = curwin_save->w_buffer->b_help;
9389 do_exedit(eap, NULL);
9390 keep_help_flag = FALSE;
9391 if (curwin != curwin_save && win_valid(curwin_save))
9392 {
9393 /* Return cursor to where we were */
9394 validate_cursor();
9395 redraw_later(VALID);
9396 win_enter(curwin_save, TRUE);
9397 }
9398 g_do_tagpreview = 0;
9399}
9400# endif
9401
9402/*
9403 * ":stag", ":stselect" and ":stjump".
9404 */
9405 static void
9406ex_stag(eap)
9407 exarg_T *eap;
9408{
9409 postponed_split = -1;
9410 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009411 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9413 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009414 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415}
9416#endif
9417
9418/*
9419 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9420 */
9421 static void
9422ex_tag(eap)
9423 exarg_T *eap;
9424{
9425 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9426}
9427
9428 static void
9429ex_tag_cmd(eap, name)
9430 exarg_T *eap;
9431 char_u *name;
9432{
9433 int cmd;
9434
9435 switch (name[1])
9436 {
9437 case 'j': cmd = DT_JUMP; /* ":tjump" */
9438 break;
9439 case 's': cmd = DT_SELECT; /* ":tselect" */
9440 break;
9441 case 'p': cmd = DT_PREV; /* ":tprevious" */
9442 break;
9443 case 'N': cmd = DT_PREV; /* ":tNext" */
9444 break;
9445 case 'n': cmd = DT_NEXT; /* ":tnext" */
9446 break;
9447 case 'o': cmd = DT_POP; /* ":pop" */
9448 break;
9449 case 'f': /* ":tfirst" */
9450 case 'r': cmd = DT_FIRST; /* ":trewind" */
9451 break;
9452 case 'l': cmd = DT_LAST; /* ":tlast" */
9453 break;
9454 default: /* ":tag" */
9455#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009456 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 {
9458 do_cstag(eap);
9459 return;
9460 }
9461#endif
9462 cmd = DT_TAG;
9463 break;
9464 }
9465
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009466 if (name[0] == 'l')
9467 {
9468#ifndef FEAT_QUICKFIX
9469 ex_ni(eap);
9470 return;
9471#else
9472 cmd = DT_LTAG;
9473#endif
9474 }
9475
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9477 eap->forceit, TRUE);
9478}
9479
9480/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009481 * Check "str" for starting with a special cmdline variable.
9482 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9483 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9484 */
9485 int
9486find_cmdline_var(src, usedlen)
9487 char_u *src;
9488 int *usedlen;
9489{
9490 int len;
9491 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009492 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009493 "%",
9494#define SPEC_PERC 0
9495 "#",
9496#define SPEC_HASH 1
9497 "<cword>", /* cursor word */
9498#define SPEC_CWORD 2
9499 "<cWORD>", /* cursor WORD */
9500#define SPEC_CCWORD 3
9501 "<cfile>", /* cursor path name */
9502#define SPEC_CFILE 4
9503 "<sfile>", /* ":so" file name */
9504#define SPEC_SFILE 5
9505#ifdef FEAT_AUTOCMD
9506 "<afile>", /* autocommand file name */
9507# define SPEC_AFILE 6
9508 "<abuf>", /* autocommand buffer number */
9509# define SPEC_ABUF 7
9510 "<amatch>", /* autocommand match name */
9511# define SPEC_AMATCH 8
9512#endif
9513#ifdef FEAT_CLIENTSERVER
9514 "<client>"
9515# define SPEC_CLIENT 9
9516#endif
9517 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009518
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009519 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009520 {
9521 len = (int)STRLEN(spec_str[i]);
9522 if (STRNCMP(src, spec_str[i], len) == 0)
9523 {
9524 *usedlen = len;
9525 return i;
9526 }
9527 }
9528 return -1;
9529}
9530
9531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 * Evaluate cmdline variables.
9533 *
9534 * change '%' to curbuf->b_ffname
9535 * '#' to curwin->w_altfile
9536 * '<cword>' to word under the cursor
9537 * '<cWORD>' to WORD under the cursor
9538 * '<cfile>' to path name under the cursor
9539 * '<sfile>' to sourced file name
9540 * '<afile>' to file name for autocommand
9541 * '<abuf>' to buffer number for autocommand
9542 * '<amatch>' to matching name for autocommand
9543 *
9544 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9545 * "" for error without a message) and NULL is returned.
9546 * Returns an allocated string if a valid match was found.
9547 * Returns NULL if no match was found. "usedlen" then still contains the
9548 * number of characters to skip.
9549 */
9550 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009551eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009553 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 int *usedlen; /* characters after src that are used */
9555 linenr_T *lnump; /* line number for :e command, or NULL */
9556 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009557 int *escaped; /* return value has escaped white space (can
9558 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 int i;
9561 char_u *s;
9562 char_u *result;
9563 char_u *resultbuf = NULL;
9564 int resultlen;
9565 buf_T *buf;
9566 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9567 int spec_idx;
9568#ifdef FEAT_MODIFY_FNAME
9569 int skip_mod = FALSE;
9570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
9572#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9573 char_u strbuf[30];
9574#endif
9575
9576 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009577 if (escaped != NULL)
9578 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579
9580 /*
9581 * Check if there is something to do.
9582 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009583 spec_idx = find_cmdline_var(src, usedlen);
9584 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 {
9586 *usedlen = 1;
9587 return NULL;
9588 }
9589
9590 /*
9591 * Skip when preceded with a backslash "\%" and "\#".
9592 * Note: In "\\%" the % is also not recognized!
9593 */
9594 if (src > srcstart && src[-1] == '\\')
9595 {
9596 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009597 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 return NULL;
9599 }
9600
9601 /*
9602 * word or WORD under cursor
9603 */
9604 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9605 {
9606 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9607 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9608 if (resultlen == 0)
9609 {
9610 *errormsg = (char_u *)"";
9611 return NULL;
9612 }
9613 }
9614
9615 /*
9616 * '#': Alternate file name
9617 * '%': Current file name
9618 * File name under the cursor
9619 * File name for autocommand
9620 * and following modifiers
9621 */
9622 else
9623 {
9624 switch (spec_idx)
9625 {
9626 case SPEC_PERC: /* '%': current file */
9627 if (curbuf->b_fname == NULL)
9628 {
9629 result = (char_u *)"";
9630 valid = 0; /* Must have ":p:h" to be valid */
9631 }
9632 else
9633#ifdef RISCOS
9634 /* Always use the full path for RISC OS if possible. */
9635 result = curbuf->b_ffname;
9636 if (result == NULL)
9637 result = curbuf->b_fname;
9638#else
9639 result = curbuf->b_fname;
9640#endif
9641 break;
9642
9643 case SPEC_HASH: /* '#' or "#99": alternate file */
9644 if (src[1] == '#') /* "##": the argument list */
9645 {
9646 result = arg_all();
9647 resultbuf = result;
9648 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009649 if (escaped != NULL)
9650 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#ifdef FEAT_MODIFY_FNAME
9652 skip_mod = TRUE;
9653#endif
9654 break;
9655 }
9656 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009657 if (*s == '<') /* "#<99" uses v:oldfiles */
9658 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 i = (int)getdigits(&s);
9660 *usedlen = (int)(s - src); /* length of what we expand */
9661
Bram Moolenaard812df62008-11-09 12:46:09 +00009662 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009664 if (*usedlen < 2)
9665 {
9666 /* Should we give an error message for #<text? */
9667 *usedlen = 1;
9668 return NULL;
9669 }
9670#ifdef FEAT_EVAL
9671 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9672 (long)i);
9673 if (result == NULL)
9674 {
9675 *errormsg = (char_u *)"";
9676 return NULL;
9677 }
9678#else
9679 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
9683 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009684 {
9685 buf = buflist_findnr(i);
9686 if (buf == NULL)
9687 {
9688 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9689 return NULL;
9690 }
9691 if (lnump != NULL)
9692 *lnump = ECMD_LAST;
9693 if (buf->b_fname == NULL)
9694 {
9695 result = (char_u *)"";
9696 valid = 0; /* Must have ":p:h" to be valid */
9697 }
9698 else
9699 result = buf->b_fname;
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 break;
9702
9703#ifdef FEAT_SEARCHPATH
9704 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009705 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (result == NULL)
9707 {
9708 *errormsg = (char_u *)"";
9709 return NULL;
9710 }
9711 resultbuf = result; /* remember allocated string */
9712 break;
9713#endif
9714
9715#ifdef FEAT_AUTOCMD
9716 case SPEC_AFILE: /* file name for autocommand */
9717 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009718 if (result != NULL && !autocmd_fname_full)
9719 {
9720 /* Still need to turn the fname into a full path. It is
9721 * postponed to avoid a delay when <afile> is not used. */
9722 autocmd_fname_full = TRUE;
9723 result = FullName_save(autocmd_fname, FALSE);
9724 vim_free(autocmd_fname);
9725 autocmd_fname = result;
9726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (result == NULL)
9728 {
9729 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9730 return NULL;
9731 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009732 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 break;
9734
9735 case SPEC_ABUF: /* buffer number for autocommand */
9736 if (autocmd_bufnr <= 0)
9737 {
9738 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9739 return NULL;
9740 }
9741 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9742 result = strbuf;
9743 break;
9744
9745 case SPEC_AMATCH: /* match name for autocommand */
9746 result = autocmd_match;
9747 if (result == NULL)
9748 {
9749 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9750 return NULL;
9751 }
9752 break;
9753
9754#endif
9755 case SPEC_SFILE: /* file name for ":so" command */
9756 result = sourcing_name;
9757 if (result == NULL)
9758 {
9759 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9760 return NULL;
9761 }
9762 break;
9763#if defined(FEAT_CLIENTSERVER)
9764 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009765 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9766 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 result = strbuf;
9768 break;
9769#endif
9770 }
9771
9772 resultlen = (int)STRLEN(result); /* length of new string */
9773 if (src[*usedlen] == '<') /* remove the file name extension */
9774 {
9775 ++*usedlen;
9776#ifdef RISCOS
9777 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9778#else
9779 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9780#endif
9781 resultlen = (int)(s - result);
9782 }
9783#ifdef FEAT_MODIFY_FNAME
9784 else if (!skip_mod)
9785 {
9786 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9787 &resultlen);
9788 if (result == NULL)
9789 {
9790 *errormsg = (char_u *)"";
9791 return NULL;
9792 }
9793 }
9794#endif
9795 }
9796
9797 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9798 {
9799 if (valid != VALID_HEAD + VALID_PATH)
9800 /* xgettext:no-c-format */
9801 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9802 else
9803 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9804 result = NULL;
9805 }
9806 else
9807 result = vim_strnsave(result, resultlen);
9808 vim_free(resultbuf);
9809 return result;
9810}
9811
9812/*
9813 * Concatenate all files in the argument list, separated by spaces, and return
9814 * it in one allocated string.
9815 * Spaces and backslashes in the file names are escaped with a backslash.
9816 * Returns NULL when out of memory.
9817 */
9818 static char_u *
9819arg_all()
9820{
9821 int len;
9822 int idx;
9823 char_u *retval = NULL;
9824 char_u *p;
9825
9826 /*
9827 * Do this loop two times:
9828 * first time: compute the total length
9829 * second time: concatenate the names
9830 */
9831 for (;;)
9832 {
9833 len = 0;
9834 for (idx = 0; idx < ARGCOUNT; ++idx)
9835 {
9836 p = alist_name(&ARGLIST[idx]);
9837 if (p != NULL)
9838 {
9839 if (len > 0)
9840 {
9841 /* insert a space in between names */
9842 if (retval != NULL)
9843 retval[len] = ' ';
9844 ++len;
9845 }
9846 for ( ; *p != NUL; ++p)
9847 {
9848 if (*p == ' ' || *p == '\\')
9849 {
9850 /* insert a backslash */
9851 if (retval != NULL)
9852 retval[len] = '\\';
9853 ++len;
9854 }
9855 if (retval != NULL)
9856 retval[len] = *p;
9857 ++len;
9858 }
9859 }
9860 }
9861
9862 /* second time: break here */
9863 if (retval != NULL)
9864 {
9865 retval[len] = NUL;
9866 break;
9867 }
9868
9869 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009870 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 if (retval == NULL)
9872 break;
9873 }
9874
9875 return retval;
9876}
9877
9878#if defined(FEAT_AUTOCMD) || defined(PROTO)
9879/*
9880 * Expand the <sfile> string in "arg".
9881 *
9882 * Returns an allocated string, or NULL for any error.
9883 */
9884 char_u *
9885expand_sfile(arg)
9886 char_u *arg;
9887{
9888 char_u *errormsg;
9889 int len;
9890 char_u *result;
9891 char_u *newres;
9892 char_u *repl;
9893 int srclen;
9894 char_u *p;
9895
9896 result = vim_strsave(arg);
9897 if (result == NULL)
9898 return NULL;
9899
9900 for (p = result; *p; )
9901 {
9902 if (STRNCMP(p, "<sfile>", 7) != 0)
9903 ++p;
9904 else
9905 {
9906 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009907 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 if (errormsg != NULL)
9909 {
9910 if (*errormsg)
9911 emsg(errormsg);
9912 vim_free(result);
9913 return NULL;
9914 }
9915 if (repl == NULL) /* no match (cannot happen) */
9916 {
9917 p += srclen;
9918 continue;
9919 }
9920 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9921 newres = alloc(len);
9922 if (newres == NULL)
9923 {
9924 vim_free(repl);
9925 vim_free(result);
9926 return NULL;
9927 }
9928 mch_memmove(newres, result, (size_t)(p - result));
9929 STRCPY(newres + (p - result), repl);
9930 len = (int)STRLEN(newres);
9931 STRCAT(newres, p + srclen);
9932 vim_free(repl);
9933 vim_free(result);
9934 result = newres;
9935 p = newres + len; /* continue after the match */
9936 }
9937 }
9938
9939 return result;
9940}
9941#endif
9942
9943#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009944static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9945 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9947static frame_T *ses_skipframe __ARGS((frame_T *fr));
9948static int ses_do_frame __ARGS((frame_T *fr));
9949static int ses_do_win __ARGS((win_T *wp));
9950static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9951static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9952static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9953
9954/*
9955 * Write openfile commands for the current buffers to an .exrc file.
9956 * Return FAIL on error, OK otherwise.
9957 */
9958 static int
9959makeopens(fd, dirnow)
9960 FILE *fd;
9961 char_u *dirnow; /* Current directory name */
9962{
9963 buf_T *buf;
9964 int only_save_windows = TRUE;
9965 int nr;
9966 int cnr = 1;
9967 int restore_size = TRUE;
9968 win_T *wp;
9969 char_u *sname;
9970 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009971 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009972 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009973 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009974 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009975 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
9977 if (ssop_flags & SSOP_BUFFERS)
9978 only_save_windows = FALSE; /* Save ALL buffers */
9979
9980 /*
9981 * Begin by setting the this_session variable, and then other
9982 * sessionable variables.
9983 */
9984#ifdef FEAT_EVAL
9985 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9986 return FAIL;
9987 if (ssop_flags & SSOP_GLOBALS)
9988 if (store_session_globals(fd) == FAIL)
9989 return FAIL;
9990#endif
9991
9992 /*
9993 * Close all windows but one.
9994 */
9995 if (put_line(fd, "silent only") == FAIL)
9996 return FAIL;
9997
9998 /*
9999 * Now a :cd command to the session directory or the current directory
10000 */
10001 if (ssop_flags & SSOP_SESDIR)
10002 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010003 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10004 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 return FAIL;
10006 }
10007 else if (ssop_flags & SSOP_CURDIR)
10008 {
10009 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10010 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010011 || fputs("cd ", fd) < 0
10012 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10013 || put_eol(fd) == FAIL)
10014 {
10015 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 vim_free(sname);
10019 }
10020
10021 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010022 * If there is an empty, unnamed buffer we will wipe it out later.
10023 * Remember the buffer number.
10024 */
10025 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10026 return FAIL;
10027 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10028 return FAIL;
10029 if (put_line(fd, "endif") == FAIL)
10030 return FAIL;
10031
10032 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 * Now save the current files, current buffer first.
10034 */
10035 if (put_line(fd, "set shortmess=aoO") == FAIL)
10036 return FAIL;
10037
10038 /* Now put the other buffers into the buffer list */
10039 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10040 {
10041 if (!(only_save_windows && buf->b_nwindows == 0)
10042 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10043 && buf->b_fname != NULL
10044 && buf->b_p_bl)
10045 {
10046 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10047 : buf->b_wininfo->wi_fpos.lnum) < 0
10048 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10049 return FAIL;
10050 }
10051 }
10052
10053 /* the global argument list */
10054 if (ses_arglist(fd, "args", &global_alist.al_ga,
10055 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10056 return FAIL;
10057
10058 if (ssop_flags & SSOP_RESIZE)
10059 {
10060 /* Note: after the restore we still check it worked!*/
10061 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10062 || put_eol(fd) == FAIL)
10063 return FAIL;
10064 }
10065
10066#ifdef FEAT_GUI
10067 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10068 {
10069 int x, y;
10070
10071 if (gui_mch_get_winpos(&x, &y) == OK)
10072 {
10073 /* Note: after the restore we still check it worked!*/
10074 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10075 return FAIL;
10076 }
10077 }
10078#endif
10079
10080 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010081 * May repeat putting Windows for each tab, when "tabpages" is in
10082 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010083 * Don't use goto_tabpage(), it may change directory and trigger
10084 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010086 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010087 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010088 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010090 int need_tabnew = FALSE;
10091
Bram Moolenaar18144c82006-04-12 21:52:12 +000010092 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010094 tabpage_T *tp = find_tabpage(tabnr);
10095
10096 if (tp == NULL)
10097 break; /* done all tab pages */
10098 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010099 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010100 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010101 tab_topframe = topframe;
10102 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010103 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010104 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010105 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010106 tab_topframe = tp->tp_topframe;
10107 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010108 if (tabnr > 1)
10109 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaar18144c82006-04-12 21:52:12 +000010112 /*
10113 * Before creating the window layout, try loading one file. If this
10114 * is aborted we don't end up with a number of useless windows.
10115 * This may have side effects! (e.g., compressed or network file).
10116 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010117 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010118 {
10119 if (ses_do_win(wp)
10120 && wp->w_buffer->b_ffname != NULL
10121 && !wp->w_buffer->b_help
10122#ifdef FEAT_QUICKFIX
10123 && !bt_nofile(wp->w_buffer)
10124#endif
10125 )
10126 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010127 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010128 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10129 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010130 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010131 if (!wp->w_arg_idx_invalid)
10132 edited_win = wp;
10133 break;
10134 }
10135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010137 /* If no file got edited create an empty tab page. */
10138 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10139 return FAIL;
10140
Bram Moolenaar18144c82006-04-12 21:52:12 +000010141 /*
10142 * Save current window layout.
10143 */
10144 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010146 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010148 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10149 return FAIL;
10150 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
Bram Moolenaar18144c82006-04-12 21:52:12 +000010153 /*
10154 * Check if window sizes can be restored (no windows omitted).
10155 * Remember the window number of the current window after restoring.
10156 */
10157 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010158 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010159 {
10160 if (ses_do_win(wp))
10161 ++nr;
10162 else
10163 restore_size = FALSE;
10164 if (curwin == wp)
10165 cnr = nr;
10166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167
Bram Moolenaar18144c82006-04-12 21:52:12 +000010168 /* Go to the first window. */
10169 if (put_line(fd, "wincmd t") == FAIL)
10170 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010171
Bram Moolenaar18144c82006-04-12 21:52:12 +000010172 /*
10173 * If more than one window, see if sizes can be restored.
10174 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10175 * resized when moving between windows.
10176 * Do this before restoring the view, so that the topline and the
10177 * cursor can be set. This is done again below.
10178 */
10179 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10180 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010181 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
Bram Moolenaar18144c82006-04-12 21:52:12 +000010184 /*
10185 * Restore the view of the window (options, file, cursor, etc.).
10186 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010187 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010188 {
10189 if (!ses_do_win(wp))
10190 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010191 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10192 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010193 return FAIL;
10194 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10195 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010196 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010197 }
10198
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010199 /* The argument index in the first tab page is zero, need to set it in
10200 * each window. For further tab pages it's the window where we do
10201 * "tabedit". */
10202 cur_arg_idx = next_arg_idx;
10203
Bram Moolenaar18144c82006-04-12 21:52:12 +000010204 /*
10205 * Restore cursor to the current window if it's not the first one.
10206 */
10207 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10208 || put_eol(fd) == FAIL))
10209 return FAIL;
10210
10211 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010212 * Restore window sizes again after jumping around in windows, because
10213 * the current window has a minimum size while others may not.
10214 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010215 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010216 return FAIL;
10217
Bram Moolenaar18144c82006-04-12 21:52:12 +000010218 /* Don't continue in another tab page when doing only the current one
10219 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010220 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010221 break;
10222 }
10223
10224 if (ssop_flags & SSOP_TABPAGES)
10225 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010226 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10227 || put_eol(fd) == FAIL)
10228 return FAIL;
10229 }
10230
Bram Moolenaar9c102382006-05-03 21:26:49 +000010231 /*
10232 * Wipe out an empty unnamed buffer we started in.
10233 */
10234 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10235 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010236 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010237 return FAIL;
10238 if (put_line(fd, "endif") == FAIL)
10239 return FAIL;
10240 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10241 return FAIL;
10242
10243 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10244 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10245 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247
10248 /*
10249 * Lastly, execute the x.vim file if it exists.
10250 */
10251 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10252 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010253 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 || put_line(fd, "endif") == FAIL)
10255 return FAIL;
10256
10257 return OK;
10258}
10259
10260 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010261ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 FILE *fd;
10263 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010264 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265{
10266 int n = 0;
10267 win_T *wp;
10268
10269 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10270 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010271 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 {
10273 if (!ses_do_win(wp))
10274 continue;
10275 ++n;
10276
10277 /* restore height when not full height */
10278 if (wp->w_height + wp->w_status_height < topframe->fr_height
10279 && (fprintf(fd,
10280 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10281 n, (long)wp->w_height, Rows / 2, Rows) < 0
10282 || put_eol(fd) == FAIL))
10283 return FAIL;
10284
10285 /* restore width when not full width */
10286 if (wp->w_width < Columns && (fprintf(fd,
10287 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10288 n, (long)wp->w_width, Columns / 2, Columns) < 0
10289 || put_eol(fd) == FAIL))
10290 return FAIL;
10291 }
10292 }
10293 else
10294 {
10295 /* Just equalise window sizes */
10296 if (put_line(fd, "wincmd =") == FAIL)
10297 return FAIL;
10298 }
10299 return OK;
10300}
10301
10302/*
10303 * Write commands to "fd" to recursively create windows for frame "fr",
10304 * horizontally and vertically split.
10305 * After the commands the last window in the frame is the current window.
10306 * Returns FAIL when writing the commands to "fd" fails.
10307 */
10308 static int
10309ses_win_rec(fd, fr)
10310 FILE *fd;
10311 frame_T *fr;
10312{
10313 frame_T *frc;
10314 int count = 0;
10315
10316 if (fr->fr_layout != FR_LEAF)
10317 {
10318 /* Find first frame that's not skipped and then create a window for
10319 * each following one (first frame is already there). */
10320 frc = ses_skipframe(fr->fr_child);
10321 if (frc != NULL)
10322 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10323 {
10324 /* Make window as big as possible so that we have lots of room
10325 * to split. */
10326 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10327 || put_line(fd, fr->fr_layout == FR_COL
10328 ? "split" : "vsplit") == FAIL)
10329 return FAIL;
10330 ++count;
10331 }
10332
10333 /* Go back to the first window. */
10334 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10335 ? "%dwincmd k" : "%dwincmd h", count) < 0
10336 || put_eol(fd) == FAIL))
10337 return FAIL;
10338
10339 /* Recursively create frames/windows in each window of this column or
10340 * row. */
10341 frc = ses_skipframe(fr->fr_child);
10342 while (frc != NULL)
10343 {
10344 ses_win_rec(fd, frc);
10345 frc = ses_skipframe(frc->fr_next);
10346 /* Go to next window. */
10347 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10348 return FAIL;
10349 }
10350 }
10351 return OK;
10352}
10353
10354/*
10355 * Skip frames that don't contain windows we want to save in the Session.
10356 * Returns NULL when there none.
10357 */
10358 static frame_T *
10359ses_skipframe(fr)
10360 frame_T *fr;
10361{
10362 frame_T *frc;
10363
10364 for (frc = fr; frc != NULL; frc = frc->fr_next)
10365 if (ses_do_frame(frc))
10366 break;
10367 return frc;
10368}
10369
10370/*
10371 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10372 * the Session.
10373 */
10374 static int
10375ses_do_frame(fr)
10376 frame_T *fr;
10377{
10378 frame_T *frc;
10379
10380 if (fr->fr_layout == FR_LEAF)
10381 return ses_do_win(fr->fr_win);
10382 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10383 if (ses_do_frame(frc))
10384 return TRUE;
10385 return FALSE;
10386}
10387
10388/*
10389 * Return non-zero if window "wp" is to be stored in the Session.
10390 */
10391 static int
10392ses_do_win(wp)
10393 win_T *wp;
10394{
10395 if (wp->w_buffer->b_fname == NULL
10396#ifdef FEAT_QUICKFIX
10397 /* When 'buftype' is "nofile" can't restore the window contents. */
10398 || bt_nofile(wp->w_buffer)
10399#endif
10400 )
10401 return (ssop_flags & SSOP_BLANK);
10402 if (wp->w_buffer->b_help)
10403 return (ssop_flags & SSOP_HELP);
10404 return TRUE;
10405}
10406
10407/*
10408 * Write commands to "fd" to restore the view of a window.
10409 * Caller must make sure 'scrolloff' is zero.
10410 */
10411 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010412put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 FILE *fd;
10414 win_T *wp;
10415 int add_edit; /* add ":edit" command to view */
10416 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010417 int current_arg_idx; /* current argument index of the window, use
10418 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419{
10420 win_T *save_curwin;
10421 int f;
10422 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010423 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424
10425 /* Always restore cursor position for ":mksession". For ":mkview" only
10426 * when 'viewoptions' contains "cursor". */
10427 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10428
10429 /*
10430 * Local argument list.
10431 */
10432 if (wp->w_alist == &global_alist)
10433 {
10434 if (put_line(fd, "argglobal") == FAIL)
10435 return FAIL;
10436 }
10437 else
10438 {
10439 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10440 flagp == &vop_flags
10441 || !(*flagp & SSOP_CURDIR)
10442 || wp->w_localdir != NULL, flagp) == FAIL)
10443 return FAIL;
10444 }
10445
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010446 /* Only when part of a session: restore the argument index. Some
10447 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010448 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010449 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010451 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 || put_eol(fd) == FAIL)
10453 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010454 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 }
10456
10457 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010458 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 {
10460 /*
10461 * Load the file.
10462 */
10463 if (wp->w_buffer->b_ffname != NULL
10464#ifdef FEAT_QUICKFIX
10465 && !bt_nofile(wp->w_buffer)
10466#endif
10467 )
10468 {
10469 /*
10470 * Editing a file in this buffer: use ":edit file".
10471 * This may have side effects! (e.g., compressed or network file).
10472 */
10473 if (fputs("edit ", fd) < 0
10474 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10475 return FAIL;
10476 }
10477 else
10478 {
10479 /* No file in this buffer, just make it empty. */
10480 if (put_line(fd, "enew") == FAIL)
10481 return FAIL;
10482#ifdef FEAT_QUICKFIX
10483 if (wp->w_buffer->b_ffname != NULL)
10484 {
10485 /* The buffer does have a name, but it's not a file name. */
10486 if (fputs("file ", fd) < 0
10487 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10488 return FAIL;
10489 }
10490#endif
10491 do_cursor = FALSE;
10492 }
10493 }
10494
10495 /*
10496 * Local mappings and abbreviations.
10497 */
10498 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10499 && makemap(fd, wp->w_buffer) == FAIL)
10500 return FAIL;
10501
10502 /*
10503 * Local options. Need to go to the window temporarily.
10504 * Store only local values when using ":mkview" and when ":mksession" is
10505 * used and 'sessionoptions' doesn't include "options".
10506 * Some folding options are always stored when "folds" is included,
10507 * otherwise the folds would not be restored correctly.
10508 */
10509 save_curwin = curwin;
10510 curwin = wp;
10511 curbuf = curwin->w_buffer;
10512 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10513 f = makeset(fd, OPT_LOCAL,
10514 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10515#ifdef FEAT_FOLDING
10516 else if (*flagp & SSOP_FOLDS)
10517 f = makefoldset(fd);
10518#endif
10519 else
10520 f = OK;
10521 curwin = save_curwin;
10522 curbuf = curwin->w_buffer;
10523 if (f == FAIL)
10524 return FAIL;
10525
10526#ifdef FEAT_FOLDING
10527 /*
10528 * Save Folds when 'buftype' is empty and for help files.
10529 */
10530 if ((*flagp & SSOP_FOLDS)
10531 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010532# ifdef FEAT_QUICKFIX
10533 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10534# endif
10535 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 {
10537 if (put_folds(fd, wp) == FAIL)
10538 return FAIL;
10539 }
10540#endif
10541
10542 /*
10543 * Set the cursor after creating folds, since that moves the cursor.
10544 */
10545 if (do_cursor)
10546 {
10547
10548 /* Restore the cursor line in the file and relatively in the
10549 * window. Don't use "G", it changes the jumplist. */
10550 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10551 (long)wp->w_cursor.lnum,
10552 (long)(wp->w_cursor.lnum - wp->w_topline),
10553 (long)wp->w_height / 2, (long)wp->w_height) < 0
10554 || put_eol(fd) == FAIL
10555 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10556 || put_line(fd, "exe s:l") == FAIL
10557 || put_line(fd, "normal! zt") == FAIL
10558 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10559 || put_eol(fd) == FAIL)
10560 return FAIL;
10561 /* Restore the cursor column and left offset when not wrapping. */
10562 if (wp->w_cursor.col == 0)
10563 {
10564 if (put_line(fd, "normal! 0") == FAIL)
10565 return FAIL;
10566 }
10567 else
10568 {
10569 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10570 {
10571 if (fprintf(fd,
10572 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10573 (long)wp->w_cursor.col,
10574 (long)(wp->w_cursor.col - wp->w_leftcol),
10575 (long)wp->w_width / 2, (long)wp->w_width) < 0
10576 || put_eol(fd) == FAIL
10577 || put_line(fd, "if s:c > 0") == FAIL
10578 || fprintf(fd,
10579 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10580 (long)wp->w_cursor.col) < 0
10581 || put_eol(fd) == FAIL
10582 || put_line(fd, "else") == FAIL
10583 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10584 || put_eol(fd) == FAIL
10585 || put_line(fd, "endif") == FAIL)
10586 return FAIL;
10587 }
10588 else
10589 {
10590 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10591 || put_eol(fd) == FAIL)
10592 return FAIL;
10593 }
10594 }
10595 }
10596
10597 /*
10598 * Local directory.
10599 */
10600 if (wp->w_localdir != NULL)
10601 {
10602 if (fputs("lcd ", fd) < 0
10603 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10604 || put_eol(fd) == FAIL)
10605 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010606 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 }
10608
10609 return OK;
10610}
10611
10612/*
10613 * Write an argument list to the session file.
10614 * Returns FAIL if writing fails.
10615 */
10616 static int
10617ses_arglist(fd, cmd, gap, fullname, flagp)
10618 FILE *fd;
10619 char *cmd;
10620 garray_T *gap;
10621 int fullname; /* TRUE: use full path name */
10622 unsigned *flagp;
10623{
10624 int i;
10625 char_u buf[MAXPATHL];
10626 char_u *s;
10627
10628 if (gap->ga_len == 0)
10629 return put_line(fd, "silent! argdel *");
10630 if (fputs(cmd, fd) < 0)
10631 return FAIL;
10632 for (i = 0; i < gap->ga_len; ++i)
10633 {
10634 /* NULL file names are skipped (only happens when out of memory). */
10635 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10636 if (s != NULL)
10637 {
10638 if (fullname)
10639 {
10640 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10641 s = buf;
10642 }
10643 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10644 return FAIL;
10645 }
10646 }
10647 return put_eol(fd);
10648}
10649
10650/*
10651 * Write a buffer name to the session file.
10652 * Also ends the line.
10653 * Returns FAIL if writing fails.
10654 */
10655 static int
10656ses_fname(fd, buf, flagp)
10657 FILE *fd;
10658 buf_T *buf;
10659 unsigned *flagp;
10660{
10661 char_u *name;
10662
10663 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010664 * the session file will be sourced.
10665 * Don't do this for ":mkview", we don't know the current directory.
10666 * Don't do this after ":lcd", we don't keep track of what the current
10667 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 if (buf->b_sfname != NULL
10669 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010670 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010671#ifdef FEAT_AUTOCHDIR
10672 && !p_acd
10673#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010674 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 name = buf->b_sfname;
10676 else
10677 name = buf->b_ffname;
10678 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10679 return FAIL;
10680 return OK;
10681}
10682
10683/*
10684 * Write a file name to the session file.
10685 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10686 * characters.
10687 * Returns FAIL if writing fails.
10688 */
10689 static int
10690ses_put_fname(fd, name, flagp)
10691 FILE *fd;
10692 char_u *name;
10693 unsigned *flagp;
10694{
10695 char_u *sname;
10696 int retval = OK;
10697 int c;
10698
10699 sname = home_replace_save(NULL, name);
10700 if (sname != NULL)
10701 name = sname;
10702 while (*name != NUL)
10703 {
10704#ifdef FEAT_MBYTE
10705 {
10706 int l;
10707
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010708 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
10710 /* copy a multibyte char */
10711 while (--l >= 0)
10712 {
10713 if (putc(*name, fd) != *name)
10714 retval = FAIL;
10715 ++name;
10716 }
10717 continue;
10718 }
10719 }
10720#endif
10721 c = *name++;
10722 if (c == '\\' && (*flagp & SSOP_SLASH))
10723 /* change a backslash to a forward slash */
10724 c = '/';
10725 else if ((vim_strchr(escape_chars, c) != NULL
10726#ifdef BACKSLASH_IN_FILENAME
10727 && c != '\\'
10728#endif
10729 ) || c == '#' || c == '%')
10730 {
10731 /* escape a special character with a backslash */
10732 if (putc('\\', fd) != '\\')
10733 retval = FAIL;
10734 }
10735 if (putc(c, fd) != c)
10736 retval = FAIL;
10737 }
10738 vim_free(sname);
10739 return retval;
10740}
10741
10742/*
10743 * ":loadview [nr]"
10744 */
10745 static void
10746ex_loadview(eap)
10747 exarg_T *eap;
10748{
10749 char_u *fname;
10750
10751 fname = get_view_file(*eap->arg);
10752 if (fname != NULL)
10753 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010754 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 vim_free(fname);
10756 }
10757}
10758
10759/*
10760 * Get the name of the view file for the current buffer.
10761 */
10762 static char_u *
10763get_view_file(c)
10764 int c;
10765{
10766 int len = 0;
10767 char_u *p, *s;
10768 char_u *retval;
10769 char_u *sname;
10770
10771 if (curbuf->b_ffname == NULL)
10772 {
10773 EMSG(_(e_noname));
10774 return NULL;
10775 }
10776 sname = home_replace_save(NULL, curbuf->b_ffname);
10777 if (sname == NULL)
10778 return NULL;
10779
10780 /*
10781 * We want a file name without separators, because we're not going to make
10782 * a directory.
10783 * "normal" path separator -> "=+"
10784 * "=" -> "=="
10785 * ":" path separator -> "=-"
10786 */
10787 for (p = sname; *p; ++p)
10788 if (*p == '=' || vim_ispathsep(*p))
10789 ++len;
10790 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10791 if (retval != NULL)
10792 {
10793 STRCPY(retval, p_vdir);
10794 add_pathsep(retval);
10795 s = retval + STRLEN(retval);
10796 for (p = sname; *p; ++p)
10797 {
10798 if (*p == '=')
10799 {
10800 *s++ = '=';
10801 *s++ = '=';
10802 }
10803 else if (vim_ispathsep(*p))
10804 {
10805 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010806#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 || defined(VMS)
10808 if (*p == ':')
10809 *s++ = '-';
10810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010812 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 }
10814 else
10815 *s++ = *p;
10816 }
10817 *s++ = '=';
10818 *s++ = c;
10819 STRCPY(s, ".vim");
10820 }
10821
10822 vim_free(sname);
10823 return retval;
10824}
10825
10826#endif /* FEAT_SESSION */
10827
10828/*
10829 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10830 * Return FAIL for a write error.
10831 */
10832 int
10833put_eol(fd)
10834 FILE *fd;
10835{
10836 if (
10837#ifdef USE_CRNL
10838 (
10839# ifdef MKSESSION_NL
10840 !mksession_nl &&
10841# endif
10842 (putc('\r', fd) < 0)) ||
10843#endif
10844 (putc('\n', fd) < 0))
10845 return FAIL;
10846 return OK;
10847}
10848
10849/*
10850 * Write a line to "fd".
10851 * Return FAIL for a write error.
10852 */
10853 int
10854put_line(fd, s)
10855 FILE *fd;
10856 char *s;
10857{
10858 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10859 return FAIL;
10860 return OK;
10861}
10862
10863#ifdef FEAT_VIMINFO
10864/*
10865 * ":rviminfo" and ":wviminfo".
10866 */
10867 static void
10868ex_viminfo(eap)
10869 exarg_T *eap;
10870{
10871 char_u *save_viminfo;
10872
10873 save_viminfo = p_viminfo;
10874 if (*p_viminfo == NUL)
10875 p_viminfo = (char_u *)"'100";
10876 if (eap->cmdidx == CMD_rviminfo)
10877 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010878 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10879 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 EMSG(_("E195: Cannot open viminfo file for reading"));
10881 }
10882 else
10883 write_viminfo(eap->arg, eap->forceit);
10884 p_viminfo = save_viminfo;
10885}
10886#endif
10887
10888#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010889/*
10890 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010891 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 void
10894dialog_msg(buff, format, fname)
10895 char_u *buff;
10896 char *format;
10897 char_u *fname;
10898{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 if (fname == NULL)
10900 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010901 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902}
10903#endif
10904
10905/*
10906 * ":behave {mswin,xterm}"
10907 */
10908 static void
10909ex_behave(eap)
10910 exarg_T *eap;
10911{
10912 if (STRCMP(eap->arg, "mswin") == 0)
10913 {
10914 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10915 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10916 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10917 set_option_value((char_u *)"keymodel", 0L,
10918 (char_u *)"startsel,stopsel", 0);
10919 }
10920 else if (STRCMP(eap->arg, "xterm") == 0)
10921 {
10922 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10923 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10924 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10925 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10926 }
10927 else
10928 EMSG2(_(e_invarg2), eap->arg);
10929}
10930
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010931#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10932/*
10933 * Function given to ExpandGeneric() to obtain the possible arguments of the
10934 * ":behave {mswin,xterm}" command.
10935 */
10936 char_u *
10937get_behave_arg(xp, idx)
10938 expand_T *xp UNUSED;
10939 int idx;
10940{
10941 if (idx == 0)
10942 return (char_u *)"mswin";
10943 if (idx == 1)
10944 return (char_u *)"xterm";
10945 return NULL;
10946}
10947#endif
10948
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949#ifdef FEAT_AUTOCMD
10950static int filetype_detect = FALSE;
10951static int filetype_plugin = FALSE;
10952static int filetype_indent = FALSE;
10953
10954/*
10955 * ":filetype [plugin] [indent] {on,off,detect}"
10956 * on: Load the filetype.vim file to install autocommands for file types.
10957 * off: Load the ftoff.vim file to remove all autocommands for file types.
10958 * plugin on: load filetype.vim and ftplugin.vim
10959 * plugin off: load ftplugof.vim
10960 * indent on: load filetype.vim and indent.vim
10961 * indent off: load indoff.vim
10962 */
10963 static void
10964ex_filetype(eap)
10965 exarg_T *eap;
10966{
10967 char_u *arg = eap->arg;
10968 int plugin = FALSE;
10969 int indent = FALSE;
10970
10971 if (*eap->arg == NUL)
10972 {
10973 /* Print current status. */
10974 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10975 filetype_detect ? "ON" : "OFF",
10976 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10977 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10978 return;
10979 }
10980
10981 /* Accept "plugin" and "indent" in any order. */
10982 for (;;)
10983 {
10984 if (STRNCMP(arg, "plugin", 6) == 0)
10985 {
10986 plugin = TRUE;
10987 arg = skipwhite(arg + 6);
10988 continue;
10989 }
10990 if (STRNCMP(arg, "indent", 6) == 0)
10991 {
10992 indent = TRUE;
10993 arg = skipwhite(arg + 6);
10994 continue;
10995 }
10996 break;
10997 }
10998 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10999 {
11000 if (*arg == 'o' || !filetype_detect)
11001 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011002 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 filetype_detect = TRUE;
11004 if (plugin)
11005 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011006 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 filetype_plugin = TRUE;
11008 }
11009 if (indent)
11010 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011011 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 filetype_indent = TRUE;
11013 }
11014 }
11015 if (*arg == 'd')
11016 {
11017 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011018 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 }
11020 }
11021 else if (STRCMP(arg, "off") == 0)
11022 {
11023 if (plugin || indent)
11024 {
11025 if (plugin)
11026 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011027 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 filetype_plugin = FALSE;
11029 }
11030 if (indent)
11031 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011032 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 filetype_indent = FALSE;
11034 }
11035 }
11036 else
11037 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011038 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 filetype_detect = FALSE;
11040 }
11041 }
11042 else
11043 EMSG2(_(e_invarg2), arg);
11044}
11045
11046/*
11047 * ":setfiletype {name}"
11048 */
11049 static void
11050ex_setfiletype(eap)
11051 exarg_T *eap;
11052{
11053 if (!did_filetype)
11054 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11055}
11056#endif
11057
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 static void
11059ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011060 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062#ifdef FEAT_DIGRAPHS
11063 if (*eap->arg != NUL)
11064 putdigraph(eap->arg);
11065 else
11066 listdigraphs();
11067#else
11068 EMSG(_("E196: No digraphs in this version"));
11069#endif
11070}
11071
11072 static void
11073ex_set(eap)
11074 exarg_T *eap;
11075{
11076 int flags = 0;
11077
11078 if (eap->cmdidx == CMD_setlocal)
11079 flags = OPT_LOCAL;
11080 else if (eap->cmdidx == CMD_setglobal)
11081 flags = OPT_GLOBAL;
11082#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11083 if (cmdmod.browse && flags == 0)
11084 ex_options(eap);
11085 else
11086#endif
11087 (void)do_set(eap->arg, flags);
11088}
11089
11090#ifdef FEAT_SEARCH_EXTRA
11091/*
11092 * ":nohlsearch"
11093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 static void
11095ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011096 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011099 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100}
11101
11102/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011103 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 * Sets nextcmd to the start of the next command, if any. Also called when
11105 * skipping commands to find the next command.
11106 */
11107 static void
11108ex_match(eap)
11109 exarg_T *eap;
11110{
11111 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011112 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 char_u *end;
11114 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011115 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011116
11117 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011118 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011119 else
11120 {
11121 EMSG(e_invcmd);
11122 return;
11123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124
11125 /* First clear any old pattern. */
11126 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011127 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
11129 if (ends_excmd(*eap->arg))
11130 end = eap->arg;
11131 else if ((STRNICMP(eap->arg, "none", 4) == 0
11132 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11133 end = eap->arg + 4;
11134 else
11135 {
11136 p = skiptowhite(eap->arg);
11137 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011138 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 p = skipwhite(p);
11140 if (*p == NUL)
11141 {
11142 /* There must be two arguments. */
11143 EMSG2(_(e_invarg2), eap->arg);
11144 return;
11145 }
11146 end = skip_regexp(p + 1, *p, TRUE, NULL);
11147 if (!eap->skip)
11148 {
11149 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11150 {
11151 eap->errmsg = e_trailing;
11152 return;
11153 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011154 if (*end != *p)
11155 {
11156 EMSG2(_(e_invarg2), p);
11157 return;
11158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159
11160 c = *end;
11161 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011162 match_add(curwin, g, p + 1, 10, id);
11163 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011164 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 }
11166 }
11167 eap->nextcmd = find_nextcmd(end);
11168}
11169#endif
11170
11171#ifdef FEAT_CRYPT
11172/*
11173 * ":X": Get crypt key
11174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 static void
11176ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011177 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011179 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11180 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181}
11182#endif
11183
11184#ifdef FEAT_FOLDING
11185 static void
11186ex_fold(eap)
11187 exarg_T *eap;
11188{
11189 if (foldManualAllowed(TRUE))
11190 foldCreate(eap->line1, eap->line2);
11191}
11192
11193 static void
11194ex_foldopen(eap)
11195 exarg_T *eap;
11196{
11197 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11198 eap->forceit, FALSE);
11199}
11200
11201 static void
11202ex_folddo(eap)
11203 exarg_T *eap;
11204{
11205 linenr_T lnum;
11206
11207 /* First set the marks for all lines closed/open. */
11208 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11209 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11210 ml_setmarked(lnum);
11211
11212 /* Execute the command on the marked lines. */
11213 global_exe(eap->arg);
11214 ml_clearmarked(); /* clear rest of the marks */
11215}
11216#endif