blob: b9a9da310709d6c5444d06ec0fc1e5fbb68fe7cb [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 Moolenaar325b7a22004-07-05 15:58:32 +0000251#ifndef FEAT_MZSCHEME
252# define ex_mzscheme ex_script_ni
253# define ex_mzfile ex_ni
254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#ifndef FEAT_PERL
256# define ex_perl ex_script_ni
257# define ex_perldo ex_ni
258#endif
259#ifndef FEAT_PYTHON
260# define ex_python ex_script_ni
261# define ex_pyfile ex_ni
262#endif
263#ifndef FEAT_TCL
264# define ex_tcl ex_script_ni
265# define ex_tcldo ex_ni
266# define ex_tclfile ex_ni
267#endif
268#ifndef FEAT_RUBY
269# define ex_ruby ex_script_ni
270# define ex_rubydo ex_ni
271# define ex_rubyfile ex_ni
272#endif
273#ifndef FEAT_SNIFF
274# define ex_sniff ex_ni
275#endif
276#ifndef FEAT_KEYMAP
277# define ex_loadkeymap ex_ni
278#endif
279static void ex_swapname __ARGS((exarg_T *eap));
280static void ex_syncbind __ARGS((exarg_T *eap));
281static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282static void ex_pwd __ARGS((exarg_T *eap));
283static void ex_equal __ARGS((exarg_T *eap));
284static void ex_sleep __ARGS((exarg_T *eap));
285static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
286static void ex_winsize __ARGS((exarg_T *eap));
287#ifdef FEAT_WINDOWS
288static void ex_wincmd __ARGS((exarg_T *eap));
289#else
290# define ex_wincmd ex_ni
291#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000292#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static void ex_winpos __ARGS((exarg_T *eap));
294#else
295# define ex_winpos ex_ni
296#endif
297static void ex_operators __ARGS((exarg_T *eap));
298static void ex_put __ARGS((exarg_T *eap));
299static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000300static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static void ex_submagic __ARGS((exarg_T *eap));
302static void ex_join __ARGS((exarg_T *eap));
303static void ex_at __ARGS((exarg_T *eap));
304static void ex_bang __ARGS((exarg_T *eap));
305static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200306#ifdef FEAT_PERSISTENT_UNDO
307static void ex_wundo __ARGS((exarg_T *eap));
308static void ex_rundo __ARGS((exarg_T *eap));
309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000311static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static void ex_redir __ARGS((exarg_T *eap));
313static void ex_redraw __ARGS((exarg_T *eap));
314static void ex_redrawstatus __ARGS((exarg_T *eap));
315static void close_redir __ARGS((void));
316static void ex_mkrc __ARGS((exarg_T *eap));
317static void ex_mark __ARGS((exarg_T *eap));
318#ifdef FEAT_USR_CMDS
319static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000320static 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 +0000321#endif
322#ifdef FEAT_EX_EXTRA
323static void ex_normal __ARGS((exarg_T *eap));
324static void ex_startinsert __ARGS((exarg_T *eap));
325static void ex_stopinsert __ARGS((exarg_T *eap));
326#else
327# define ex_normal ex_ni
328# define ex_align ex_ni
329# define ex_retab ex_ni
330# define ex_startinsert ex_ni
331# define ex_stopinsert ex_ni
332# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000333# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#endif
335#ifdef FEAT_FIND_ID
336static void ex_checkpath __ARGS((exarg_T *eap));
337static void ex_findpat __ARGS((exarg_T *eap));
338#else
339# define ex_findpat ex_ni
340# define ex_checkpath ex_ni
341#endif
342#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
343static void ex_psearch __ARGS((exarg_T *eap));
344#else
345# define ex_psearch ex_ni
346#endif
347static void ex_tag __ARGS((exarg_T *eap));
348static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
349#ifndef FEAT_EVAL
350# define ex_scriptnames ex_ni
351# define ex_finish ex_ni
352# define ex_echo ex_ni
353# define ex_echohl ex_ni
354# define ex_execute ex_ni
355# define ex_call ex_ni
356# define ex_if ex_ni
357# define ex_endif ex_ni
358# define ex_else ex_ni
359# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000360# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_continue ex_ni
362# define ex_break ex_ni
363# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000364# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_throw ex_ni
366# define ex_try ex_ni
367# define ex_catch ex_ni
368# define ex_finally ex_ni
369# define ex_endtry ex_ni
370# define ex_endfunction ex_ni
371# define ex_let ex_ni
372# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000373# define ex_lockvar ex_ni
374# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_function ex_ni
376# define ex_delfunction ex_ni
377# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000378# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379#endif
380static char_u *arg_all __ARGS((void));
381#ifdef FEAT_SESSION
382static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000383static 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 +0000384static void ex_loadview __ARGS((exarg_T *eap));
385static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000386static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#else
388# define ex_loadview ex_ni
389#endif
390#ifndef FEAT_EVAL
391# define ex_compiler ex_ni
392#endif
393#ifdef FEAT_VIMINFO
394static void ex_viminfo __ARGS((exarg_T *eap));
395#else
396# define ex_viminfo ex_ni
397#endif
398static void ex_behave __ARGS((exarg_T *eap));
399#ifdef FEAT_AUTOCMD
400static void ex_filetype __ARGS((exarg_T *eap));
401static void ex_setfiletype __ARGS((exarg_T *eap));
402#else
403# define ex_filetype ex_ni
404# define ex_setfiletype ex_ni
405#endif
406#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000407# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# define ex_diffpatch ex_ni
409# define ex_diffgetput ex_ni
410# define ex_diffsplit ex_ni
411# define ex_diffthis ex_ni
412# define ex_diffupdate ex_ni
413#endif
414static void ex_digraphs __ARGS((exarg_T *eap));
415static void ex_set __ARGS((exarg_T *eap));
416#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
417# define ex_options ex_ni
418#endif
419#ifdef FEAT_SEARCH_EXTRA
420static void ex_nohlsearch __ARGS((exarg_T *eap));
421static void ex_match __ARGS((exarg_T *eap));
422#else
423# define ex_nohlsearch ex_ni
424# define ex_match ex_ni
425#endif
426#ifdef FEAT_CRYPT
427static void ex_X __ARGS((exarg_T *eap));
428#else
429# define ex_X ex_ni
430#endif
431#ifdef FEAT_FOLDING
432static void ex_fold __ARGS((exarg_T *eap));
433static void ex_foldopen __ARGS((exarg_T *eap));
434static void ex_folddo __ARGS((exarg_T *eap));
435#else
436# define ex_fold ex_ni
437# define ex_foldopen ex_ni
438# define ex_folddo ex_ni
439#endif
440#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
441 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
442# define ex_language ex_ni
443#endif
444#ifndef FEAT_SIGNS
445# define ex_sign ex_ni
446#endif
447#ifndef FEAT_SUN_WORKSHOP
448# define ex_wsverb ex_ni
449#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000450#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200451# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000452# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200453# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456#ifndef FEAT_EVAL
457# define ex_debug ex_ni
458# define ex_breakadd ex_ni
459# define ex_debuggreedy ex_ni
460# define ex_breakdel ex_ni
461# define ex_breaklist ex_ni
462#endif
463
464#ifndef FEAT_CMDHIST
465# define ex_history ex_ni
466#endif
467#ifndef FEAT_JUMPLIST
468# define ex_jumps ex_ni
469# define ex_changes ex_ni
470#endif
471
Bram Moolenaar05159a02005-02-26 23:04:13 +0000472#ifndef FEAT_PROFILE
473# define ex_profile ex_ni
474#endif
475
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476/*
477 * Declare cmdnames[].
478 */
479#define DO_DECLARE_EXCMD
480#include "ex_cmds.h"
481
482/*
483 * Table used to quickly search for a command, based on its first character.
484 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000485static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 CMD_append,
488 CMD_buffer,
489 CMD_change,
490 CMD_delete,
491 CMD_edit,
492 CMD_file,
493 CMD_global,
494 CMD_help,
495 CMD_insert,
496 CMD_join,
497 CMD_k,
498 CMD_list,
499 CMD_move,
500 CMD_next,
501 CMD_open,
502 CMD_print,
503 CMD_quit,
504 CMD_read,
505 CMD_substitute,
506 CMD_t,
507 CMD_undo,
508 CMD_vglobal,
509 CMD_write,
510 CMD_xit,
511 CMD_yank,
512 CMD_z,
513 CMD_bang
514};
515
516static char_u dollar_command[2] = {'$', 0};
517
518
519#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000520/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521typedef struct
522{
523 char_u *line; /* command line */
524 linenr_T lnum; /* sourcing_lnum of the line */
525} wcmd_T;
526
527/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000528 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000530 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 garray_T *lines_gap; /* growarray with line info */
535 int current_line; /* last read line from growarray */
536 int repeating; /* TRUE when looping a second time */
537 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
538 char_u *(*getline) __ARGS((int, void *, int));
539 void *cookie;
540};
541
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
543static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000545
546/* Struct to save a few things while debugging. Used in do_cmdline() only. */
547struct dbg_stuff
548{
549 int trylevel;
550 int force_abort;
551 except_T *caught_stack;
552 char_u *vv_exception;
553 char_u *vv_throwpoint;
554 int did_emsg;
555 int got_int;
556 int did_throw;
557 int need_rethrow;
558 int check_cstack;
559 except_T *current_exception;
560};
561
562static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
563static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
564
565 static void
566save_dbg_stuff(dsp)
567 struct dbg_stuff *dsp;
568{
569 dsp->trylevel = trylevel; trylevel = 0;
570 dsp->force_abort = force_abort; force_abort = FALSE;
571 dsp->caught_stack = caught_stack; caught_stack = NULL;
572 dsp->vv_exception = v_exception(NULL);
573 dsp->vv_throwpoint = v_throwpoint(NULL);
574
575 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
576 dsp->did_emsg = did_emsg; did_emsg = FALSE;
577 dsp->got_int = got_int; got_int = FALSE;
578 dsp->did_throw = did_throw; did_throw = FALSE;
579 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
580 dsp->check_cstack = check_cstack; check_cstack = FALSE;
581 dsp->current_exception = current_exception; current_exception = NULL;
582}
583
584 static void
585restore_dbg_stuff(dsp)
586 struct dbg_stuff *dsp;
587{
588 suppress_errthrow = FALSE;
589 trylevel = dsp->trylevel;
590 force_abort = dsp->force_abort;
591 caught_stack = dsp->caught_stack;
592 (void)v_exception(dsp->vv_exception);
593 (void)v_throwpoint(dsp->vv_throwpoint);
594 did_emsg = dsp->did_emsg;
595 got_int = dsp->got_int;
596 did_throw = dsp->did_throw;
597 need_rethrow = dsp->need_rethrow;
598 check_cstack = dsp->check_cstack;
599 current_exception = dsp->current_exception;
600}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#endif
602
603
604/*
605 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
606 * command is given.
607 */
608 void
609do_exmode(improved)
610 int improved; /* TRUE for "improved Ex" mode */
611{
612 int save_msg_scroll;
613 int prev_msg_row;
614 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000615 int changedtick;
616
617 if (improved)
618 exmode_active = EXMODE_VIM;
619 else
620 exmode_active = EXMODE_NORMAL;
621 State = NORMAL;
622
623 /* When using ":global /pat/ visual" and then "Q" we return to continue
624 * the :global command. */
625 if (global_busy)
626 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628 save_msg_scroll = msg_scroll;
629 ++RedrawingDisabled; /* don't redisplay the window */
630 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_GUI
632 /* Ignore scrollbar and mouse events in Ex mode */
633 ++hold_gui_events;
634#endif
635#ifdef FEAT_SNIFF
636 want_sniff_request = 0; /* No K_SNIFF wanted */
637#endif
638
639 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
640 while (exmode_active)
641 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000642#ifdef FEAT_EX_EXTRA
643 /* Check for a ":normal" command and no more characters left. */
644 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
645 {
646 exmode_active = FALSE;
647 break;
648 }
649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 msg_scroll = TRUE;
651 need_wait_return = FALSE;
652 ex_pressedreturn = FALSE;
653 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000654 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 prev_msg_row = msg_row;
656 prev_line = curwin->w_cursor.lnum;
657#ifdef FEAT_SNIFF
658 ProcessSniffRequests();
659#endif
660 if (improved)
661 {
662 cmdline_row = msg_row;
663 do_cmdline(NULL, getexline, NULL, 0);
664 }
665 else
666 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
667 lines_left = Rows - 1;
668
Bram Moolenaardf177f62005-02-22 08:39:57 +0000669 if ((prev_line != curwin->w_cursor.lnum
670 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000672 if (curbuf->b_ml.ml_flags & ML_EMPTY)
673 EMSG(_(e_emptybuf));
674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 if (ex_pressedreturn)
677 {
678 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000679 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000680 msg_row = prev_msg_row;
681 if (prev_msg_row == Rows - 1)
682 msg_row--;
683 }
684 msg_col = 0;
685 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
686 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
690 {
691 if (curbuf->b_ml.ml_flags & ML_EMPTY)
692 EMSG(_(e_emptybuf));
693 else
694 EMSG(_("E501: At end-of-file"));
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697
698#ifdef FEAT_GUI
699 --hold_gui_events;
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 --RedrawingDisabled;
702 --no_wait_return;
703 update_screen(CLEAR);
704 need_wait_return = FALSE;
705 msg_scroll = save_msg_scroll;
706}
707
708/*
709 * Execute a simple command line. Used for translated commands like "*".
710 */
711 int
712do_cmdline_cmd(cmd)
713 char_u *cmd;
714{
715 return do_cmdline(cmd, NULL, NULL,
716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
717}
718
719/*
720 * do_cmdline(): execute one Ex command line
721 *
722 * 1. Execute "cmdline" when it is not NULL.
723 * If "cmdline" is NULL, or more lines are needed, getline() is used.
724 * 2. Split up in parts separated with '|'.
725 *
726 * This function can be called recursively!
727 *
728 * flags:
729 * DOCMD_VERBOSE - The command will be included in the error message.
730 * DOCMD_NOWAIT - Don't call wait_return() and friends.
731 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
732 * DOCMD_KEYTYPED - Don't reset KeyTyped.
733 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
734 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
735 *
736 * return FAIL if cmdline could not be executed, OK otherwise
737 */
738 int
739do_cmdline(cmdline, getline, cookie, flags)
740 char_u *cmdline;
741 char_u *(*getline) __ARGS((int, void *, int));
742 void *cookie; /* argument for getline() */
743 int flags;
744{
745 char_u *next_cmdline; /* next cmd to execute */
746 char_u *cmdline_copy = NULL; /* copy of cmd line */
747 int used_getline = FALSE; /* used "getline" to obtain command */
748 static int recursive = 0; /* recursive depth */
749 int msg_didout_before_start = 0;
750 int count = 0; /* line number count */
751 int did_inc = FALSE; /* incremented RedrawingDisabled */
752 int retval = OK;
753#ifdef FEAT_EVAL
754 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int current_line = 0; /* active line in lines_ga */
757 char_u *fname = NULL; /* function or script name */
758 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
759 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000760 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int initial_trylevel;
762 struct msglist **saved_msg_list = NULL;
763 struct msglist *private_msg_list;
764
765 /* "getline" and "cookie" passed to do_one_cmd() */
766 char_u *(*cmd_getline) __ARGS((int, void *, int));
767 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else
772# define cmd_getline getline
773# define cmd_cookie cookie
774#endif
775 static int call_depth = 0; /* recursiveness */
776
777#ifdef FEAT_EVAL
778 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
779 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000780 * This ensures that the do_errthrow() call in do_one_cmd() does not
781 * combine the messages stored by an earlier invocation of do_one_cmd()
782 * with the command name of the later one. This would happen when
783 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 saved_msg_list = msg_list;
785 msg_list = &private_msg_list;
786 private_msg_list = NULL;
787#endif
788
789 /* It's possible to create an endless loop with ":execute", catch that
790 * here. The value of 200 allows nested function calls, ":source", etc. */
791 if (call_depth == 200)
792 {
793 EMSG(_("E169: Command too recursive"));
794#ifdef FEAT_EVAL
795 /* When converting to an exception, we do not include the command name
796 * since this is not an error of the specific command. */
797 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
798 msg_list = saved_msg_list;
799#endif
800 return FAIL;
801 }
802 ++call_depth;
803
804#ifdef FEAT_EVAL
805 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 cstack.cs_trylevel = 0;
808 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
811
812 real_cookie = getline_cookie(getline, cookie);
813
814 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 getline_is_func = getline_equal(getline, cookie, get_func_line);
816 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ++ex_nesting_level;
818
819 /* Get the function or script name and the address where the next breakpoint
820 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 fname = func_name(real_cookie);
824 breakpoint = func_breakpoint(real_cookie);
825 dbg_tick = func_dbg_tick(real_cookie);
826 }
827 else if (getline_equal(getline, cookie, getsourceline))
828 {
829 fname = sourcing_name;
830 breakpoint = source_breakpoint(real_cookie);
831 dbg_tick = source_dbg_tick(real_cookie);
832 }
833
834 /*
835 * Initialize "force_abort" and "suppress_errthrow" at the top level.
836 */
837 if (!recursive)
838 {
839 force_abort = FALSE;
840 suppress_errthrow = FALSE;
841 }
842
843 /*
844 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 * exception handling (used when debugging). Otherwise clear it to avoid
846 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000848 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000849 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200851 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 initial_trylevel = trylevel;
854
855 /*
856 * "did_throw" will be set to TRUE when an exception is being thrown.
857 */
858 did_throw = FALSE;
859#endif
860 /*
861 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * If force_abort is set, we cancel everything.
864 */
865 did_emsg = FALSE;
866
867 /*
868 * KeyTyped is only set when calling vgetc(). Reset it here when not
869 * calling vgetc() (sourced command lines).
870 */
871 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
872 KeyTyped = FALSE;
873
874 /*
875 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000876 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * - for multiple commands on one line, separated with '|'
878 * - when repeating until there are no more lines (for ":source")
879 */
880 next_cmdline = cmdline;
881 do
882 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883#ifdef FEAT_EVAL
884 getline_is_func = getline_equal(getline, cookie, get_func_line);
885#endif
886
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000887 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (next_cmdline == NULL
889#ifdef FEAT_EVAL
890 && !force_abort
891 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894 )
895 did_emsg = FALSE;
896
897 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * 2. If no line given: Get an allocated line with getline().
900 * 3. If a line is given: Make a copy, so we can mess with it.
901 */
902
903#ifdef FEAT_EVAL
904 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
907 /* Each '|' separated command is stored separately in lines_ga, to
908 * be able to jump to it. Don't use next_cmdline now. */
909 vim_free(cmdline_copy);
910 cmdline_copy = NULL;
911
912 /* Check if a function has returned or, unless it has an unclosed
913 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000917 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918 func_line_end(real_cookie);
919# endif
920 if (func_has_ended(real_cookie))
921 {
922 retval = FAIL;
923 break;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000927 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 && getline_equal(getline, cookie, getsourceline))
929 script_line_end();
930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 /* Check if a sourced file hit a ":finish" command. */
933 if (source_finished(getline, cookie))
934 {
935 retval = FAIL;
936 break;
937 }
938
939 /* If breakpoints have been added/deleted need to check for it. */
940 if (breakpoint != NULL && dbg_tick != NULL
941 && *dbg_tick != debug_tick)
942 {
943 *breakpoint = dbg_find_breakpoint(
944 getline_equal(getline, cookie, getsourceline),
945 fname, sourcing_lnum);
946 *dbg_tick = debug_tick;
947 }
948
949 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
950 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
951
952 /* Did we encounter a breakpoint? */
953 if (breakpoint != NULL && *breakpoint != 0
954 && *breakpoint <= sourcing_lnum)
955 {
956 dbg_breakpoint(fname, sourcing_lnum);
957 /* Find next breakpoint. */
958 *breakpoint = dbg_find_breakpoint(
959 getline_equal(getline, cookie, getsourceline),
960 fname, sourcing_lnum);
961 *dbg_tick = debug_tick;
962 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000963# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000964 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965 {
966 if (getline_is_func)
967 func_line_start(real_cookie);
968 else if (getline_equal(getline, cookie, getsourceline))
969 script_line_start();
970 }
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000974 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000976 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 * again. Pass a different "getline" function to do_one_cmd()
978 * below, so that it stores lines in or reads them from
979 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000980 * while/for loop. */
981 cmd_getline = get_loop_line;
982 cmd_cookie = (void *)&cmd_loop_cookie;
983 cmd_loop_cookie.lines_gap = &lines_ga;
984 cmd_loop_cookie.current_line = current_line;
985 cmd_loop_cookie.getline = getline;
986 cmd_loop_cookie.cookie = cookie;
987 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989 else
990 {
991 cmd_getline = getline;
992 cmd_cookie = cookie;
993 }
994#endif
995
996 /* 2. If no line given, get an allocated line with getline(). */
997 if (next_cmdline == NULL)
998 {
999 /*
1000 * Need to set msg_didout for the first line after an ":if",
1001 * otherwise the ":if" will be overwritten.
1002 */
1003 if (count == 1 && getline_equal(getline, cookie, getexline))
1004 msg_didout = TRUE;
1005 if (getline == NULL || (next_cmdline = getline(':', cookie,
1006#ifdef FEAT_EVAL
1007 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1008#else
1009 0
1010#endif
1011 )) == NULL)
1012 {
1013 /* Don't call wait_return for aborted command line. The NULL
1014 * returned for the end of a sourced file or executed function
1015 * doesn't do this. */
1016 if (KeyTyped && !(flags & DOCMD_REPEAT))
1017 need_wait_return = FALSE;
1018 retval = FAIL;
1019 break;
1020 }
1021 used_getline = TRUE;
1022
1023 /*
1024 * Keep the first typed line. Clear it when more lines are typed.
1025 */
1026 if (flags & DOCMD_KEEPLINE)
1027 {
1028 vim_free(repeat_cmdline);
1029 if (count == 0)
1030 repeat_cmdline = vim_strsave(next_cmdline);
1031 else
1032 repeat_cmdline = NULL;
1033 }
1034 }
1035
1036 /* 3. Make a copy of the command so we can mess with it. */
1037 else if (cmdline_copy == NULL)
1038 {
1039 next_cmdline = vim_strsave(next_cmdline);
1040 if (next_cmdline == NULL)
1041 {
1042 EMSG(_(e_outofmem));
1043 retval = FAIL;
1044 break;
1045 }
1046 }
1047 cmdline_copy = next_cmdline;
1048
1049#ifdef FEAT_EVAL
1050 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 * Save the current line when inside a ":while" or ":for", and when
1052 * the command looks like a ":while" or ":for", because we may need it
1053 * later. When there is a '|' and another command, it is stored
1054 * separately, because we need to be able to jump back to it from an
1055 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 if (current_line == lines_ga.ga_len
1058 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
1062 retval = FAIL;
1063 break;
1064 }
1065 }
1066 did_endif = FALSE;
1067#endif
1068
1069 if (count++ == 0)
1070 {
1071 /*
1072 * All output from the commands is put below each other, without
1073 * waiting for a return. Don't do this when executing commands
1074 * from a script or when being called recursive (e.g. for ":e
1075 * +command file").
1076 */
1077 if (!(flags & DOCMD_NOWAIT) && !recursive)
1078 {
1079 msg_didout_before_start = msg_didout;
1080 msg_didany = FALSE; /* no output yet */
1081 msg_start();
1082 msg_scroll = TRUE; /* put messages below each other */
1083 ++no_wait_return; /* dont wait for return until finished */
1084 ++RedrawingDisabled;
1085 did_inc = TRUE;
1086 }
1087 }
1088
1089 if (p_verbose >= 15 && sourcing_name != NULL)
1090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001092 verbose_enter_scroll();
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 smsg((char_u *)_("line %ld: %s"),
1095 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001096 if (msg_silent == 0)
1097 msg_puts((char_u *)"\n"); /* don't overwrite this */
1098
1099 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 --no_wait_return;
1101 }
1102
1103 /*
1104 * 2. Execute one '|' separated command.
1105 * do_one_cmd() will return NULL if there is no trailing '|'.
1106 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1107 */
1108 ++recursive;
1109 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1110#ifdef FEAT_EVAL
1111 &cstack,
1112#endif
1113 cmd_getline, cmd_cookie);
1114 --recursive;
1115
1116#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cmd_cookie == (void *)&cmd_loop_cookie)
1118 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#endif
1122
1123 if (next_cmdline == NULL)
1124 {
1125 vim_free(cmdline_copy);
1126 cmdline_copy = NULL;
1127#ifdef FEAT_CMDHIST
1128 /*
1129 * If the command was typed, remember it for the ':' register.
1130 * Do this AFTER executing the command to make :@: work.
1131 */
1132 if (getline_equal(getline, cookie, getexline)
1133 && new_last_cmdline != NULL)
1134 {
1135 vim_free(last_cmdline);
1136 last_cmdline = new_last_cmdline;
1137 new_last_cmdline = NULL;
1138 }
1139#endif
1140 }
1141 else
1142 {
1143 /* need to copy the command after the '|' to cmdline_copy, for the
1144 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001145 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 next_cmdline = cmdline_copy;
1147 }
1148
1149
1150#ifdef FEAT_EVAL
1151 /* reset did_emsg for a function that is not aborted by an error */
1152 if (did_emsg && !force_abort
1153 && getline_equal(getline, cookie, get_func_line)
1154 && !func_has_abort(real_cookie))
1155 did_emsg = FALSE;
1156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 ++current_line;
1160
1161 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * An ":endwhile", ":endfor" and ":continue" is handled here.
1163 * If we were executing commands, jump back to the ":while" or
1164 * ":for".
1165 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 /* Jump back to the matching ":while" or ":for". Be careful
1172 * not to use a cs_line[] from an entry that isn't a ":while"
1173 * or ":for": It would make "current_line" invalid and can
1174 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (!did_emsg && !got_int && !did_throw
1176 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 && (cstack.cs_flags[cstack.cs_idx]
1178 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && cstack.cs_line[cstack.cs_idx] >= 0
1180 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1181 {
1182 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 /* remember we jumped there */
1184 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 line_breakcheck(); /* check if CTRL-C typed */
1186
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 /* Check for the next breakpoint at or after the ":while"
1188 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (breakpoint != NULL)
1190 {
1191 *breakpoint = dbg_find_breakpoint(
1192 getline_equal(getline, cookie, getsourceline),
1193 fname,
1194 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1195 *dbg_tick = debug_tick;
1196 }
1197 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001202 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1203 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 }
1206
1207 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1214 }
1215 }
1216
1217 /*
1218 * When not inside any ":while" loop, clear remembered lines.
1219 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
1222 if (lines_ga.ga_len > 0)
1223 {
1224 sourcing_lnum =
1225 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1226 free_cmdlines(&lines_ga);
1227 }
1228 current_line = 0;
1229 }
1230
1231 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1233 * being restored at the ":endtry". Reset them here and set the
1234 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1235 * This includes the case where a missing ":endif", ":endwhile" or
1236 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 cstack.cs_lflags &= ~CSL_HAD_FINA;
1241 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1242 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 did_throw ? (void *)current_exception : NULL);
1244 did_emsg = got_int = did_throw = FALSE;
1245 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1246 }
1247
1248 /* Update global "trylevel" for recursive calls to do_cmdline() from
1249 * within this loop. */
1250 trylevel = initial_trylevel + cstack.cs_trylevel;
1251
1252 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001253 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * files) is aborted because of an error, an interrupt, or an uncaught
1255 * exception, cancel everything. If it is left normally, reset
1256 * force_abort to get the non-EH compatible abortion behavior for
1257 * the rest of the script.
1258 */
1259 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1260 force_abort = FALSE;
1261
1262 /* Convert an interrupt to an exception if appropriate. */
1263 (void)do_intthrow(&cstack);
1264#endif /* FEAT_EVAL */
1265
1266 }
1267 /*
1268 * Continue executing command lines when:
1269 * - no CTRL-C typed, no aborting error, no exception thrown or try
1270 * conditionals need to be checked for executing finally clauses or
1271 * catching an interrupt exception
1272 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001273 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * looping for ":source" command or function call.
1275 */
1276 while (!((got_int
1277#ifdef FEAT_EVAL
1278 || (did_emsg && force_abort) || did_throw
1279#endif
1280 )
1281#ifdef FEAT_EVAL
1282 && cstack.cs_trylevel == 0
1283#endif
1284 )
1285 && !(did_emsg && used_getline
1286 && (getline_equal(getline, cookie, getexmodeline)
1287 || getline_equal(getline, cookie, getexline)))
1288 && (next_cmdline != NULL
1289#ifdef FEAT_EVAL
1290 || cstack.cs_idx >= 0
1291#endif
1292 || (flags & DOCMD_REPEAT)));
1293
1294 vim_free(cmdline_copy);
1295#ifdef FEAT_EVAL
1296 free_cmdlines(&lines_ga);
1297 ga_clear(&lines_ga);
1298
1299 if (cstack.cs_idx >= 0)
1300 {
1301 /*
1302 * If a sourced file or executed function ran to its end, report the
1303 * unclosed conditional.
1304 */
1305 if (!got_int && !did_throw
1306 && ((getline_equal(getline, cookie, getsourceline)
1307 && !source_finished(getline, cookie))
1308 || (getline_equal(getline, cookie, get_func_line)
1309 && !func_has_ended(real_cookie))))
1310 {
1311 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1312 EMSG(_(e_endtry));
1313 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1314 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001315 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1316 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 else
1318 EMSG(_(e_endif));
1319 }
1320
1321 /*
1322 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1323 * ":endtry" in a sourced file or executed function. If the try
1324 * conditional is in its finally clause, ignore anything pending.
1325 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001326 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 */
1328 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001329 {
1330 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1331
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001332 if (idx >= 0)
1333 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001334 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1335 &cstack.cs_looplevel);
1336 }
1337 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 trylevel = initial_trylevel;
1339 }
1340
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001341 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1342 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * exception, do this now after rewinding the cstack. */
1344 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1345 ? (char_u *)"endfunction" : (char_u *)NULL);
1346
1347 if (trylevel == 0)
1348 {
1349 /*
1350 * When an exception is being thrown out of the outermost try
1351 * conditional, discard the uncaught exception, disable the conversion
1352 * of interrupts or errors to exceptions, and ensure that no more
1353 * commands are executed.
1354 */
1355 if (did_throw)
1356 {
1357 void *p = NULL;
1358 char_u *saved_sourcing_name;
1359 int saved_sourcing_lnum;
1360 struct msglist *messages = NULL, *next;
1361
1362 /*
1363 * If the uncaught exception is a user exception, report it as an
1364 * error. If it is an error exception, display the saved error
1365 * message now. For an interrupt exception, do nothing; the
1366 * interrupt message is given elsewhere.
1367 */
1368 switch (current_exception->type)
1369 {
1370 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001371 vim_snprintf((char *)IObuff, IOSIZE,
1372 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 current_exception->value);
1374 p = vim_strsave(IObuff);
1375 break;
1376 case ET_ERROR:
1377 messages = current_exception->messages;
1378 current_exception->messages = NULL;
1379 break;
1380 case ET_INTERRUPT:
1381 break;
1382 default:
1383 p = vim_strsave((char_u *)_(e_internal));
1384 }
1385
1386 saved_sourcing_name = sourcing_name;
1387 saved_sourcing_lnum = sourcing_lnum;
1388 sourcing_name = current_exception->throw_name;
1389 sourcing_lnum = current_exception->throw_lnum;
1390 current_exception->throw_name = NULL;
1391
1392 discard_current_exception(); /* uses IObuff if 'verbose' */
1393 suppress_errthrow = TRUE;
1394 force_abort = TRUE;
1395
1396 if (messages != NULL)
1397 {
1398 do
1399 {
1400 next = messages->next;
1401 emsg(messages->msg);
1402 vim_free(messages->msg);
1403 vim_free(messages);
1404 messages = next;
1405 }
1406 while (messages != NULL);
1407 }
1408 else if (p != NULL)
1409 {
1410 emsg(p);
1411 vim_free(p);
1412 }
1413 vim_free(sourcing_name);
1414 sourcing_name = saved_sourcing_name;
1415 sourcing_lnum = saved_sourcing_lnum;
1416 }
1417
1418 /*
1419 * On an interrupt or an aborting error not converted to an exception,
1420 * disable the conversion of errors to exceptions. (Interrupts are not
1421 * converted any more, here.) This enables also the interrupt message
1422 * when force_abort is set and did_emsg unset in case of an interrupt
1423 * from a finally clause after an error.
1424 */
1425 else if (got_int || (did_emsg && force_abort))
1426 suppress_errthrow = TRUE;
1427 }
1428
1429 /*
1430 * The current cstack will be freed when do_cmdline() returns. An uncaught
1431 * exception will have to be rethrown in the previous cstack. If a function
1432 * has just returned or a script file was just finished and the previous
1433 * cstack belongs to the same function or, respectively, script file, it
1434 * will have to be checked for finally clauses to be executed due to the
1435 * ":return" or ":finish". This is done in do_one_cmd().
1436 */
1437 if (did_throw)
1438 need_rethrow = TRUE;
1439 if ((getline_equal(getline, cookie, getsourceline)
1440 && ex_nesting_level > source_level(real_cookie))
1441 || (getline_equal(getline, cookie, get_func_line)
1442 && ex_nesting_level > func_level(real_cookie) + 1))
1443 {
1444 if (!did_throw)
1445 check_cstack = TRUE;
1446 }
1447 else
1448 {
1449 /* When leaving a function, reduce nesting level. */
1450 if (getline_equal(getline, cookie, get_func_line))
1451 --ex_nesting_level;
1452 /*
1453 * Go to debug mode when returning from a function in which we are
1454 * single-stepping.
1455 */
1456 if ((getline_equal(getline, cookie, getsourceline)
1457 || getline_equal(getline, cookie, get_func_line))
1458 && ex_nesting_level + 1 <= debug_break_level)
1459 do_debug(getline_equal(getline, cookie, getsourceline)
1460 ? (char_u *)_("End of sourced file")
1461 : (char_u *)_("End of function"));
1462 }
1463
1464 /*
1465 * Restore the exception environment (done after returning from the
1466 * debugger).
1467 */
1468 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001469 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
1471 msg_list = saved_msg_list;
1472#endif /* FEAT_EVAL */
1473
1474 /*
1475 * If there was too much output to fit on the command line, ask the user to
1476 * hit return before redrawing the screen. With the ":global" command we do
1477 * this only once after the command is finished.
1478 */
1479 if (did_inc)
1480 {
1481 --RedrawingDisabled;
1482 --no_wait_return;
1483 msg_scroll = FALSE;
1484
1485 /*
1486 * When just finished an ":if"-":else" which was typed, no need to
1487 * wait for hit-return. Also for an error situation.
1488 */
1489 if (retval == FAIL
1490#ifdef FEAT_EVAL
1491 || (did_endif && KeyTyped && !did_emsg)
1492#endif
1493 )
1494 {
1495 need_wait_return = FALSE;
1496 msg_didany = FALSE; /* don't wait when restarting edit */
1497 }
1498 else if (need_wait_return)
1499 {
1500 /*
1501 * The msg_start() above clears msg_didout. The wait_return we do
1502 * here should not overwrite the command that may be shown before
1503 * doing that.
1504 */
1505 msg_didout |= msg_didout_before_start;
1506 wait_return(FALSE);
1507 }
1508 }
1509
1510#ifndef FEAT_EVAL
1511 /*
1512 * Reset if_level, in case a sourced script file contains more ":if" than
1513 * ":endif" (could be ":if x | foo | endif").
1514 */
1515 if_level = 0;
1516#endif
1517
1518 --call_depth;
1519 return retval;
1520}
1521
1522#ifdef FEAT_EVAL
1523/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
1526 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 int c;
1529 void *cookie;
1530 int indent;
1531{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 wcmd_T *wp;
1534 char_u *line;
1535
1536 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1537 {
1538 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001539 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (cp->getline == NULL)
1543 line = getcmdline(c, 0L, indent);
1544 else
1545 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001546 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 ++cp->current_line;
1548
1549 return line;
1550 }
1551
1552 KeyTyped = FALSE;
1553 ++cp->current_line;
1554 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1555 sourcing_lnum = wp->lnum;
1556 return vim_strsave(wp->line);
1557}
1558
1559/*
1560 * Store a line in "gap" so that a ":while" loop can execute it again.
1561 */
1562 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 garray_T *gap;
1565 char_u *line;
1566{
1567 if (ga_grow(gap, 1) == FAIL)
1568 return FAIL;
1569 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1570 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1571 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 return OK;
1573}
1574
1575/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 */
1578 static void
1579free_cmdlines(gap)
1580 garray_T *gap;
1581{
1582 while (gap->ga_len > 0)
1583 {
1584 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1585 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587}
1588#endif
1589
1590/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001591 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1592 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595getline_equal(fgetline, cookie, func)
1596 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001597 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 char_u *(*func) __ARGS((int, void *, int));
1599{
1600#ifdef FEAT_EVAL
1601 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001605 * function that's originally used to obtain the lines. This may be
1606 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001608 cp = (struct loop_cookie *)cookie;
1609 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 gp = cp->getline;
1612 cp = cp->cookie;
1613 }
1614 return gp == func;
1615#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617#endif
1618}
1619
1620#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1621/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001622 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * getline function. Otherwise return "cookie".
1624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001626getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001627 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630# ifdef FEAT_EVAL
1631 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001632 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaar89d40322006-08-29 15:30:07 +00001634 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001635 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001638 cp = (struct loop_cookie *)cookie;
1639 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641 gp = cp->getline;
1642 cp = cp->cookie;
1643 }
1644 return cp;
1645# else
1646 return cookie;
1647# endif
1648}
1649#endif
1650
1651/*
1652 * Execute one Ex command.
1653 *
1654 * If 'sourcing' is TRUE, the command will be included in the error message.
1655 *
1656 * 1. skip comment lines and leading space
1657 * 2. handle command modifiers
1658 * 3. parse range
1659 * 4. parse command
1660 * 5. parse arguments
1661 * 6. switch on command name
1662 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001663 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 *
1665 * This function may be called recursively!
1666 */
1667#if (_MSC_VER == 1200)
1668/*
Bram Moolenaared203462004-06-16 11:19:22 +00001669 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001671 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
1673 static char_u *
1674do_one_cmd(cmdlinep, sourcing,
1675#ifdef FEAT_EVAL
1676 cstack,
1677#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001678 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 char_u **cmdlinep;
1680 int sourcing;
1681#ifdef FEAT_EVAL
1682 struct condstack *cstack;
1683#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001684 char_u *(*fgetline) __ARGS((int, void *, int));
1685 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 char_u *p;
1688 linenr_T lnum;
1689 long n;
1690 char_u *errormsg = NULL; /* error message */
1691 exarg_T ea; /* Ex command arguments */
1692 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001693 int save_msg_scroll = msg_scroll;
1694 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001696#ifdef HAVE_SANDBOX
1697 int did_sandbox = FALSE;
1698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 cmdmod_T save_cmdmod;
1700 int ni; /* set when Not Implemented */
1701
1702 vim_memset(&ea, 0, sizeof(ea));
1703 ea.line1 = 1;
1704 ea.line2 = 1;
1705#ifdef FEAT_EVAL
1706 ++ex_nesting_level;
1707#endif
1708
1709 /* when not editing the last file :q has to be typed twice */
1710 if (quitmore
1711#ifdef FEAT_EVAL
1712 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001713 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714#endif
1715 )
1716 --quitmore;
1717
1718 /*
1719 * Reset browse, confirm, etc.. They are restored when returning, for
1720 * recursive calls.
1721 */
1722 save_cmdmod = cmdmod;
1723 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1724
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001725 /* "#!anything" is handled like a comment. */
1726 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1727 goto doend;
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * Repeat until no more command modifiers are found.
1731 */
1732 ea.cmd = *cmdlinep;
1733 for (;;)
1734 {
1735/*
1736 * 1. skip comment lines and leading white space and colons
1737 */
1738 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1739 ++ea.cmd;
1740
1741 /* in ex mode, an empty line works like :+ */
1742 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001743 && (getline_equal(fgetline, cookie, getexmodeline)
1744 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001745 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747 ea.cmd = (char_u *)"+";
1748 ex_pressedreturn = TRUE;
1749 }
1750
1751 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001752 if (*ea.cmd == '"')
1753 goto doend;
1754 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001755 {
1756 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760/*
1761 * 2. handle command modifiers.
1762 */
1763 p = ea.cmd;
1764 if (VIM_ISDIGIT(*ea.cmd))
1765 p = skipwhite(skipdigits(ea.cmd));
1766 switch (*p)
1767 {
1768 /* When adding an entry, also modify cmd_exists(). */
1769 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1770 break;
1771#ifdef FEAT_WINDOWS
1772 cmdmod.split |= WSP_ABOVE;
1773#endif
1774 continue;
1775
1776 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1777 {
1778#ifdef FEAT_WINDOWS
1779 cmdmod.split |= WSP_BELOW;
1780#endif
1781 continue;
1782 }
1783 if (checkforcmd(&ea.cmd, "browse", 3))
1784 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001785#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 cmdmod.browse = TRUE;
1787#endif
1788 continue;
1789 }
1790 if (!checkforcmd(&ea.cmd, "botright", 2))
1791 break;
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_BOT;
1794#endif
1795 continue;
1796
1797 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1798 break;
1799#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1800 cmdmod.confirm = TRUE;
1801#endif
1802 continue;
1803
1804 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1805 {
1806 cmdmod.keepmarks = TRUE;
1807 continue;
1808 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001809 if (checkforcmd(&ea.cmd, "keepalt", 5))
1810 {
1811 cmdmod.keepalt = TRUE;
1812 continue;
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1815 break;
1816 cmdmod.keepjumps = TRUE;
1817 continue;
1818
1819 /* ":hide" and ":hide | cmd" are not modifiers */
1820 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1821 || *p == NUL || ends_excmd(*p))
1822 break;
1823 ea.cmd = p;
1824 cmdmod.hide = TRUE;
1825 continue;
1826
1827 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1828 {
1829 cmdmod.lockmarks = TRUE;
1830 continue;
1831 }
1832
1833 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1834 break;
1835#ifdef FEAT_WINDOWS
1836 cmdmod.split |= WSP_ABOVE;
1837#endif
1838 continue;
1839
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001840 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1841 break;
1842#ifdef FEAT_AUTOCMD
1843 if (cmdmod.save_ei == NULL)
1844 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001845 /* Set 'eventignore' to "all". Restore the
1846 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001847 cmdmod.save_ei = vim_strsave(p_ei);
1848 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001849 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001850 }
1851#endif
1852 continue;
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1855 break;
1856#ifdef FEAT_WINDOWS
1857 cmdmod.split |= WSP_BELOW;
1858#endif
1859 continue;
1860
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001861 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1862 {
1863#ifdef HAVE_SANDBOX
1864 if (!did_sandbox)
1865 ++sandbox;
1866 did_sandbox = TRUE;
1867#endif
1868 continue;
1869 }
1870 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001872 if (save_msg_silent == -1)
1873 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1876 {
1877 /* ":silent!", but not "silent !cmd" */
1878 ea.cmd = skipwhite(ea.cmd + 1);
1879 ++emsg_silent;
1880 ++did_esilent;
1881 }
1882 continue;
1883
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001884 case 't': if (checkforcmd(&p, "tab", 3))
1885 {
1886#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001887 if (vim_isdigit(*ea.cmd))
1888 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1889 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001890 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001891 ea.cmd = p;
1892#endif
1893 continue;
1894 }
1895 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 break;
1897#ifdef FEAT_WINDOWS
1898 cmdmod.split |= WSP_TOP;
1899#endif
1900 continue;
1901
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001902 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1903 break;
1904 if (save_msg_silent == -1)
1905 save_msg_silent = msg_silent;
1906 msg_silent = 0;
1907 continue;
1908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1910 {
1911#ifdef FEAT_VERTSPLIT
1912 cmdmod.split |= WSP_VERT;
1913#endif
1914 continue;
1915 }
1916 if (!checkforcmd(&p, "verbose", 4))
1917 break;
1918 if (verbose_save < 0)
1919 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001920 if (vim_isdigit(*ea.cmd))
1921 p_verbose = atoi((char *)ea.cmd);
1922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 p_verbose = 1;
1924 ea.cmd = p;
1925 continue;
1926 }
1927 break;
1928 }
1929
1930#ifdef FEAT_EVAL
1931 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1932 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1933#else
1934 ea.skip = (if_level > 0);
1935#endif
1936
1937#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001938# ifdef FEAT_PROFILE
1939 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001940 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001941 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001942 if (getline_equal(fgetline, cookie, get_func_line))
1943 func_line_exec(getline_cookie(fgetline, cookie));
1944 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001945 script_line_exec();
1946 }
1947#endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 /* May go to debug mode. If this happens and the ">quit" debug command is
1950 * used, throw an interrupt exception and skip the next command. */
1951 dbg_check_breakpoint(&ea);
1952 if (!ea.skip && got_int)
1953 {
1954 ea.skip = TRUE;
1955 (void)do_intthrow(cstack);
1956 }
1957#endif
1958
1959/*
1960 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1961 *
1962 * where 'addr' is:
1963 *
1964 * % (entire file)
1965 * $ [+-NUM]
1966 * 'x [+-NUM] (where x denotes a currently defined mark)
1967 * . [+-NUM]
1968 * [+-NUM]..
1969 * NUM
1970 *
1971 * The ea.cmd pointer is updated to point to the first character following the
1972 * range spec. If an initial address is found, but no second, the upper bound
1973 * is equal to the lower.
1974 */
1975
1976 /* repeat for all ',' or ';' separated addresses */
1977 for (;;)
1978 {
1979 ea.line1 = ea.line2;
1980 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1981 ea.cmd = skipwhite(ea.cmd);
1982 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1983 if (ea.cmd == NULL) /* error detected */
1984 goto doend;
1985 if (lnum == MAXLNUM)
1986 {
1987 if (*ea.cmd == '%') /* '%' - all lines */
1988 {
1989 ++ea.cmd;
1990 ea.line1 = 1;
1991 ea.line2 = curbuf->b_ml.ml_line_count;
1992 ++ea.addr_count;
1993 }
1994 /* '*' - visual area */
1995 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1996 {
1997 pos_T *fp;
1998
1999 ++ea.cmd;
2000 if (!ea.skip)
2001 {
2002 fp = getmark('<', FALSE);
2003 if (check_mark(fp) == FAIL)
2004 goto doend;
2005 ea.line1 = fp->lnum;
2006 fp = getmark('>', FALSE);
2007 if (check_mark(fp) == FAIL)
2008 goto doend;
2009 ea.line2 = fp->lnum;
2010 ++ea.addr_count;
2011 }
2012 }
2013 }
2014 else
2015 ea.line2 = lnum;
2016 ea.addr_count++;
2017
2018 if (*ea.cmd == ';')
2019 {
2020 if (!ea.skip)
2021 curwin->w_cursor.lnum = ea.line2;
2022 }
2023 else if (*ea.cmd != ',')
2024 break;
2025 ++ea.cmd;
2026 }
2027
2028 /* One address given: set start and end lines */
2029 if (ea.addr_count == 1)
2030 {
2031 ea.line1 = ea.line2;
2032 /* ... but only implicit: really no address given */
2033 if (lnum == MAXLNUM)
2034 ea.addr_count = 0;
2035 }
2036
2037 /* Don't leave the cursor on an illegal line (caused by ';') */
2038 check_cursor_lnum();
2039
2040/*
2041 * 4. parse command
2042 */
2043
2044 /*
2045 * Skip ':' and any white space
2046 */
2047 ea.cmd = skipwhite(ea.cmd);
2048 while (*ea.cmd == ':')
2049 ea.cmd = skipwhite(ea.cmd + 1);
2050
2051 /*
2052 * If we got a line, but no command, then go to the line.
2053 * If we find a '|' or '\n' we set ea.nextcmd.
2054 */
2055 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2056 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2057 {
2058 /*
2059 * strange vi behaviour:
2060 * ":3" jumps to line 3
2061 * ":3|..." prints line 3
2062 * ":|" prints current line
2063 */
2064 if (ea.skip) /* skip this if inside :if */
2065 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002066 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 ea.cmdidx = CMD_print;
2069 ea.argt = RANGE+COUNT+TRLBAR;
2070 if ((errormsg = invalid_range(&ea)) == NULL)
2071 {
2072 correct_range(&ea);
2073 ex_print(&ea);
2074 }
2075 }
2076 else if (ea.addr_count != 0)
2077 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002078 if (ea.line2 > curbuf->b_ml.ml_line_count)
2079 {
2080 /* With '-' in 'cpoptions' a line number past the file is an
2081 * error, otherwise put it at the end of the file. */
2082 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2083 ea.line2 = -1;
2084 else
2085 ea.line2 = curbuf->b_ml.ml_line_count;
2086 }
2087
2088 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002089 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else
2091 {
2092 if (ea.line2 == 0)
2093 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else
2095 curwin->w_cursor.lnum = ea.line2;
2096 beginline(BL_SOL | BL_FIX);
2097 }
2098 }
2099 goto doend;
2100 }
2101
2102 /* Find the command and let "p" point to after it. */
2103 p = find_command(&ea, NULL);
2104
2105#ifdef FEAT_USR_CMDS
2106 if (p == NULL)
2107 {
2108 if (!ea.skip)
2109 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2110 goto doend;
2111 }
2112 /* Check for wrong commands. */
2113 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2114 {
2115 errormsg = uc_fun_cmd();
2116 goto doend;
2117 }
2118#endif
2119 if (ea.cmdidx == CMD_SIZE)
2120 {
2121 if (!ea.skip)
2122 {
2123 STRCPY(IObuff, _("E492: Not an editor command"));
2124 if (!sourcing)
2125 {
2126 STRCAT(IObuff, ": ");
2127 STRNCAT(IObuff, *cmdlinep, 40);
2128 }
2129 errormsg = IObuff;
2130 }
2131 goto doend;
2132 }
2133
2134 ni = (
2135#ifdef FEAT_USR_CMDS
2136 !USER_CMDIDX(ea.cmdidx) &&
2137#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002138 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002139#ifdef HAVE_EX_SCRIPT_NI
2140 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2141#endif
2142 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144#ifndef FEAT_EVAL
2145 /*
2146 * When the expression evaluation is disabled, recognize the ":if" and
2147 * ":endif" commands and ignore everything in between it.
2148 */
2149 if (ea.cmdidx == CMD_if)
2150 ++if_level;
2151 if (if_level)
2152 {
2153 if (ea.cmdidx == CMD_endif)
2154 --if_level;
2155 goto doend;
2156 }
2157
2158#endif
2159
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002160 /* forced commands */
2161 if (*p == '!' && ea.cmdidx != CMD_substitute
2162 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 ++p;
2165 ea.forceit = TRUE;
2166 }
2167 else
2168 ea.forceit = FALSE;
2169
2170/*
2171 * 5. parse arguments
2172 */
2173#ifdef FEAT_USR_CMDS
2174 if (!USER_CMDIDX(ea.cmdidx))
2175#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002176 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 if (!ea.skip)
2179 {
2180#ifdef HAVE_SANDBOX
2181 if (sandbox != 0 && !(ea.argt & SBOXOK))
2182 {
2183 /* Command not allowed in sandbox. */
2184 errormsg = (char_u *)_(e_sandbox);
2185 goto doend;
2186 }
2187#endif
2188 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2189 {
2190 /* Command not allowed in non-'modifiable' buffer */
2191 errormsg = (char_u *)_(e_modifiable);
2192 goto doend;
2193 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002194
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002195 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196# ifdef FEAT_USR_CMDS
2197 && !USER_CMDIDX(ea.cmdidx)
2198# endif
2199 )
2200 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002201 /* Command not allowed when editing the command line. */
2202#ifdef FEAT_CMDWIN
2203 if (cmdwin_type != 0)
2204 errormsg = (char_u *)_(e_cmdwin);
2205 else
2206#endif
2207 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 goto doend;
2209 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002211 /* Disallow editing another buffer when "curbuf_lock" is set.
2212 * Do allow ":edit" (check for argument later).
2213 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002214 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002215 && ea.cmdidx != CMD_edit
2216 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002217# ifdef FEAT_USR_CMDS
2218 && !USER_CMDIDX(ea.cmdidx)
2219# endif
2220 && curbuf_locked())
2221 goto doend;
2222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2225 {
2226 /* no range allowed */
2227 errormsg = (char_u *)_(e_norange);
2228 goto doend;
2229 }
2230 }
2231
2232 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2233 {
2234 errormsg = (char_u *)_(e_nobang);
2235 goto doend;
2236 }
2237
2238 /*
2239 * Don't complain about the range if it is not used
2240 * (could happen if line_count is accidentally set to 0).
2241 */
2242 if (!ea.skip && !ni)
2243 {
2244 /*
2245 * If the range is backwards, ask for confirmation and, if given, swap
2246 * ea.line1 & ea.line2 so it's forwards again.
2247 * When global command is busy, don't ask, will fail below.
2248 */
2249 if (!global_busy && ea.line1 > ea.line2)
2250 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002251 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002253 if (sourcing || exmode_active)
2254 {
2255 errormsg = (char_u *)_("E493: Backwards range given");
2256 goto doend;
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (ask_yesno((char_u *)
2259 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002260 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 lnum = ea.line1;
2263 ea.line1 = ea.line2;
2264 ea.line2 = lnum;
2265 }
2266 if ((errormsg = invalid_range(&ea)) != NULL)
2267 goto doend;
2268 }
2269
2270 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2271 ea.line2 = 1;
2272
2273 correct_range(&ea);
2274
2275#ifdef FEAT_FOLDING
2276 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2277 {
2278 /* Put the first line at the start of a closed fold, put the last line
2279 * at the end of a closed fold. */
2280 (void)hasFolding(ea.line1, &ea.line1, NULL);
2281 (void)hasFolding(ea.line2, NULL, &ea.line2);
2282 }
2283#endif
2284
2285#ifdef FEAT_QUICKFIX
2286 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002287 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * option here, so things like % get expanded.
2289 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002290 p = replace_makeprg(&ea, p, cmdlinep);
2291 if (p == NULL)
2292 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#endif
2294
2295 /*
2296 * Skip to start of argument.
2297 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2298 */
2299 if (ea.cmdidx == CMD_bang)
2300 ea.arg = p;
2301 else
2302 ea.arg = skipwhite(p);
2303
2304 /*
2305 * Check for "++opt=val" argument.
2306 * Must be first, allow ":w ++enc=utf8 !cmd"
2307 */
2308 if (ea.argt & ARGOPT)
2309 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2310 if (getargopt(&ea) == FAIL && !ni)
2311 {
2312 errormsg = (char_u *)_(e_invarg);
2313 goto doend;
2314 }
2315
2316 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2317 {
2318 if (*ea.arg == '>') /* append */
2319 {
2320 if (*++ea.arg != '>') /* typed wrong */
2321 {
2322 errormsg = (char_u *)_("E494: Use w or w>>");
2323 goto doend;
2324 }
2325 ea.arg = skipwhite(ea.arg + 1);
2326 ea.append = TRUE;
2327 }
2328 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2329 {
2330 ++ea.arg;
2331 ea.usefilter = TRUE;
2332 }
2333 }
2334
2335 if (ea.cmdidx == CMD_read)
2336 {
2337 if (ea.forceit)
2338 {
2339 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2340 ea.forceit = FALSE;
2341 }
2342 else if (*ea.arg == '!') /* :r !filter */
2343 {
2344 ++ea.arg;
2345 ea.usefilter = TRUE;
2346 }
2347 }
2348
2349 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2350 {
2351 ea.amount = 1;
2352 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2353 {
2354 ++ea.arg;
2355 ++ea.amount;
2356 }
2357 ea.arg = skipwhite(ea.arg);
2358 }
2359
2360 /*
2361 * Check for "+command" argument, before checking for next command.
2362 * Don't do this for ":read !cmd" and ":write !cmd".
2363 */
2364 if ((ea.argt & EDITCMD) && !ea.usefilter)
2365 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2366
2367 /*
2368 * Check for '|' to separate commands and '"' to start comments.
2369 * Don't do this for ":read !cmd" and ":write !cmd".
2370 */
2371 if ((ea.argt & TRLBAR) && !ea.usefilter)
2372 separate_nextcmd(&ea);
2373
2374 /*
2375 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002376 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2377 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 else if (ea.cmdidx == CMD_bang
2380 || ea.cmdidx == CMD_global
2381 || ea.cmdidx == CMD_vglobal
2382 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 for (p = ea.arg; *p; ++p)
2385 {
2386 /* Remove one backslash before a newline, so that it's possible to
2387 * pass a newline to the shell and also a newline that is preceded
2388 * with a backslash. This makes it impossible to end a shell
2389 * command in a backslash, but that doesn't appear useful.
2390 * Halving the number of backslashes is incompatible with previous
2391 * versions. */
2392 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002393 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 else if (*p == '\n')
2395 {
2396 ea.nextcmd = p + 1;
2397 *p = NUL;
2398 break;
2399 }
2400 }
2401 }
2402
2403 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2404 {
2405 ea.line1 = 1;
2406 ea.line2 = curbuf->b_ml.ml_line_count;
2407 }
2408
2409 /* accept numbered register only when no count allowed (:put) */
2410 if ( (ea.argt & REGSTR)
2411 && *ea.arg != NUL
2412#ifdef FEAT_USR_CMDS
2413 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2414 && USER_CMDIDX(ea.cmdidx)))
2415 /* Do not allow register = for user commands */
2416 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2417#else
2418 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2419#endif
2420 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2421 {
2422 ea.regname = *ea.arg++;
2423#ifdef FEAT_EVAL
2424 /* for '=' register: accept the rest of the line as an expression */
2425 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2426 {
2427 set_expr_line(vim_strsave(ea.arg));
2428 ea.arg += STRLEN(ea.arg);
2429 }
2430#endif
2431 ea.arg = skipwhite(ea.arg);
2432 }
2433
2434 /*
2435 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2436 * count, it's a buffer name.
2437 */
2438 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2439 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2440 || vim_iswhite(*p)))
2441 {
2442 n = getdigits(&ea.arg);
2443 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002444 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 errormsg = (char_u *)_(e_zerocount);
2447 goto doend;
2448 }
2449 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2450 {
2451 ea.line2 = n;
2452 if (ea.addr_count == 0)
2453 ea.addr_count = 1;
2454 }
2455 else
2456 {
2457 ea.line1 = ea.line2;
2458 ea.line2 += n - 1;
2459 ++ea.addr_count;
2460 /*
2461 * Be vi compatible: no error message for out of range.
2462 */
2463 if (ea.line2 > curbuf->b_ml.ml_line_count)
2464 ea.line2 = curbuf->b_ml.ml_line_count;
2465 }
2466 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002467
2468 /*
2469 * Check for flags: 'l', 'p' and '#'.
2470 */
2471 if (ea.argt & EXFLAGS)
2472 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002474 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002475 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 errormsg = (char_u *)_(e_trailing);
2478 goto doend;
2479 }
2480
2481 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2482 {
2483 errormsg = (char_u *)_(e_argreq);
2484 goto doend;
2485 }
2486
2487#ifdef FEAT_EVAL
2488 /*
2489 * Skip the command when it's not going to be executed.
2490 * The commands like :if, :endif, etc. always need to be executed.
2491 * Also make an exception for commands that handle a trailing command
2492 * themselves.
2493 */
2494 if (ea.skip)
2495 {
2496 switch (ea.cmdidx)
2497 {
2498 /* commands that need evaluation */
2499 case CMD_while:
2500 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002501 case CMD_for:
2502 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_if:
2504 case CMD_elseif:
2505 case CMD_else:
2506 case CMD_endif:
2507 case CMD_try:
2508 case CMD_catch:
2509 case CMD_finally:
2510 case CMD_endtry:
2511 case CMD_function:
2512 break;
2513
2514 /* Commands that handle '|' themselves. Check: A command should
2515 * either have the TRLBAR flag, appear in this list or appear in
2516 * the list at ":help :bar". */
2517 case CMD_aboveleft:
2518 case CMD_and:
2519 case CMD_belowright:
2520 case CMD_botright:
2521 case CMD_browse:
2522 case CMD_call:
2523 case CMD_confirm:
2524 case CMD_delfunction:
2525 case CMD_djump:
2526 case CMD_dlist:
2527 case CMD_dsearch:
2528 case CMD_dsplit:
2529 case CMD_echo:
2530 case CMD_echoerr:
2531 case CMD_echomsg:
2532 case CMD_echon:
2533 case CMD_execute:
2534 case CMD_help:
2535 case CMD_hide:
2536 case CMD_ijump:
2537 case CMD_ilist:
2538 case CMD_isearch:
2539 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002540 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 case CMD_keepjumps:
2542 case CMD_keepmarks:
2543 case CMD_leftabove:
2544 case CMD_let:
2545 case CMD_lockmarks:
2546 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002547 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 case CMD_perl:
2549 case CMD_psearch:
2550 case CMD_python:
2551 case CMD_return:
2552 case CMD_rightbelow:
2553 case CMD_ruby:
2554 case CMD_silent:
2555 case CMD_smagic:
2556 case CMD_snomagic:
2557 case CMD_substitute:
2558 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002559 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 case CMD_tcl:
2561 case CMD_throw:
2562 case CMD_tilde:
2563 case CMD_topleft:
2564 case CMD_unlet:
2565 case CMD_verbose:
2566 case CMD_vertical:
2567 break;
2568
2569 default: goto doend;
2570 }
2571 }
2572#endif
2573
2574 if (ea.argt & XFILE)
2575 {
2576 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2577 goto doend;
2578 }
2579
2580#ifdef FEAT_LISTCMDS
2581 /*
2582 * Accept buffer name. Cannot be used at the same time with a buffer
2583 * number. Don't do this for a user command.
2584 */
2585 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2586# ifdef FEAT_USR_CMDS
2587 && !USER_CMDIDX(ea.cmdidx)
2588# endif
2589 )
2590 {
2591 /*
2592 * :bdelete, :bwipeout and :bunload take several arguments, separated
2593 * by spaces: find next space (skipping over escaped characters).
2594 * The others take one argument: ignore trailing spaces.
2595 */
2596 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2597 || ea.cmdidx == CMD_bunload)
2598 p = skiptowhite_esc(ea.arg);
2599 else
2600 {
2601 p = ea.arg + STRLEN(ea.arg);
2602 while (p > ea.arg && vim_iswhite(p[-1]))
2603 --p;
2604 }
2605 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2606 if (ea.line2 < 0) /* failed */
2607 goto doend;
2608 ea.addr_count = 1;
2609 ea.arg = skipwhite(p);
2610 }
2611#endif
2612
2613/*
2614 * 6. switch on command name
2615 *
2616 * The "ea" structure holds the arguments that can be used.
2617 */
2618 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002619 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 ea.cookie = cookie;
2621#ifdef FEAT_EVAL
2622 ea.cstack = cstack;
2623#endif
2624
2625#ifdef FEAT_USR_CMDS
2626 if (USER_CMDIDX(ea.cmdidx))
2627 {
2628 /*
2629 * Execute a user-defined command.
2630 */
2631 do_ucmd(&ea);
2632 }
2633 else
2634#endif
2635 {
2636 /*
2637 * Call the function to execute the command.
2638 */
2639 ea.errmsg = NULL;
2640 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2641 if (ea.errmsg != NULL)
2642 errormsg = (char_u *)_(ea.errmsg);
2643 }
2644
2645#ifdef FEAT_EVAL
2646 /*
2647 * If the command just executed called do_cmdline(), any throw or ":return"
2648 * or ":finish" encountered there must also check the cstack of the still
2649 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2650 * exception, or reanimate a returned function or finished script file and
2651 * return or finish it again.
2652 */
2653 if (need_rethrow)
2654 do_throw(cstack);
2655 else if (check_cstack)
2656 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002657 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 && current_func_returned())
2661 do_return(&ea, TRUE, FALSE, NULL);
2662 }
2663 need_rethrow = check_cstack = FALSE;
2664#endif
2665
2666doend:
2667 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2668 curwin->w_cursor.lnum = 1;
2669
2670 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2671 {
2672 if (sourcing)
2673 {
2674 if (errormsg != IObuff)
2675 {
2676 STRCPY(IObuff, errormsg);
2677 errormsg = IObuff;
2678 }
2679 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002680 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 emsg(errormsg);
2683 }
2684#ifdef FEAT_EVAL
2685 do_errthrow(cstack,
2686 (ea.cmdidx != CMD_SIZE
2687# ifdef FEAT_USR_CMDS
2688 && !USER_CMDIDX(ea.cmdidx)
2689# endif
2690 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2691#endif
2692
2693 if (verbose_save >= 0)
2694 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002695#ifdef FEAT_AUTOCMD
2696 if (cmdmod.save_ei != NULL)
2697 {
2698 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002699 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2700 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002701 free_string_option(cmdmod.save_ei);
2702 }
2703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 cmdmod = save_cmdmod;
2706
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002707 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 /* messages could be enabled for a serious error, need to check if the
2710 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002711 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002712 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 emsg_silent -= did_esilent;
2714 if (emsg_silent < 0)
2715 emsg_silent = 0;
2716 /* Restore msg_scroll, it's set by file I/O commands, even when no
2717 * message is actually displayed. */
2718 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002719
2720 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2721 * somewhere in the line. Put it back in the first column. */
2722 if (redirecting())
2723 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002726#ifdef HAVE_SANDBOX
2727 if (did_sandbox)
2728 --sandbox;
2729#endif
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2732 ea.nextcmd = NULL;
2733
2734#ifdef FEAT_EVAL
2735 --ex_nesting_level;
2736#endif
2737
2738 return ea.nextcmd;
2739}
2740#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002741 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
2743
2744/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002745 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 * If there is a match advance "pp" to the argument and return TRUE.
2747 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002748 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002750 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 char *cmd; /* name of command */
2752 int len; /* required length */
2753{
2754 int i;
2755
2756 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002757 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 break;
2759 if (i >= len && !isalpha((*pp)[i]))
2760 {
2761 *pp = skipwhite(*pp + i);
2762 return TRUE;
2763 }
2764 return FALSE;
2765}
2766
2767/*
2768 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002769 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002771 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 * Returns NULL for an ambiguous user command.
2773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 static char_u *
2775find_command(eap, full)
2776 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002777 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int len;
2780 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002781 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /*
2784 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002785 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 * - the 'k' command can directly be followed by any character.
2787 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2788 * but :sre[wind] is another command, as are :scrip[tnames],
2789 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002790 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
2792 p = eap->cmd;
2793 if (*p == 'k')
2794 {
2795 eap->cmdidx = CMD_k;
2796 ++p;
2797 }
2798 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002799 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2800 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 || p[1] == 'g'
2802 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2803 || p[1] == 'I'
2804 || (p[1] == 'r' && p[2] != 'e')))
2805 {
2806 eap->cmdidx = CMD_substitute;
2807 ++p;
2808 }
2809 else
2810 {
2811 while (ASCII_ISALPHA(*p))
2812 ++p;
2813 /* check for non-alpha command */
2814 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2815 ++p;
2816 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002817 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2818 {
2819 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2820 * :delete with the 'l' flag. Same for 'p'. */
2821 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002822 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002823 break;
2824 if (i == len - 1)
2825 {
2826 --len;
2827 if (p[-1] == 'l')
2828 eap->flags |= EXFLAG_LIST;
2829 else
2830 eap->flags |= EXFLAG_PRINT;
2831 }
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 if (ASCII_ISLOWER(*eap->cmd))
2835 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2836 else
2837 eap->cmdidx = cmdidxs[26];
2838
2839 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2840 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2841 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2842 (size_t)len) == 0)
2843 {
2844#ifdef FEAT_EVAL
2845 if (full != NULL
2846 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2847 *full = TRUE;
2848#endif
2849 break;
2850 }
2851
2852#ifdef FEAT_USR_CMDS
2853 /* Look for a user defined command as a last resort */
2854 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2855 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002856 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 while (ASCII_ISALNUM(*p))
2858 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002859 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002862 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 eap->cmdidx = CMD_SIZE;
2864 }
2865
2866 return p;
2867}
2868
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002869#ifdef FEAT_USR_CMDS
2870/*
2871 * Search for a user command that matches "eap->cmd".
2872 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2873 * Return a pointer to just after the command.
2874 * Return NULL if there is no matching command.
2875 */
2876 static char_u *
2877find_ucmd(eap, p, full, xp, compl)
2878 exarg_T *eap;
2879 char_u *p; /* end of the command (possibly including count) */
2880 int *full; /* set to TRUE for a full match */
2881 expand_T *xp; /* used for completion, NULL otherwise */
2882 int *compl; /* completion flags or NULL */
2883{
2884 int len = (int)(p - eap->cmd);
2885 int j, k, matchlen = 0;
2886 ucmd_T *uc;
2887 int found = FALSE;
2888 int possible = FALSE;
2889 char_u *cp, *np; /* Point into typed cmd and test name */
2890 garray_T *gap;
2891 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2892 only full match global is accepted. */
2893
2894 /*
2895 * Look for buffer-local user commands first, then global ones.
2896 */
2897 gap = &curbuf->b_ucmds;
2898 for (;;)
2899 {
2900 for (j = 0; j < gap->ga_len; ++j)
2901 {
2902 uc = USER_CMD_GA(gap, j);
2903 cp = eap->cmd;
2904 np = uc->uc_name;
2905 k = 0;
2906 while (k < len && *np != NUL && *cp++ == *np++)
2907 k++;
2908 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2909 {
2910 /* If finding a second match, the command is ambiguous. But
2911 * not if a buffer-local command wasn't a full match and a
2912 * global command is a full match. */
2913 if (k == len && found && *np != NUL)
2914 {
2915 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002916 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002917 amb_local = TRUE;
2918 }
2919
2920 if (!found || (k == len && *np == NUL))
2921 {
2922 /* If we matched up to a digit, then there could
2923 * be another command including the digit that we
2924 * should use instead.
2925 */
2926 if (k == len)
2927 found = TRUE;
2928 else
2929 possible = TRUE;
2930
2931 if (gap == &ucmds)
2932 eap->cmdidx = CMD_USER;
2933 else
2934 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002935 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002936 eap->useridx = j;
2937
2938# ifdef FEAT_CMDL_COMPL
2939 if (compl != NULL)
2940 *compl = uc->uc_compl;
2941# ifdef FEAT_EVAL
2942 if (xp != NULL)
2943 {
2944 xp->xp_arg = uc->uc_compl_arg;
2945 xp->xp_scriptID = uc->uc_scriptID;
2946 }
2947# endif
2948# endif
2949 /* Do not search for further abbreviations
2950 * if this is an exact match. */
2951 matchlen = k;
2952 if (k == len && *np == NUL)
2953 {
2954 if (full != NULL)
2955 *full = TRUE;
2956 amb_local = FALSE;
2957 break;
2958 }
2959 }
2960 }
2961 }
2962
2963 /* Stop if we found a full match or searched all. */
2964 if (j < gap->ga_len || gap == &ucmds)
2965 break;
2966 gap = &ucmds;
2967 }
2968
2969 /* Only found ambiguous matches. */
2970 if (amb_local)
2971 {
2972 if (xp != NULL)
2973 xp->xp_context = EXPAND_UNSUCCESSFUL;
2974 return NULL;
2975 }
2976
2977 /* The match we found may be followed immediately by a number. Move "p"
2978 * back to point to it. */
2979 if (found || possible)
2980 return p + (matchlen - len);
2981 return p;
2982}
2983#endif
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00002986static struct cmdmod
2987{
2988 char *name;
2989 int minlen;
2990 int has_count; /* :123verbose :3tab */
2991} cmdmods[] = {
2992 {"aboveleft", 3, FALSE},
2993 {"belowright", 3, FALSE},
2994 {"botright", 2, FALSE},
2995 {"browse", 3, FALSE},
2996 {"confirm", 4, FALSE},
2997 {"hide", 3, FALSE},
2998 {"keepalt", 5, FALSE},
2999 {"keepjumps", 5, FALSE},
3000 {"keepmarks", 3, FALSE},
3001 {"leftabove", 5, FALSE},
3002 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003003 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003004 {"rightbelow", 6, FALSE},
3005 {"sandbox", 3, FALSE},
3006 {"silent", 3, FALSE},
3007 {"tab", 3, TRUE},
3008 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003009 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003010 {"verbose", 4, TRUE},
3011 {"vertical", 4, FALSE},
3012};
3013
3014/*
3015 * Return length of a command modifier (including optional count).
3016 * Return zero when it's not a modifier.
3017 */
3018 int
3019modifier_len(cmd)
3020 char_u *cmd;
3021{
3022 int i, j;
3023 char_u *p = cmd;
3024
3025 if (VIM_ISDIGIT(*cmd))
3026 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003027 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003028 {
3029 for (j = 0; p[j] != NUL; ++j)
3030 if (p[j] != cmdmods[i].name[j])
3031 break;
3032 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3033 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003034 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003035 }
3036 return 0;
3037}
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * Return > 0 if an Ex command "name" exists.
3041 * Return 2 if there is an exact match.
3042 * Return 3 if there is an ambiguous match.
3043 */
3044 int
3045cmd_exists(name)
3046 char_u *name;
3047{
3048 exarg_T ea;
3049 int full = FALSE;
3050 int i;
3051 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003052 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003055 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 for (j = 0; name[j] != NUL; ++j)
3058 if (name[j] != cmdmods[i].name[j])
3059 break;
3060 if (name[j] == NUL && j >= cmdmods[i].minlen)
3061 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3062 }
3063
Bram Moolenaara9587612006-05-04 21:47:50 +00003064 /* Check built-in commands and user defined commands.
3065 * For ":2match" and ":3match" we need to skip the number. */
3066 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003068 p = find_command(&ea, &full);
3069 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003071 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3072 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003073 if (*skipwhite(p) != NUL)
3074 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3076}
3077#endif
3078
3079/*
3080 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3081 * we don't need/want deleted. Maybe this could be done better if we didn't
3082 * repeat all this stuff. The only problem is that they may not stay
3083 * perfectly compatible with each other, but then the command line syntax
3084 * probably won't change that much -- webb.
3085 */
3086 char_u *
3087set_one_cmd_context(xp, buff)
3088 expand_T *xp;
3089 char_u *buff; /* buffer for command string */
3090{
3091 char_u *p;
3092 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003093 int len = 0;
3094 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3096 int compl = EXPAND_NOTHING;
3097#endif
3098#ifdef FEAT_CMDL_COMPL
3099 int delim;
3100#endif
3101 int forceit = FALSE;
3102 int usefilter = FALSE; /* filter instead of file name */
3103
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003104 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 xp->xp_pattern = buff;
3106 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003107 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109/*
3110 * 2. skip comment lines and leading space, colons or bars
3111 */
3112 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3113 ;
3114 xp->xp_pattern = cmd;
3115
3116 if (*cmd == NUL)
3117 return NULL;
3118 if (*cmd == '"') /* ignore comment lines */
3119 {
3120 xp->xp_context = EXPAND_NOTHING;
3121 return NULL;
3122 }
3123
3124/*
3125 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3126 */
3127 cmd = skip_range(cmd, &xp->xp_context);
3128
3129/*
3130 * 4. parse command
3131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 xp->xp_pattern = cmd;
3133 if (*cmd == NUL)
3134 return NULL;
3135 if (*cmd == '"')
3136 {
3137 xp->xp_context = EXPAND_NOTHING;
3138 return NULL;
3139 }
3140
3141 if (*cmd == '|' || *cmd == '\n')
3142 return cmd + 1; /* There's another command */
3143
3144 /*
3145 * Isolate the command and search for it in the command table.
3146 * Exceptions:
3147 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003148 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3150 */
3151 if (*cmd == 'k' && cmd[1] != 'e')
3152 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003153 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 p = cmd + 1;
3155 }
3156 else
3157 {
3158 p = cmd;
3159 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3160 ++p;
3161 /* check for non-alpha command */
3162 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3163 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003164 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003166 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 xp->xp_context = EXPAND_UNSUCCESSFUL;
3169 return NULL;
3170 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003172 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3173 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3174 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 break;
3176
3177#ifdef FEAT_USR_CMDS
3178 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3180 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#endif
3182 }
3183
3184 /*
3185 * If the cursor is touching the command, and it ends in an alpha-numeric
3186 * character, complete the command name.
3187 */
3188 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3189 return NULL;
3190
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3194 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 p = cmd + 1;
3197 }
3198#ifdef FEAT_USR_CMDS
3199 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3200 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 ea.cmd = cmd;
3202 p = find_ucmd(&ea, p, NULL, xp,
3203# if defined(FEAT_CMDL_COMPL)
3204 &compl
3205# else
3206 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003209 if (p == NULL)
3210 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212#endif
3213 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 /* Not still touching the command and it was an illegal one */
3217 xp->xp_context = EXPAND_UNSUCCESSFUL;
3218 return NULL;
3219 }
3220
3221 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3222
3223 if (*p == '!') /* forced commands */
3224 {
3225 forceit = TRUE;
3226 ++p;
3227 }
3228
3229/*
3230 * 5. parse arguments
3231 */
3232#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003235 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 arg = skipwhite(p);
3238
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
3241 if (*arg == '>') /* append */
3242 {
3243 if (*++arg == '>')
3244 ++arg;
3245 arg = skipwhite(arg);
3246 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
3249 ++arg;
3250 usefilter = TRUE;
3251 }
3252 }
3253
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 usefilter = forceit; /* :r! filter if forced */
3257 if (*arg == '!') /* :r !filter */
3258 {
3259 ++arg;
3260 usefilter = TRUE;
3261 }
3262 }
3263
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 while (*arg == *cmd) /* allow any number of '>' or '<' */
3267 ++arg;
3268 arg = skipwhite(arg);
3269 }
3270
3271 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 /* Check if we're in the +command */
3275 p = arg + 1;
3276 arg = skip_cmd_arg(arg, FALSE);
3277
3278 /* Still touching the command after '+'? */
3279 if (*arg == NUL)
3280 return p;
3281
3282 /* Skip space(s) after +command to get to the real argument */
3283 arg = skipwhite(arg);
3284 }
3285
3286 /*
3287 * Check for '|' to separate commands and '"' to start comments.
3288 * Don't do this for ":read !cmd" and ":write !cmd".
3289 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003290 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 p = arg;
3293 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 p += 2;
3296 while (*p)
3297 {
3298 if (*p == Ctrl_V)
3299 {
3300 if (p[1] != NUL)
3301 ++p;
3302 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 || *p == '|' || *p == '\n')
3305 {
3306 if (*(p - 1) != '\\')
3307 {
3308 if (*p == '|' || *p == '\n')
3309 return p + 1;
3310 return NULL; /* It's a comment */
3311 }
3312 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003313 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 }
3316
3317 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 vim_strchr((char_u *)"|\"", *arg) == NULL)
3320 return NULL;
3321
3322 /* Find start of last argument (argument just before cursor): */
3323 p = buff + STRLEN(buff);
3324 while (p != arg && *p != ' ' && *p != TAB)
3325 p--;
3326 if (*p == ' ' || *p == TAB)
3327 p++;
3328 xp->xp_pattern = p;
3329
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003332 int c;
3333 int in_quote = FALSE;
3334 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /*
3337 * Allow spaces within back-quotes to count as part of the argument
3338 * being expanded.
3339 */
3340 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003341 p = xp->xp_pattern;
3342 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003344#ifdef FEAT_MBYTE
3345 if (has_mbyte)
3346 c = mb_ptr2char(p);
3347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003349 c = *p;
3350 if (c == '\\' && p[1] != NUL)
3351 ++p;
3352 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
3354 if (!in_quote)
3355 {
3356 xp->xp_pattern = p;
3357 bow = p + 1;
3358 }
3359 in_quote = !in_quote;
3360 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003361 /* An argument can contain just about everything, except
3362 * characters that end the command and white space. */
3363 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003364#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003365 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003366#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003367 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003368 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003369 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370 while (*p != NUL)
3371 {
3372#ifdef FEAT_MBYTE
3373 if (has_mbyte)
3374 c = mb_ptr2char(p);
3375 else
3376#endif
3377 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003378 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003379 break;
3380#ifdef FEAT_MBYTE
3381 if (has_mbyte)
3382 len = (*mb_ptr2len)(p);
3383 else
3384#endif
3385 len = 1;
3386 mb_ptr_adv(p);
3387 }
3388 if (in_quote)
3389 bow = p;
3390 else
3391 xp->xp_pattern = p;
3392 p -= len;
3393 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003394 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
3396
3397 /*
3398 * If we are still inside the quotes, and we passed a space, just
3399 * expand from there.
3400 */
3401 if (bow != NULL && in_quote)
3402 xp->xp_pattern = bow;
3403 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003404
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003405#ifndef BACKSLASH_IN_FILENAME
3406 /* For a shell command more chars need to be escaped. */
3407 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003408 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003409 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003410
3411 /* When still after the command name expand executables. */
3412 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003413 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003414 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /* Check for environment variable */
3418 if (*xp->xp_pattern == '$'
3419#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3420 || *xp->xp_pattern == '%'
3421#endif
3422 )
3423 {
3424 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3425 if (!vim_isIDc(*p))
3426 break;
3427 if (*p == NUL)
3428 {
3429 xp->xp_context = EXPAND_ENV_VARS;
3430 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003431#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3432 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003433 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003434 compl = EXPAND_ENV_VARS;
3435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 }
3438 }
3439
3440/*
3441 * 6. switch on command name
3442 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003443 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003445 case CMD_find:
3446 case CMD_sfind:
3447 case CMD_tabfind:
3448 xp->xp_context = EXPAND_FILES_IN_PATH;
3449 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 case CMD_cd:
3451 case CMD_chdir:
3452 case CMD_lcd:
3453 case CMD_lchdir:
3454 if (xp->xp_context == EXPAND_FILES)
3455 xp->xp_context = EXPAND_DIRECTORIES;
3456 break;
3457 case CMD_help:
3458 xp->xp_context = EXPAND_HELP;
3459 xp->xp_pattern = arg;
3460 break;
3461
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003462 /* Command modifiers: return the argument.
3463 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003465 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 case CMD_belowright:
3467 case CMD_botright:
3468 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003469 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003471 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 case CMD_folddoclosed:
3473 case CMD_folddoopen:
3474 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003475 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 case CMD_keepjumps:
3477 case CMD_keepmarks:
3478 case CMD_leftabove:
3479 case CMD_lockmarks:
3480 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003481 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003483 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 case CMD_topleft:
3485 case CMD_verbose:
3486 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003487 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return arg;
3489
Bram Moolenaar4f688582007-07-24 12:34:30 +00003490#ifdef FEAT_CMDL_COMPL
3491# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 case CMD_match:
3493 if (*arg == NUL || !ends_excmd(*arg))
3494 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003495 /* also complete "None" */
3496 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 arg = skipwhite(skiptowhite(arg));
3498 if (*arg != NUL)
3499 {
3500 xp->xp_context = EXPAND_NOTHING;
3501 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3502 }
3503 }
3504 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507/*
3508 * All completion for the +cmdline_compl feature goes here.
3509 */
3510
3511# ifdef FEAT_USR_CMDS
3512 case CMD_command:
3513 /* Check for attributes */
3514 while (*arg == '-')
3515 {
3516 arg++; /* Skip "-" */
3517 p = skiptowhite(arg);
3518 if (*p == NUL)
3519 {
3520 /* Cursor is still in the attribute */
3521 p = vim_strchr(arg, '=');
3522 if (p == NULL)
3523 {
3524 /* No "=", so complete attribute names */
3525 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3526 xp->xp_pattern = arg;
3527 return NULL;
3528 }
3529
3530 /* For the -complete and -nargs attributes, we complete
3531 * their arguments as well.
3532 */
3533 if (STRNICMP(arg, "complete", p - arg) == 0)
3534 {
3535 xp->xp_context = EXPAND_USER_COMPLETE;
3536 xp->xp_pattern = p + 1;
3537 return NULL;
3538 }
3539 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3540 {
3541 xp->xp_context = EXPAND_USER_NARGS;
3542 xp->xp_pattern = p + 1;
3543 return NULL;
3544 }
3545 return NULL;
3546 }
3547 arg = skipwhite(p);
3548 }
3549
3550 /* After the attributes comes the new command name */
3551 p = skiptowhite(arg);
3552 if (*p == NUL)
3553 {
3554 xp->xp_context = EXPAND_USER_COMMANDS;
3555 xp->xp_pattern = arg;
3556 break;
3557 }
3558
3559 /* And finally comes a normal command */
3560 return skipwhite(p);
3561
3562 case CMD_delcommand:
3563 xp->xp_context = EXPAND_USER_COMMANDS;
3564 xp->xp_pattern = arg;
3565 break;
3566# endif
3567
3568 case CMD_global:
3569 case CMD_vglobal:
3570 delim = *arg; /* get the delimiter */
3571 if (delim)
3572 ++arg; /* skip delimiter if there is one */
3573
3574 while (arg[0] != NUL && arg[0] != delim)
3575 {
3576 if (arg[0] == '\\' && arg[1] != NUL)
3577 ++arg;
3578 ++arg;
3579 }
3580 if (arg[0] != NUL)
3581 return arg + 1;
3582 break;
3583 case CMD_and:
3584 case CMD_substitute:
3585 delim = *arg;
3586 if (delim)
3587 {
3588 /* skip "from" part */
3589 ++arg;
3590 arg = skip_regexp(arg, delim, p_magic, NULL);
3591 }
3592 /* skip "to" part */
3593 while (arg[0] != NUL && arg[0] != delim)
3594 {
3595 if (arg[0] == '\\' && arg[1] != NUL)
3596 ++arg;
3597 ++arg;
3598 }
3599 if (arg[0] != NUL) /* skip delimiter */
3600 ++arg;
3601 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3602 ++arg;
3603 if (arg[0] != NUL)
3604 return arg;
3605 break;
3606 case CMD_isearch:
3607 case CMD_dsearch:
3608 case CMD_ilist:
3609 case CMD_dlist:
3610 case CMD_ijump:
3611 case CMD_psearch:
3612 case CMD_djump:
3613 case CMD_isplit:
3614 case CMD_dsplit:
3615 arg = skipwhite(skipdigits(arg)); /* skip count */
3616 if (*arg == '/') /* Match regexp, not just whole words */
3617 {
3618 for (++arg; *arg && *arg != '/'; arg++)
3619 if (*arg == '\\' && arg[1] != NUL)
3620 arg++;
3621 if (*arg)
3622 {
3623 arg = skipwhite(arg + 1);
3624
3625 /* Check for trailing illegal characters */
3626 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3627 xp->xp_context = EXPAND_NOTHING;
3628 else
3629 return arg;
3630 }
3631 }
3632 break;
3633#ifdef FEAT_AUTOCMD
3634 case CMD_autocmd:
3635 return set_context_in_autocmd(xp, arg, FALSE);
3636
3637 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003638 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return set_context_in_autocmd(xp, arg, TRUE);
3640#endif
3641 case CMD_set:
3642 set_context_in_set_cmd(xp, arg, 0);
3643 break;
3644 case CMD_setglobal:
3645 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3646 break;
3647 case CMD_setlocal:
3648 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3649 break;
3650 case CMD_tag:
3651 case CMD_stag:
3652 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003653 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 case CMD_tselect:
3655 case CMD_stselect:
3656 case CMD_ptselect:
3657 case CMD_tjump:
3658 case CMD_stjump:
3659 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003660 if (*p_wop != NUL)
3661 xp->xp_context = EXPAND_TAGS_LISTFILES;
3662 else
3663 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 xp->xp_pattern = arg;
3665 break;
3666 case CMD_augroup:
3667 xp->xp_context = EXPAND_AUGROUP;
3668 xp->xp_pattern = arg;
3669 break;
3670#ifdef FEAT_SYN_HL
3671 case CMD_syntax:
3672 set_context_in_syntax_cmd(xp, arg);
3673 break;
3674#endif
3675#ifdef FEAT_EVAL
3676 case CMD_let:
3677 case CMD_if:
3678 case CMD_elseif:
3679 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003680 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 case CMD_echo:
3682 case CMD_echon:
3683 case CMD_execute:
3684 case CMD_echomsg:
3685 case CMD_echoerr:
3686 case CMD_call:
3687 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003688 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 break;
3690
3691 case CMD_unlet:
3692 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3693 arg = xp->xp_pattern + 1;
3694 xp->xp_context = EXPAND_USER_VARS;
3695 xp->xp_pattern = arg;
3696 break;
3697
3698 case CMD_function:
3699 case CMD_delfunction:
3700 xp->xp_context = EXPAND_USER_FUNC;
3701 xp->xp_pattern = arg;
3702 break;
3703
3704 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003705 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 break;
3707#endif
3708 case CMD_highlight:
3709 set_context_in_highlight_cmd(xp, arg);
3710 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003711#ifdef FEAT_CSCOPE
3712 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003713 case CMD_lcscope:
3714 case CMD_scscope:
3715 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003716 break;
3717#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003718#ifdef FEAT_SIGNS
3719 case CMD_sign:
3720 set_context_in_sign_cmd(xp, arg);
3721 break;
3722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#ifdef FEAT_LISTCMDS
3724 case CMD_bdelete:
3725 case CMD_bwipeout:
3726 case CMD_bunload:
3727 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3728 arg = xp->xp_pattern + 1;
3729 /*FALLTHROUGH*/
3730 case CMD_buffer:
3731 case CMD_sbuffer:
3732 case CMD_checktime:
3733 xp->xp_context = EXPAND_BUFFERS;
3734 xp->xp_pattern = arg;
3735 break;
3736#endif
3737#ifdef FEAT_USR_CMDS
3738 case CMD_USER:
3739 case CMD_USER_BUF:
3740 if (compl != EXPAND_NOTHING)
3741 {
3742 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003743 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
3745# ifdef FEAT_MENU
3746 if (compl == EXPAND_MENUS)
3747 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3748# endif
3749 if (compl == EXPAND_COMMANDS)
3750 return arg;
3751 if (compl == EXPAND_MAPPINGS)
3752 return set_context_in_map_cmd(xp, (char_u *)"map",
3753 arg, forceit, FALSE, FALSE, CMD_map);
3754 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3755 arg = xp->xp_pattern + 1;
3756 xp->xp_pattern = arg;
3757 }
3758 xp->xp_context = compl;
3759 }
3760 break;
3761#endif
3762 case CMD_map: case CMD_noremap:
3763 case CMD_nmap: case CMD_nnoremap:
3764 case CMD_vmap: case CMD_vnoremap:
3765 case CMD_omap: case CMD_onoremap:
3766 case CMD_imap: case CMD_inoremap:
3767 case CMD_cmap: case CMD_cnoremap:
3768 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003769 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_unmap:
3771 case CMD_nunmap:
3772 case CMD_vunmap:
3773 case CMD_ounmap:
3774 case CMD_iunmap:
3775 case CMD_cunmap:
3776 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003777 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 case CMD_abbreviate: case CMD_noreabbrev:
3779 case CMD_cabbrev: case CMD_cnoreabbrev:
3780 case CMD_iabbrev: case CMD_inoreabbrev:
3781 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003782 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 case CMD_unabbreviate:
3784 case CMD_cunabbrev:
3785 case CMD_iunabbrev:
3786 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003787 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788#ifdef FEAT_MENU
3789 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3790 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3791 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3792 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3793 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3794 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3795 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3796 case CMD_tmenu: case CMD_tunmenu:
3797 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3798 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3799#endif
3800
3801 case CMD_colorscheme:
3802 xp->xp_context = EXPAND_COLORS;
3803 xp->xp_pattern = arg;
3804 break;
3805
3806 case CMD_compiler:
3807 xp->xp_context = EXPAND_COMPILER;
3808 xp->xp_pattern = arg;
3809 break;
3810
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003811 case CMD_ownsyntax:
3812 xp->xp_context = EXPAND_FILETYPE;
3813 xp->xp_pattern = arg;
3814 break;
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3817 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3818 case CMD_language:
3819 if (*skiptowhite(arg) == NUL)
3820 {
3821 xp->xp_context = EXPAND_LANGUAGE;
3822 xp->xp_pattern = arg;
3823 }
3824 else
3825 xp->xp_context = EXPAND_NOTHING;
3826 break;
3827#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003828#if defined(FEAT_PROFILE)
3829 case CMD_profile:
3830 set_context_in_profile_cmd(xp, arg);
3831 break;
3832#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003833 case CMD_behave:
3834 xp->xp_context = EXPAND_BEHAVE;
3835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837#endif /* FEAT_CMDL_COMPL */
3838
3839 default:
3840 break;
3841 }
3842 return NULL;
3843}
3844
3845/*
3846 * skip a range specifier of the form: addr [,addr] [;addr] ..
3847 *
3848 * Backslashed delimiters after / or ? will be skipped, and commands will
3849 * not be expanded between /'s and ?'s or after "'".
3850 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003851 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 * Returns the "cmd" pointer advanced to beyond the range.
3853 */
3854 char_u *
3855skip_range(cmd, ctx)
3856 char_u *cmd;
3857 int *ctx; /* pointer to xp_context or NULL */
3858{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003859 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
Bram Moolenaardf177f62005-02-22 08:39:57 +00003861 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
3863 if (*cmd == '\'')
3864 {
3865 if (*++cmd == NUL && ctx != NULL)
3866 *ctx = EXPAND_NOTHING;
3867 }
3868 else if (*cmd == '/' || *cmd == '?')
3869 {
3870 delim = *cmd++;
3871 while (*cmd != NUL && *cmd != delim)
3872 if (*cmd++ == '\\' && *cmd != NUL)
3873 ++cmd;
3874 if (*cmd == NUL && ctx != NULL)
3875 *ctx = EXPAND_NOTHING;
3876 }
3877 if (*cmd != NUL)
3878 ++cmd;
3879 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003880
3881 /* Skip ":" and white space. */
3882 while (*cmd == ':')
3883 cmd = skipwhite(cmd + 1);
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return cmd;
3886}
3887
3888/*
3889 * get a single EX address
3890 *
3891 * Set ptr to the next character after the part that was interpreted.
3892 * Set ptr to NULL when an error is encountered.
3893 *
3894 * Return MAXLNUM when no Ex address was found.
3895 */
3896 static linenr_T
3897get_address(ptr, skip, to_other_file)
3898 char_u **ptr;
3899 int skip; /* only skip the address, don't use it */
3900 int to_other_file; /* flag: may jump to other file */
3901{
3902 int c;
3903 int i;
3904 long n;
3905 char_u *cmd;
3906 pos_T pos;
3907 pos_T *fp;
3908 linenr_T lnum;
3909
3910 cmd = skipwhite(*ptr);
3911 lnum = MAXLNUM;
3912 do
3913 {
3914 switch (*cmd)
3915 {
3916 case '.': /* '.' - Cursor position */
3917 ++cmd;
3918 lnum = curwin->w_cursor.lnum;
3919 break;
3920
3921 case '$': /* '$' - last line */
3922 ++cmd;
3923 lnum = curbuf->b_ml.ml_line_count;
3924 break;
3925
3926 case '\'': /* ''' - mark */
3927 if (*++cmd == NUL)
3928 {
3929 cmd = NULL;
3930 goto error;
3931 }
3932 if (skip)
3933 ++cmd;
3934 else
3935 {
3936 /* Only accept a mark in another file when it is
3937 * used by itself: ":'M". */
3938 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3939 ++cmd;
3940 if (fp == (pos_T *)-1)
3941 /* Jumped to another file. */
3942 lnum = curwin->w_cursor.lnum;
3943 else
3944 {
3945 if (check_mark(fp) == FAIL)
3946 {
3947 cmd = NULL;
3948 goto error;
3949 }
3950 lnum = fp->lnum;
3951 }
3952 }
3953 break;
3954
3955 case '/':
3956 case '?': /* '/' or '?' - search */
3957 c = *cmd++;
3958 if (skip) /* skip "/pat/" */
3959 {
3960 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3961 if (*cmd == c)
3962 ++cmd;
3963 }
3964 else
3965 {
3966 pos = curwin->w_cursor; /* save curwin->w_cursor */
3967 /*
3968 * When '/' or '?' follows another address, start
3969 * from there.
3970 */
3971 if (lnum != MAXLNUM)
3972 curwin->w_cursor.lnum = lnum;
3973 /*
3974 * Start a forward search at the end of the line.
3975 * Start a backward search at the start of the line.
3976 * This makes sure we never match in the current
3977 * line, and can match anywhere in the
3978 * next/previous line.
3979 */
3980 if (c == '/')
3981 curwin->w_cursor.col = MAXCOL;
3982 else
3983 curwin->w_cursor.col = 0;
3984 searchcmdlen = 0;
3985 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003986 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
3988 curwin->w_cursor = pos;
3989 cmd = NULL;
3990 goto error;
3991 }
3992 lnum = curwin->w_cursor.lnum;
3993 curwin->w_cursor = pos;
3994 /* adjust command string pointer */
3995 cmd += searchcmdlen;
3996 }
3997 break;
3998
3999 case '\\': /* "\?", "\/" or "\&", repeat search */
4000 ++cmd;
4001 if (*cmd == '&')
4002 i = RE_SUBST;
4003 else if (*cmd == '?' || *cmd == '/')
4004 i = RE_SEARCH;
4005 else
4006 {
4007 EMSG(_(e_backslash));
4008 cmd = NULL;
4009 goto error;
4010 }
4011
4012 if (!skip)
4013 {
4014 /*
4015 * When search follows another address, start from
4016 * there.
4017 */
4018 if (lnum != MAXLNUM)
4019 pos.lnum = lnum;
4020 else
4021 pos.lnum = curwin->w_cursor.lnum;
4022
4023 /*
4024 * Start the search just like for the above
4025 * do_search().
4026 */
4027 if (*cmd != '?')
4028 pos.col = MAXCOL;
4029 else
4030 pos.col = 0;
4031 if (searchit(curwin, curbuf, &pos,
4032 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004033 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004034 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 lnum = pos.lnum;
4036 else
4037 {
4038 cmd = NULL;
4039 goto error;
4040 }
4041 }
4042 ++cmd;
4043 break;
4044
4045 default:
4046 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4047 lnum = getdigits(&cmd);
4048 }
4049
4050 for (;;)
4051 {
4052 cmd = skipwhite(cmd);
4053 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4054 break;
4055
4056 if (lnum == MAXLNUM)
4057 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4058 if (VIM_ISDIGIT(*cmd))
4059 i = '+'; /* "number" is same as "+number" */
4060 else
4061 i = *cmd++;
4062 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4063 n = 1;
4064 else
4065 n = getdigits(&cmd);
4066 if (i == '-')
4067 lnum -= n;
4068 else
4069 lnum += n;
4070 }
4071 } while (*cmd == '/' || *cmd == '?');
4072
4073error:
4074 *ptr = cmd;
4075 return lnum;
4076}
4077
4078/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004079 * Get flags from an Ex command argument.
4080 */
4081 static void
4082get_flags(eap)
4083 exarg_T *eap;
4084{
4085 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4086 {
4087 if (*eap->arg == 'l')
4088 eap->flags |= EXFLAG_LIST;
4089 else if (*eap->arg == 'p')
4090 eap->flags |= EXFLAG_PRINT;
4091 else
4092 eap->flags |= EXFLAG_NR;
4093 eap->arg = skipwhite(eap->arg + 1);
4094 }
4095}
4096
4097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 * Function called for command which is Not Implemented. NI!
4099 */
4100 void
4101ex_ni(eap)
4102 exarg_T *eap;
4103{
4104 if (!eap->skip)
4105 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4106}
4107
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004108#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109/*
4110 * Function called for script command which is Not Implemented. NI!
4111 * Skips over ":perl <<EOF" constructs.
4112 */
4113 static void
4114ex_script_ni(eap)
4115 exarg_T *eap;
4116{
4117 if (!eap->skip)
4118 ex_ni(eap);
4119 else
4120 vim_free(script_get(eap, eap->arg));
4121}
4122#endif
4123
4124/*
4125 * Check range in Ex command for validity.
4126 * Return NULL when valid, error message when invalid.
4127 */
4128 static char_u *
4129invalid_range(eap)
4130 exarg_T *eap;
4131{
4132 if ( eap->line1 < 0
4133 || eap->line2 < 0
4134 || eap->line1 > eap->line2
4135 || ((eap->argt & RANGE)
4136 && !(eap->argt & NOTADR)
4137 && eap->line2 > curbuf->b_ml.ml_line_count
4138#ifdef FEAT_DIFF
4139 + (eap->cmdidx == CMD_diffget)
4140#endif
4141 ))
4142 return (char_u *)_(e_invrange);
4143 return NULL;
4144}
4145
4146/*
4147 * Correct the range for zero line number, if required.
4148 */
4149 static void
4150correct_range(eap)
4151 exarg_T *eap;
4152{
4153 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4154 {
4155 if (eap->line1 == 0)
4156 eap->line1 = 1;
4157 if (eap->line2 == 0)
4158 eap->line2 = 1;
4159 }
4160}
4161
Bram Moolenaar748bf032005-02-02 23:04:36 +00004162#ifdef FEAT_QUICKFIX
4163static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4164
4165/*
4166 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4167 * pattern. Otherwise return eap->arg.
4168 */
4169 static char_u *
4170skip_grep_pat(eap)
4171 exarg_T *eap;
4172{
4173 char_u *p = eap->arg;
4174
Bram Moolenaara37420f2006-02-04 22:37:47 +00004175 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4176 || eap->cmdidx == CMD_vimgrepadd
4177 || eap->cmdidx == CMD_lvimgrepadd
4178 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004179 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004180 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004181 if (p == NULL)
4182 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004183 }
4184 return p;
4185}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004186
4187/*
4188 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4189 * in the command line, so that things like % get expanded.
4190 */
4191 static char_u *
4192replace_makeprg(eap, p, cmdlinep)
4193 exarg_T *eap;
4194 char_u *p;
4195 char_u **cmdlinep;
4196{
4197 char_u *new_cmdline;
4198 char_u *program;
4199 char_u *pos;
4200 char_u *ptr;
4201 int len;
4202 int i;
4203
4204 /*
4205 * Don't do it when ":vimgrep" is used for ":grep".
4206 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004207 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4208 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4209 || eap->cmdidx == CMD_grepadd
4210 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004211 && !grep_internal(eap->cmdidx))
4212 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004213 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4214 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004215 {
4216 if (*curbuf->b_p_gp == NUL)
4217 program = p_gp;
4218 else
4219 program = curbuf->b_p_gp;
4220 }
4221 else
4222 {
4223 if (*curbuf->b_p_mp == NUL)
4224 program = p_mp;
4225 else
4226 program = curbuf->b_p_mp;
4227 }
4228
4229 p = skipwhite(p);
4230
4231 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4232 {
4233 /* replace $* by given arguments */
4234 i = 1;
4235 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4236 ++i;
4237 len = (int)STRLEN(p);
4238 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4239 if (new_cmdline == NULL)
4240 return NULL; /* out of memory */
4241 ptr = new_cmdline;
4242 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4243 {
4244 i = (int)(pos - program);
4245 STRNCPY(ptr, program, i);
4246 STRCPY(ptr += i, p);
4247 ptr += len;
4248 program = pos + 2;
4249 }
4250 STRCPY(ptr, program);
4251 }
4252 else
4253 {
4254 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4255 if (new_cmdline == NULL)
4256 return NULL; /* out of memory */
4257 STRCPY(new_cmdline, program);
4258 STRCAT(new_cmdline, " ");
4259 STRCAT(new_cmdline, p);
4260 }
4261 msg_make(p);
4262
4263 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4264 vim_free(*cmdlinep);
4265 *cmdlinep = new_cmdline;
4266 p = new_cmdline;
4267 }
4268 return p;
4269}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004270#endif
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272/*
4273 * Expand file name in Ex command argument.
4274 * Return FAIL for failure, OK otherwise.
4275 */
4276 int
4277expand_filename(eap, cmdlinep, errormsgp)
4278 exarg_T *eap;
4279 char_u **cmdlinep;
4280 char_u **errormsgp;
4281{
4282 int has_wildcards; /* need to expand wildcards */
4283 char_u *repl;
4284 int srclen;
4285 char_u *p;
4286 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004287 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaar748bf032005-02-02 23:04:36 +00004289#ifdef FEAT_QUICKFIX
4290 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4291 p = skip_grep_pat(eap);
4292#else
4293 p = eap->arg;
4294#endif
4295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 /*
4297 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4298 * the file name contains a wildcard it should not cause expanding.
4299 * (it will be expanded anyway if there is a wildcard before replacing).
4300 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004301 has_wildcards = mch_has_wildcard(p);
4302 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004304#ifdef FEAT_EVAL
4305 /* Skip over `=expr`, wildcards in it are not expanded. */
4306 if (p[0] == '`' && p[1] == '=')
4307 {
4308 p += 2;
4309 (void)skip_expr(&p);
4310 if (*p == '`')
4311 ++p;
4312 continue;
4313 }
4314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /*
4316 * Quick check if this cannot be the start of a special string.
4317 * Also removes backslash before '%', '#' and '<'.
4318 */
4319 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4320 {
4321 ++p;
4322 continue;
4323 }
4324
4325 /*
4326 * Try to find a match at this position.
4327 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004328 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4329 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (*errormsgp != NULL) /* error detected */
4331 return FAIL;
4332 if (repl == NULL) /* no match found */
4333 {
4334 p += srclen;
4335 continue;
4336 }
4337
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004338 /* Wildcards won't be expanded below, the replacement is taken
4339 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4340 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4341 {
4342 char_u *l = repl;
4343
4344 repl = expand_env_save(repl);
4345 vim_free(l);
4346 }
4347
Bram Moolenaar63b92542007-03-27 14:57:09 +00004348 /* Need to escape white space et al. with a backslash.
4349 * Don't do this for:
4350 * - replacement that already has been escaped: "##"
4351 * - shell commands (may have to use quotes instead).
4352 * - non-unix systems when there is a single argument (spaces don't
4353 * separate arguments then).
4354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004356 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 && eap->cmdidx != CMD_bang
4358 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004359 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004361 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004363 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#ifndef UNIX
4365 && !(eap->argt & NOSPC)
4366#endif
4367 )
4368 {
4369 char_u *l;
4370#ifdef BACKSLASH_IN_FILENAME
4371 /* Don't escape a backslash here, because rem_backslash() doesn't
4372 * remove it later. */
4373 static char_u *nobslash = (char_u *)" \t\"|";
4374# define ESCAPE_CHARS nobslash
4375#else
4376# define ESCAPE_CHARS escape_chars
4377#endif
4378
4379 for (l = repl; *l; ++l)
4380 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4381 {
4382 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4383 if (l != NULL)
4384 {
4385 vim_free(repl);
4386 repl = l;
4387 }
4388 break;
4389 }
4390 }
4391
4392 /* For a shell command a '!' must be escaped. */
4393 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004394 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
4396 char_u *l;
4397
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004398 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 if (l != NULL)
4400 {
4401 vim_free(repl);
4402 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004403 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (strstr((char *)p_sh, "sh") != NULL)
4405 {
4406 l = vim_strsave_escaped(repl, (char_u *)"!");
4407 if (l != NULL)
4408 {
4409 vim_free(repl);
4410 repl = l;
4411 }
4412 }
4413 }
4414 }
4415
4416 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4417 vim_free(repl);
4418 if (p == NULL)
4419 return FAIL;
4420 }
4421
4422 /*
4423 * One file argument: Expand wildcards.
4424 * Don't do this with ":r !command" or ":w !command".
4425 */
4426 if ((eap->argt & NOSPC) && !eap->usefilter)
4427 {
4428 /*
4429 * May do this twice:
4430 * 1. Replace environment variables.
4431 * 2. Replace any other wildcards, remove backslashes.
4432 */
4433 for (n = 1; n <= 2; ++n)
4434 {
4435 if (n == 2)
4436 {
4437#ifdef UNIX
4438 /*
4439 * Only for Unix we check for more than one file name.
4440 * For other systems spaces are considered to be part
4441 * of the file name.
4442 * Only check here if there is no wildcard, otherwise
4443 * ExpandOne() will check for errors. This allows
4444 * ":e `ls ve*.c`" on Unix.
4445 */
4446 if (!has_wildcards)
4447 for (p = eap->arg; *p; ++p)
4448 {
4449 /* skip escaped characters */
4450 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4451 ++p;
4452 else if (vim_iswhite(*p))
4453 {
4454 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4455 return FAIL;
4456 }
4457 }
4458#endif
4459
4460 /*
4461 * Halve the number of backslashes (this is Vi compatible).
4462 * For Unix and OS/2, when wildcards are expanded, this is
4463 * done by ExpandOne() below.
4464 */
4465#if defined(UNIX) || defined(OS2)
4466 if (!has_wildcards)
4467#endif
4468 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470
4471 if (has_wildcards)
4472 {
4473 if (n == 1)
4474 {
4475 /*
4476 * First loop: May expand environment variables. This
4477 * can be done much faster with expand_env() than with
4478 * something else (e.g., calling a shell).
4479 * After expanding environment variables, check again
4480 * if there are still wildcards present.
4481 */
4482 if (vim_strchr(eap->arg, '$') != NULL
4483 || vim_strchr(eap->arg, '~') != NULL)
4484 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004485 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004486 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 has_wildcards = mch_has_wildcard(NameBuff);
4488 p = NameBuff;
4489 }
4490 else
4491 p = NULL;
4492 }
4493 else /* n == 2 */
4494 {
4495 expand_T xpc;
4496
4497 ExpandInit(&xpc);
4498 xpc.xp_context = EXPAND_FILES;
4499 p = ExpandOne(&xpc, eap->arg, NULL,
4500 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4501 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (p == NULL)
4503 return FAIL;
4504 }
4505 if (p != NULL)
4506 {
4507 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4508 p, cmdlinep);
4509 if (n == 2) /* p came from ExpandOne() */
4510 vim_free(p);
4511 }
4512 }
4513 }
4514 }
4515 return OK;
4516}
4517
4518/*
4519 * Replace part of the command line, keeping eap->cmd, eap->arg and
4520 * eap->nextcmd correct.
4521 * "src" points to the part that is to be replaced, of length "srclen".
4522 * "repl" is the replacement string.
4523 * Returns a pointer to the character after the replaced string.
4524 * Returns NULL for failure.
4525 */
4526 static char_u *
4527repl_cmdline(eap, src, srclen, repl, cmdlinep)
4528 exarg_T *eap;
4529 char_u *src;
4530 int srclen;
4531 char_u *repl;
4532 char_u **cmdlinep;
4533{
4534 int len;
4535 int i;
4536 char_u *new_cmdline;
4537
4538 /*
4539 * The new command line is build in new_cmdline[].
4540 * First allocate it.
4541 * Careful: a "+cmd" argument may have been NUL terminated.
4542 */
4543 len = (int)STRLEN(repl);
4544 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004545 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4547 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4548 return NULL; /* out of memory! */
4549
4550 /*
4551 * Copy the stuff before the expanded part.
4552 * Copy the expanded stuff.
4553 * Copy what came after the expanded part.
4554 * Copy the next commands, if there are any.
4555 */
4556 i = (int)(src - *cmdlinep); /* length of part before match */
4557 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 mch_memmove(new_cmdline + i, repl, (size_t)len);
4560 i += len; /* remember the end of the string */
4561 STRCPY(new_cmdline + i, src + srclen);
4562 src = new_cmdline + i; /* remember where to continue */
4563
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004564 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 i = (int)STRLEN(new_cmdline) + 1;
4567 STRCPY(new_cmdline + i, eap->nextcmd);
4568 eap->nextcmd = new_cmdline + i;
4569 }
4570 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4571 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4572 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4573 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4574 vim_free(*cmdlinep);
4575 *cmdlinep = new_cmdline;
4576
4577 return src;
4578}
4579
4580/*
4581 * Check for '|' to separate commands and '"' to start comments.
4582 */
4583 void
4584separate_nextcmd(eap)
4585 exarg_T *eap;
4586{
4587 char_u *p;
4588
Bram Moolenaar86b68352004-12-27 21:59:20 +00004589#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004590 p = skip_grep_pat(eap);
4591#else
4592 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004593#endif
4594
4595 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
4597 if (*p == Ctrl_V)
4598 {
4599 if (eap->argt & (USECTRLV | XFILE))
4600 ++p; /* skip CTRL-V and next char */
4601 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004602 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004603 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (*p == NUL) /* stop at NUL after CTRL-V */
4605 break;
4606 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004607
4608#ifdef FEAT_EVAL
4609 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004610 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004611 {
4612 p += 2;
4613 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004614 }
4615#endif
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 /* Check for '"': start of comment or '|': next command */
4618 /* :@" and :*" do not start a comment!
4619 * :redir @" doesn't either. */
4620 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4621 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4622 || p != eap->arg)
4623 && (eap->cmdidx != CMD_redir
4624 || p != eap->arg + 1 || p[-1] != '@'))
4625 || *p == '|' || *p == '\n')
4626 {
4627 /*
4628 * We remove the '\' before the '|', unless USECTRLV is used
4629 * AND 'b' is present in 'cpoptions'.
4630 */
4631 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4632 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4633 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004634 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 --p;
4636 }
4637 else
4638 {
4639 eap->nextcmd = check_nextcmd(p);
4640 *p = NUL;
4641 break;
4642 }
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4647 del_trailing_spaces(eap->arg);
4648}
4649
4650/*
4651 * get + command from ex argument
4652 */
4653 static char_u *
4654getargcmd(argp)
4655 char_u **argp;
4656{
4657 char_u *arg = *argp;
4658 char_u *command = NULL;
4659
4660 if (*arg == '+') /* +[command] */
4661 {
4662 ++arg;
4663 if (vim_isspace(*arg))
4664 command = dollar_command;
4665 else
4666 {
4667 command = arg;
4668 arg = skip_cmd_arg(command, TRUE);
4669 if (*arg != NUL)
4670 *arg++ = NUL; /* terminate command with NUL */
4671 }
4672
4673 arg = skipwhite(arg); /* skip over spaces */
4674 *argp = arg;
4675 }
4676 return command;
4677}
4678
4679/*
4680 * Find end of "+command" argument. Skip over "\ " and "\\".
4681 */
4682 static char_u *
4683skip_cmd_arg(p, rembs)
4684 char_u *p;
4685 int rembs; /* TRUE to halve the number of backslashes */
4686{
4687 while (*p && !vim_isspace(*p))
4688 {
4689 if (*p == '\\' && p[1] != NUL)
4690 {
4691 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004692 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 else
4694 ++p;
4695 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004696 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698 return p;
4699}
4700
4701/*
4702 * Get "++opt=arg" argument.
4703 * Return FAIL or OK.
4704 */
4705 static int
4706getargopt(eap)
4707 exarg_T *eap;
4708{
4709 char_u *arg = eap->arg + 2;
4710 int *pp = NULL;
4711#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004712 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u *p;
4714#endif
4715
4716 /* ":edit ++[no]bin[ary] file" */
4717 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4718 {
4719 if (*arg == 'n')
4720 {
4721 arg += 2;
4722 eap->force_bin = FORCE_NOBIN;
4723 }
4724 else
4725 eap->force_bin = FORCE_BIN;
4726 if (!checkforcmd(&arg, "binary", 3))
4727 return FAIL;
4728 eap->arg = skipwhite(arg);
4729 return OK;
4730 }
4731
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004732 /* ":read ++edit file" */
4733 if (STRNCMP(arg, "edit", 4) == 0)
4734 {
4735 eap->read_edit = TRUE;
4736 eap->arg = skipwhite(arg + 4);
4737 return OK;
4738 }
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (STRNCMP(arg, "ff", 2) == 0)
4741 {
4742 arg += 2;
4743 pp = &eap->force_ff;
4744 }
4745 else if (STRNCMP(arg, "fileformat", 10) == 0)
4746 {
4747 arg += 10;
4748 pp = &eap->force_ff;
4749 }
4750#ifdef FEAT_MBYTE
4751 else if (STRNCMP(arg, "enc", 3) == 0)
4752 {
4753 arg += 3;
4754 pp = &eap->force_enc;
4755 }
4756 else if (STRNCMP(arg, "encoding", 8) == 0)
4757 {
4758 arg += 8;
4759 pp = &eap->force_enc;
4760 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004761 else if (STRNCMP(arg, "bad", 3) == 0)
4762 {
4763 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004764 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766#endif
4767
4768 if (pp == NULL || *arg != '=')
4769 return FAIL;
4770
4771 ++arg;
4772 *pp = (int)(arg - eap->cmd);
4773 arg = skip_cmd_arg(arg, FALSE);
4774 eap->arg = skipwhite(arg);
4775 *arg = NUL;
4776
4777#ifdef FEAT_MBYTE
4778 if (pp == &eap->force_ff)
4779 {
4780#endif
4781 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4782 return FAIL;
4783#ifdef FEAT_MBYTE
4784 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004785 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
4787 /* Make 'fileencoding' lower case. */
4788 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4789 *p = TOLOWER_ASC(*p);
4790 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004791 else
4792 {
4793 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4794 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004795 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004796 if (STRICMP(p, "keep") == 0)
4797 eap->bad_char = BAD_KEEP;
4798 else if (STRICMP(p, "drop") == 0)
4799 eap->bad_char = BAD_DROP;
4800 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4801 eap->bad_char = *p;
4802 else
4803 return FAIL;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#endif
4806
4807 return OK;
4808}
4809
4810/*
4811 * ":abbreviate" and friends.
4812 */
4813 static void
4814ex_abbreviate(eap)
4815 exarg_T *eap;
4816{
4817 do_exmap(eap, TRUE); /* almost the same as mapping */
4818}
4819
4820/*
4821 * ":map" and friends.
4822 */
4823 static void
4824ex_map(eap)
4825 exarg_T *eap;
4826{
4827 /*
4828 * If we are sourcing .exrc or .vimrc in current directory we
4829 * print the mappings for security reasons.
4830 */
4831 if (secure)
4832 {
4833 secure = 2;
4834 msg_outtrans(eap->cmd);
4835 msg_putchar('\n');
4836 }
4837 do_exmap(eap, FALSE);
4838}
4839
4840/*
4841 * ":unmap" and friends.
4842 */
4843 static void
4844ex_unmap(eap)
4845 exarg_T *eap;
4846{
4847 do_exmap(eap, FALSE);
4848}
4849
4850/*
4851 * ":mapclear" and friends.
4852 */
4853 static void
4854ex_mapclear(eap)
4855 exarg_T *eap;
4856{
4857 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4858}
4859
4860/*
4861 * ":abclear" and friends.
4862 */
4863 static void
4864ex_abclear(eap)
4865 exarg_T *eap;
4866{
4867 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4868}
4869
4870#ifdef FEAT_AUTOCMD
4871 static void
4872ex_autocmd(eap)
4873 exarg_T *eap;
4874{
4875 /*
4876 * Disallow auto commands from .exrc and .vimrc in current
4877 * directory for security reasons.
4878 */
4879 if (secure)
4880 {
4881 secure = 2;
4882 eap->errmsg = e_curdir;
4883 }
4884 else if (eap->cmdidx == CMD_autocmd)
4885 do_autocmd(eap->arg, eap->forceit);
4886 else
4887 do_augroup(eap->arg, eap->forceit);
4888}
4889
4890/*
4891 * ":doautocmd": Apply the automatic commands to the current buffer.
4892 */
4893 static void
4894ex_doautocmd(eap)
4895 exarg_T *eap;
4896{
4897 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004898 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899}
4900#endif
4901
4902#ifdef FEAT_LISTCMDS
4903/*
4904 * :[N]bunload[!] [N] [bufname] unload buffer
4905 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4906 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4907 */
4908 static void
4909ex_bunload(eap)
4910 exarg_T *eap;
4911{
4912 eap->errmsg = do_bufdel(
4913 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4914 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4915 : DOBUF_UNLOAD, eap->arg,
4916 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4917}
4918
4919/*
4920 * :[N]buffer [N] to buffer N
4921 * :[N]sbuffer [N] to buffer N
4922 */
4923 static void
4924ex_buffer(eap)
4925 exarg_T *eap;
4926{
4927 if (*eap->arg)
4928 eap->errmsg = e_trailing;
4929 else
4930 {
4931 if (eap->addr_count == 0) /* default is current buffer */
4932 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4933 else
4934 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4935 }
4936}
4937
4938/*
4939 * :[N]bmodified [N] to next mod. buffer
4940 * :[N]sbmodified [N] to next mod. buffer
4941 */
4942 static void
4943ex_bmodified(eap)
4944 exarg_T *eap;
4945{
4946 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4947}
4948
4949/*
4950 * :[N]bnext [N] to next buffer
4951 * :[N]sbnext [N] split and to next buffer
4952 */
4953 static void
4954ex_bnext(eap)
4955 exarg_T *eap;
4956{
4957 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4958}
4959
4960/*
4961 * :[N]bNext [N] to previous buffer
4962 * :[N]bprevious [N] to previous buffer
4963 * :[N]sbNext [N] split and to previous buffer
4964 * :[N]sbprevious [N] split and to previous buffer
4965 */
4966 static void
4967ex_bprevious(eap)
4968 exarg_T *eap;
4969{
4970 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4971}
4972
4973/*
4974 * :brewind to first buffer
4975 * :bfirst to first buffer
4976 * :sbrewind split and to first buffer
4977 * :sbfirst split and to first buffer
4978 */
4979 static void
4980ex_brewind(eap)
4981 exarg_T *eap;
4982{
4983 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4984}
4985
4986/*
4987 * :blast to last buffer
4988 * :sblast split and to last buffer
4989 */
4990 static void
4991ex_blast(eap)
4992 exarg_T *eap;
4993{
4994 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4995}
4996#endif
4997
4998 int
4999ends_excmd(c)
5000 int c;
5001{
5002 return (c == NUL || c == '|' || c == '"' || c == '\n');
5003}
5004
5005#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5006 || defined(PROTO)
5007/*
5008 * Return the next command, after the first '|' or '\n'.
5009 * Return NULL if not found.
5010 */
5011 char_u *
5012find_nextcmd(p)
5013 char_u *p;
5014{
5015 while (*p != '|' && *p != '\n')
5016 {
5017 if (*p == NUL)
5018 return NULL;
5019 ++p;
5020 }
5021 return (p + 1);
5022}
5023#endif
5024
5025/*
5026 * Check if *p is a separator between Ex commands.
5027 * Return NULL if it isn't, (p + 1) if it is.
5028 */
5029 char_u *
5030check_nextcmd(p)
5031 char_u *p;
5032{
5033 p = skipwhite(p);
5034 if (*p == '|' || *p == '\n')
5035 return (p + 1);
5036 else
5037 return NULL;
5038}
5039
5040/*
5041 * - if there are more files to edit
5042 * - and this is the last window
5043 * - and forceit not used
5044 * - and not repeated twice on a row
5045 * return FAIL and give error message if 'message' TRUE
5046 * return OK otherwise
5047 */
5048 static int
5049check_more(message, forceit)
5050 int message; /* when FALSE check only, no messages */
5051 int forceit;
5052{
5053 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5054
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005055 if (!forceit && only_one_window()
5056 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
5058 if (message)
5059 {
5060#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5061 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5062 {
5063 char_u buff[IOSIZE];
5064
5065 if (n == 1)
5066 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5067 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005068 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 _("%d more files to edit. Quit anyway?"), n);
5070 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5071 return OK;
5072 return FAIL;
5073 }
5074#endif
5075 if (n == 1)
5076 EMSG(_("E173: 1 more file to edit"));
5077 else
5078 EMSGN(_("E173: %ld more files to edit"), n);
5079 quitmore = 2; /* next try to quit is allowed */
5080 }
5081 return FAIL;
5082 }
5083 return OK;
5084}
5085
5086#ifdef FEAT_CMDL_COMPL
5087/*
5088 * Function given to ExpandGeneric() to obtain the list of command names.
5089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 char_u *
5091get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005092 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 int idx;
5094{
5095 if (idx >= (int)CMD_SIZE)
5096# ifdef FEAT_USR_CMDS
5097 return get_user_command_name(idx);
5098# else
5099 return NULL;
5100# endif
5101 return cmdnames[idx].cmd_name;
5102}
5103#endif
5104
5105#if defined(FEAT_USR_CMDS) || defined(PROTO)
5106static 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));
5107static void uc_list __ARGS((char_u *name, size_t name_len));
5108static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5109static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5110static 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));
5111
5112 static int
5113uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5114 char_u *name;
5115 size_t name_len;
5116 char_u *rep;
5117 long argt;
5118 long def;
5119 int flags;
5120 int compl;
5121 char_u *compl_arg;
5122 int force;
5123{
5124 ucmd_T *cmd = NULL;
5125 char_u *p;
5126 int i;
5127 int cmp = 1;
5128 char_u *rep_buf = NULL;
5129 garray_T *gap;
5130
Bram Moolenaar9c102382006-05-03 21:26:49 +00005131 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (rep_buf == NULL)
5133 {
5134 /* Can't replace termcodes - try using the string as is */
5135 rep_buf = vim_strsave(rep);
5136
5137 /* Give up if out of memory */
5138 if (rep_buf == NULL)
5139 return FAIL;
5140 }
5141
5142 /* get address of growarray: global or in curbuf */
5143 if (flags & UC_BUFFER)
5144 {
5145 gap = &curbuf->b_ucmds;
5146 if (gap->ga_itemsize == 0)
5147 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5148 }
5149 else
5150 gap = &ucmds;
5151
5152 /* Search for the command in the already defined commands. */
5153 for (i = 0; i < gap->ga_len; ++i)
5154 {
5155 size_t len;
5156
5157 cmd = USER_CMD_GA(gap, i);
5158 len = STRLEN(cmd->uc_name);
5159 cmp = STRNCMP(name, cmd->uc_name, name_len);
5160 if (cmp == 0)
5161 {
5162 if (name_len < len)
5163 cmp = -1;
5164 else if (name_len > len)
5165 cmp = 1;
5166 }
5167
5168 if (cmp == 0)
5169 {
5170 if (!force)
5171 {
5172 EMSG(_("E174: Command already exists: add ! to replace it"));
5173 goto fail;
5174 }
5175
5176 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005177 cmd->uc_rep = NULL;
5178#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5179 vim_free(cmd->uc_compl_arg);
5180 cmd->uc_compl_arg = NULL;
5181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183 }
5184
5185 /* Stop as soon as we pass the name to add */
5186 if (cmp < 0)
5187 break;
5188 }
5189
5190 /* Extend the array unless we're replacing an existing command */
5191 if (cmp != 0)
5192 {
5193 if (ga_grow(gap, 1) != OK)
5194 goto fail;
5195 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5196 goto fail;
5197
5198 cmd = USER_CMD_GA(gap, i);
5199 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5200
5201 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 cmd->uc_name = p;
5204 }
5205
5206 cmd->uc_rep = rep_buf;
5207 cmd->uc_argt = argt;
5208 cmd->uc_def = def;
5209 cmd->uc_compl = compl;
5210#ifdef FEAT_EVAL
5211 cmd->uc_scriptID = current_SID;
5212# ifdef FEAT_CMDL_COMPL
5213 cmd->uc_compl_arg = compl_arg;
5214# endif
5215#endif
5216
5217 return OK;
5218
5219fail:
5220 vim_free(rep_buf);
5221#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5222 vim_free(compl_arg);
5223#endif
5224 return FAIL;
5225}
5226
5227/*
5228 * List of names for completion for ":command" with the EXPAND_ flag.
5229 * Must be alphabetical for completion.
5230 */
5231static struct
5232{
5233 int expand;
5234 char *name;
5235} command_complete[] =
5236{
5237 {EXPAND_AUGROUP, "augroup"},
5238 {EXPAND_BUFFERS, "buffer"},
5239 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005240#if defined(FEAT_CSCOPE)
5241 {EXPAND_CSCOPE, "cscope"},
5242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5244 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005245 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
5247 {EXPAND_DIRECTORIES, "dir"},
5248 {EXPAND_ENV_VARS, "environment"},
5249 {EXPAND_EVENTS, "event"},
5250 {EXPAND_EXPRESSION, "expression"},
5251 {EXPAND_FILES, "file"},
5252 {EXPAND_FUNCTIONS, "function"},
5253 {EXPAND_HELP, "help"},
5254 {EXPAND_HIGHLIGHT, "highlight"},
5255 {EXPAND_MAPPINGS, "mapping"},
5256 {EXPAND_MENUS, "menu"},
5257 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005258 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005259#if defined(FEAT_SIGNS)
5260 {EXPAND_SIGN, "sign"},
5261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {EXPAND_TAGS, "tag"},
5263 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5264 {EXPAND_USER_VARS, "var"},
5265 {0, NULL}
5266};
5267
5268 static void
5269uc_list(name, name_len)
5270 char_u *name;
5271 size_t name_len;
5272{
5273 int i, j;
5274 int found = FALSE;
5275 ucmd_T *cmd;
5276 int len;
5277 long a;
5278 garray_T *gap;
5279
5280 gap = &curbuf->b_ucmds;
5281 for (;;)
5282 {
5283 for (i = 0; i < gap->ga_len; ++i)
5284 {
5285 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005286 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /* Skip commands which don't match the requested prefix */
5289 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5290 continue;
5291
5292 /* Put out the title first time */
5293 if (!found)
5294 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5295 found = TRUE;
5296 msg_putchar('\n');
5297 if (got_int)
5298 break;
5299
5300 /* Special cases */
5301 msg_putchar(a & BANG ? '!' : ' ');
5302 msg_putchar(a & REGSTR ? '"' : ' ');
5303 msg_putchar(gap != &ucmds ? 'b' : ' ');
5304 msg_putchar(' ');
5305
5306 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5307 len = (int)STRLEN(cmd->uc_name) + 4;
5308
5309 do {
5310 msg_putchar(' ');
5311 ++len;
5312 } while (len < 16);
5313
5314 len = 0;
5315
5316 /* Arguments */
5317 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5318 {
5319 case 0: IObuff[len++] = '0'; break;
5320 case (EXTRA): IObuff[len++] = '*'; break;
5321 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5322 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5323 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5324 }
5325
5326 do {
5327 IObuff[len++] = ' ';
5328 } while (len < 5);
5329
5330 /* Range */
5331 if (a & (RANGE|COUNT))
5332 {
5333 if (a & COUNT)
5334 {
5335 /* -count=N */
5336 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5337 len += (int)STRLEN(IObuff + len);
5338 }
5339 else if (a & DFLALL)
5340 IObuff[len++] = '%';
5341 else if (cmd->uc_def >= 0)
5342 {
5343 /* -range=N */
5344 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5345 len += (int)STRLEN(IObuff + len);
5346 }
5347 else
5348 IObuff[len++] = '.';
5349 }
5350
5351 do {
5352 IObuff[len++] = ' ';
5353 } while (len < 11);
5354
5355 /* Completion */
5356 for (j = 0; command_complete[j].expand != 0; ++j)
5357 if (command_complete[j].expand == cmd->uc_compl)
5358 {
5359 STRCPY(IObuff + len, command_complete[j].name);
5360 len += (int)STRLEN(IObuff + len);
5361 break;
5362 }
5363
5364 do {
5365 IObuff[len++] = ' ';
5366 } while (len < 21);
5367
5368 IObuff[len] = '\0';
5369 msg_outtrans(IObuff);
5370
5371 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005372#ifdef FEAT_EVAL
5373 if (p_verbose > 0)
5374 last_set_msg(cmd->uc_scriptID);
5375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 out_flush();
5377 ui_breakcheck();
5378 if (got_int)
5379 break;
5380 }
5381 if (gap == &ucmds || i < gap->ga_len)
5382 break;
5383 gap = &ucmds;
5384 }
5385
5386 if (!found)
5387 MSG(_("No user-defined commands found"));
5388}
5389
5390 static char_u *
5391uc_fun_cmd()
5392{
5393 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5394 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5395 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5396 0xb9, 0x7f, 0};
5397 int i;
5398
5399 for (i = 0; fcmd[i]; ++i)
5400 IObuff[i] = fcmd[i] - 0x40;
5401 IObuff[i] = 0;
5402 return IObuff;
5403}
5404
5405 static int
5406uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5407 char_u *attr;
5408 size_t len;
5409 long *argt;
5410 long *def;
5411 int *flags;
5412 int *compl;
5413 char_u **compl_arg;
5414{
5415 char_u *p;
5416
5417 if (len == 0)
5418 {
5419 EMSG(_("E175: No attribute specified"));
5420 return FAIL;
5421 }
5422
5423 /* First, try the simple attributes (no arguments) */
5424 if (STRNICMP(attr, "bang", len) == 0)
5425 *argt |= BANG;
5426 else if (STRNICMP(attr, "buffer", len) == 0)
5427 *flags |= UC_BUFFER;
5428 else if (STRNICMP(attr, "register", len) == 0)
5429 *argt |= REGSTR;
5430 else if (STRNICMP(attr, "bar", len) == 0)
5431 *argt |= TRLBAR;
5432 else
5433 {
5434 int i;
5435 char_u *val = NULL;
5436 size_t vallen = 0;
5437 size_t attrlen = len;
5438
5439 /* Look for the attribute name - which is the part before any '=' */
5440 for (i = 0; i < (int)len; ++i)
5441 {
5442 if (attr[i] == '=')
5443 {
5444 val = &attr[i + 1];
5445 vallen = len - i - 1;
5446 attrlen = i;
5447 break;
5448 }
5449 }
5450
5451 if (STRNICMP(attr, "nargs", attrlen) == 0)
5452 {
5453 if (vallen == 1)
5454 {
5455 if (*val == '0')
5456 /* Do nothing - this is the default */;
5457 else if (*val == '1')
5458 *argt |= (EXTRA | NOSPC | NEEDARG);
5459 else if (*val == '*')
5460 *argt |= EXTRA;
5461 else if (*val == '?')
5462 *argt |= (EXTRA | NOSPC);
5463 else if (*val == '+')
5464 *argt |= (EXTRA | NEEDARG);
5465 else
5466 goto wrong_nargs;
5467 }
5468 else
5469 {
5470wrong_nargs:
5471 EMSG(_("E176: Invalid number of arguments"));
5472 return FAIL;
5473 }
5474 }
5475 else if (STRNICMP(attr, "range", attrlen) == 0)
5476 {
5477 *argt |= RANGE;
5478 if (vallen == 1 && *val == '%')
5479 *argt |= DFLALL;
5480 else if (val != NULL)
5481 {
5482 p = val;
5483 if (*def >= 0)
5484 {
5485two_count:
5486 EMSG(_("E177: Count cannot be specified twice"));
5487 return FAIL;
5488 }
5489
5490 *def = getdigits(&p);
5491 *argt |= (ZEROR | NOTADR);
5492
5493 if (p != val + vallen || vallen == 0)
5494 {
5495invalid_count:
5496 EMSG(_("E178: Invalid default value for count"));
5497 return FAIL;
5498 }
5499 }
5500 }
5501 else if (STRNICMP(attr, "count", attrlen) == 0)
5502 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005503 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 if (val != NULL)
5506 {
5507 p = val;
5508 if (*def >= 0)
5509 goto two_count;
5510
5511 *def = getdigits(&p);
5512
5513 if (p != val + vallen)
5514 goto invalid_count;
5515 }
5516
5517 if (*def < 0)
5518 *def = 0;
5519 }
5520 else if (STRNICMP(attr, "complete", attrlen) == 0)
5521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (val == NULL)
5523 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005524 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
5526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005528 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5529 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
5532 else
5533 {
5534 char_u ch = attr[len];
5535 attr[len] = '\0';
5536 EMSG2(_("E181: Invalid attribute: %s"), attr);
5537 attr[len] = ch;
5538 return FAIL;
5539 }
5540 }
5541
5542 return OK;
5543}
5544
Bram Moolenaara850a712009-01-28 14:42:59 +00005545/*
5546 * ":command ..."
5547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 static void
5549ex_command(eap)
5550 exarg_T *eap;
5551{
5552 char_u *name;
5553 char_u *end;
5554 char_u *p;
5555 long argt = 0;
5556 long def = -1;
5557 int flags = 0;
5558 int compl = EXPAND_NOTHING;
5559 char_u *compl_arg = NULL;
5560 int has_attr = (eap->arg[0] == '-');
5561
5562 p = eap->arg;
5563
5564 /* Check for attributes */
5565 while (*p == '-')
5566 {
5567 ++p;
5568 end = skiptowhite(p);
5569 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5570 == FAIL)
5571 return;
5572 p = skipwhite(end);
5573 }
5574
5575 /* Get the name (if any) and skip to the following argument */
5576 name = p;
5577 if (ASCII_ISALPHA(*p))
5578 while (ASCII_ISALNUM(*p))
5579 ++p;
5580 if (!ends_excmd(*p) && !vim_iswhite(*p))
5581 {
5582 EMSG(_("E182: Invalid command name"));
5583 return;
5584 }
5585 end = p;
5586
5587 /* If there is nothing after the name, and no attributes were specified,
5588 * we are listing commands
5589 */
5590 p = skipwhite(end);
5591 if (!has_attr && ends_excmd(*p))
5592 {
5593 uc_list(name, end - name);
5594 }
5595 else if (!ASCII_ISUPPER(*name))
5596 {
5597 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5598 return;
5599 }
5600 else
5601 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5602 eap->forceit);
5603}
5604
5605/*
5606 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 * Clear all user commands, global and for current buffer.
5608 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005609 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005611 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 uc_clear(&ucmds);
5614 uc_clear(&curbuf->b_ucmds);
5615}
5616
5617/*
5618 * Clear all user commands for "gap".
5619 */
5620 void
5621uc_clear(gap)
5622 garray_T *gap;
5623{
5624 int i;
5625 ucmd_T *cmd;
5626
5627 for (i = 0; i < gap->ga_len; ++i)
5628 {
5629 cmd = USER_CMD_GA(gap, i);
5630 vim_free(cmd->uc_name);
5631 vim_free(cmd->uc_rep);
5632# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5633 vim_free(cmd->uc_compl_arg);
5634# endif
5635 }
5636 ga_clear(gap);
5637}
5638
5639 static void
5640ex_delcommand(eap)
5641 exarg_T *eap;
5642{
5643 int i = 0;
5644 ucmd_T *cmd = NULL;
5645 int cmp = -1;
5646 garray_T *gap;
5647
5648 gap = &curbuf->b_ucmds;
5649 for (;;)
5650 {
5651 for (i = 0; i < gap->ga_len; ++i)
5652 {
5653 cmd = USER_CMD_GA(gap, i);
5654 cmp = STRCMP(eap->arg, cmd->uc_name);
5655 if (cmp <= 0)
5656 break;
5657 }
5658 if (gap == &ucmds || cmp == 0)
5659 break;
5660 gap = &ucmds;
5661 }
5662
5663 if (cmp != 0)
5664 {
5665 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5666 return;
5667 }
5668
5669 vim_free(cmd->uc_name);
5670 vim_free(cmd->uc_rep);
5671# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5672 vim_free(cmd->uc_compl_arg);
5673# endif
5674
5675 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677 if (i < gap->ga_len)
5678 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5679}
5680
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005681/*
5682 * split and quote args for <f-args>
5683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 static char_u *
5685uc_split_args(arg, lenp)
5686 char_u *arg;
5687 size_t *lenp;
5688{
5689 char_u *buf;
5690 char_u *p;
5691 char_u *q;
5692 int len;
5693
5694 /* Precalculate length */
5695 p = arg;
5696 len = 2; /* Initial and final quotes */
5697
5698 while (*p)
5699 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005700 if (p[0] == '\\' && p[1] == '\\')
5701 {
5702 len += 2;
5703 p += 2;
5704 }
5705 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 len += 1;
5708 p += 2;
5709 }
5710 else if (*p == '\\' || *p == '"')
5711 {
5712 len += 2;
5713 p += 1;
5714 }
5715 else if (vim_iswhite(*p))
5716 {
5717 p = skipwhite(p);
5718 if (*p == NUL)
5719 break;
5720 len += 3; /* "," */
5721 }
5722 else
5723 {
5724 ++len;
5725 ++p;
5726 }
5727 }
5728
5729 buf = alloc(len + 1);
5730 if (buf == NULL)
5731 {
5732 *lenp = 0;
5733 return buf;
5734 }
5735
5736 p = arg;
5737 q = buf;
5738 *q++ = '"';
5739 while (*p)
5740 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005741 if (p[0] == '\\' && p[1] == '\\')
5742 {
5743 *q++ = '\\';
5744 *q++ = '\\';
5745 p += 2;
5746 }
5747 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 *q++ = p[1];
5750 p += 2;
5751 }
5752 else if (*p == '\\' || *p == '"')
5753 {
5754 *q++ = '\\';
5755 *q++ = *p++;
5756 }
5757 else if (vim_iswhite(*p))
5758 {
5759 p = skipwhite(p);
5760 if (*p == NUL)
5761 break;
5762 *q++ = '"';
5763 *q++ = ',';
5764 *q++ = '"';
5765 }
5766 else
5767 {
5768 *q++ = *p++;
5769 }
5770 }
5771 *q++ = '"';
5772 *q = 0;
5773
5774 *lenp = len;
5775 return buf;
5776}
5777
5778/*
5779 * Check for a <> code in a user command.
5780 * "code" points to the '<'. "len" the length of the <> (inclusive).
5781 * "buf" is where the result is to be added.
5782 * "split_buf" points to a buffer used for splitting, caller should free it.
5783 * "split_len" is the length of what "split_buf" contains.
5784 * Returns the length of the replacement, which has been added to "buf".
5785 * Returns -1 if there was no match, and only the "<" has been copied.
5786 */
5787 static size_t
5788uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5789 char_u *code;
5790 size_t len;
5791 char_u *buf;
5792 ucmd_T *cmd; /* the user command we're expanding */
5793 exarg_T *eap; /* ex arguments */
5794 char_u **split_buf;
5795 size_t *split_len;
5796{
5797 size_t result = 0;
5798 char_u *p = code + 1;
5799 size_t l = len - 2;
5800 int quote = 0;
5801 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5802 ct_LT, ct_NONE } type = ct_NONE;
5803
5804 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5805 {
5806 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5807 p += 2;
5808 l -= 2;
5809 }
5810
Bram Moolenaar371d5402006-03-20 21:47:49 +00005811 ++l;
5812 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005814 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005816 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005818 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005820 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005822 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005824 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005826 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 type = ct_REGISTER;
5828
5829 switch (type)
5830 {
5831 case ct_ARGS:
5832 /* Simple case first */
5833 if (*eap->arg == NUL)
5834 {
5835 if (quote == 1)
5836 {
5837 result = 2;
5838 if (buf != NULL)
5839 STRCPY(buf, "''");
5840 }
5841 else
5842 result = 0;
5843 break;
5844 }
5845
5846 /* When specified there is a single argument don't split it.
5847 * Works for ":Cmd %" when % is "a b c". */
5848 if ((eap->argt & NOSPC) && quote == 2)
5849 quote = 1;
5850
5851 switch (quote)
5852 {
5853 case 0: /* No quoting, no splitting */
5854 result = STRLEN(eap->arg);
5855 if (buf != NULL)
5856 STRCPY(buf, eap->arg);
5857 break;
5858 case 1: /* Quote, but don't split */
5859 result = STRLEN(eap->arg) + 2;
5860 for (p = eap->arg; *p; ++p)
5861 {
5862 if (*p == '\\' || *p == '"')
5863 ++result;
5864 }
5865
5866 if (buf != NULL)
5867 {
5868 *buf++ = '"';
5869 for (p = eap->arg; *p; ++p)
5870 {
5871 if (*p == '\\' || *p == '"')
5872 *buf++ = '\\';
5873 *buf++ = *p;
5874 }
5875 *buf = '"';
5876 }
5877
5878 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005879 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 /* This is hard, so only do it once, and cache the result */
5881 if (*split_buf == NULL)
5882 *split_buf = uc_split_args(eap->arg, split_len);
5883
5884 result = *split_len;
5885 if (buf != NULL && result != 0)
5886 STRCPY(buf, *split_buf);
5887
5888 break;
5889 }
5890 break;
5891
5892 case ct_BANG:
5893 result = eap->forceit ? 1 : 0;
5894 if (quote)
5895 result += 2;
5896 if (buf != NULL)
5897 {
5898 if (quote)
5899 *buf++ = '"';
5900 if (eap->forceit)
5901 *buf++ = '!';
5902 if (quote)
5903 *buf = '"';
5904 }
5905 break;
5906
5907 case ct_LINE1:
5908 case ct_LINE2:
5909 case ct_COUNT:
5910 {
5911 char num_buf[20];
5912 long num = (type == ct_LINE1) ? eap->line1 :
5913 (type == ct_LINE2) ? eap->line2 :
5914 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5915 size_t num_len;
5916
5917 sprintf(num_buf, "%ld", num);
5918 num_len = STRLEN(num_buf);
5919 result = num_len;
5920
5921 if (quote)
5922 result += 2;
5923
5924 if (buf != NULL)
5925 {
5926 if (quote)
5927 *buf++ = '"';
5928 STRCPY(buf, num_buf);
5929 buf += num_len;
5930 if (quote)
5931 *buf = '"';
5932 }
5933
5934 break;
5935 }
5936
5937 case ct_REGISTER:
5938 result = eap->regname ? 1 : 0;
5939 if (quote)
5940 result += 2;
5941 if (buf != NULL)
5942 {
5943 if (quote)
5944 *buf++ = '\'';
5945 if (eap->regname)
5946 *buf++ = eap->regname;
5947 if (quote)
5948 *buf = '\'';
5949 }
5950 break;
5951
5952 case ct_LT:
5953 result = 1;
5954 if (buf != NULL)
5955 *buf = '<';
5956 break;
5957
5958 default:
5959 /* Not recognized: just copy the '<' and return -1. */
5960 result = (size_t)-1;
5961 if (buf != NULL)
5962 *buf = '<';
5963 break;
5964 }
5965
5966 return result;
5967}
5968
5969 static void
5970do_ucmd(eap)
5971 exarg_T *eap;
5972{
5973 char_u *buf;
5974 char_u *p;
5975 char_u *q;
5976
5977 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005978 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005979 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 size_t len, totlen;
5981
5982 size_t split_len = 0;
5983 char_u *split_buf = NULL;
5984 ucmd_T *cmd;
5985#ifdef FEAT_EVAL
5986 scid_T save_current_SID = current_SID;
5987#endif
5988
5989 if (eap->cmdidx == CMD_USER)
5990 cmd = USER_CMD(eap->useridx);
5991 else
5992 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5993
5994 /*
5995 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00005996 * First round: "buf" is NULL, compute length, allocate "buf".
5997 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 */
5999 buf = NULL;
6000 for (;;)
6001 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006002 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006003 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006005
6006 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006008 start = vim_strchr(p, '<');
6009 if (start != NULL)
6010 end = vim_strchr(start + 1, '>');
6011 if (buf != NULL)
6012 {
6013 ksp = vim_strchr(p, K_SPECIAL);
6014 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6015 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6016# ifdef FEAT_GUI
6017 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6018# endif
6019 ))
6020 {
6021 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6022 * KS_SPECIAL KE_FILLER, like for mappings, but
6023 * do_cmdline() doesn't handle that, so convert it back.
6024 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6025 len = ksp - p;
6026 if (len > 0)
6027 {
6028 mch_memmove(q, p, len);
6029 q += len;
6030 }
6031 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6032 p = ksp + 3;
6033 continue;
6034 }
6035 }
6036
6037 /* break if there no <item> is found */
6038 if (start == NULL || end == NULL)
6039 break;
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 /* Include the '>' */
6042 ++end;
6043
6044 /* Take everything up to the '<' */
6045 len = start - p;
6046 if (buf == NULL)
6047 totlen += len;
6048 else
6049 {
6050 mch_memmove(q, p, len);
6051 q += len;
6052 }
6053
6054 len = uc_check_code(start, end - start, q, cmd, eap,
6055 &split_buf, &split_len);
6056 if (len == (size_t)-1)
6057 {
6058 /* no match, continue after '<' */
6059 p = start + 1;
6060 len = 1;
6061 }
6062 else
6063 p = end;
6064 if (buf == NULL)
6065 totlen += len;
6066 else
6067 q += len;
6068 }
6069 if (buf != NULL) /* second time here, finished */
6070 {
6071 STRCPY(q, p);
6072 break;
6073 }
6074
6075 totlen += STRLEN(p); /* Add on the trailing characters */
6076 buf = alloc((unsigned)(totlen + 1));
6077 if (buf == NULL)
6078 {
6079 vim_free(split_buf);
6080 return;
6081 }
6082 }
6083
6084#ifdef FEAT_EVAL
6085 current_SID = cmd->uc_scriptID;
6086#endif
6087 (void)do_cmdline(buf, eap->getline, eap->cookie,
6088 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6089#ifdef FEAT_EVAL
6090 current_SID = save_current_SID;
6091#endif
6092 vim_free(buf);
6093 vim_free(split_buf);
6094}
6095
6096# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6097 static char_u *
6098get_user_command_name(idx)
6099 int idx;
6100{
6101 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6102}
6103
6104/*
6105 * Function given to ExpandGeneric() to obtain the list of user command names.
6106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 char_u *
6108get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006109 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 int idx;
6111{
6112 if (idx < curbuf->b_ucmds.ga_len)
6113 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6114 idx -= curbuf->b_ucmds.ga_len;
6115 if (idx < ucmds.ga_len)
6116 return USER_CMD(idx)->uc_name;
6117 return NULL;
6118}
6119
6120/*
6121 * Function given to ExpandGeneric() to obtain the list of user command
6122 * attributes.
6123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 char_u *
6125get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006126 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 int idx;
6128{
6129 static char *user_cmd_flags[] =
6130 {"bang", "bar", "buffer", "complete", "count",
6131 "nargs", "range", "register"};
6132
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006133 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 return NULL;
6135 return (char_u *)user_cmd_flags[idx];
6136}
6137
6138/*
6139 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 char_u *
6142get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006143 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 int idx;
6145{
6146 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6147
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006148 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 return NULL;
6150 return (char_u *)user_cmd_nargs[idx];
6151}
6152
6153/*
6154 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 char_u *
6157get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006158 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 int idx;
6160{
6161 return (char_u *)command_complete[idx].name;
6162}
6163# endif /* FEAT_CMDL_COMPL */
6164
6165#endif /* FEAT_USR_CMDS */
6166
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006167#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6168/*
6169 * Parse a completion argument "value[vallen]".
6170 * The detected completion goes in "*complp", argument type in "*argt".
6171 * When there is an argument, for function and user defined completion, it's
6172 * copied to allocated memory and stored in "*compl_arg".
6173 * Returns FAIL if something is wrong.
6174 */
6175 int
6176parse_compl_arg(value, vallen, complp, argt, compl_arg)
6177 char_u *value;
6178 int vallen;
6179 int *complp;
6180 long *argt;
6181 char_u **compl_arg;
6182{
6183 char_u *arg = NULL;
6184 size_t arglen = 0;
6185 int i;
6186 int valend = vallen;
6187
6188 /* Look for any argument part - which is the part after any ',' */
6189 for (i = 0; i < vallen; ++i)
6190 {
6191 if (value[i] == ',')
6192 {
6193 arg = &value[i + 1];
6194 arglen = vallen - i - 1;
6195 valend = i;
6196 break;
6197 }
6198 }
6199
6200 for (i = 0; command_complete[i].expand != 0; ++i)
6201 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006202 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006203 && STRNCMP(value, command_complete[i].name, valend) == 0)
6204 {
6205 *complp = command_complete[i].expand;
6206 if (command_complete[i].expand == EXPAND_BUFFERS)
6207 *argt |= BUFNAME;
6208 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6209 || command_complete[i].expand == EXPAND_FILES)
6210 *argt |= XFILE;
6211 break;
6212 }
6213 }
6214
6215 if (command_complete[i].expand == 0)
6216 {
6217 EMSG2(_("E180: Invalid complete value: %s"), value);
6218 return FAIL;
6219 }
6220
6221# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6222 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6223 && arg != NULL)
6224# else
6225 if (arg != NULL)
6226# endif
6227 {
6228 EMSG(_("E468: Completion argument only allowed for custom completion"));
6229 return FAIL;
6230 }
6231
6232# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6233 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6234 && arg == NULL)
6235 {
6236 EMSG(_("E467: Custom completion requires a function argument"));
6237 return FAIL;
6238 }
6239
6240 if (arg != NULL)
6241 *compl_arg = vim_strnsave(arg, (int)arglen);
6242# endif
6243 return OK;
6244}
6245#endif
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 static void
6248ex_colorscheme(eap)
6249 exarg_T *eap;
6250{
Bram Moolenaare6850792010-05-14 15:28:44 +02006251 if (*eap->arg == NUL)
6252 {
6253#ifdef FEAT_EVAL
6254 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6255 char_u *p = NULL;
6256
6257 if (expr != NULL)
6258 {
6259 ++emsg_off;
6260 p = eval_to_string(expr, NULL, FALSE);
6261 --emsg_off;
6262 vim_free(expr);
6263 }
6264 if (p != NULL)
6265 {
6266 MSG(p);
6267 vim_free(p);
6268 }
6269 else
6270 MSG("default");
6271#else
6272 MSG(_("unknown"));
6273#endif
6274 }
6275 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6277}
6278
6279 static void
6280ex_highlight(eap)
6281 exarg_T *eap;
6282{
6283 if (*eap->arg == NUL && eap->cmd[2] == '!')
6284 MSG(_("Greetings, Vim user!"));
6285 do_highlight(eap->arg, eap->forceit, FALSE);
6286}
6287
6288
6289/*
6290 * Call this function if we thought we were going to exit, but we won't
6291 * (because of an error). May need to restore the terminal mode.
6292 */
6293 void
6294not_exiting()
6295{
6296 exiting = FALSE;
6297 settmode(TMODE_RAW);
6298}
6299
6300/*
6301 * ":quit": quit current window, quit Vim if closed the last window.
6302 */
6303 static void
6304ex_quit(eap)
6305 exarg_T *eap;
6306{
6307#ifdef FEAT_CMDWIN
6308 if (cmdwin_type != 0)
6309 {
6310 cmdwin_result = Ctrl_C;
6311 return;
6312 }
6313#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006314 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006315 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006316 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006317 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006318 return;
6319 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006320#ifdef FEAT_AUTOCMD
6321 if (curbuf_locked())
6322 return;
6323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
6325#ifdef FEAT_NETBEANS_INTG
6326 netbeansForcedQuit = eap->forceit;
6327#endif
6328
6329 /*
6330 * If there are more files or windows we won't exit.
6331 */
6332 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6333 exiting = TRUE;
6334 if ((!P_HID(curbuf)
6335 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6336 || check_more(TRUE, eap->forceit) == FAIL
6337 || (only_one_window() && check_changed_any(eap->forceit)))
6338 {
6339 not_exiting();
6340 }
6341 else
6342 {
6343#ifdef FEAT_WINDOWS
6344 if (only_one_window()) /* quit last window */
6345#endif
6346 getout(0);
6347#ifdef FEAT_WINDOWS
6348# ifdef FEAT_GUI
6349 need_mouse_correct = TRUE;
6350# endif
6351 /* close window; may free buffer */
6352 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6353#endif
6354 }
6355}
6356
6357/*
6358 * ":cquit".
6359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 static void
6361ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006362 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 getout(1); /* this does not always pass on the exit code to the Manx
6365 compiler. why? */
6366}
6367
6368/*
6369 * ":qall": try to quit all windows
6370 */
6371 static void
6372ex_quit_all(eap)
6373 exarg_T *eap;
6374{
6375# ifdef FEAT_CMDWIN
6376 if (cmdwin_type != 0)
6377 {
6378 if (eap->forceit)
6379 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6380 else
6381 cmdwin_result = K_XF2;
6382 return;
6383 }
6384# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006385
6386 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006387 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006388 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006389 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006390 return;
6391 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006392#ifdef FEAT_AUTOCMD
6393 if (curbuf_locked())
6394 return;
6395#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 exiting = TRUE;
6398 if (eap->forceit || !check_changed_any(FALSE))
6399 getout(0);
6400 not_exiting();
6401}
6402
Bram Moolenaard9967712006-03-11 21:18:15 +00006403#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404/*
6405 * ":close": close current window, unless it is the last one
6406 */
6407 static void
6408ex_close(eap)
6409 exarg_T *eap;
6410{
6411# ifdef FEAT_CMDWIN
6412 if (cmdwin_type != 0)
6413 cmdwin_result = K_IGNORE;
6414 else
6415# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006416 if (!text_locked()
6417#ifdef FEAT_AUTOCMD
6418 && !curbuf_locked()
6419#endif
6420 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006421 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422}
6423
Bram Moolenaard9967712006-03-11 21:18:15 +00006424# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425/*
6426 * ":pclose": Close any preview window.
6427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431{
6432 win_T *win;
6433
6434 for (win = firstwin; win != NULL; win = win->w_next)
6435 if (win->w_p_pvw)
6436 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006437 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 break;
6439 }
6440}
Bram Moolenaard9967712006-03-11 21:18:15 +00006441# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006442
Bram Moolenaarf740b292006-02-16 22:11:02 +00006443/*
6444 * Close window "win" and take care of handling closing the last window for a
6445 * modified buffer.
6446 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006448ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006451 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 int need_hide;
6454 buf_T *buf = win->w_buffer;
6455
6456 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006459# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 if ((p_confirm || cmdmod.confirm) && p_write)
6461 {
6462 dialog_changed(buf, FALSE);
6463 if (buf_valid(buf) && bufIsChanged(buf))
6464 return;
6465 need_hide = FALSE;
6466 }
6467 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 {
6470 EMSG(_(e_nowrtmsg));
6471 return;
6472 }
6473 }
6474
Bram Moolenaard9967712006-03-11 21:18:15 +00006475# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006477# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006480 if (tp == NULL)
6481 win_close(win, !need_hide && !P_HID(buf));
6482 else
6483 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006487 * ":tabclose": close current tab page, unless it is the last one.
6488 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 */
6490 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 exarg_T *eap;
6493{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006494 tabpage_T *tp;
6495
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496# ifdef FEAT_CMDWIN
6497 if (cmdwin_type != 0)
6498 cmdwin_result = K_IGNORE;
6499 else
6500# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006501 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006502 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006503 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006505 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006507 tp = find_tabpage((int)eap->line2);
6508 if (tp == NULL)
6509 {
6510 beep_flush();
6511 return;
6512 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006513 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006514 {
6515 tabpage_close_other(tp, eap->forceit);
6516 return;
6517 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006519 if (!text_locked()
6520#ifdef FEAT_AUTOCMD
6521 && !curbuf_locked()
6522#endif
6523 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006526}
6527
6528/*
6529 * ":tabonly": close all tab pages except the current one
6530 */
6531 static void
6532ex_tabonly(eap)
6533 exarg_T *eap;
6534{
6535 tabpage_T *tp;
6536 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006537
6538# ifdef FEAT_CMDWIN
6539 if (cmdwin_type != 0)
6540 cmdwin_result = K_IGNORE;
6541 else
6542# endif
6543 if (first_tabpage->tp_next == NULL)
6544 MSG(_("Already only one tab page"));
6545 else
6546 {
6547 /* Repeat this up to a 1000 times, because autocommands may mess
6548 * up the lists. */
6549 for (done = 0; done < 1000; ++done)
6550 {
6551 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6552 if (tp->tp_topframe != topframe)
6553 {
6554 tabpage_close_other(tp, eap->forceit);
6555 /* if we failed to close it quit */
6556 if (valid_tabpage(tp))
6557 done = 1000;
6558 /* start over, "tp" is now invalid */
6559 break;
6560 }
6561 if (first_tabpage->tp_next == NULL)
6562 break;
6563 }
6564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568 * Close the current tab page.
6569 */
6570 void
6571tabpage_close(forceit)
6572 int forceit;
6573{
6574 /* First close all the windows but the current one. If that worked then
6575 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006576 if (lastwin != firstwin)
6577 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006578 if (lastwin == firstwin)
6579 ex_win_close(forceit, curwin, NULL);
6580# ifdef FEAT_GUI
6581 need_mouse_correct = TRUE;
6582# endif
6583}
6584
6585/*
6586 * Close tab page "tp", which is not the current tab page.
6587 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006588 * Also takes care of the tab pages line disappearing when closing the
6589 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006590 */
6591 void
6592tabpage_close_other(tp, forceit)
6593 tabpage_T *tp;
6594 int forceit;
6595{
6596 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006597 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006598 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006599
6600 /* Limit to 1000 windows, autocommands may add a window while we close
6601 * one. OK, so I'm paranoid... */
6602 while (++done < 1000)
6603 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006604 wp = tp->tp_firstwin;
6605 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006606
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006607 /* Autocommands may delete the tab page under our fingers and we may
6608 * fail to close a window with a modified buffer. */
6609 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006610 break;
6611 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006612
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006613 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006614 if (h != tabline_height())
6615 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006616}
6617
6618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 * ":only".
6620 */
6621 static void
6622ex_only(eap)
6623 exarg_T *eap;
6624{
6625# ifdef FEAT_GUI
6626 need_mouse_correct = TRUE;
6627# endif
6628 close_others(TRUE, eap->forceit);
6629}
6630
6631/*
6632 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006633 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006635 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636ex_all(eap)
6637 exarg_T *eap;
6638{
6639 if (eap->addr_count == 0)
6640 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006641 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642}
6643#endif /* FEAT_WINDOWS */
6644
6645 static void
6646ex_hide(eap)
6647 exarg_T *eap;
6648{
6649 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6650 eap->errmsg = e_invarg;
6651 else
6652 {
6653 /* ":hide" or ":hide | cmd": hide current window */
6654 eap->nextcmd = check_nextcmd(eap->arg);
6655#ifdef FEAT_WINDOWS
6656 if (!eap->skip)
6657 {
6658# ifdef FEAT_GUI
6659 need_mouse_correct = TRUE;
6660# endif
6661 win_close(curwin, FALSE); /* don't free buffer */
6662 }
6663#endif
6664 }
6665}
6666
6667/*
6668 * ":stop" and ":suspend": Suspend Vim.
6669 */
6670 static void
6671ex_stop(eap)
6672 exarg_T *eap;
6673{
6674 /*
6675 * Disallow suspending for "rvim".
6676 */
6677 if (!check_restricted()
6678#ifdef WIN3264
6679 /*
6680 * Check if external commands are allowed now.
6681 */
6682 && can_end_termcap_mode(TRUE)
6683#endif
6684 )
6685 {
6686 if (!eap->forceit)
6687 autowrite_all();
6688 windgoto((int)Rows - 1, 0);
6689 out_char('\n');
6690 out_flush();
6691 stoptermcap();
6692 out_flush(); /* needed for SUN to restore xterm buffer */
6693#ifdef FEAT_TITLE
6694 mch_restore_title(3); /* restore window titles */
6695#endif
6696 ui_suspend(); /* call machine specific function */
6697#ifdef FEAT_TITLE
6698 maketitle();
6699 resettitle(); /* force updating the title */
6700#endif
6701 starttermcap();
6702 scroll_start(); /* scroll screen before redrawing */
6703 redraw_later_clear();
6704 shell_resized(); /* may have resized window */
6705 }
6706}
6707
6708/*
6709 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6710 */
6711 static void
6712ex_exit(eap)
6713 exarg_T *eap;
6714{
6715#ifdef FEAT_CMDWIN
6716 if (cmdwin_type != 0)
6717 {
6718 cmdwin_result = Ctrl_C;
6719 return;
6720 }
6721#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006722 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006723 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006724 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006725 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006726 return;
6727 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006728#ifdef FEAT_AUTOCMD
6729 if (curbuf_locked())
6730 return;
6731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 /*
6734 * if more files or windows we won't exit
6735 */
6736 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6737 exiting = TRUE;
6738 if ( ((eap->cmdidx == CMD_wq
6739 || curbufIsChanged())
6740 && do_write(eap) == FAIL)
6741 || check_more(TRUE, eap->forceit) == FAIL
6742 || (only_one_window() && check_changed_any(eap->forceit)))
6743 {
6744 not_exiting();
6745 }
6746 else
6747 {
6748#ifdef FEAT_WINDOWS
6749 if (only_one_window()) /* quit last window, exit Vim */
6750#endif
6751 getout(0);
6752#ifdef FEAT_WINDOWS
6753# ifdef FEAT_GUI
6754 need_mouse_correct = TRUE;
6755# endif
6756 /* quit current window, may free buffer */
6757 win_close(curwin, !P_HID(curwin->w_buffer));
6758#endif
6759 }
6760}
6761
6762/*
6763 * ":print", ":list", ":number".
6764 */
6765 static void
6766ex_print(eap)
6767 exarg_T *eap;
6768{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006769 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6770 EMSG(_(e_emptybuf));
6771 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006773 for ( ;!got_int; ui_breakcheck())
6774 {
6775 print_line(eap->line1,
6776 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6777 || (eap->flags & EXFLAG_NR)),
6778 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6779 if (++eap->line1 > eap->line2)
6780 break;
6781 out_flush(); /* show one line at a time */
6782 }
6783 setpcmark();
6784 /* put cursor at last line */
6785 curwin->w_cursor.lnum = eap->line2;
6786 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
6788
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790}
6791
6792#ifdef FEAT_BYTEOFF
6793 static void
6794ex_goto(eap)
6795 exarg_T *eap;
6796{
6797 goto_byte(eap->line2);
6798}
6799#endif
6800
6801/*
6802 * ":shell".
6803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 static void
6805ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006806 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 do_shell(NULL, 0);
6809}
6810
6811#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6812 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006813 || defined(FEAT_GUI_MSWIN) \
6814 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 || defined(PROTO)
6816
6817/*
6818 * Handle a file drop. The code is here because a drop is *nearly* like an
6819 * :args command, but not quite (we have a list of exact filenames, so we
6820 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6821 * code is very similar to :args and hence needs access to a lot of the static
6822 * functions in this file.
6823 *
6824 * The list should be allocated using alloc(), as should each item in the
6825 * list. This function takes over responsibility for freeing the list.
6826 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006827 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6829 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6830 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6831 * file functionality is (currently) not in EMX this is not presently a
6832 * problem.
6833 */
6834 void
6835handle_drop(filec, filev, split)
6836 int filec; /* the number of files dropped */
6837 char_u **filev; /* the list of files dropped */
6838 int split; /* force splitting the window */
6839{
6840 exarg_T ea;
6841 int save_msg_scroll = msg_scroll;
6842
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006843 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006844 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006846#ifdef FEAT_AUTOCMD
6847 if (curbuf_locked())
6848 return;
6849#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006850 /* When the screen is being updated we should not change buffers and
6851 * windows structures, it may cause freed memory to be used. */
6852 if (updating_screen)
6853 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 /* Check whether the current buffer is changed. If so, we will need
6856 * to split the current window or data could be lost.
6857 * We don't need to check if the 'hidden' option is set, as in this
6858 * case the buffer won't be lost.
6859 */
6860 if (!P_HID(curbuf) && !split)
6861 {
6862 ++emsg_off;
6863 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6864 --emsg_off;
6865 }
6866 if (split)
6867 {
6868# ifdef FEAT_WINDOWS
6869 if (win_split(0, 0) == FAIL)
6870 return;
6871# ifdef FEAT_SCROLLBIND
6872 curwin->w_p_scb = FALSE;
6873# endif
6874
6875 /* When splitting the window, create a new alist. Otherwise the
6876 * existing one is overwritten. */
6877 alist_unlink(curwin->w_alist);
6878 alist_new();
6879# else
6880 return; /* can't split, always fail */
6881# endif
6882 }
6883
6884 /*
6885 * Set up the new argument list.
6886 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006887 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
6889 /*
6890 * Move to the first file.
6891 */
6892 /* Fake up a minimal "next" command for do_argfile() */
6893 vim_memset(&ea, 0, sizeof(ea));
6894 ea.cmd = (char_u *)"next";
6895 do_argfile(&ea, 0);
6896
6897 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6898 * mode that is not needed here. */
6899 need_start_insertmode = FALSE;
6900
6901 /* Restore msg_scroll, otherwise a following command may cause scrolling
6902 * unexpectedly. The screen will be redrawn by the caller, thus
6903 * msg_scroll being set by displaying a message is irrelevant. */
6904 msg_scroll = save_msg_scroll;
6905}
6906#endif
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908/*
6909 * Clear an argument list: free all file names and reset it to zero entries.
6910 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006911 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912alist_clear(al)
6913 alist_T *al;
6914{
6915 while (--al->al_ga.ga_len >= 0)
6916 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6917 ga_clear(&al->al_ga);
6918}
6919
6920/*
6921 * Init an argument list.
6922 */
6923 void
6924alist_init(al)
6925 alist_T *al;
6926{
6927 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6928}
6929
6930#if defined(FEAT_WINDOWS) || defined(PROTO)
6931
6932/*
6933 * Remove a reference from an argument list.
6934 * Ignored when the argument list is the global one.
6935 * If the argument list is no longer used by any window, free it.
6936 */
6937 void
6938alist_unlink(al)
6939 alist_T *al;
6940{
6941 if (al != &global_alist && --al->al_refcount <= 0)
6942 {
6943 alist_clear(al);
6944 vim_free(al);
6945 }
6946}
6947
6948# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6949/*
6950 * Create a new argument list and use it for the current window.
6951 */
6952 void
6953alist_new()
6954{
6955 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6956 if (curwin->w_alist == NULL)
6957 {
6958 curwin->w_alist = &global_alist;
6959 ++global_alist.al_refcount;
6960 }
6961 else
6962 {
6963 curwin->w_alist->al_refcount = 1;
6964 alist_init(curwin->w_alist);
6965 }
6966}
6967# endif
6968#endif
6969
6970#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6971/*
6972 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006973 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6974 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 */
6976 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006977alist_expand(fnum_list, fnum_len)
6978 int *fnum_list;
6979 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006982 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 char_u **new_arg_files;
6984 int new_arg_file_count;
6985 char_u *save_p_su = p_su;
6986 int i;
6987
6988 /* Don't use 'suffixes' here. This should work like the shell did the
6989 * expansion. Also, the vimrc file isn't read yet, thus the user
6990 * can't set the options. */
6991 p_su = empty_option;
6992 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6993 if (old_arg_files != NULL)
6994 {
6995 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006996 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6997 old_arg_count = GARGCOUNT;
6998 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 &new_arg_file_count, &new_arg_files,
7000 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7001 && new_arg_file_count > 0)
7002 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007003 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7004 TRUE, fnum_list, fnum_len);
7005 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
7008 p_su = save_p_su;
7009}
7010#endif
7011
7012/*
7013 * Set the argument list for the current window.
7014 * Takes over the allocated files[] and the allocated fnames in it.
7015 */
7016 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007017alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 alist_T *al;
7019 int count;
7020 char_u **files;
7021 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007022 int *fnum_list;
7023 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 int i;
7026
7027 alist_clear(al);
7028 if (ga_grow(&al->al_ga, count) == OK)
7029 {
7030 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007031 {
7032 if (got_int)
7033 {
7034 /* When adding many buffers this can take a long time. Allow
7035 * interrupting here. */
7036 while (i < count)
7037 vim_free(files[i++]);
7038 break;
7039 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007040
7041 /* May set buffer name of a buffer previously used for the
7042 * argument list, so that it's re-used by alist_add. */
7043 if (fnum_list != NULL && i < fnum_len)
7044 buf_set_name(fnum_list[i], files[i]);
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007047 ui_breakcheck();
7048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 vim_free(files);
7050 }
7051 else
7052 FreeWild(count, files);
7053#ifdef FEAT_WINDOWS
7054 if (al == &global_alist)
7055#endif
7056 arg_had_last = FALSE;
7057}
7058
7059/*
7060 * Add file "fname" to argument list "al".
7061 * "fname" must have been allocated and "al" must have been checked for room.
7062 */
7063 void
7064alist_add(al, fname, set_fnum)
7065 alist_T *al;
7066 char_u *fname;
7067 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7068{
7069 if (fname == NULL) /* don't add NULL file names */
7070 return;
7071#ifdef BACKSLASH_IN_FILENAME
7072 slash_adjust(fname);
7073#endif
7074 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7075 if (set_fnum > 0)
7076 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7077 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7078 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079}
7080
7081#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7082/*
7083 * Adjust slashes in file names. Called after 'shellslash' was set.
7084 */
7085 void
7086alist_slash_adjust()
7087{
7088 int i;
7089# ifdef FEAT_WINDOWS
7090 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007091 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092# endif
7093
7094 for (i = 0; i < GARGCOUNT; ++i)
7095 if (GARGLIST[i].ae_fname != NULL)
7096 slash_adjust(GARGLIST[i].ae_fname);
7097# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007098 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (wp->w_alist != &global_alist)
7100 for (i = 0; i < WARGCOUNT(wp); ++i)
7101 if (WARGLIST(wp)[i].ae_fname != NULL)
7102 slash_adjust(WARGLIST(wp)[i].ae_fname);
7103# endif
7104}
7105#endif
7106
7107/*
7108 * ":preserve".
7109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 static void
7111ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007112 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007114 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 ml_preserve(curbuf, TRUE);
7116}
7117
7118/*
7119 * ":recover".
7120 */
7121 static void
7122ex_recover(eap)
7123 exarg_T *eap;
7124{
7125 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7126 recoverymode = TRUE;
7127 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7128 && (*eap->arg == NUL
7129 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7130 ml_recover();
7131 recoverymode = FALSE;
7132}
7133
7134/*
7135 * Command modifier used in a wrong way.
7136 */
7137 static void
7138ex_wrongmodifier(eap)
7139 exarg_T *eap;
7140{
7141 eap->errmsg = e_invcmd;
7142}
7143
7144#ifdef FEAT_WINDOWS
7145/*
7146 * :sview [+command] file split window with new file, read-only
7147 * :split [[+command] file] split window with current or new file
7148 * :vsplit [[+command] file] split window vertically with current or new file
7149 * :new [[+command] file] split window with no or new file
7150 * :vnew [[+command] file] split vertically window with no or new file
7151 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007152 *
7153 * :tabedit open new Tab page with empty window
7154 * :tabedit [+command] file open new Tab page and edit "file"
7155 * :tabnew [[+command] file] just like :tabedit
7156 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
7158 void
7159ex_splitview(eap)
7160 exarg_T *eap;
7161{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007162 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007163# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165# endif
7166# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007170# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7172 {
7173 ex_ni(eap);
7174 return;
7175 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007178# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007182# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007184 * quickfix windows. But it's OK when doing ":tab split". */
7185 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
7187 if (eap->cmdidx == CMD_split)
7188 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007189# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (eap->cmdidx == CMD_vsplit)
7191 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007196# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007197 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
7199 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7200 FNAME_MESS, TRUE, curbuf->b_ffname);
7201 if (fname == NULL)
7202 goto theend;
7203 eap->arg = fname;
7204 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007205# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007209# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007211# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 && eap->cmdidx != CMD_new)
7215 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007216# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007217 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007218# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007219 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007220# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007221 au_has_group((char_u *)"FileExplorer"))
7222 {
7223 /* No browsing supported but we do have the file explorer:
7224 * Edit the directory. */
7225 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7226 eap->arg = (char_u *)".";
7227 }
7228 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007229# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007230 {
7231 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007233 if (fname == NULL)
7234 goto theend;
7235 eap->arg = fname;
7236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007241 /*
7242 * Either open new tab page or split the window.
7243 */
7244 if (eap->cmdidx == CMD_tabedit
7245 || eap->cmdidx == CMD_tabfind
7246 || eap->cmdidx == CMD_tabnew)
7247 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007248 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7249 : eap->addr_count == 0 ? 0
7250 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007251 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007252 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007253
7254 /* set the alternate buffer for the window we came from */
7255 if (curwin != old_curwin
7256 && win_valid(old_curwin)
7257 && old_curwin->w_buffer != curbuf
7258 && !cmdmod.keepalt)
7259 old_curwin->w_alt_fnum = curbuf->b_fnum;
7260 }
7261 }
7262 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7264 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007265# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 /* Reset 'scrollbind' when editing another file, but keep it when
7267 * doing ":split" without arguments. */
7268 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007269# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 )
7273 curwin->w_p_scb = FALSE;
7274 else
7275 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 do_exedit(eap, old_curwin);
7278 }
7279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007280# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285theend:
7286 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007289
7290/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007291 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007292 */
7293 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007294tabpage_new()
7295{
7296 exarg_T ea;
7297
7298 vim_memset(&ea, 0, sizeof(ea));
7299 ea.cmdidx = CMD_tabnew;
7300 ea.cmd = (char_u *)"tabn";
7301 ea.arg = (char_u *)"";
7302 ex_splitview(&ea);
7303}
7304
7305/*
7306 * :tabnext command
7307 */
7308 static void
7309ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007310 exarg_T *eap;
7311{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007312 switch (eap->cmdidx)
7313 {
7314 case CMD_tabfirst:
7315 case CMD_tabrewind:
7316 goto_tabpage(1);
7317 break;
7318 case CMD_tablast:
7319 goto_tabpage(9999);
7320 break;
7321 case CMD_tabprevious:
7322 case CMD_tabNext:
7323 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7324 break;
7325 default: /* CMD_tabnext */
7326 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7327 break;
7328 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007329}
7330
7331/*
7332 * :tabmove command
7333 */
7334 static void
7335ex_tabmove(eap)
7336 exarg_T *eap;
7337{
7338 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007339}
7340
7341/*
7342 * :tabs command: List tabs and their contents.
7343 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007344 static void
7345ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007346 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007347{
7348 tabpage_T *tp;
7349 win_T *wp;
7350 int tabcount = 1;
7351
7352 msg_start();
7353 msg_scroll = TRUE;
7354 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7355 {
7356 msg_putchar('\n');
7357 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7358 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7359 out_flush(); /* output one line at a time */
7360 ui_breakcheck();
7361
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007362 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363 wp = firstwin;
7364 else
7365 wp = tp->tp_firstwin;
7366 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7367 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007368 msg_putchar('\n');
7369 msg_putchar(wp == curwin ? '>' : ' ');
7370 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007371 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7372 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373 if (buf_spname(wp->w_buffer) != NULL)
7374 STRCPY(IObuff, buf_spname(wp->w_buffer));
7375 else
7376 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7377 IObuff, IOSIZE, TRUE);
7378 msg_outtrans(IObuff);
7379 out_flush(); /* output one line at a time */
7380 ui_breakcheck();
7381 }
7382 }
7383}
7384
7385#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386
7387/*
7388 * ":mode": Set screen mode.
7389 * If no argument given, just get the screen size and redraw.
7390 */
7391 static void
7392ex_mode(eap)
7393 exarg_T *eap;
7394{
7395 if (*eap->arg == NUL)
7396 shell_resized();
7397 else
7398 mch_screenmode(eap->arg);
7399}
7400
7401#ifdef FEAT_WINDOWS
7402/*
7403 * ":resize".
7404 * set, increment or decrement current window height
7405 */
7406 static void
7407ex_resize(eap)
7408 exarg_T *eap;
7409{
7410 int n;
7411 win_T *wp = curwin;
7412
7413 if (eap->addr_count > 0)
7414 {
7415 n = eap->line2;
7416 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7417 ;
7418 }
7419
7420#ifdef FEAT_GUI
7421 need_mouse_correct = TRUE;
7422#endif
7423 n = atol((char *)eap->arg);
7424#ifdef FEAT_VERTSPLIT
7425 if (cmdmod.split & WSP_VERT)
7426 {
7427 if (*eap->arg == '-' || *eap->arg == '+')
7428 n += W_WIDTH(curwin);
7429 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7430 n = 9999;
7431 win_setwidth_win((int)n, wp);
7432 }
7433 else
7434#endif
7435 {
7436 if (*eap->arg == '-' || *eap->arg == '+')
7437 n += curwin->w_height;
7438 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7439 n = 9999;
7440 win_setheight_win((int)n, wp);
7441 }
7442}
7443#endif
7444
7445/*
7446 * ":find [+command] <file>" command.
7447 */
7448 static void
7449ex_find(eap)
7450 exarg_T *eap;
7451{
7452#ifdef FEAT_SEARCHPATH
7453 char_u *fname;
7454 int count;
7455
7456 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7457 TRUE, curbuf->b_ffname);
7458 if (eap->addr_count > 0)
7459 {
7460 /* Repeat finding the file "count" times. This matters when it
7461 * appears several times in the path. */
7462 count = eap->line2;
7463 while (fname != NULL && --count > 0)
7464 {
7465 vim_free(fname);
7466 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7467 FALSE, curbuf->b_ffname);
7468 }
7469 }
7470
7471 if (fname != NULL)
7472 {
7473 eap->arg = fname;
7474#endif
7475 do_exedit(eap, NULL);
7476#ifdef FEAT_SEARCHPATH
7477 vim_free(fname);
7478 }
7479#endif
7480}
7481
7482/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007483 * ":open" simulation: for now just work like ":visual".
7484 */
7485 static void
7486ex_open(eap)
7487 exarg_T *eap;
7488{
7489 regmatch_T regmatch;
7490 char_u *p;
7491
7492 curwin->w_cursor.lnum = eap->line2;
7493 beginline(BL_SOL | BL_FIX);
7494 if (*eap->arg == '/')
7495 {
7496 /* ":open /pattern/": put cursor in column found with pattern */
7497 ++eap->arg;
7498 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7499 *p = NUL;
7500 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7501 if (regmatch.regprog != NULL)
7502 {
7503 regmatch.rm_ic = p_ic;
7504 p = ml_get_curline();
7505 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007506 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007507 else
7508 EMSG(_(e_nomatch));
7509 vim_free(regmatch.regprog);
7510 }
7511 /* Move to the NUL, ignore any other arguments. */
7512 eap->arg += STRLEN(eap->arg);
7513 }
7514 check_cursor();
7515
7516 eap->cmdidx = CMD_visual;
7517 do_exedit(eap, NULL);
7518}
7519
7520/*
7521 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 */
7523 static void
7524ex_edit(eap)
7525 exarg_T *eap;
7526{
7527 do_exedit(eap, NULL);
7528}
7529
7530/*
7531 * ":edit <file>" command and alikes.
7532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 void
7534do_exedit(eap, old_curwin)
7535 exarg_T *eap;
7536 win_T *old_curwin; /* curwin before doing a split or NULL */
7537{
7538 int n;
7539#ifdef FEAT_WINDOWS
7540 int need_hide;
7541#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007542 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
7544 /*
7545 * ":vi" command ends Ex mode.
7546 */
7547 if (exmode_active && (eap->cmdidx == CMD_visual
7548 || eap->cmdidx == CMD_view))
7549 {
7550 exmode_active = FALSE;
7551 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007552 {
7553 /* Special case: ":global/pat/visual\NLvi-commands" */
7554 if (global_busy)
7555 {
7556 int rd = RedrawingDisabled;
7557 int nwr = no_wait_return;
7558 int ms = msg_scroll;
7559#ifdef FEAT_GUI
7560 int he = hold_gui_events;
7561#endif
7562
7563 if (eap->nextcmd != NULL)
7564 {
7565 stuffReadbuff(eap->nextcmd);
7566 eap->nextcmd = NULL;
7567 }
7568
7569 if (exmode_was != EXMODE_VIM)
7570 settmode(TMODE_RAW);
7571 RedrawingDisabled = 0;
7572 no_wait_return = 0;
7573 need_wait_return = FALSE;
7574 msg_scroll = 0;
7575#ifdef FEAT_GUI
7576 hold_gui_events = 0;
7577#endif
7578 must_redraw = CLEAR;
7579
7580 main_loop(FALSE, TRUE);
7581
7582 RedrawingDisabled = rd;
7583 no_wait_return = nwr;
7584 msg_scroll = ms;
7585#ifdef FEAT_GUI
7586 hold_gui_events = he;
7587#endif
7588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592
7593 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007594 || eap->cmdidx == CMD_tabnew
7595 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596#ifdef FEAT_VERTSPLIT
7597 || eap->cmdidx == CMD_vnew
7598#endif
7599 ) && *eap->arg == NUL)
7600 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007601 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 setpcmark();
7603 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007604 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7605 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607 else if ((eap->cmdidx != CMD_split
7608#ifdef FEAT_VERTSPLIT
7609 && eap->cmdidx != CMD_vsplit
7610#endif
7611 )
7612 || *eap->arg != NUL
7613#ifdef FEAT_BROWSE
7614 || cmdmod.browse
7615#endif
7616 )
7617 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007618#ifdef FEAT_AUTOCMD
7619 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7620 * can bring us here, others are stopped earlier. */
7621 if (*eap->arg != NUL && curbuf_locked())
7622 return;
7623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 n = readonlymode;
7625 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7626 readonlymode = TRUE;
7627 else if (eap->cmdidx == CMD_enew)
7628 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7629 empty buffer */
7630 setpcmark();
7631 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7632 NULL, eap,
7633 /* ":edit" goes to first line if Vi compatible */
7634 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7635 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7636 ? ECMD_ONE : eap->do_ecmd_lnum,
7637 (P_HID(curbuf) ? ECMD_HIDE : 0)
7638 + (eap->forceit ? ECMD_FORCEIT : 0)
7639#ifdef FEAT_LISTCMDS
7640 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7641#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007642 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
7644 /* Editing the file failed. If the window was split, close it. */
7645#ifdef FEAT_WINDOWS
7646 if (old_curwin != NULL)
7647 {
7648 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7649 if (!need_hide || P_HID(curbuf))
7650 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007651# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7652 cleanup_T cs;
7653
7654 /* Reset the error/interrupt/exception state here so that
7655 * aborting() returns FALSE when closing a window. */
7656 enter_cleanup(&cs);
7657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658# ifdef FEAT_GUI
7659 need_mouse_correct = TRUE;
7660# endif
7661 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007662
7663# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7664 /* Restore the error/interrupt/exception state if not
7665 * discarded by a new aborting error, interrupt, or
7666 * uncaught exception. */
7667 leave_cleanup(&cs);
7668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670 }
7671#endif
7672 }
7673 else if (readonlymode && curbuf->b_nwindows == 1)
7674 {
7675 /* When editing an already visited buffer, 'readonly' won't be set
7676 * but the previous value is kept. With ":view" and ":sview" we
7677 * want the file to be readonly, except when another window is
7678 * editing the same buffer. */
7679 curbuf->b_p_ro = TRUE;
7680 }
7681 readonlymode = n;
7682 }
7683 else
7684 {
7685 if (eap->do_ecmd_cmd != NULL)
7686 do_cmdline_cmd(eap->do_ecmd_cmd);
7687#ifdef FEAT_TITLE
7688 n = curwin->w_arg_idx_invalid;
7689#endif
7690 check_arg_idx(curwin);
7691#ifdef FEAT_TITLE
7692 if (n != curwin->w_arg_idx_invalid)
7693 maketitle();
7694#endif
7695 }
7696
7697#ifdef FEAT_WINDOWS
7698 /*
7699 * if ":split file" worked, set alternate file name in old window to new
7700 * file
7701 */
7702 if (old_curwin != NULL
7703 && *eap->arg != NUL
7704 && curwin != old_curwin
7705 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007706 && old_curwin->w_buffer != curbuf
7707 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 old_curwin->w_alt_fnum = curbuf->b_fnum;
7709#endif
7710
7711 ex_no_reprint = TRUE;
7712}
7713
7714#ifndef FEAT_GUI
7715/*
7716 * ":gui" and ":gvim" when there is no GUI.
7717 */
7718 static void
7719ex_nogui(eap)
7720 exarg_T *eap;
7721{
7722 eap->errmsg = e_nogvim;
7723}
7724#endif
7725
7726#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7727 static void
7728ex_tearoff(eap)
7729 exarg_T *eap;
7730{
7731 gui_make_tearoff(eap->arg);
7732}
7733#endif
7734
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007735#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 static void
7737ex_popup(eap)
7738 exarg_T *eap;
7739{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007740 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741}
7742#endif
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 static void
7745ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007746 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7749 MSG(_("No swap file"));
7750 else
7751 msg(curbuf->b_ml.ml_mfp->mf_fname);
7752}
7753
7754/*
7755 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7756 * offset.
7757 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 static void
7760ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007761 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763#ifdef FEAT_SCROLLBIND
7764 win_T *wp;
7765 long topline;
7766 long y;
7767 linenr_T old_linenr = curwin->w_cursor.lnum;
7768
7769 setpcmark();
7770
7771 /*
7772 * determine max topline
7773 */
7774 if (curwin->w_p_scb)
7775 {
7776 topline = curwin->w_topline;
7777 for (wp = firstwin; wp; wp = wp->w_next)
7778 {
7779 if (wp->w_p_scb && wp->w_buffer)
7780 {
7781 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7782 if (topline > y)
7783 topline = y;
7784 }
7785 }
7786 if (topline < 1)
7787 topline = 1;
7788 }
7789 else
7790 {
7791 topline = 1;
7792 }
7793
7794
7795 /*
7796 * set all scrollbind windows to the same topline
7797 */
7798 wp = curwin;
7799 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7800 {
7801 if (curwin->w_p_scb)
7802 {
7803 y = topline - curwin->w_topline;
7804 if (y > 0)
7805 scrollup(y, TRUE);
7806 else
7807 scrolldown(-y, TRUE);
7808 curwin->w_scbind_pos = topline;
7809 redraw_later(VALID);
7810 cursor_correct();
7811#ifdef FEAT_WINDOWS
7812 curwin->w_redr_status = TRUE;
7813#endif
7814 }
7815 }
7816 curwin = wp;
7817 if (curwin->w_p_scb)
7818 {
7819 did_syncbind = TRUE;
7820 checkpcmark();
7821 if (old_linenr != curwin->w_cursor.lnum)
7822 {
7823 char_u ctrl_o[2];
7824
7825 ctrl_o[0] = Ctrl_O;
7826 ctrl_o[1] = 0;
7827 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7828 }
7829 }
7830#endif
7831}
7832
7833
7834 static void
7835ex_read(eap)
7836 exarg_T *eap;
7837{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007838 int i;
7839 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7840 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841
7842 if (eap->usefilter) /* :r!cmd */
7843 do_bang(1, eap, FALSE, FALSE, TRUE);
7844 else
7845 {
7846 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7847 return;
7848
7849#ifdef FEAT_BROWSE
7850 if (cmdmod.browse)
7851 {
7852 char_u *browseFile;
7853
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007854 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 NULL, NULL, NULL, curbuf);
7856 if (browseFile != NULL)
7857 {
7858 i = readfile(browseFile, NULL,
7859 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7860 vim_free(browseFile);
7861 }
7862 else
7863 i = OK;
7864 }
7865 else
7866#endif
7867 if (*eap->arg == NUL)
7868 {
7869 if (check_fname() == FAIL) /* check for no file name */
7870 return;
7871 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7872 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7873 }
7874 else
7875 {
7876 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7877 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7878 i = readfile(eap->arg, NULL,
7879 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7880
7881 }
7882 if (i == FAIL)
7883 {
7884#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7885 if (!aborting())
7886#endif
7887 EMSG2(_(e_notopen), eap->arg);
7888 }
7889 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007890 {
7891 if (empty && exmode_active)
7892 {
7893 /* Delete the empty line that remains. Historically ex does
7894 * this but vi doesn't. */
7895 if (eap->line2 == 0)
7896 lnum = curbuf->b_ml.ml_line_count;
7897 else
7898 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007899 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007900 {
7901 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007902 if (curwin->w_cursor.lnum > 1
7903 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007905 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007906 }
7907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 }
7911}
7912
Bram Moolenaarea408852005-06-25 22:49:46 +00007913static char_u *prev_dir = NULL;
7914
7915#if defined(EXITFREE) || defined(PROTO)
7916 void
7917free_cd_dir()
7918{
7919 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007920 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007921
7922 vim_free(globaldir);
7923 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007924}
7925#endif
7926
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928/*
7929 * ":cd", ":lcd", ":chdir" and ":lchdir".
7930 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007931 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932ex_cd(eap)
7933 exarg_T *eap;
7934{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 char_u *new_dir;
7936 char_u *tofree;
7937
7938 new_dir = eap->arg;
7939#if !defined(UNIX) && !defined(VMS)
7940 /* for non-UNIX ":cd" means: print current directory */
7941 if (*new_dir == NUL)
7942 ex_pwd(NULL);
7943 else
7944#endif
7945 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007946#ifdef FEAT_AUTOCMD
7947 if (allbuf_locked())
7948 return;
7949#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7951 && !eap->forceit)
7952 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007953 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007954 return;
7955 }
7956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 /* ":cd -": Change to previous directory */
7958 if (STRCMP(new_dir, "-") == 0)
7959 {
7960 if (prev_dir == NULL)
7961 {
7962 EMSG(_("E186: No previous directory"));
7963 return;
7964 }
7965 new_dir = prev_dir;
7966 }
7967
7968 /* Save current directory for next ":cd -" */
7969 tofree = prev_dir;
7970 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7971 prev_dir = vim_strsave(NameBuff);
7972 else
7973 prev_dir = NULL;
7974
7975#if defined(UNIX) || defined(VMS)
7976 /* for UNIX ":cd" means: go to home directory */
7977 if (*new_dir == NUL)
7978 {
7979 /* use NameBuff for home directory name */
7980# ifdef VMS
7981 char_u *p;
7982
7983 p = mch_getenv((char_u *)"SYS$LOGIN");
7984 if (p == NULL || *p == NUL) /* empty is the same as not set */
7985 NameBuff[0] = NUL;
7986 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007987 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988# else
7989 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7990# endif
7991 new_dir = NameBuff;
7992 }
7993#endif
7994 if (new_dir == NULL || vim_chdir(new_dir))
7995 EMSG(_(e_failed));
7996 else
7997 {
7998 vim_free(curwin->w_localdir);
7999 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8000 {
8001 /* If still in global directory, need to remember current
8002 * directory as global directory. */
8003 if (globaldir == NULL && prev_dir != NULL)
8004 globaldir = vim_strsave(prev_dir);
8005 /* Remember this local directory for the window. */
8006 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8007 curwin->w_localdir = vim_strsave(NameBuff);
8008 }
8009 else
8010 {
8011 /* We are now in the global directory, no need to remember its
8012 * name. */
8013 vim_free(globaldir);
8014 globaldir = NULL;
8015 curwin->w_localdir = NULL;
8016 }
8017
8018 shorten_fnames(TRUE);
8019
8020 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008021 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 ex_pwd(eap);
8023 }
8024 vim_free(tofree);
8025 }
8026}
8027
8028/*
8029 * ":pwd".
8030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 static void
8032ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008033 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
8035 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8036 {
8037#ifdef BACKSLASH_IN_FILENAME
8038 slash_adjust(NameBuff);
8039#endif
8040 msg(NameBuff);
8041 }
8042 else
8043 EMSG(_("E187: Unknown"));
8044}
8045
8046/*
8047 * ":=".
8048 */
8049 static void
8050ex_equal(eap)
8051 exarg_T *eap;
8052{
8053 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008054 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057 static void
8058ex_sleep(eap)
8059 exarg_T *eap;
8060{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008061 int n;
8062 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 if (cursor_valid())
8065 {
8066 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8067 if (n >= 0)
8068 windgoto((int)n, curwin->w_wcol);
8069 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008070
8071 len = eap->line2;
8072 switch (*eap->arg)
8073 {
8074 case 'm': break;
8075 case NUL: len *= 1000L; break;
8076 default: EMSG2(_(e_invarg2), eap->arg); return;
8077 }
8078 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079}
8080
8081/*
8082 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8083 */
8084 void
8085do_sleep(msec)
8086 long msec;
8087{
8088 long done;
8089
8090 cursor_on();
8091 out_flush();
8092 for (done = 0; !got_int && done < msec; done += 1000L)
8093 {
8094 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8095 ui_breakcheck();
8096 }
8097}
8098
8099 static void
8100do_exmap(eap, isabbrev)
8101 exarg_T *eap;
8102 int isabbrev;
8103{
8104 int mode;
8105 char_u *cmdp;
8106
8107 cmdp = eap->cmd;
8108 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8109
8110 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8111 eap->arg, mode, isabbrev))
8112 {
8113 case 1: EMSG(_(e_invarg));
8114 break;
8115 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8116 break;
8117 }
8118}
8119
8120/*
8121 * ":winsize" command (obsolete).
8122 */
8123 static void
8124ex_winsize(eap)
8125 exarg_T *eap;
8126{
8127 int w, h;
8128 char_u *arg = eap->arg;
8129 char_u *p;
8130
8131 w = getdigits(&arg);
8132 arg = skipwhite(arg);
8133 p = arg;
8134 h = getdigits(&arg);
8135 if (*p != NUL && *arg == NUL)
8136 set_shellsize(w, h, TRUE);
8137 else
8138 EMSG(_("E465: :winsize requires two number arguments"));
8139}
8140
8141#ifdef FEAT_WINDOWS
8142 static void
8143ex_wincmd(eap)
8144 exarg_T *eap;
8145{
8146 int xchar = NUL;
8147 char_u *p;
8148
8149 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8150 {
8151 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8152 if (eap->arg[1] == NUL)
8153 {
8154 EMSG(_(e_invarg));
8155 return;
8156 }
8157 xchar = eap->arg[1];
8158 p = eap->arg + 2;
8159 }
8160 else
8161 p = eap->arg + 1;
8162
8163 eap->nextcmd = check_nextcmd(p);
8164 p = skipwhite(p);
8165 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8166 EMSG(_(e_invarg));
8167 else
8168 {
8169 /* Pass flags on for ":vertical wincmd ]". */
8170 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008171 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8173 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008174 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 }
8176}
8177#endif
8178
Bram Moolenaar843ee412004-06-30 16:16:41 +00008179#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180/*
8181 * ":winpos".
8182 */
8183 static void
8184ex_winpos(eap)
8185 exarg_T *eap;
8186{
8187 int x, y;
8188 char_u *arg = eap->arg;
8189 char_u *p;
8190
8191 if (*arg == NUL)
8192 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008193# if defined(FEAT_GUI) || defined(MSWIN)
8194# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008196# else
8197 if (mch_get_winpos(&x, &y) != FAIL)
8198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8201 msg(IObuff);
8202 }
8203 else
8204# endif
8205 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8206 }
8207 else
8208 {
8209 x = getdigits(&arg);
8210 arg = skipwhite(arg);
8211 p = arg;
8212 y = getdigits(&arg);
8213 if (*p == NUL || *arg != NUL)
8214 {
8215 EMSG(_("E466: :winpos requires two number arguments"));
8216 return;
8217 }
8218# ifdef FEAT_GUI
8219 if (gui.in_use)
8220 gui_mch_set_winpos(x, y);
8221 else if (gui.starting)
8222 {
8223 /* Remember the coordinates for when the window is opened. */
8224 gui_win_x = x;
8225 gui_win_y = y;
8226 }
8227# ifdef HAVE_TGETENT
8228 else
8229# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008230# else
8231# ifdef MSWIN
8232 mch_set_winpos(x, y);
8233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234# endif
8235# ifdef HAVE_TGETENT
8236 if (*T_CWP)
8237 term_set_winpos(x, y);
8238# endif
8239 }
8240}
8241#endif
8242
8243/*
8244 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8245 */
8246 static void
8247ex_operators(eap)
8248 exarg_T *eap;
8249{
8250 oparg_T oa;
8251
8252 clear_oparg(&oa);
8253 oa.regname = eap->regname;
8254 oa.start.lnum = eap->line1;
8255 oa.end.lnum = eap->line2;
8256 oa.line_count = eap->line2 - eap->line1 + 1;
8257 oa.motion_type = MLINE;
8258#ifdef FEAT_VIRTUALEDIT
8259 virtual_op = FALSE;
8260#endif
8261 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8262 {
8263 setpcmark();
8264 curwin->w_cursor.lnum = eap->line1;
8265 beginline(BL_SOL | BL_FIX);
8266 }
8267
8268 switch (eap->cmdidx)
8269 {
8270 case CMD_delete:
8271 oa.op_type = OP_DELETE;
8272 op_delete(&oa);
8273 break;
8274
8275 case CMD_yank:
8276 oa.op_type = OP_YANK;
8277 (void)op_yank(&oa, FALSE, TRUE);
8278 break;
8279
8280 default: /* CMD_rshift or CMD_lshift */
8281 if ((eap->cmdidx == CMD_rshift)
8282#ifdef FEAT_RIGHTLEFT
8283 ^ curwin->w_p_rl
8284#endif
8285 )
8286 oa.op_type = OP_RSHIFT;
8287 else
8288 oa.op_type = OP_LSHIFT;
8289 op_shift(&oa, FALSE, eap->amount);
8290 break;
8291 }
8292#ifdef FEAT_VIRTUALEDIT
8293 virtual_op = MAYBE;
8294#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008295 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296}
8297
8298/*
8299 * ":put".
8300 */
8301 static void
8302ex_put(eap)
8303 exarg_T *eap;
8304{
8305 /* ":0put" works like ":1put!". */
8306 if (eap->line2 == 0)
8307 {
8308 eap->line2 = 1;
8309 eap->forceit = TRUE;
8310 }
8311 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008312 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8313 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314}
8315
8316/*
8317 * Handle ":copy" and ":move".
8318 */
8319 static void
8320ex_copymove(eap)
8321 exarg_T *eap;
8322{
8323 long n;
8324
8325 n = get_address(&eap->arg, FALSE, FALSE);
8326 if (eap->arg == NULL) /* error detected */
8327 {
8328 eap->nextcmd = NULL;
8329 return;
8330 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008331 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
8333 /*
8334 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8335 */
8336 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8337 {
8338 EMSG(_(e_invaddr));
8339 return;
8340 }
8341
8342 if (eap->cmdidx == CMD_move)
8343 {
8344 if (do_move(eap->line1, eap->line2, n) == FAIL)
8345 return;
8346 }
8347 else
8348 ex_copy(eap->line1, eap->line2, n);
8349 u_clearline();
8350 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008351 ex_may_print(eap);
8352}
8353
8354/*
8355 * Print the current line if flags were given to the Ex command.
8356 */
8357 static void
8358ex_may_print(eap)
8359 exarg_T *eap;
8360{
8361 if (eap->flags != 0)
8362 {
8363 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8364 (eap->flags & EXFLAG_LIST));
8365 ex_no_reprint = TRUE;
8366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367}
8368
8369/*
8370 * ":smagic" and ":snomagic".
8371 */
8372 static void
8373ex_submagic(eap)
8374 exarg_T *eap;
8375{
8376 int magic_save = p_magic;
8377
8378 p_magic = (eap->cmdidx == CMD_smagic);
8379 do_sub(eap);
8380 p_magic = magic_save;
8381}
8382
8383/*
8384 * ":join".
8385 */
8386 static void
8387ex_join(eap)
8388 exarg_T *eap;
8389{
8390 curwin->w_cursor.lnum = eap->line1;
8391 if (eap->line1 == eap->line2)
8392 {
8393 if (eap->addr_count >= 2) /* :2,2join does nothing */
8394 return;
8395 if (eap->line2 == curbuf->b_ml.ml_line_count)
8396 {
8397 beep_flush();
8398 return;
8399 }
8400 ++eap->line2;
8401 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008402 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008404 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405}
8406
8407/*
8408 * ":[addr]@r" or ":[addr]*r": execute register
8409 */
8410 static void
8411ex_at(eap)
8412 exarg_T *eap;
8413{
8414 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008415 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
8417 curwin->w_cursor.lnum = eap->line2;
8418
8419#ifdef USE_ON_FLY_SCROLL
8420 dont_scroll = TRUE; /* disallow scrolling here */
8421#endif
8422
8423 /* get the register name. No name means to use the previous one */
8424 c = *eap->arg;
8425 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8426 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008427 /* Put the register in the typeahead buffer with the "silent" flag. */
8428 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8429 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 else
8434 {
8435 int save_efr = exec_from_reg;
8436
8437 exec_from_reg = TRUE;
8438
8439 /*
8440 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008441 * Continue until the stuff buffer is empty and all added characters
8442 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008444 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8446
8447 exec_from_reg = save_efr;
8448 }
8449}
8450
8451/*
8452 * ":!".
8453 */
8454 static void
8455ex_bang(eap)
8456 exarg_T *eap;
8457{
8458 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8459}
8460
8461/*
8462 * ":undo".
8463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 static void
8465ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008466 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008468 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008469 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008470 else
8471 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472}
8473
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008474#ifdef FEAT_PERSISTENT_UNDO
8475 void
8476ex_wundo(eap)
8477 exarg_T *eap;
8478{
8479 char_u hash[UNDO_HASH_SIZE];
8480
8481 u_compute_hash(hash);
8482 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8483}
8484
8485 void
8486ex_rundo(eap)
8487 exarg_T *eap;
8488{
8489 char_u hash[UNDO_HASH_SIZE];
8490
8491 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008492 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008493}
8494#endif
8495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496/*
8497 * ":redo".
8498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 static void
8500ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008501 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 u_redo(1);
8504}
8505
8506/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008507 * ":earlier" and ":later".
8508 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008509 static void
8510ex_later(eap)
8511 exarg_T *eap;
8512{
8513 long count = 0;
8514 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008515 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008516 char_u *p = eap->arg;
8517
8518 if (*p == NUL)
8519 count = 1;
8520 else if (isdigit(*p))
8521 {
8522 count = getdigits(&p);
8523 switch (*p)
8524 {
8525 case 's': ++p; sec = TRUE; break;
8526 case 'm': ++p; sec = TRUE; count *= 60; break;
8527 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008528 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8529 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008530 }
8531 }
8532
8533 if (*p != NUL)
8534 EMSG2(_(e_invarg2), eap->arg);
8535 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008536 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8537 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008538}
8539
8540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 * ":redir": start/stop redirection.
8542 */
8543 static void
8544ex_redir(eap)
8545 exarg_T *eap;
8546{
8547 char *mode;
8548 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008549 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550
8551 if (STRICMP(eap->arg, "END") == 0)
8552 close_redir();
8553 else
8554 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008555 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008557 ++arg;
8558 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008560 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 mode = "a";
8562 }
8563 else
8564 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008565 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 close_redir();
8568
8569 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008570 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 if (fname == NULL)
8572 return;
8573#ifdef FEAT_BROWSE
8574 if (cmdmod.browse)
8575 {
8576 char_u *browseFile;
8577
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008578 browseFile = do_browse(BROWSE_SAVE,
8579 (char_u *)_("Save Redirection"),
8580 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if (browseFile == NULL)
8582 return; /* operation cancelled */
8583 vim_free(fname);
8584 fname = browseFile;
8585 eap->forceit = TRUE; /* since dialog already asked */
8586 }
8587#endif
8588
8589 redir_fd = open_exfile(fname, eap->forceit, mode);
8590 vim_free(fname);
8591 }
8592#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008593 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {
8595 /* redirect to a register a-z (resp. A-Z for appending) */
8596 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008597 ++arg;
8598 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008600 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008601 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008603 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008605 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008606 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008607 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008608 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008610 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008611 if (*arg == '>')
8612 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008613 /* Make register empty when not using @A-@Z and the
8614 * command is valid. */
8615 if (*arg == NUL && !isupper(redir_reg))
8616 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008618 }
8619 if (*arg != NUL)
8620 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008621 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008622 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008623 }
8624 }
8625 else if (*arg == '=' && arg[1] == '>')
8626 {
8627 int append;
8628
8629 /* redirect to a variable */
8630 close_redir();
8631 arg += 2;
8632
8633 if (*arg == '>')
8634 {
8635 ++arg;
8636 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008639 append = FALSE;
8640
8641 if (var_redir_start(skipwhite(arg), append) == OK)
8642 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 }
8644#endif
8645
8646 /* TODO: redirect to a buffer */
8647
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008649 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008651
8652 /* Make sure redirection is not off. Can happen for cmdline completion
8653 * that indirectly invokes a command to catch its output. */
8654 if (redir_fd != NULL
8655#ifdef FEAT_EVAL
8656 || redir_reg || redir_vname
8657#endif
8658 )
8659 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
8662/*
8663 * ":redraw": force redraw
8664 */
8665 static void
8666ex_redraw(eap)
8667 exarg_T *eap;
8668{
8669 int r = RedrawingDisabled;
8670 int p = p_lz;
8671
8672 RedrawingDisabled = 0;
8673 p_lz = FALSE;
8674 update_topline();
8675 update_screen(eap->forceit ? CLEAR :
8676#ifdef FEAT_VISUAL
8677 VIsual_active ? INVERTED :
8678#endif
8679 0);
8680#ifdef FEAT_TITLE
8681 if (need_maketitle)
8682 maketitle();
8683#endif
8684 RedrawingDisabled = r;
8685 p_lz = p;
8686
8687 /* Reset msg_didout, so that a message that's there is overwritten. */
8688 msg_didout = FALSE;
8689 msg_col = 0;
8690
8691 out_flush();
8692}
8693
8694/*
8695 * ":redrawstatus": force redraw of status line(s)
8696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 static void
8698ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008699 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701#if defined(FEAT_WINDOWS)
8702 int r = RedrawingDisabled;
8703 int p = p_lz;
8704
8705 RedrawingDisabled = 0;
8706 p_lz = FALSE;
8707 if (eap->forceit)
8708 status_redraw_all();
8709 else
8710 status_redraw_curbuf();
8711 update_screen(
8712# ifdef FEAT_VISUAL
8713 VIsual_active ? INVERTED :
8714# endif
8715 0);
8716 RedrawingDisabled = r;
8717 p_lz = p;
8718 out_flush();
8719#endif
8720}
8721
8722 static void
8723close_redir()
8724{
8725 if (redir_fd != NULL)
8726 {
8727 fclose(redir_fd);
8728 redir_fd = NULL;
8729 }
8730#ifdef FEAT_EVAL
8731 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008732 if (redir_vname)
8733 {
8734 var_redir_stop();
8735 redir_vname = 0;
8736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737#endif
8738}
8739
8740#if defined(FEAT_SESSION) && defined(USE_CRNL)
8741# define MKSESSION_NL
8742static int mksession_nl = FALSE; /* use NL only in put_eol() */
8743#endif
8744
8745/*
8746 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8747 */
8748 static void
8749ex_mkrc(eap)
8750 exarg_T *eap;
8751{
8752 FILE *fd;
8753 int failed = FALSE;
8754 char_u *fname;
8755#ifdef FEAT_BROWSE
8756 char_u *browseFile = NULL;
8757#endif
8758#ifdef FEAT_SESSION
8759 int view_session = FALSE;
8760 int using_vdir = FALSE; /* using 'viewdir'? */
8761 char_u *viewFile = NULL;
8762 unsigned *flagp;
8763#endif
8764
8765 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8766 {
8767#ifdef FEAT_SESSION
8768 view_session = TRUE;
8769#else
8770 ex_ni(eap);
8771 return;
8772#endif
8773 }
8774
8775#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008776 /* Use the short file name until ":lcd" is used. We also don't use the
8777 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008778 did_lcd = FALSE;
8779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8781 if (eap->cmdidx == CMD_mkview
8782 && (*eap->arg == NUL
8783 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8784 {
8785 eap->forceit = TRUE;
8786 fname = get_view_file(*eap->arg);
8787 if (fname == NULL)
8788 return;
8789 viewFile = fname;
8790 using_vdir = TRUE;
8791 }
8792 else
8793#endif
8794 if (*eap->arg != NUL)
8795 fname = eap->arg;
8796 else if (eap->cmdidx == CMD_mkvimrc)
8797 fname = (char_u *)VIMRC_FILE;
8798#ifdef FEAT_SESSION
8799 else if (eap->cmdidx == CMD_mksession)
8800 fname = (char_u *)SESSION_FILE;
8801#endif
8802 else
8803 fname = (char_u *)EXRC_FILE;
8804
8805#ifdef FEAT_BROWSE
8806 if (cmdmod.browse)
8807 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008808 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809# ifdef FEAT_SESSION
8810 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8811 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8812# endif
8813 (char_u *)_("Save Setup"),
8814 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8815 if (browseFile == NULL)
8816 goto theend;
8817 fname = browseFile;
8818 eap->forceit = TRUE; /* since dialog already asked */
8819 }
8820#endif
8821
8822#if defined(FEAT_SESSION) && defined(vim_mkdir)
8823 /* When using 'viewdir' may have to create the directory. */
8824 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008825 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826#endif
8827
8828 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8829 if (fd != NULL)
8830 {
8831#ifdef FEAT_SESSION
8832 if (eap->cmdidx == CMD_mkview)
8833 flagp = &vop_flags;
8834 else
8835 flagp = &ssop_flags;
8836#endif
8837
8838#ifdef MKSESSION_NL
8839 /* "unix" in 'sessionoptions': use NL line separator */
8840 if (view_session && (*flagp & SSOP_UNIX))
8841 mksession_nl = TRUE;
8842#endif
8843
8844 /* Write the version command for :mkvimrc */
8845 if (eap->cmdidx == CMD_mkvimrc)
8846 (void)put_line(fd, "version 6.0");
8847
8848#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008849 if (eap->cmdidx == CMD_mksession)
8850 {
8851 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8852 failed = TRUE;
8853 }
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (eap->cmdidx != CMD_mkview)
8856#endif
8857 {
8858 /* Write setting 'compatible' first, because it has side effects.
8859 * For that same reason only do it when needed. */
8860 if (p_cp)
8861 (void)put_line(fd, "if !&cp | set cp | endif");
8862 else
8863 (void)put_line(fd, "if &cp | set nocp | endif");
8864 }
8865
8866#ifdef FEAT_SESSION
8867 if (!view_session
8868 || (eap->cmdidx == CMD_mksession
8869 && (*flagp & SSOP_OPTIONS)))
8870#endif
8871 failed |= (makemap(fd, NULL) == FAIL
8872 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8873
8874#ifdef FEAT_SESSION
8875 if (!failed && view_session)
8876 {
8877 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8878 failed = TRUE;
8879 if (eap->cmdidx == CMD_mksession)
8880 {
8881 char_u dirnow[MAXPATHL]; /* current directory */
8882
8883 /*
8884 * Change to session file's dir.
8885 */
8886 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8887 || mch_chdir((char *)dirnow) != 0)
8888 *dirnow = NUL;
8889 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8890 {
8891 if (vim_chdirfile(fname) == OK)
8892 shorten_fnames(TRUE);
8893 }
8894 else if (*dirnow != NUL
8895 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8896 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008897 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008898 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900
8901 failed |= (makeopens(fd, dirnow) == FAIL);
8902
8903 /* restore original dir */
8904 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8905 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8906 {
8907 if (mch_chdir((char *)dirnow) != 0)
8908 EMSG(_(e_prev_dir));
8909 shorten_fnames(TRUE);
8910 }
8911 }
8912 else
8913 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008914 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8915 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 }
8917 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8918 == FAIL)
8919 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008920 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8921 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008922 if (eap->cmdidx == CMD_mksession)
8923 {
8924 if (put_line(fd, "unlet SessionLoad") == FAIL)
8925 failed = TRUE;
8926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008929 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8930 failed = TRUE;
8931
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 failed |= fclose(fd);
8933
8934 if (failed)
8935 EMSG(_(e_write));
8936#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8937 else if (eap->cmdidx == CMD_mksession)
8938 {
8939 /* successful session write - set this_session var */
8940 char_u tbuf[MAXPATHL];
8941
8942 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8943 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8944 }
8945#endif
8946#ifdef MKSESSION_NL
8947 mksession_nl = FALSE;
8948#endif
8949 }
8950
8951#ifdef FEAT_BROWSE
8952theend:
8953 vim_free(browseFile);
8954#endif
8955#ifdef FEAT_SESSION
8956 vim_free(viewFile);
8957#endif
8958}
8959
Bram Moolenaardf177f62005-02-22 08:39:57 +00008960#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8961 || defined(PROTO)
8962 int
8963vim_mkdir_emsg(name, prot)
8964 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008965 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008966{
8967 if (vim_mkdir(name, prot) != 0)
8968 {
8969 EMSG2(_("E739: Cannot create directory: %s"), name);
8970 return FAIL;
8971 }
8972 return OK;
8973}
8974#endif
8975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976/*
8977 * Open a file for writing for an Ex command, with some checks.
8978 * Return file descriptor, or NULL on failure.
8979 */
8980 FILE *
8981open_exfile(fname, forceit, mode)
8982 char_u *fname;
8983 int forceit;
8984 char *mode; /* "w" for create new file or "a" for append */
8985{
8986 FILE *fd;
8987
8988#ifdef UNIX
8989 /* with Unix it is possible to open a directory */
8990 if (mch_isdir(fname))
8991 {
8992 EMSG2(_(e_isadir2), fname);
8993 return NULL;
8994 }
8995#endif
8996 if (!forceit && *mode != 'a' && vim_fexists(fname))
8997 {
8998 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8999 return NULL;
9000 }
9001
9002 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9003 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9004
9005 return fd;
9006}
9007
9008/*
9009 * ":mark" and ":k".
9010 */
9011 static void
9012ex_mark(eap)
9013 exarg_T *eap;
9014{
9015 pos_T pos;
9016
9017 if (*eap->arg == NUL) /* No argument? */
9018 EMSG(_(e_argreq));
9019 else if (eap->arg[1] != NUL) /* more than one character? */
9020 EMSG(_(e_trailing));
9021 else
9022 {
9023 pos = curwin->w_cursor; /* save curwin->w_cursor */
9024 curwin->w_cursor.lnum = eap->line2;
9025 beginline(BL_WHITE | BL_FIX);
9026 if (setmark(*eap->arg) == FAIL) /* set mark */
9027 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9028 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9029 }
9030}
9031
9032/*
9033 * Update w_topline, w_leftcol and the cursor position.
9034 */
9035 void
9036update_topline_cursor()
9037{
9038 check_cursor(); /* put cursor on valid line */
9039 update_topline();
9040 if (!curwin->w_p_wrap)
9041 validate_cursor();
9042 update_curswant();
9043}
9044
9045#ifdef FEAT_EX_EXTRA
9046/*
9047 * ":normal[!] {commands}": Execute normal mode commands.
9048 */
9049 static void
9050ex_normal(eap)
9051 exarg_T *eap;
9052{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 int save_msg_scroll = msg_scroll;
9054 int save_restart_edit = restart_edit;
9055 int save_msg_didout = msg_didout;
9056 int save_State = State;
9057 tasave_T tabuf;
9058 int save_insertmode = p_im;
9059 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009060 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061#ifdef FEAT_MBYTE
9062 char_u *arg = NULL;
9063 int l;
9064 char_u *p;
9065#endif
9066
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009067 if (ex_normal_lock > 0)
9068 {
9069 EMSG(_(e_secure));
9070 return;
9071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 if (ex_normal_busy >= p_mmd)
9073 {
9074 EMSG(_("E192: Recursive use of :normal too deep"));
9075 return;
9076 }
9077 ++ex_normal_busy;
9078
9079 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9080 restart_edit = 0; /* don't go to Insert mode */
9081 p_im = FALSE; /* don't use 'insertmode' */
9082
9083#ifdef FEAT_MBYTE
9084 /*
9085 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9086 * this for the K_SPECIAL leading byte, otherwise special keys will not
9087 * work.
9088 */
9089 if (has_mbyte)
9090 {
9091 int len = 0;
9092
9093 /* Count the number of characters to be escaped. */
9094 for (p = eap->arg; *p != NUL; ++p)
9095 {
9096# ifdef FEAT_GUI
9097 if (*p == CSI) /* leadbyte CSI */
9098 len += 2;
9099# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009100 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9102# ifdef FEAT_GUI
9103 || *p == CSI
9104# endif
9105 )
9106 len += 2;
9107 }
9108 if (len > 0)
9109 {
9110 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9111 if (arg != NULL)
9112 {
9113 len = 0;
9114 for (p = eap->arg; *p != NUL; ++p)
9115 {
9116 arg[len++] = *p;
9117# ifdef FEAT_GUI
9118 if (*p == CSI)
9119 {
9120 arg[len++] = KS_EXTRA;
9121 arg[len++] = (int)KE_CSI;
9122 }
9123# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009124 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
9126 arg[len++] = *++p;
9127 if (*p == K_SPECIAL)
9128 {
9129 arg[len++] = KS_SPECIAL;
9130 arg[len++] = KE_FILLER;
9131 }
9132# ifdef FEAT_GUI
9133 else if (*p == CSI)
9134 {
9135 arg[len++] = KS_EXTRA;
9136 arg[len++] = (int)KE_CSI;
9137 }
9138# endif
9139 }
9140 arg[len] = NUL;
9141 }
9142 }
9143 }
9144 }
9145#endif
9146
9147 /*
9148 * Save the current typeahead. This is required to allow using ":normal"
9149 * from an event handler and makes sure we don't hang when the argument
9150 * ends with half a command.
9151 */
9152 save_typeahead(&tabuf);
9153 if (tabuf.typebuf_valid)
9154 {
9155 /*
9156 * Repeat the :normal command for each line in the range. When no
9157 * range given, execute it just once, without positioning the cursor
9158 * first.
9159 */
9160 do
9161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 if (eap->addr_count != 0)
9163 {
9164 curwin->w_cursor.lnum = eap->line1++;
9165 curwin->w_cursor.col = 0;
9166 }
9167
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009168 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#ifdef FEAT_MBYTE
9170 arg != NULL ? arg :
9171#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009172 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
9174 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9175 }
9176
9177 /* Might not return to the main loop when in an event handler. */
9178 update_topline_cursor();
9179
9180 /* Restore the previous typeahead. */
9181 restore_typeahead(&tabuf);
9182
9183 --ex_normal_busy;
9184 msg_scroll = save_msg_scroll;
9185 restart_edit = save_restart_edit;
9186 p_im = save_insertmode;
9187 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009188 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9190
9191 /* Restore the state (needed when called from a function executed for
9192 * 'indentexpr'). */
9193 State = save_State;
9194#ifdef FEAT_MBYTE
9195 vim_free(arg);
9196#endif
9197}
9198
9199/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009200 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 */
9202 static void
9203ex_startinsert(eap)
9204 exarg_T *eap;
9205{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009206 if (eap->forceit)
9207 {
9208 coladvance((colnr_T)MAXCOL);
9209 curwin->w_curswant = MAXCOL;
9210 curwin->w_set_curswant = FALSE;
9211 }
9212
Bram Moolenaara40c5002005-01-09 21:16:21 +00009213 /* Ignore the command when already in Insert mode. Inserting an
9214 * expression register that invokes a function can do this. */
9215 if (State & INSERT)
9216 return;
9217
Bram Moolenaar64969662005-12-14 21:59:55 +00009218 if (eap->cmdidx == CMD_startinsert)
9219 restart_edit = 'a';
9220 else if (eap->cmdidx == CMD_startreplace)
9221 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009223 restart_edit = 'V';
9224
9225 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009227 if (eap->cmdidx == CMD_startinsert)
9228 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 curwin->w_curswant = 0; /* avoid MAXCOL */
9230 }
9231}
9232
9233/*
9234 * ":stopinsert"
9235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 static void
9237ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009238 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
9240 restart_edit = 0;
9241 stop_insert_mode = TRUE;
9242}
9243#endif
9244
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009245#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9246/*
9247 * Execute normal mode command "cmd".
9248 * "remap" can be REMAP_NONE or REMAP_YES.
9249 */
9250 void
9251exec_normal_cmd(cmd, remap, silent)
9252 char_u *cmd;
9253 int remap;
9254 int silent;
9255{
9256 oparg_T oa;
9257
9258 /*
9259 * Stuff the argument into the typeahead buffer.
9260 * Execute normal_cmd() until there is no typeahead left.
9261 */
9262 clear_oparg(&oa);
9263 finish_op = FALSE;
9264 ins_typebuf(cmd, remap, 0, TRUE, silent);
9265 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9266 && !got_int)
9267 {
9268 update_topline_cursor();
9269 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9270 }
9271}
9272#endif
9273
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#ifdef FEAT_FIND_ID
9275 static void
9276ex_checkpath(eap)
9277 exarg_T *eap;
9278{
9279 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9280 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9281 (linenr_T)1, (linenr_T)MAXLNUM);
9282}
9283
9284#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9285/*
9286 * ":psearch"
9287 */
9288 static void
9289ex_psearch(eap)
9290 exarg_T *eap;
9291{
9292 g_do_tagpreview = p_pvh;
9293 ex_findpat(eap);
9294 g_do_tagpreview = 0;
9295}
9296#endif
9297
9298 static void
9299ex_findpat(eap)
9300 exarg_T *eap;
9301{
9302 int whole = TRUE;
9303 long n;
9304 char_u *p;
9305 int action;
9306
9307 switch (cmdnames[eap->cmdidx].cmd_name[2])
9308 {
9309 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9310 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9311 action = ACTION_GOTO;
9312 else
9313 action = ACTION_SHOW;
9314 break;
9315 case 'i': /* ":ilist" and ":dlist" */
9316 action = ACTION_SHOW_ALL;
9317 break;
9318 case 'u': /* ":ijump" and ":djump" */
9319 action = ACTION_GOTO;
9320 break;
9321 default: /* ":isplit" and ":dsplit" */
9322 action = ACTION_SPLIT;
9323 break;
9324 }
9325
9326 n = 1;
9327 if (vim_isdigit(*eap->arg)) /* get count */
9328 {
9329 n = getdigits(&eap->arg);
9330 eap->arg = skipwhite(eap->arg);
9331 }
9332 if (*eap->arg == '/') /* Match regexp, not just whole words */
9333 {
9334 whole = FALSE;
9335 ++eap->arg;
9336 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9337 if (*p)
9338 {
9339 *p++ = NUL;
9340 p = skipwhite(p);
9341
9342 /* Check for trailing illegal characters */
9343 if (!ends_excmd(*p))
9344 eap->errmsg = e_trailing;
9345 else
9346 eap->nextcmd = check_nextcmd(p);
9347 }
9348 }
9349 if (!eap->skip)
9350 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9351 whole, !eap->forceit,
9352 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9353 n, action, eap->line1, eap->line2);
9354}
9355#endif
9356
9357#ifdef FEAT_WINDOWS
9358
9359# ifdef FEAT_QUICKFIX
9360/*
9361 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9362 */
9363 static void
9364ex_ptag(eap)
9365 exarg_T *eap;
9366{
9367 g_do_tagpreview = p_pvh;
9368 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9369}
9370
9371/*
9372 * ":pedit"
9373 */
9374 static void
9375ex_pedit(eap)
9376 exarg_T *eap;
9377{
9378 win_T *curwin_save = curwin;
9379
9380 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009381 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 keep_help_flag = curwin_save->w_buffer->b_help;
9383 do_exedit(eap, NULL);
9384 keep_help_flag = FALSE;
9385 if (curwin != curwin_save && win_valid(curwin_save))
9386 {
9387 /* Return cursor to where we were */
9388 validate_cursor();
9389 redraw_later(VALID);
9390 win_enter(curwin_save, TRUE);
9391 }
9392 g_do_tagpreview = 0;
9393}
9394# endif
9395
9396/*
9397 * ":stag", ":stselect" and ":stjump".
9398 */
9399 static void
9400ex_stag(eap)
9401 exarg_T *eap;
9402{
9403 postponed_split = -1;
9404 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009405 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9407 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009408 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409}
9410#endif
9411
9412/*
9413 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9414 */
9415 static void
9416ex_tag(eap)
9417 exarg_T *eap;
9418{
9419 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9420}
9421
9422 static void
9423ex_tag_cmd(eap, name)
9424 exarg_T *eap;
9425 char_u *name;
9426{
9427 int cmd;
9428
9429 switch (name[1])
9430 {
9431 case 'j': cmd = DT_JUMP; /* ":tjump" */
9432 break;
9433 case 's': cmd = DT_SELECT; /* ":tselect" */
9434 break;
9435 case 'p': cmd = DT_PREV; /* ":tprevious" */
9436 break;
9437 case 'N': cmd = DT_PREV; /* ":tNext" */
9438 break;
9439 case 'n': cmd = DT_NEXT; /* ":tnext" */
9440 break;
9441 case 'o': cmd = DT_POP; /* ":pop" */
9442 break;
9443 case 'f': /* ":tfirst" */
9444 case 'r': cmd = DT_FIRST; /* ":trewind" */
9445 break;
9446 case 'l': cmd = DT_LAST; /* ":tlast" */
9447 break;
9448 default: /* ":tag" */
9449#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009450 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 {
9452 do_cstag(eap);
9453 return;
9454 }
9455#endif
9456 cmd = DT_TAG;
9457 break;
9458 }
9459
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009460 if (name[0] == 'l')
9461 {
9462#ifndef FEAT_QUICKFIX
9463 ex_ni(eap);
9464 return;
9465#else
9466 cmd = DT_LTAG;
9467#endif
9468 }
9469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9471 eap->forceit, TRUE);
9472}
9473
9474/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009475 * Check "str" for starting with a special cmdline variable.
9476 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9477 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9478 */
9479 int
9480find_cmdline_var(src, usedlen)
9481 char_u *src;
9482 int *usedlen;
9483{
9484 int len;
9485 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009486 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009487 "%",
9488#define SPEC_PERC 0
9489 "#",
9490#define SPEC_HASH 1
9491 "<cword>", /* cursor word */
9492#define SPEC_CWORD 2
9493 "<cWORD>", /* cursor WORD */
9494#define SPEC_CCWORD 3
9495 "<cfile>", /* cursor path name */
9496#define SPEC_CFILE 4
9497 "<sfile>", /* ":so" file name */
9498#define SPEC_SFILE 5
9499#ifdef FEAT_AUTOCMD
9500 "<afile>", /* autocommand file name */
9501# define SPEC_AFILE 6
9502 "<abuf>", /* autocommand buffer number */
9503# define SPEC_ABUF 7
9504 "<amatch>", /* autocommand match name */
9505# define SPEC_AMATCH 8
9506#endif
9507#ifdef FEAT_CLIENTSERVER
9508 "<client>"
9509# define SPEC_CLIENT 9
9510#endif
9511 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009512
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009513 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009514 {
9515 len = (int)STRLEN(spec_str[i]);
9516 if (STRNCMP(src, spec_str[i], len) == 0)
9517 {
9518 *usedlen = len;
9519 return i;
9520 }
9521 }
9522 return -1;
9523}
9524
9525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 * Evaluate cmdline variables.
9527 *
9528 * change '%' to curbuf->b_ffname
9529 * '#' to curwin->w_altfile
9530 * '<cword>' to word under the cursor
9531 * '<cWORD>' to WORD under the cursor
9532 * '<cfile>' to path name under the cursor
9533 * '<sfile>' to sourced file name
9534 * '<afile>' to file name for autocommand
9535 * '<abuf>' to buffer number for autocommand
9536 * '<amatch>' to matching name for autocommand
9537 *
9538 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9539 * "" for error without a message) and NULL is returned.
9540 * Returns an allocated string if a valid match was found.
9541 * Returns NULL if no match was found. "usedlen" then still contains the
9542 * number of characters to skip.
9543 */
9544 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009545eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009547 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 int *usedlen; /* characters after src that are used */
9549 linenr_T *lnump; /* line number for :e command, or NULL */
9550 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009551 int *escaped; /* return value has escaped white space (can
9552 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 int i;
9555 char_u *s;
9556 char_u *result;
9557 char_u *resultbuf = NULL;
9558 int resultlen;
9559 buf_T *buf;
9560 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9561 int spec_idx;
9562#ifdef FEAT_MODIFY_FNAME
9563 int skip_mod = FALSE;
9564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
9566#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9567 char_u strbuf[30];
9568#endif
9569
9570 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009571 if (escaped != NULL)
9572 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
9574 /*
9575 * Check if there is something to do.
9576 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009577 spec_idx = find_cmdline_var(src, usedlen);
9578 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 {
9580 *usedlen = 1;
9581 return NULL;
9582 }
9583
9584 /*
9585 * Skip when preceded with a backslash "\%" and "\#".
9586 * Note: In "\\%" the % is also not recognized!
9587 */
9588 if (src > srcstart && src[-1] == '\\')
9589 {
9590 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009591 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 return NULL;
9593 }
9594
9595 /*
9596 * word or WORD under cursor
9597 */
9598 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9599 {
9600 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9601 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9602 if (resultlen == 0)
9603 {
9604 *errormsg = (char_u *)"";
9605 return NULL;
9606 }
9607 }
9608
9609 /*
9610 * '#': Alternate file name
9611 * '%': Current file name
9612 * File name under the cursor
9613 * File name for autocommand
9614 * and following modifiers
9615 */
9616 else
9617 {
9618 switch (spec_idx)
9619 {
9620 case SPEC_PERC: /* '%': current file */
9621 if (curbuf->b_fname == NULL)
9622 {
9623 result = (char_u *)"";
9624 valid = 0; /* Must have ":p:h" to be valid */
9625 }
9626 else
9627#ifdef RISCOS
9628 /* Always use the full path for RISC OS if possible. */
9629 result = curbuf->b_ffname;
9630 if (result == NULL)
9631 result = curbuf->b_fname;
9632#else
9633 result = curbuf->b_fname;
9634#endif
9635 break;
9636
9637 case SPEC_HASH: /* '#' or "#99": alternate file */
9638 if (src[1] == '#') /* "##": the argument list */
9639 {
9640 result = arg_all();
9641 resultbuf = result;
9642 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009643 if (escaped != NULL)
9644 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645#ifdef FEAT_MODIFY_FNAME
9646 skip_mod = TRUE;
9647#endif
9648 break;
9649 }
9650 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009651 if (*s == '<') /* "#<99" uses v:oldfiles */
9652 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 i = (int)getdigits(&s);
9654 *usedlen = (int)(s - src); /* length of what we expand */
9655
Bram Moolenaard812df62008-11-09 12:46:09 +00009656 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009658 if (*usedlen < 2)
9659 {
9660 /* Should we give an error message for #<text? */
9661 *usedlen = 1;
9662 return NULL;
9663 }
9664#ifdef FEAT_EVAL
9665 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9666 (long)i);
9667 if (result == NULL)
9668 {
9669 *errormsg = (char_u *)"";
9670 return NULL;
9671 }
9672#else
9673 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 }
9677 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009678 {
9679 buf = buflist_findnr(i);
9680 if (buf == NULL)
9681 {
9682 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9683 return NULL;
9684 }
9685 if (lnump != NULL)
9686 *lnump = ECMD_LAST;
9687 if (buf->b_fname == NULL)
9688 {
9689 result = (char_u *)"";
9690 valid = 0; /* Must have ":p:h" to be valid */
9691 }
9692 else
9693 result = buf->b_fname;
9694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 break;
9696
9697#ifdef FEAT_SEARCHPATH
9698 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009699 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 if (result == NULL)
9701 {
9702 *errormsg = (char_u *)"";
9703 return NULL;
9704 }
9705 resultbuf = result; /* remember allocated string */
9706 break;
9707#endif
9708
9709#ifdef FEAT_AUTOCMD
9710 case SPEC_AFILE: /* file name for autocommand */
9711 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009712 if (result != NULL && !autocmd_fname_full)
9713 {
9714 /* Still need to turn the fname into a full path. It is
9715 * postponed to avoid a delay when <afile> is not used. */
9716 autocmd_fname_full = TRUE;
9717 result = FullName_save(autocmd_fname, FALSE);
9718 vim_free(autocmd_fname);
9719 autocmd_fname = result;
9720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 if (result == NULL)
9722 {
9723 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9724 return NULL;
9725 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009726 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 break;
9728
9729 case SPEC_ABUF: /* buffer number for autocommand */
9730 if (autocmd_bufnr <= 0)
9731 {
9732 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9733 return NULL;
9734 }
9735 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9736 result = strbuf;
9737 break;
9738
9739 case SPEC_AMATCH: /* match name for autocommand */
9740 result = autocmd_match;
9741 if (result == NULL)
9742 {
9743 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9744 return NULL;
9745 }
9746 break;
9747
9748#endif
9749 case SPEC_SFILE: /* file name for ":so" command */
9750 result = sourcing_name;
9751 if (result == NULL)
9752 {
9753 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9754 return NULL;
9755 }
9756 break;
9757#if defined(FEAT_CLIENTSERVER)
9758 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009759 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9760 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 result = strbuf;
9762 break;
9763#endif
9764 }
9765
9766 resultlen = (int)STRLEN(result); /* length of new string */
9767 if (src[*usedlen] == '<') /* remove the file name extension */
9768 {
9769 ++*usedlen;
9770#ifdef RISCOS
9771 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9772#else
9773 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9774#endif
9775 resultlen = (int)(s - result);
9776 }
9777#ifdef FEAT_MODIFY_FNAME
9778 else if (!skip_mod)
9779 {
9780 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9781 &resultlen);
9782 if (result == NULL)
9783 {
9784 *errormsg = (char_u *)"";
9785 return NULL;
9786 }
9787 }
9788#endif
9789 }
9790
9791 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9792 {
9793 if (valid != VALID_HEAD + VALID_PATH)
9794 /* xgettext:no-c-format */
9795 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9796 else
9797 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9798 result = NULL;
9799 }
9800 else
9801 result = vim_strnsave(result, resultlen);
9802 vim_free(resultbuf);
9803 return result;
9804}
9805
9806/*
9807 * Concatenate all files in the argument list, separated by spaces, and return
9808 * it in one allocated string.
9809 * Spaces and backslashes in the file names are escaped with a backslash.
9810 * Returns NULL when out of memory.
9811 */
9812 static char_u *
9813arg_all()
9814{
9815 int len;
9816 int idx;
9817 char_u *retval = NULL;
9818 char_u *p;
9819
9820 /*
9821 * Do this loop two times:
9822 * first time: compute the total length
9823 * second time: concatenate the names
9824 */
9825 for (;;)
9826 {
9827 len = 0;
9828 for (idx = 0; idx < ARGCOUNT; ++idx)
9829 {
9830 p = alist_name(&ARGLIST[idx]);
9831 if (p != NULL)
9832 {
9833 if (len > 0)
9834 {
9835 /* insert a space in between names */
9836 if (retval != NULL)
9837 retval[len] = ' ';
9838 ++len;
9839 }
9840 for ( ; *p != NUL; ++p)
9841 {
9842 if (*p == ' ' || *p == '\\')
9843 {
9844 /* insert a backslash */
9845 if (retval != NULL)
9846 retval[len] = '\\';
9847 ++len;
9848 }
9849 if (retval != NULL)
9850 retval[len] = *p;
9851 ++len;
9852 }
9853 }
9854 }
9855
9856 /* second time: break here */
9857 if (retval != NULL)
9858 {
9859 retval[len] = NUL;
9860 break;
9861 }
9862
9863 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009864 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 if (retval == NULL)
9866 break;
9867 }
9868
9869 return retval;
9870}
9871
9872#if defined(FEAT_AUTOCMD) || defined(PROTO)
9873/*
9874 * Expand the <sfile> string in "arg".
9875 *
9876 * Returns an allocated string, or NULL for any error.
9877 */
9878 char_u *
9879expand_sfile(arg)
9880 char_u *arg;
9881{
9882 char_u *errormsg;
9883 int len;
9884 char_u *result;
9885 char_u *newres;
9886 char_u *repl;
9887 int srclen;
9888 char_u *p;
9889
9890 result = vim_strsave(arg);
9891 if (result == NULL)
9892 return NULL;
9893
9894 for (p = result; *p; )
9895 {
9896 if (STRNCMP(p, "<sfile>", 7) != 0)
9897 ++p;
9898 else
9899 {
9900 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009901 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 if (errormsg != NULL)
9903 {
9904 if (*errormsg)
9905 emsg(errormsg);
9906 vim_free(result);
9907 return NULL;
9908 }
9909 if (repl == NULL) /* no match (cannot happen) */
9910 {
9911 p += srclen;
9912 continue;
9913 }
9914 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9915 newres = alloc(len);
9916 if (newres == NULL)
9917 {
9918 vim_free(repl);
9919 vim_free(result);
9920 return NULL;
9921 }
9922 mch_memmove(newres, result, (size_t)(p - result));
9923 STRCPY(newres + (p - result), repl);
9924 len = (int)STRLEN(newres);
9925 STRCAT(newres, p + srclen);
9926 vim_free(repl);
9927 vim_free(result);
9928 result = newres;
9929 p = newres + len; /* continue after the match */
9930 }
9931 }
9932
9933 return result;
9934}
9935#endif
9936
9937#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009938static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9939 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9941static frame_T *ses_skipframe __ARGS((frame_T *fr));
9942static int ses_do_frame __ARGS((frame_T *fr));
9943static int ses_do_win __ARGS((win_T *wp));
9944static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9945static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9946static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9947
9948/*
9949 * Write openfile commands for the current buffers to an .exrc file.
9950 * Return FAIL on error, OK otherwise.
9951 */
9952 static int
9953makeopens(fd, dirnow)
9954 FILE *fd;
9955 char_u *dirnow; /* Current directory name */
9956{
9957 buf_T *buf;
9958 int only_save_windows = TRUE;
9959 int nr;
9960 int cnr = 1;
9961 int restore_size = TRUE;
9962 win_T *wp;
9963 char_u *sname;
9964 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009965 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009966 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009967 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009968 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009969 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
9971 if (ssop_flags & SSOP_BUFFERS)
9972 only_save_windows = FALSE; /* Save ALL buffers */
9973
9974 /*
9975 * Begin by setting the this_session variable, and then other
9976 * sessionable variables.
9977 */
9978#ifdef FEAT_EVAL
9979 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9980 return FAIL;
9981 if (ssop_flags & SSOP_GLOBALS)
9982 if (store_session_globals(fd) == FAIL)
9983 return FAIL;
9984#endif
9985
9986 /*
9987 * Close all windows but one.
9988 */
9989 if (put_line(fd, "silent only") == FAIL)
9990 return FAIL;
9991
9992 /*
9993 * Now a :cd command to the session directory or the current directory
9994 */
9995 if (ssop_flags & SSOP_SESDIR)
9996 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009997 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9998 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 return FAIL;
10000 }
10001 else if (ssop_flags & SSOP_CURDIR)
10002 {
10003 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10004 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010005 || fputs("cd ", fd) < 0
10006 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10007 || put_eol(fd) == FAIL)
10008 {
10009 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 vim_free(sname);
10013 }
10014
10015 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010016 * If there is an empty, unnamed buffer we will wipe it out later.
10017 * Remember the buffer number.
10018 */
10019 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10020 return FAIL;
10021 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10022 return FAIL;
10023 if (put_line(fd, "endif") == FAIL)
10024 return FAIL;
10025
10026 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 * Now save the current files, current buffer first.
10028 */
10029 if (put_line(fd, "set shortmess=aoO") == FAIL)
10030 return FAIL;
10031
10032 /* Now put the other buffers into the buffer list */
10033 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10034 {
10035 if (!(only_save_windows && buf->b_nwindows == 0)
10036 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10037 && buf->b_fname != NULL
10038 && buf->b_p_bl)
10039 {
10040 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10041 : buf->b_wininfo->wi_fpos.lnum) < 0
10042 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10043 return FAIL;
10044 }
10045 }
10046
10047 /* the global argument list */
10048 if (ses_arglist(fd, "args", &global_alist.al_ga,
10049 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10050 return FAIL;
10051
10052 if (ssop_flags & SSOP_RESIZE)
10053 {
10054 /* Note: after the restore we still check it worked!*/
10055 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10056 || put_eol(fd) == FAIL)
10057 return FAIL;
10058 }
10059
10060#ifdef FEAT_GUI
10061 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10062 {
10063 int x, y;
10064
10065 if (gui_mch_get_winpos(&x, &y) == OK)
10066 {
10067 /* Note: after the restore we still check it worked!*/
10068 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10069 return FAIL;
10070 }
10071 }
10072#endif
10073
10074 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010075 * May repeat putting Windows for each tab, when "tabpages" is in
10076 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010077 * Don't use goto_tabpage(), it may change directory and trigger
10078 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010080 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010081 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010082 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010084 int need_tabnew = FALSE;
10085
Bram Moolenaar18144c82006-04-12 21:52:12 +000010086 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010088 tabpage_T *tp = find_tabpage(tabnr);
10089
10090 if (tp == NULL)
10091 break; /* done all tab pages */
10092 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010093 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010094 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010095 tab_topframe = topframe;
10096 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010097 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010098 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010099 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010100 tab_topframe = tp->tp_topframe;
10101 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010102 if (tabnr > 1)
10103 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
Bram Moolenaar18144c82006-04-12 21:52:12 +000010106 /*
10107 * Before creating the window layout, try loading one file. If this
10108 * is aborted we don't end up with a number of useless windows.
10109 * This may have side effects! (e.g., compressed or network file).
10110 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010111 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010112 {
10113 if (ses_do_win(wp)
10114 && wp->w_buffer->b_ffname != NULL
10115 && !wp->w_buffer->b_help
10116#ifdef FEAT_QUICKFIX
10117 && !bt_nofile(wp->w_buffer)
10118#endif
10119 )
10120 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010121 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010122 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10123 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010124 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010125 if (!wp->w_arg_idx_invalid)
10126 edited_win = wp;
10127 break;
10128 }
10129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010131 /* If no file got edited create an empty tab page. */
10132 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10133 return FAIL;
10134
Bram Moolenaar18144c82006-04-12 21:52:12 +000010135 /*
10136 * Save current window layout.
10137 */
10138 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010140 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010142 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10143 return FAIL;
10144 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaar18144c82006-04-12 21:52:12 +000010147 /*
10148 * Check if window sizes can be restored (no windows omitted).
10149 * Remember the window number of the current window after restoring.
10150 */
10151 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010152 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010153 {
10154 if (ses_do_win(wp))
10155 ++nr;
10156 else
10157 restore_size = FALSE;
10158 if (curwin == wp)
10159 cnr = nr;
10160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaar18144c82006-04-12 21:52:12 +000010162 /* Go to the first window. */
10163 if (put_line(fd, "wincmd t") == FAIL)
10164 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010165
Bram Moolenaar18144c82006-04-12 21:52:12 +000010166 /*
10167 * If more than one window, see if sizes can be restored.
10168 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10169 * resized when moving between windows.
10170 * Do this before restoring the view, so that the topline and the
10171 * cursor can be set. This is done again below.
10172 */
10173 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10174 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010175 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177
Bram Moolenaar18144c82006-04-12 21:52:12 +000010178 /*
10179 * Restore the view of the window (options, file, cursor, etc.).
10180 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010181 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010182 {
10183 if (!ses_do_win(wp))
10184 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010185 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10186 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010187 return FAIL;
10188 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10189 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010190 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010191 }
10192
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010193 /* The argument index in the first tab page is zero, need to set it in
10194 * each window. For further tab pages it's the window where we do
10195 * "tabedit". */
10196 cur_arg_idx = next_arg_idx;
10197
Bram Moolenaar18144c82006-04-12 21:52:12 +000010198 /*
10199 * Restore cursor to the current window if it's not the first one.
10200 */
10201 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10202 || put_eol(fd) == FAIL))
10203 return FAIL;
10204
10205 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010206 * Restore window sizes again after jumping around in windows, because
10207 * the current window has a minimum size while others may not.
10208 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010209 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010210 return FAIL;
10211
Bram Moolenaar18144c82006-04-12 21:52:12 +000010212 /* Don't continue in another tab page when doing only the current one
10213 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010214 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010215 break;
10216 }
10217
10218 if (ssop_flags & SSOP_TABPAGES)
10219 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010220 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10221 || put_eol(fd) == FAIL)
10222 return FAIL;
10223 }
10224
Bram Moolenaar9c102382006-05-03 21:26:49 +000010225 /*
10226 * Wipe out an empty unnamed buffer we started in.
10227 */
10228 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10229 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010230 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010231 return FAIL;
10232 if (put_line(fd, "endif") == FAIL)
10233 return FAIL;
10234 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10235 return FAIL;
10236
10237 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10238 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10239 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241
10242 /*
10243 * Lastly, execute the x.vim file if it exists.
10244 */
10245 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10246 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010247 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 || put_line(fd, "endif") == FAIL)
10249 return FAIL;
10250
10251 return OK;
10252}
10253
10254 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010255ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 FILE *fd;
10257 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010258 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 int n = 0;
10261 win_T *wp;
10262
10263 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10264 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010265 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 {
10267 if (!ses_do_win(wp))
10268 continue;
10269 ++n;
10270
10271 /* restore height when not full height */
10272 if (wp->w_height + wp->w_status_height < topframe->fr_height
10273 && (fprintf(fd,
10274 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10275 n, (long)wp->w_height, Rows / 2, Rows) < 0
10276 || put_eol(fd) == FAIL))
10277 return FAIL;
10278
10279 /* restore width when not full width */
10280 if (wp->w_width < Columns && (fprintf(fd,
10281 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10282 n, (long)wp->w_width, Columns / 2, Columns) < 0
10283 || put_eol(fd) == FAIL))
10284 return FAIL;
10285 }
10286 }
10287 else
10288 {
10289 /* Just equalise window sizes */
10290 if (put_line(fd, "wincmd =") == FAIL)
10291 return FAIL;
10292 }
10293 return OK;
10294}
10295
10296/*
10297 * Write commands to "fd" to recursively create windows for frame "fr",
10298 * horizontally and vertically split.
10299 * After the commands the last window in the frame is the current window.
10300 * Returns FAIL when writing the commands to "fd" fails.
10301 */
10302 static int
10303ses_win_rec(fd, fr)
10304 FILE *fd;
10305 frame_T *fr;
10306{
10307 frame_T *frc;
10308 int count = 0;
10309
10310 if (fr->fr_layout != FR_LEAF)
10311 {
10312 /* Find first frame that's not skipped and then create a window for
10313 * each following one (first frame is already there). */
10314 frc = ses_skipframe(fr->fr_child);
10315 if (frc != NULL)
10316 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10317 {
10318 /* Make window as big as possible so that we have lots of room
10319 * to split. */
10320 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10321 || put_line(fd, fr->fr_layout == FR_COL
10322 ? "split" : "vsplit") == FAIL)
10323 return FAIL;
10324 ++count;
10325 }
10326
10327 /* Go back to the first window. */
10328 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10329 ? "%dwincmd k" : "%dwincmd h", count) < 0
10330 || put_eol(fd) == FAIL))
10331 return FAIL;
10332
10333 /* Recursively create frames/windows in each window of this column or
10334 * row. */
10335 frc = ses_skipframe(fr->fr_child);
10336 while (frc != NULL)
10337 {
10338 ses_win_rec(fd, frc);
10339 frc = ses_skipframe(frc->fr_next);
10340 /* Go to next window. */
10341 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10342 return FAIL;
10343 }
10344 }
10345 return OK;
10346}
10347
10348/*
10349 * Skip frames that don't contain windows we want to save in the Session.
10350 * Returns NULL when there none.
10351 */
10352 static frame_T *
10353ses_skipframe(fr)
10354 frame_T *fr;
10355{
10356 frame_T *frc;
10357
10358 for (frc = fr; frc != NULL; frc = frc->fr_next)
10359 if (ses_do_frame(frc))
10360 break;
10361 return frc;
10362}
10363
10364/*
10365 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10366 * the Session.
10367 */
10368 static int
10369ses_do_frame(fr)
10370 frame_T *fr;
10371{
10372 frame_T *frc;
10373
10374 if (fr->fr_layout == FR_LEAF)
10375 return ses_do_win(fr->fr_win);
10376 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10377 if (ses_do_frame(frc))
10378 return TRUE;
10379 return FALSE;
10380}
10381
10382/*
10383 * Return non-zero if window "wp" is to be stored in the Session.
10384 */
10385 static int
10386ses_do_win(wp)
10387 win_T *wp;
10388{
10389 if (wp->w_buffer->b_fname == NULL
10390#ifdef FEAT_QUICKFIX
10391 /* When 'buftype' is "nofile" can't restore the window contents. */
10392 || bt_nofile(wp->w_buffer)
10393#endif
10394 )
10395 return (ssop_flags & SSOP_BLANK);
10396 if (wp->w_buffer->b_help)
10397 return (ssop_flags & SSOP_HELP);
10398 return TRUE;
10399}
10400
10401/*
10402 * Write commands to "fd" to restore the view of a window.
10403 * Caller must make sure 'scrolloff' is zero.
10404 */
10405 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010406put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 FILE *fd;
10408 win_T *wp;
10409 int add_edit; /* add ":edit" command to view */
10410 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010411 int current_arg_idx; /* current argument index of the window, use
10412 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413{
10414 win_T *save_curwin;
10415 int f;
10416 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010417 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
10419 /* Always restore cursor position for ":mksession". For ":mkview" only
10420 * when 'viewoptions' contains "cursor". */
10421 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10422
10423 /*
10424 * Local argument list.
10425 */
10426 if (wp->w_alist == &global_alist)
10427 {
10428 if (put_line(fd, "argglobal") == FAIL)
10429 return FAIL;
10430 }
10431 else
10432 {
10433 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10434 flagp == &vop_flags
10435 || !(*flagp & SSOP_CURDIR)
10436 || wp->w_localdir != NULL, flagp) == FAIL)
10437 return FAIL;
10438 }
10439
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010440 /* Only when part of a session: restore the argument index. Some
10441 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010442 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010443 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010445 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 || put_eol(fd) == FAIL)
10447 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010448 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 }
10450
10451 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010452 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 {
10454 /*
10455 * Load the file.
10456 */
10457 if (wp->w_buffer->b_ffname != NULL
10458#ifdef FEAT_QUICKFIX
10459 && !bt_nofile(wp->w_buffer)
10460#endif
10461 )
10462 {
10463 /*
10464 * Editing a file in this buffer: use ":edit file".
10465 * This may have side effects! (e.g., compressed or network file).
10466 */
10467 if (fputs("edit ", fd) < 0
10468 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10469 return FAIL;
10470 }
10471 else
10472 {
10473 /* No file in this buffer, just make it empty. */
10474 if (put_line(fd, "enew") == FAIL)
10475 return FAIL;
10476#ifdef FEAT_QUICKFIX
10477 if (wp->w_buffer->b_ffname != NULL)
10478 {
10479 /* The buffer does have a name, but it's not a file name. */
10480 if (fputs("file ", fd) < 0
10481 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10482 return FAIL;
10483 }
10484#endif
10485 do_cursor = FALSE;
10486 }
10487 }
10488
10489 /*
10490 * Local mappings and abbreviations.
10491 */
10492 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10493 && makemap(fd, wp->w_buffer) == FAIL)
10494 return FAIL;
10495
10496 /*
10497 * Local options. Need to go to the window temporarily.
10498 * Store only local values when using ":mkview" and when ":mksession" is
10499 * used and 'sessionoptions' doesn't include "options".
10500 * Some folding options are always stored when "folds" is included,
10501 * otherwise the folds would not be restored correctly.
10502 */
10503 save_curwin = curwin;
10504 curwin = wp;
10505 curbuf = curwin->w_buffer;
10506 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10507 f = makeset(fd, OPT_LOCAL,
10508 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10509#ifdef FEAT_FOLDING
10510 else if (*flagp & SSOP_FOLDS)
10511 f = makefoldset(fd);
10512#endif
10513 else
10514 f = OK;
10515 curwin = save_curwin;
10516 curbuf = curwin->w_buffer;
10517 if (f == FAIL)
10518 return FAIL;
10519
10520#ifdef FEAT_FOLDING
10521 /*
10522 * Save Folds when 'buftype' is empty and for help files.
10523 */
10524 if ((*flagp & SSOP_FOLDS)
10525 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010526# ifdef FEAT_QUICKFIX
10527 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10528# endif
10529 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 {
10531 if (put_folds(fd, wp) == FAIL)
10532 return FAIL;
10533 }
10534#endif
10535
10536 /*
10537 * Set the cursor after creating folds, since that moves the cursor.
10538 */
10539 if (do_cursor)
10540 {
10541
10542 /* Restore the cursor line in the file and relatively in the
10543 * window. Don't use "G", it changes the jumplist. */
10544 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10545 (long)wp->w_cursor.lnum,
10546 (long)(wp->w_cursor.lnum - wp->w_topline),
10547 (long)wp->w_height / 2, (long)wp->w_height) < 0
10548 || put_eol(fd) == FAIL
10549 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10550 || put_line(fd, "exe s:l") == FAIL
10551 || put_line(fd, "normal! zt") == FAIL
10552 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10553 || put_eol(fd) == FAIL)
10554 return FAIL;
10555 /* Restore the cursor column and left offset when not wrapping. */
10556 if (wp->w_cursor.col == 0)
10557 {
10558 if (put_line(fd, "normal! 0") == FAIL)
10559 return FAIL;
10560 }
10561 else
10562 {
10563 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10564 {
10565 if (fprintf(fd,
10566 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10567 (long)wp->w_cursor.col,
10568 (long)(wp->w_cursor.col - wp->w_leftcol),
10569 (long)wp->w_width / 2, (long)wp->w_width) < 0
10570 || put_eol(fd) == FAIL
10571 || put_line(fd, "if s:c > 0") == FAIL
10572 || fprintf(fd,
10573 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10574 (long)wp->w_cursor.col) < 0
10575 || put_eol(fd) == FAIL
10576 || put_line(fd, "else") == FAIL
10577 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10578 || put_eol(fd) == FAIL
10579 || put_line(fd, "endif") == FAIL)
10580 return FAIL;
10581 }
10582 else
10583 {
10584 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10585 || put_eol(fd) == FAIL)
10586 return FAIL;
10587 }
10588 }
10589 }
10590
10591 /*
10592 * Local directory.
10593 */
10594 if (wp->w_localdir != NULL)
10595 {
10596 if (fputs("lcd ", fd) < 0
10597 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10598 || put_eol(fd) == FAIL)
10599 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010600 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 }
10602
10603 return OK;
10604}
10605
10606/*
10607 * Write an argument list to the session file.
10608 * Returns FAIL if writing fails.
10609 */
10610 static int
10611ses_arglist(fd, cmd, gap, fullname, flagp)
10612 FILE *fd;
10613 char *cmd;
10614 garray_T *gap;
10615 int fullname; /* TRUE: use full path name */
10616 unsigned *flagp;
10617{
10618 int i;
10619 char_u buf[MAXPATHL];
10620 char_u *s;
10621
10622 if (gap->ga_len == 0)
10623 return put_line(fd, "silent! argdel *");
10624 if (fputs(cmd, fd) < 0)
10625 return FAIL;
10626 for (i = 0; i < gap->ga_len; ++i)
10627 {
10628 /* NULL file names are skipped (only happens when out of memory). */
10629 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10630 if (s != NULL)
10631 {
10632 if (fullname)
10633 {
10634 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10635 s = buf;
10636 }
10637 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10638 return FAIL;
10639 }
10640 }
10641 return put_eol(fd);
10642}
10643
10644/*
10645 * Write a buffer name to the session file.
10646 * Also ends the line.
10647 * Returns FAIL if writing fails.
10648 */
10649 static int
10650ses_fname(fd, buf, flagp)
10651 FILE *fd;
10652 buf_T *buf;
10653 unsigned *flagp;
10654{
10655 char_u *name;
10656
10657 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010658 * the session file will be sourced.
10659 * Don't do this for ":mkview", we don't know the current directory.
10660 * Don't do this after ":lcd", we don't keep track of what the current
10661 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 if (buf->b_sfname != NULL
10663 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010664 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010665#ifdef FEAT_AUTOCHDIR
10666 && !p_acd
10667#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010668 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 name = buf->b_sfname;
10670 else
10671 name = buf->b_ffname;
10672 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10673 return FAIL;
10674 return OK;
10675}
10676
10677/*
10678 * Write a file name to the session file.
10679 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10680 * characters.
10681 * Returns FAIL if writing fails.
10682 */
10683 static int
10684ses_put_fname(fd, name, flagp)
10685 FILE *fd;
10686 char_u *name;
10687 unsigned *flagp;
10688{
10689 char_u *sname;
10690 int retval = OK;
10691 int c;
10692
10693 sname = home_replace_save(NULL, name);
10694 if (sname != NULL)
10695 name = sname;
10696 while (*name != NUL)
10697 {
10698#ifdef FEAT_MBYTE
10699 {
10700 int l;
10701
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010702 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 {
10704 /* copy a multibyte char */
10705 while (--l >= 0)
10706 {
10707 if (putc(*name, fd) != *name)
10708 retval = FAIL;
10709 ++name;
10710 }
10711 continue;
10712 }
10713 }
10714#endif
10715 c = *name++;
10716 if (c == '\\' && (*flagp & SSOP_SLASH))
10717 /* change a backslash to a forward slash */
10718 c = '/';
10719 else if ((vim_strchr(escape_chars, c) != NULL
10720#ifdef BACKSLASH_IN_FILENAME
10721 && c != '\\'
10722#endif
10723 ) || c == '#' || c == '%')
10724 {
10725 /* escape a special character with a backslash */
10726 if (putc('\\', fd) != '\\')
10727 retval = FAIL;
10728 }
10729 if (putc(c, fd) != c)
10730 retval = FAIL;
10731 }
10732 vim_free(sname);
10733 return retval;
10734}
10735
10736/*
10737 * ":loadview [nr]"
10738 */
10739 static void
10740ex_loadview(eap)
10741 exarg_T *eap;
10742{
10743 char_u *fname;
10744
10745 fname = get_view_file(*eap->arg);
10746 if (fname != NULL)
10747 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010748 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 vim_free(fname);
10750 }
10751}
10752
10753/*
10754 * Get the name of the view file for the current buffer.
10755 */
10756 static char_u *
10757get_view_file(c)
10758 int c;
10759{
10760 int len = 0;
10761 char_u *p, *s;
10762 char_u *retval;
10763 char_u *sname;
10764
10765 if (curbuf->b_ffname == NULL)
10766 {
10767 EMSG(_(e_noname));
10768 return NULL;
10769 }
10770 sname = home_replace_save(NULL, curbuf->b_ffname);
10771 if (sname == NULL)
10772 return NULL;
10773
10774 /*
10775 * We want a file name without separators, because we're not going to make
10776 * a directory.
10777 * "normal" path separator -> "=+"
10778 * "=" -> "=="
10779 * ":" path separator -> "=-"
10780 */
10781 for (p = sname; *p; ++p)
10782 if (*p == '=' || vim_ispathsep(*p))
10783 ++len;
10784 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10785 if (retval != NULL)
10786 {
10787 STRCPY(retval, p_vdir);
10788 add_pathsep(retval);
10789 s = retval + STRLEN(retval);
10790 for (p = sname; *p; ++p)
10791 {
10792 if (*p == '=')
10793 {
10794 *s++ = '=';
10795 *s++ = '=';
10796 }
10797 else if (vim_ispathsep(*p))
10798 {
10799 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010800#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 || defined(VMS)
10802 if (*p == ':')
10803 *s++ = '-';
10804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010806 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 }
10808 else
10809 *s++ = *p;
10810 }
10811 *s++ = '=';
10812 *s++ = c;
10813 STRCPY(s, ".vim");
10814 }
10815
10816 vim_free(sname);
10817 return retval;
10818}
10819
10820#endif /* FEAT_SESSION */
10821
10822/*
10823 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10824 * Return FAIL for a write error.
10825 */
10826 int
10827put_eol(fd)
10828 FILE *fd;
10829{
10830 if (
10831#ifdef USE_CRNL
10832 (
10833# ifdef MKSESSION_NL
10834 !mksession_nl &&
10835# endif
10836 (putc('\r', fd) < 0)) ||
10837#endif
10838 (putc('\n', fd) < 0))
10839 return FAIL;
10840 return OK;
10841}
10842
10843/*
10844 * Write a line to "fd".
10845 * Return FAIL for a write error.
10846 */
10847 int
10848put_line(fd, s)
10849 FILE *fd;
10850 char *s;
10851{
10852 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10853 return FAIL;
10854 return OK;
10855}
10856
10857#ifdef FEAT_VIMINFO
10858/*
10859 * ":rviminfo" and ":wviminfo".
10860 */
10861 static void
10862ex_viminfo(eap)
10863 exarg_T *eap;
10864{
10865 char_u *save_viminfo;
10866
10867 save_viminfo = p_viminfo;
10868 if (*p_viminfo == NUL)
10869 p_viminfo = (char_u *)"'100";
10870 if (eap->cmdidx == CMD_rviminfo)
10871 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010872 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10873 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 EMSG(_("E195: Cannot open viminfo file for reading"));
10875 }
10876 else
10877 write_viminfo(eap->arg, eap->forceit);
10878 p_viminfo = save_viminfo;
10879}
10880#endif
10881
10882#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010883/*
10884 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010885 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 void
10888dialog_msg(buff, format, fname)
10889 char_u *buff;
10890 char *format;
10891 char_u *fname;
10892{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 if (fname == NULL)
10894 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010895 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896}
10897#endif
10898
10899/*
10900 * ":behave {mswin,xterm}"
10901 */
10902 static void
10903ex_behave(eap)
10904 exarg_T *eap;
10905{
10906 if (STRCMP(eap->arg, "mswin") == 0)
10907 {
10908 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10909 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10910 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10911 set_option_value((char_u *)"keymodel", 0L,
10912 (char_u *)"startsel,stopsel", 0);
10913 }
10914 else if (STRCMP(eap->arg, "xterm") == 0)
10915 {
10916 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10917 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10918 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10919 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10920 }
10921 else
10922 EMSG2(_(e_invarg2), eap->arg);
10923}
10924
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010925#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10926/*
10927 * Function given to ExpandGeneric() to obtain the possible arguments of the
10928 * ":behave {mswin,xterm}" command.
10929 */
10930 char_u *
10931get_behave_arg(xp, idx)
10932 expand_T *xp UNUSED;
10933 int idx;
10934{
10935 if (idx == 0)
10936 return (char_u *)"mswin";
10937 if (idx == 1)
10938 return (char_u *)"xterm";
10939 return NULL;
10940}
10941#endif
10942
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943#ifdef FEAT_AUTOCMD
10944static int filetype_detect = FALSE;
10945static int filetype_plugin = FALSE;
10946static int filetype_indent = FALSE;
10947
10948/*
10949 * ":filetype [plugin] [indent] {on,off,detect}"
10950 * on: Load the filetype.vim file to install autocommands for file types.
10951 * off: Load the ftoff.vim file to remove all autocommands for file types.
10952 * plugin on: load filetype.vim and ftplugin.vim
10953 * plugin off: load ftplugof.vim
10954 * indent on: load filetype.vim and indent.vim
10955 * indent off: load indoff.vim
10956 */
10957 static void
10958ex_filetype(eap)
10959 exarg_T *eap;
10960{
10961 char_u *arg = eap->arg;
10962 int plugin = FALSE;
10963 int indent = FALSE;
10964
10965 if (*eap->arg == NUL)
10966 {
10967 /* Print current status. */
10968 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10969 filetype_detect ? "ON" : "OFF",
10970 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10971 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10972 return;
10973 }
10974
10975 /* Accept "plugin" and "indent" in any order. */
10976 for (;;)
10977 {
10978 if (STRNCMP(arg, "plugin", 6) == 0)
10979 {
10980 plugin = TRUE;
10981 arg = skipwhite(arg + 6);
10982 continue;
10983 }
10984 if (STRNCMP(arg, "indent", 6) == 0)
10985 {
10986 indent = TRUE;
10987 arg = skipwhite(arg + 6);
10988 continue;
10989 }
10990 break;
10991 }
10992 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10993 {
10994 if (*arg == 'o' || !filetype_detect)
10995 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010996 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 filetype_detect = TRUE;
10998 if (plugin)
10999 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011000 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 filetype_plugin = TRUE;
11002 }
11003 if (indent)
11004 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011005 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 filetype_indent = TRUE;
11007 }
11008 }
11009 if (*arg == 'd')
11010 {
11011 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011012 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 }
11014 }
11015 else if (STRCMP(arg, "off") == 0)
11016 {
11017 if (plugin || indent)
11018 {
11019 if (plugin)
11020 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011021 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 filetype_plugin = FALSE;
11023 }
11024 if (indent)
11025 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011026 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 filetype_indent = FALSE;
11028 }
11029 }
11030 else
11031 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011032 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 filetype_detect = FALSE;
11034 }
11035 }
11036 else
11037 EMSG2(_(e_invarg2), arg);
11038}
11039
11040/*
11041 * ":setfiletype {name}"
11042 */
11043 static void
11044ex_setfiletype(eap)
11045 exarg_T *eap;
11046{
11047 if (!did_filetype)
11048 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11049}
11050#endif
11051
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 static void
11053ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011054 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055{
11056#ifdef FEAT_DIGRAPHS
11057 if (*eap->arg != NUL)
11058 putdigraph(eap->arg);
11059 else
11060 listdigraphs();
11061#else
11062 EMSG(_("E196: No digraphs in this version"));
11063#endif
11064}
11065
11066 static void
11067ex_set(eap)
11068 exarg_T *eap;
11069{
11070 int flags = 0;
11071
11072 if (eap->cmdidx == CMD_setlocal)
11073 flags = OPT_LOCAL;
11074 else if (eap->cmdidx == CMD_setglobal)
11075 flags = OPT_GLOBAL;
11076#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11077 if (cmdmod.browse && flags == 0)
11078 ex_options(eap);
11079 else
11080#endif
11081 (void)do_set(eap->arg, flags);
11082}
11083
11084#ifdef FEAT_SEARCH_EXTRA
11085/*
11086 * ":nohlsearch"
11087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 static void
11089ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011090 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
11092 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011093 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094}
11095
11096/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011097 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 * Sets nextcmd to the start of the next command, if any. Also called when
11099 * skipping commands to find the next command.
11100 */
11101 static void
11102ex_match(eap)
11103 exarg_T *eap;
11104{
11105 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011106 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 char_u *end;
11108 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011109 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011110
11111 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011112 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011113 else
11114 {
11115 EMSG(e_invcmd);
11116 return;
11117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
11119 /* First clear any old pattern. */
11120 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011121 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122
11123 if (ends_excmd(*eap->arg))
11124 end = eap->arg;
11125 else if ((STRNICMP(eap->arg, "none", 4) == 0
11126 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11127 end = eap->arg + 4;
11128 else
11129 {
11130 p = skiptowhite(eap->arg);
11131 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011132 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 p = skipwhite(p);
11134 if (*p == NUL)
11135 {
11136 /* There must be two arguments. */
11137 EMSG2(_(e_invarg2), eap->arg);
11138 return;
11139 }
11140 end = skip_regexp(p + 1, *p, TRUE, NULL);
11141 if (!eap->skip)
11142 {
11143 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11144 {
11145 eap->errmsg = e_trailing;
11146 return;
11147 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011148 if (*end != *p)
11149 {
11150 EMSG2(_(e_invarg2), p);
11151 return;
11152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
11154 c = *end;
11155 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011156 match_add(curwin, g, p + 1, 10, id);
11157 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011158 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 }
11160 }
11161 eap->nextcmd = find_nextcmd(end);
11162}
11163#endif
11164
11165#ifdef FEAT_CRYPT
11166/*
11167 * ":X": Get crypt key
11168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 static void
11170ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011171 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011173 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11174 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176#endif
11177
11178#ifdef FEAT_FOLDING
11179 static void
11180ex_fold(eap)
11181 exarg_T *eap;
11182{
11183 if (foldManualAllowed(TRUE))
11184 foldCreate(eap->line1, eap->line2);
11185}
11186
11187 static void
11188ex_foldopen(eap)
11189 exarg_T *eap;
11190{
11191 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11192 eap->forceit, FALSE);
11193}
11194
11195 static void
11196ex_folddo(eap)
11197 exarg_T *eap;
11198{
11199 linenr_T lnum;
11200
11201 /* First set the marks for all lines closed/open. */
11202 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11203 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11204 ml_setmarked(lnum);
11205
11206 /* Execute the command on the marked lines. */
11207 global_exe(eap->arg);
11208 ml_clearmarked(); /* clear rest of the marks */
11209}
11210#endif