blob: 6d0ce2413ebec8b8a7a565977d20e5ddd90d3c14 [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 Moolenaarf71a3db2006-03-12 21:50:18 +0000238#endif
239#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000240# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000242# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000243# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000244# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000245#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200246#ifndef FEAT_PERSISTENT_UNDO
247# define ex_rundo ex_ni
248# define ex_wundo ex_ni
249#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000250#ifndef FEAT_MZSCHEME
251# define ex_mzscheme ex_script_ni
252# define ex_mzfile ex_ni
253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#ifndef FEAT_PERL
255# define ex_perl ex_script_ni
256# define ex_perldo ex_ni
257#endif
258#ifndef FEAT_PYTHON
259# define ex_python ex_script_ni
260# define ex_pyfile ex_ni
261#endif
262#ifndef FEAT_TCL
263# define ex_tcl ex_script_ni
264# define ex_tcldo ex_ni
265# define ex_tclfile ex_ni
266#endif
267#ifndef FEAT_RUBY
268# define ex_ruby ex_script_ni
269# define ex_rubydo ex_ni
270# define ex_rubyfile ex_ni
271#endif
272#ifndef FEAT_SNIFF
273# define ex_sniff ex_ni
274#endif
275#ifndef FEAT_KEYMAP
276# define ex_loadkeymap ex_ni
277#endif
278static void ex_swapname __ARGS((exarg_T *eap));
279static void ex_syncbind __ARGS((exarg_T *eap));
280static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281static void ex_pwd __ARGS((exarg_T *eap));
282static void ex_equal __ARGS((exarg_T *eap));
283static void ex_sleep __ARGS((exarg_T *eap));
284static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
285static void ex_winsize __ARGS((exarg_T *eap));
286#ifdef FEAT_WINDOWS
287static void ex_wincmd __ARGS((exarg_T *eap));
288#else
289# define ex_wincmd ex_ni
290#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000291#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292static void ex_winpos __ARGS((exarg_T *eap));
293#else
294# define ex_winpos ex_ni
295#endif
296static void ex_operators __ARGS((exarg_T *eap));
297static void ex_put __ARGS((exarg_T *eap));
298static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000299static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300static void ex_submagic __ARGS((exarg_T *eap));
301static void ex_join __ARGS((exarg_T *eap));
302static void ex_at __ARGS((exarg_T *eap));
303static void ex_bang __ARGS((exarg_T *eap));
304static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200305#ifdef FEAT_PERSISTENT_UNDO
306static void ex_wundo __ARGS((exarg_T *eap));
307static void ex_rundo __ARGS((exarg_T *eap));
308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000310static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311static void ex_redir __ARGS((exarg_T *eap));
312static void ex_redraw __ARGS((exarg_T *eap));
313static void ex_redrawstatus __ARGS((exarg_T *eap));
314static void close_redir __ARGS((void));
315static void ex_mkrc __ARGS((exarg_T *eap));
316static void ex_mark __ARGS((exarg_T *eap));
317#ifdef FEAT_USR_CMDS
318static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000319static 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 +0000320#endif
321#ifdef FEAT_EX_EXTRA
322static void ex_normal __ARGS((exarg_T *eap));
323static void ex_startinsert __ARGS((exarg_T *eap));
324static void ex_stopinsert __ARGS((exarg_T *eap));
325#else
326# define ex_normal ex_ni
327# define ex_align ex_ni
328# define ex_retab ex_ni
329# define ex_startinsert ex_ni
330# define ex_stopinsert ex_ni
331# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000332# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#endif
334#ifdef FEAT_FIND_ID
335static void ex_checkpath __ARGS((exarg_T *eap));
336static void ex_findpat __ARGS((exarg_T *eap));
337#else
338# define ex_findpat ex_ni
339# define ex_checkpath ex_ni
340#endif
341#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
342static void ex_psearch __ARGS((exarg_T *eap));
343#else
344# define ex_psearch ex_ni
345#endif
346static void ex_tag __ARGS((exarg_T *eap));
347static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
348#ifndef FEAT_EVAL
349# define ex_scriptnames ex_ni
350# define ex_finish ex_ni
351# define ex_echo ex_ni
352# define ex_echohl ex_ni
353# define ex_execute ex_ni
354# define ex_call ex_ni
355# define ex_if ex_ni
356# define ex_endif ex_ni
357# define ex_else ex_ni
358# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000359# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360# define ex_continue ex_ni
361# define ex_break ex_ni
362# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000363# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_throw ex_ni
365# define ex_try ex_ni
366# define ex_catch ex_ni
367# define ex_finally ex_ni
368# define ex_endtry ex_ni
369# define ex_endfunction ex_ni
370# define ex_let ex_ni
371# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000372# define ex_lockvar ex_ni
373# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_function ex_ni
375# define ex_delfunction ex_ni
376# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000377# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#endif
379static char_u *arg_all __ARGS((void));
380#ifdef FEAT_SESSION
381static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000382static 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 +0000383static void ex_loadview __ARGS((exarg_T *eap));
384static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000385static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386#else
387# define ex_loadview ex_ni
388#endif
389#ifndef FEAT_EVAL
390# define ex_compiler ex_ni
391#endif
392#ifdef FEAT_VIMINFO
393static void ex_viminfo __ARGS((exarg_T *eap));
394#else
395# define ex_viminfo ex_ni
396#endif
397static void ex_behave __ARGS((exarg_T *eap));
398#ifdef FEAT_AUTOCMD
399static void ex_filetype __ARGS((exarg_T *eap));
400static void ex_setfiletype __ARGS((exarg_T *eap));
401#else
402# define ex_filetype ex_ni
403# define ex_setfiletype ex_ni
404#endif
405#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000406# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# define ex_diffpatch ex_ni
408# define ex_diffgetput ex_ni
409# define ex_diffsplit ex_ni
410# define ex_diffthis ex_ni
411# define ex_diffupdate ex_ni
412#endif
413static void ex_digraphs __ARGS((exarg_T *eap));
414static void ex_set __ARGS((exarg_T *eap));
415#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
416# define ex_options ex_ni
417#endif
418#ifdef FEAT_SEARCH_EXTRA
419static void ex_nohlsearch __ARGS((exarg_T *eap));
420static void ex_match __ARGS((exarg_T *eap));
421#else
422# define ex_nohlsearch ex_ni
423# define ex_match ex_ni
424#endif
425#ifdef FEAT_CRYPT
426static void ex_X __ARGS((exarg_T *eap));
427#else
428# define ex_X ex_ni
429#endif
430#ifdef FEAT_FOLDING
431static void ex_fold __ARGS((exarg_T *eap));
432static void ex_foldopen __ARGS((exarg_T *eap));
433static void ex_folddo __ARGS((exarg_T *eap));
434#else
435# define ex_fold ex_ni
436# define ex_foldopen ex_ni
437# define ex_folddo ex_ni
438#endif
439#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
440 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
441# define ex_language ex_ni
442#endif
443#ifndef FEAT_SIGNS
444# define ex_sign ex_ni
445#endif
446#ifndef FEAT_SUN_WORKSHOP
447# define ex_wsverb ex_ni
448#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000449#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200450# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000451# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200452# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454
455#ifndef FEAT_EVAL
456# define ex_debug ex_ni
457# define ex_breakadd ex_ni
458# define ex_debuggreedy ex_ni
459# define ex_breakdel ex_ni
460# define ex_breaklist ex_ni
461#endif
462
463#ifndef FEAT_CMDHIST
464# define ex_history ex_ni
465#endif
466#ifndef FEAT_JUMPLIST
467# define ex_jumps ex_ni
468# define ex_changes ex_ni
469#endif
470
Bram Moolenaar05159a02005-02-26 23:04:13 +0000471#ifndef FEAT_PROFILE
472# define ex_profile ex_ni
473#endif
474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475/*
476 * Declare cmdnames[].
477 */
478#define DO_DECLARE_EXCMD
479#include "ex_cmds.h"
480
481/*
482 * Table used to quickly search for a command, based on its first character.
483 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000484static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
486 CMD_append,
487 CMD_buffer,
488 CMD_change,
489 CMD_delete,
490 CMD_edit,
491 CMD_file,
492 CMD_global,
493 CMD_help,
494 CMD_insert,
495 CMD_join,
496 CMD_k,
497 CMD_list,
498 CMD_move,
499 CMD_next,
500 CMD_open,
501 CMD_print,
502 CMD_quit,
503 CMD_read,
504 CMD_substitute,
505 CMD_t,
506 CMD_undo,
507 CMD_vglobal,
508 CMD_write,
509 CMD_xit,
510 CMD_yank,
511 CMD_z,
512 CMD_bang
513};
514
515static char_u dollar_command[2] = {'$', 0};
516
517
518#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520typedef struct
521{
522 char_u *line; /* command line */
523 linenr_T lnum; /* sourcing_lnum of the line */
524} wcmd_T;
525
526/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000527 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000529 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533 garray_T *lines_gap; /* growarray with line info */
534 int current_line; /* last read line from growarray */
535 int repeating; /* TRUE when looping a second time */
536 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
537 char_u *(*getline) __ARGS((int, void *, int));
538 void *cookie;
539};
540
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
542static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000544
545/* Struct to save a few things while debugging. Used in do_cmdline() only. */
546struct dbg_stuff
547{
548 int trylevel;
549 int force_abort;
550 except_T *caught_stack;
551 char_u *vv_exception;
552 char_u *vv_throwpoint;
553 int did_emsg;
554 int got_int;
555 int did_throw;
556 int need_rethrow;
557 int check_cstack;
558 except_T *current_exception;
559};
560
561static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
562static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
563
564 static void
565save_dbg_stuff(dsp)
566 struct dbg_stuff *dsp;
567{
568 dsp->trylevel = trylevel; trylevel = 0;
569 dsp->force_abort = force_abort; force_abort = FALSE;
570 dsp->caught_stack = caught_stack; caught_stack = NULL;
571 dsp->vv_exception = v_exception(NULL);
572 dsp->vv_throwpoint = v_throwpoint(NULL);
573
574 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
575 dsp->did_emsg = did_emsg; did_emsg = FALSE;
576 dsp->got_int = got_int; got_int = FALSE;
577 dsp->did_throw = did_throw; did_throw = FALSE;
578 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
579 dsp->check_cstack = check_cstack; check_cstack = FALSE;
580 dsp->current_exception = current_exception; current_exception = NULL;
581}
582
583 static void
584restore_dbg_stuff(dsp)
585 struct dbg_stuff *dsp;
586{
587 suppress_errthrow = FALSE;
588 trylevel = dsp->trylevel;
589 force_abort = dsp->force_abort;
590 caught_stack = dsp->caught_stack;
591 (void)v_exception(dsp->vv_exception);
592 (void)v_throwpoint(dsp->vv_throwpoint);
593 did_emsg = dsp->did_emsg;
594 got_int = dsp->got_int;
595 did_throw = dsp->did_throw;
596 need_rethrow = dsp->need_rethrow;
597 check_cstack = dsp->check_cstack;
598 current_exception = dsp->current_exception;
599}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600#endif
601
602
603/*
604 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
605 * command is given.
606 */
607 void
608do_exmode(improved)
609 int improved; /* TRUE for "improved Ex" mode */
610{
611 int save_msg_scroll;
612 int prev_msg_row;
613 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000614 int changedtick;
615
616 if (improved)
617 exmode_active = EXMODE_VIM;
618 else
619 exmode_active = EXMODE_NORMAL;
620 State = NORMAL;
621
622 /* When using ":global /pat/ visual" and then "Q" we return to continue
623 * the :global command. */
624 if (global_busy)
625 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 save_msg_scroll = msg_scroll;
628 ++RedrawingDisabled; /* don't redisplay the window */
629 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#ifdef FEAT_GUI
631 /* Ignore scrollbar and mouse events in Ex mode */
632 ++hold_gui_events;
633#endif
634#ifdef FEAT_SNIFF
635 want_sniff_request = 0; /* No K_SNIFF wanted */
636#endif
637
638 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
639 while (exmode_active)
640 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000641#ifdef FEAT_EX_EXTRA
642 /* Check for a ":normal" command and no more characters left. */
643 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
644 {
645 exmode_active = FALSE;
646 break;
647 }
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 msg_scroll = TRUE;
650 need_wait_return = FALSE;
651 ex_pressedreturn = FALSE;
652 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000653 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 prev_msg_row = msg_row;
655 prev_line = curwin->w_cursor.lnum;
656#ifdef FEAT_SNIFF
657 ProcessSniffRequests();
658#endif
659 if (improved)
660 {
661 cmdline_row = msg_row;
662 do_cmdline(NULL, getexline, NULL, 0);
663 }
664 else
665 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
666 lines_left = Rows - 1;
667
Bram Moolenaardf177f62005-02-22 08:39:57 +0000668 if ((prev_line != curwin->w_cursor.lnum
669 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 if (curbuf->b_ml.ml_flags & ML_EMPTY)
672 EMSG(_(e_emptybuf));
673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 if (ex_pressedreturn)
676 {
677 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000678 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000679 msg_row = prev_msg_row;
680 if (prev_msg_row == Rows - 1)
681 msg_row--;
682 }
683 msg_col = 0;
684 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
685 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
689 {
690 if (curbuf->b_ml.ml_flags & ML_EMPTY)
691 EMSG(_(e_emptybuf));
692 else
693 EMSG(_("E501: At end-of-file"));
694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
696
697#ifdef FEAT_GUI
698 --hold_gui_events;
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 --RedrawingDisabled;
701 --no_wait_return;
702 update_screen(CLEAR);
703 need_wait_return = FALSE;
704 msg_scroll = save_msg_scroll;
705}
706
707/*
708 * Execute a simple command line. Used for translated commands like "*".
709 */
710 int
711do_cmdline_cmd(cmd)
712 char_u *cmd;
713{
714 return do_cmdline(cmd, NULL, NULL,
715 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
716}
717
718/*
719 * do_cmdline(): execute one Ex command line
720 *
721 * 1. Execute "cmdline" when it is not NULL.
722 * If "cmdline" is NULL, or more lines are needed, getline() is used.
723 * 2. Split up in parts separated with '|'.
724 *
725 * This function can be called recursively!
726 *
727 * flags:
728 * DOCMD_VERBOSE - The command will be included in the error message.
729 * DOCMD_NOWAIT - Don't call wait_return() and friends.
730 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
731 * DOCMD_KEYTYPED - Don't reset KeyTyped.
732 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
733 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
734 *
735 * return FAIL if cmdline could not be executed, OK otherwise
736 */
737 int
738do_cmdline(cmdline, getline, cookie, flags)
739 char_u *cmdline;
740 char_u *(*getline) __ARGS((int, void *, int));
741 void *cookie; /* argument for getline() */
742 int flags;
743{
744 char_u *next_cmdline; /* next cmd to execute */
745 char_u *cmdline_copy = NULL; /* copy of cmd line */
746 int used_getline = FALSE; /* used "getline" to obtain command */
747 static int recursive = 0; /* recursive depth */
748 int msg_didout_before_start = 0;
749 int count = 0; /* line number count */
750 int did_inc = FALSE; /* incremented RedrawingDisabled */
751 int retval = OK;
752#ifdef FEAT_EVAL
753 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int current_line = 0; /* active line in lines_ga */
756 char_u *fname = NULL; /* function or script name */
757 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
758 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000759 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 int initial_trylevel;
761 struct msglist **saved_msg_list = NULL;
762 struct msglist *private_msg_list;
763
764 /* "getline" and "cookie" passed to do_one_cmd() */
765 char_u *(*cmd_getline) __ARGS((int, void *, int));
766 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#else
771# define cmd_getline getline
772# define cmd_cookie cookie
773#endif
774 static int call_depth = 0; /* recursiveness */
775
776#ifdef FEAT_EVAL
777 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
778 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000779 * This ensures that the do_errthrow() call in do_one_cmd() does not
780 * combine the messages stored by an earlier invocation of do_one_cmd()
781 * with the command name of the later one. This would happen when
782 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 saved_msg_list = msg_list;
784 msg_list = &private_msg_list;
785 private_msg_list = NULL;
786#endif
787
788 /* It's possible to create an endless loop with ":execute", catch that
789 * here. The value of 200 allows nested function calls, ":source", etc. */
790 if (call_depth == 200)
791 {
792 EMSG(_("E169: Command too recursive"));
793#ifdef FEAT_EVAL
794 /* When converting to an exception, we do not include the command name
795 * since this is not an error of the specific command. */
796 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
797 msg_list = saved_msg_list;
798#endif
799 return FAIL;
800 }
801 ++call_depth;
802
803#ifdef FEAT_EVAL
804 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 cstack.cs_trylevel = 0;
807 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000808 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
810
811 real_cookie = getline_cookie(getline, cookie);
812
813 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 getline_is_func = getline_equal(getline, cookie, get_func_line);
815 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ++ex_nesting_level;
817
818 /* Get the function or script name and the address where the next breakpoint
819 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
822 fname = func_name(real_cookie);
823 breakpoint = func_breakpoint(real_cookie);
824 dbg_tick = func_dbg_tick(real_cookie);
825 }
826 else if (getline_equal(getline, cookie, getsourceline))
827 {
828 fname = sourcing_name;
829 breakpoint = source_breakpoint(real_cookie);
830 dbg_tick = source_dbg_tick(real_cookie);
831 }
832
833 /*
834 * Initialize "force_abort" and "suppress_errthrow" at the top level.
835 */
836 if (!recursive)
837 {
838 force_abort = FALSE;
839 suppress_errthrow = FALSE;
840 }
841
842 /*
843 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000844 * exception handling (used when debugging). Otherwise clear it to avoid
845 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000847 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000848 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000849 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200850 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852 initial_trylevel = trylevel;
853
854 /*
855 * "did_throw" will be set to TRUE when an exception is being thrown.
856 */
857 did_throw = FALSE;
858#endif
859 /*
860 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * If force_abort is set, we cancel everything.
863 */
864 did_emsg = FALSE;
865
866 /*
867 * KeyTyped is only set when calling vgetc(). Reset it here when not
868 * calling vgetc() (sourced command lines).
869 */
870 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
871 KeyTyped = FALSE;
872
873 /*
874 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * - for multiple commands on one line, separated with '|'
877 * - when repeating until there are no more lines (for ":source")
878 */
879 next_cmdline = cmdline;
880 do
881 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000882#ifdef FEAT_EVAL
883 getline_is_func = getline_equal(getline, cookie, get_func_line);
884#endif
885
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000886 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (next_cmdline == NULL
888#ifdef FEAT_EVAL
889 && !force_abort
890 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#endif
893 )
894 did_emsg = FALSE;
895
896 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000897 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 * 2. If no line given: Get an allocated line with getline().
899 * 3. If a line is given: Make a copy, so we can mess with it.
900 */
901
902#ifdef FEAT_EVAL
903 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906 /* Each '|' separated command is stored separately in lines_ga, to
907 * be able to jump to it. Don't use next_cmdline now. */
908 vim_free(cmdline_copy);
909 cmdline_copy = NULL;
910
911 /* Check if a function has returned or, unless it has an unclosed
912 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000916 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917 func_line_end(real_cookie);
918# endif
919 if (func_has_ended(real_cookie))
920 {
921 retval = FAIL;
922 break;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000926 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 && getline_equal(getline, cookie, getsourceline))
928 script_line_end();
929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 /* Check if a sourced file hit a ":finish" command. */
932 if (source_finished(getline, cookie))
933 {
934 retval = FAIL;
935 break;
936 }
937
938 /* If breakpoints have been added/deleted need to check for it. */
939 if (breakpoint != NULL && dbg_tick != NULL
940 && *dbg_tick != debug_tick)
941 {
942 *breakpoint = dbg_find_breakpoint(
943 getline_equal(getline, cookie, getsourceline),
944 fname, sourcing_lnum);
945 *dbg_tick = debug_tick;
946 }
947
948 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
949 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
950
951 /* Did we encounter a breakpoint? */
952 if (breakpoint != NULL && *breakpoint != 0
953 && *breakpoint <= sourcing_lnum)
954 {
955 dbg_breakpoint(fname, sourcing_lnum);
956 /* Find next breakpoint. */
957 *breakpoint = dbg_find_breakpoint(
958 getline_equal(getline, cookie, getsourceline),
959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000962# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000963 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964 {
965 if (getline_is_func)
966 func_line_start(real_cookie);
967 else if (getline_equal(getline, cookie, getsourceline))
968 script_line_start();
969 }
970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000973 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * again. Pass a different "getline" function to do_one_cmd()
977 * below, so that it stores lines in or reads them from
978 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000979 * while/for loop. */
980 cmd_getline = get_loop_line;
981 cmd_cookie = (void *)&cmd_loop_cookie;
982 cmd_loop_cookie.lines_gap = &lines_ga;
983 cmd_loop_cookie.current_line = current_line;
984 cmd_loop_cookie.getline = getline;
985 cmd_loop_cookie.cookie = cookie;
986 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 else
989 {
990 cmd_getline = getline;
991 cmd_cookie = cookie;
992 }
993#endif
994
995 /* 2. If no line given, get an allocated line with getline(). */
996 if (next_cmdline == NULL)
997 {
998 /*
999 * Need to set msg_didout for the first line after an ":if",
1000 * otherwise the ":if" will be overwritten.
1001 */
1002 if (count == 1 && getline_equal(getline, cookie, getexline))
1003 msg_didout = TRUE;
1004 if (getline == NULL || (next_cmdline = getline(':', cookie,
1005#ifdef FEAT_EVAL
1006 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1007#else
1008 0
1009#endif
1010 )) == NULL)
1011 {
1012 /* Don't call wait_return for aborted command line. The NULL
1013 * returned for the end of a sourced file or executed function
1014 * doesn't do this. */
1015 if (KeyTyped && !(flags & DOCMD_REPEAT))
1016 need_wait_return = FALSE;
1017 retval = FAIL;
1018 break;
1019 }
1020 used_getline = TRUE;
1021
1022 /*
1023 * Keep the first typed line. Clear it when more lines are typed.
1024 */
1025 if (flags & DOCMD_KEEPLINE)
1026 {
1027 vim_free(repeat_cmdline);
1028 if (count == 0)
1029 repeat_cmdline = vim_strsave(next_cmdline);
1030 else
1031 repeat_cmdline = NULL;
1032 }
1033 }
1034
1035 /* 3. Make a copy of the command so we can mess with it. */
1036 else if (cmdline_copy == NULL)
1037 {
1038 next_cmdline = vim_strsave(next_cmdline);
1039 if (next_cmdline == NULL)
1040 {
1041 EMSG(_(e_outofmem));
1042 retval = FAIL;
1043 break;
1044 }
1045 }
1046 cmdline_copy = next_cmdline;
1047
1048#ifdef FEAT_EVAL
1049 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 * Save the current line when inside a ":while" or ":for", and when
1051 * the command looks like a ":while" or ":for", because we may need it
1052 * later. When there is a '|' and another command, it is stored
1053 * separately, because we need to be able to jump back to it from an
1054 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 if (current_line == lines_ga.ga_len
1057 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
1061 retval = FAIL;
1062 break;
1063 }
1064 }
1065 did_endif = FALSE;
1066#endif
1067
1068 if (count++ == 0)
1069 {
1070 /*
1071 * All output from the commands is put below each other, without
1072 * waiting for a return. Don't do this when executing commands
1073 * from a script or when being called recursive (e.g. for ":e
1074 * +command file").
1075 */
1076 if (!(flags & DOCMD_NOWAIT) && !recursive)
1077 {
1078 msg_didout_before_start = msg_didout;
1079 msg_didany = FALSE; /* no output yet */
1080 msg_start();
1081 msg_scroll = TRUE; /* put messages below each other */
1082 ++no_wait_return; /* dont wait for return until finished */
1083 ++RedrawingDisabled;
1084 did_inc = TRUE;
1085 }
1086 }
1087
1088 if (p_verbose >= 15 && sourcing_name != NULL)
1089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001091 verbose_enter_scroll();
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 smsg((char_u *)_("line %ld: %s"),
1094 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001095 if (msg_silent == 0)
1096 msg_puts((char_u *)"\n"); /* don't overwrite this */
1097
1098 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 --no_wait_return;
1100 }
1101
1102 /*
1103 * 2. Execute one '|' separated command.
1104 * do_one_cmd() will return NULL if there is no trailing '|'.
1105 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1106 */
1107 ++recursive;
1108 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1109#ifdef FEAT_EVAL
1110 &cstack,
1111#endif
1112 cmd_getline, cmd_cookie);
1113 --recursive;
1114
1115#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 if (cmd_cookie == (void *)&cmd_loop_cookie)
1117 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
1121
1122 if (next_cmdline == NULL)
1123 {
1124 vim_free(cmdline_copy);
1125 cmdline_copy = NULL;
1126#ifdef FEAT_CMDHIST
1127 /*
1128 * If the command was typed, remember it for the ':' register.
1129 * Do this AFTER executing the command to make :@: work.
1130 */
1131 if (getline_equal(getline, cookie, getexline)
1132 && new_last_cmdline != NULL)
1133 {
1134 vim_free(last_cmdline);
1135 last_cmdline = new_last_cmdline;
1136 new_last_cmdline = NULL;
1137 }
1138#endif
1139 }
1140 else
1141 {
1142 /* need to copy the command after the '|' to cmdline_copy, for the
1143 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001144 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 next_cmdline = cmdline_copy;
1146 }
1147
1148
1149#ifdef FEAT_EVAL
1150 /* reset did_emsg for a function that is not aborted by an error */
1151 if (did_emsg && !force_abort
1152 && getline_equal(getline, cookie, get_func_line)
1153 && !func_has_abort(real_cookie))
1154 did_emsg = FALSE;
1155
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001156 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 ++current_line;
1159
1160 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001161 * An ":endwhile", ":endfor" and ":continue" is handled here.
1162 * If we were executing commands, jump back to the ":while" or
1163 * ":for".
1164 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 /* Jump back to the matching ":while" or ":for". Be careful
1171 * not to use a cs_line[] from an entry that isn't a ":while"
1172 * or ":for": It would make "current_line" invalid and can
1173 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (!did_emsg && !got_int && !did_throw
1175 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 && (cstack.cs_flags[cstack.cs_idx]
1177 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && cstack.cs_line[cstack.cs_idx] >= 0
1179 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1180 {
1181 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 /* remember we jumped there */
1183 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 line_breakcheck(); /* check if CTRL-C typed */
1185
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* Check for the next breakpoint at or after the ":while"
1187 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (breakpoint != NULL)
1189 {
1190 *breakpoint = dbg_find_breakpoint(
1191 getline_equal(getline, cookie, getsourceline),
1192 fname,
1193 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1194 *dbg_tick = debug_tick;
1195 }
1196 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001201 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1202 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 }
1205
1206 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1213 }
1214 }
1215
1216 /*
1217 * When not inside any ":while" loop, clear remembered lines.
1218 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
1221 if (lines_ga.ga_len > 0)
1222 {
1223 sourcing_lnum =
1224 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1225 free_cmdlines(&lines_ga);
1226 }
1227 current_line = 0;
1228 }
1229
1230 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1232 * being restored at the ":endtry". Reset them here and set the
1233 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1234 * This includes the case where a missing ":endif", ":endwhile" or
1235 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 cstack.cs_lflags &= ~CSL_HAD_FINA;
1240 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1241 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 did_throw ? (void *)current_exception : NULL);
1243 did_emsg = got_int = did_throw = FALSE;
1244 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1245 }
1246
1247 /* Update global "trylevel" for recursive calls to do_cmdline() from
1248 * within this loop. */
1249 trylevel = initial_trylevel + cstack.cs_trylevel;
1250
1251 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001252 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * files) is aborted because of an error, an interrupt, or an uncaught
1254 * exception, cancel everything. If it is left normally, reset
1255 * force_abort to get the non-EH compatible abortion behavior for
1256 * the rest of the script.
1257 */
1258 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1259 force_abort = FALSE;
1260
1261 /* Convert an interrupt to an exception if appropriate. */
1262 (void)do_intthrow(&cstack);
1263#endif /* FEAT_EVAL */
1264
1265 }
1266 /*
1267 * Continue executing command lines when:
1268 * - no CTRL-C typed, no aborting error, no exception thrown or try
1269 * conditionals need to be checked for executing finally clauses or
1270 * catching an interrupt exception
1271 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001272 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 * looping for ":source" command or function call.
1274 */
1275 while (!((got_int
1276#ifdef FEAT_EVAL
1277 || (did_emsg && force_abort) || did_throw
1278#endif
1279 )
1280#ifdef FEAT_EVAL
1281 && cstack.cs_trylevel == 0
1282#endif
1283 )
1284 && !(did_emsg && used_getline
1285 && (getline_equal(getline, cookie, getexmodeline)
1286 || getline_equal(getline, cookie, getexline)))
1287 && (next_cmdline != NULL
1288#ifdef FEAT_EVAL
1289 || cstack.cs_idx >= 0
1290#endif
1291 || (flags & DOCMD_REPEAT)));
1292
1293 vim_free(cmdline_copy);
1294#ifdef FEAT_EVAL
1295 free_cmdlines(&lines_ga);
1296 ga_clear(&lines_ga);
1297
1298 if (cstack.cs_idx >= 0)
1299 {
1300 /*
1301 * If a sourced file or executed function ran to its end, report the
1302 * unclosed conditional.
1303 */
1304 if (!got_int && !did_throw
1305 && ((getline_equal(getline, cookie, getsourceline)
1306 && !source_finished(getline, cookie))
1307 || (getline_equal(getline, cookie, get_func_line)
1308 && !func_has_ended(real_cookie))))
1309 {
1310 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1311 EMSG(_(e_endtry));
1312 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1313 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001314 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1315 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 else
1317 EMSG(_(e_endif));
1318 }
1319
1320 /*
1321 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1322 * ":endtry" in a sourced file or executed function. If the try
1323 * conditional is in its finally clause, ignore anything pending.
1324 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001325 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 */
1327 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001328 {
1329 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1330
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001331 if (idx >= 0)
1332 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001333 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1334 &cstack.cs_looplevel);
1335 }
1336 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 trylevel = initial_trylevel;
1338 }
1339
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001340 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1341 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 * exception, do this now after rewinding the cstack. */
1343 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1344 ? (char_u *)"endfunction" : (char_u *)NULL);
1345
1346 if (trylevel == 0)
1347 {
1348 /*
1349 * When an exception is being thrown out of the outermost try
1350 * conditional, discard the uncaught exception, disable the conversion
1351 * of interrupts or errors to exceptions, and ensure that no more
1352 * commands are executed.
1353 */
1354 if (did_throw)
1355 {
1356 void *p = NULL;
1357 char_u *saved_sourcing_name;
1358 int saved_sourcing_lnum;
1359 struct msglist *messages = NULL, *next;
1360
1361 /*
1362 * If the uncaught exception is a user exception, report it as an
1363 * error. If it is an error exception, display the saved error
1364 * message now. For an interrupt exception, do nothing; the
1365 * interrupt message is given elsewhere.
1366 */
1367 switch (current_exception->type)
1368 {
1369 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001370 vim_snprintf((char *)IObuff, IOSIZE,
1371 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 current_exception->value);
1373 p = vim_strsave(IObuff);
1374 break;
1375 case ET_ERROR:
1376 messages = current_exception->messages;
1377 current_exception->messages = NULL;
1378 break;
1379 case ET_INTERRUPT:
1380 break;
1381 default:
1382 p = vim_strsave((char_u *)_(e_internal));
1383 }
1384
1385 saved_sourcing_name = sourcing_name;
1386 saved_sourcing_lnum = sourcing_lnum;
1387 sourcing_name = current_exception->throw_name;
1388 sourcing_lnum = current_exception->throw_lnum;
1389 current_exception->throw_name = NULL;
1390
1391 discard_current_exception(); /* uses IObuff if 'verbose' */
1392 suppress_errthrow = TRUE;
1393 force_abort = TRUE;
1394
1395 if (messages != NULL)
1396 {
1397 do
1398 {
1399 next = messages->next;
1400 emsg(messages->msg);
1401 vim_free(messages->msg);
1402 vim_free(messages);
1403 messages = next;
1404 }
1405 while (messages != NULL);
1406 }
1407 else if (p != NULL)
1408 {
1409 emsg(p);
1410 vim_free(p);
1411 }
1412 vim_free(sourcing_name);
1413 sourcing_name = saved_sourcing_name;
1414 sourcing_lnum = saved_sourcing_lnum;
1415 }
1416
1417 /*
1418 * On an interrupt or an aborting error not converted to an exception,
1419 * disable the conversion of errors to exceptions. (Interrupts are not
1420 * converted any more, here.) This enables also the interrupt message
1421 * when force_abort is set and did_emsg unset in case of an interrupt
1422 * from a finally clause after an error.
1423 */
1424 else if (got_int || (did_emsg && force_abort))
1425 suppress_errthrow = TRUE;
1426 }
1427
1428 /*
1429 * The current cstack will be freed when do_cmdline() returns. An uncaught
1430 * exception will have to be rethrown in the previous cstack. If a function
1431 * has just returned or a script file was just finished and the previous
1432 * cstack belongs to the same function or, respectively, script file, it
1433 * will have to be checked for finally clauses to be executed due to the
1434 * ":return" or ":finish". This is done in do_one_cmd().
1435 */
1436 if (did_throw)
1437 need_rethrow = TRUE;
1438 if ((getline_equal(getline, cookie, getsourceline)
1439 && ex_nesting_level > source_level(real_cookie))
1440 || (getline_equal(getline, cookie, get_func_line)
1441 && ex_nesting_level > func_level(real_cookie) + 1))
1442 {
1443 if (!did_throw)
1444 check_cstack = TRUE;
1445 }
1446 else
1447 {
1448 /* When leaving a function, reduce nesting level. */
1449 if (getline_equal(getline, cookie, get_func_line))
1450 --ex_nesting_level;
1451 /*
1452 * Go to debug mode when returning from a function in which we are
1453 * single-stepping.
1454 */
1455 if ((getline_equal(getline, cookie, getsourceline)
1456 || getline_equal(getline, cookie, get_func_line))
1457 && ex_nesting_level + 1 <= debug_break_level)
1458 do_debug(getline_equal(getline, cookie, getsourceline)
1459 ? (char_u *)_("End of sourced file")
1460 : (char_u *)_("End of function"));
1461 }
1462
1463 /*
1464 * Restore the exception environment (done after returning from the
1465 * debugger).
1466 */
1467 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001468 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 msg_list = saved_msg_list;
1471#endif /* FEAT_EVAL */
1472
1473 /*
1474 * If there was too much output to fit on the command line, ask the user to
1475 * hit return before redrawing the screen. With the ":global" command we do
1476 * this only once after the command is finished.
1477 */
1478 if (did_inc)
1479 {
1480 --RedrawingDisabled;
1481 --no_wait_return;
1482 msg_scroll = FALSE;
1483
1484 /*
1485 * When just finished an ":if"-":else" which was typed, no need to
1486 * wait for hit-return. Also for an error situation.
1487 */
1488 if (retval == FAIL
1489#ifdef FEAT_EVAL
1490 || (did_endif && KeyTyped && !did_emsg)
1491#endif
1492 )
1493 {
1494 need_wait_return = FALSE;
1495 msg_didany = FALSE; /* don't wait when restarting edit */
1496 }
1497 else if (need_wait_return)
1498 {
1499 /*
1500 * The msg_start() above clears msg_didout. The wait_return we do
1501 * here should not overwrite the command that may be shown before
1502 * doing that.
1503 */
1504 msg_didout |= msg_didout_before_start;
1505 wait_return(FALSE);
1506 }
1507 }
1508
1509#ifndef FEAT_EVAL
1510 /*
1511 * Reset if_level, in case a sourced script file contains more ":if" than
1512 * ":endif" (could be ":if x | foo | endif").
1513 */
1514 if_level = 0;
1515#endif
1516
1517 --call_depth;
1518 return retval;
1519}
1520
1521#ifdef FEAT_EVAL
1522/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 */
1525 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 int c;
1528 void *cookie;
1529 int indent;
1530{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 wcmd_T *wp;
1533 char_u *line;
1534
1535 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1536 {
1537 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (cp->getline == NULL)
1542 line = getcmdline(c, 0L, indent);
1543 else
1544 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001545 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 ++cp->current_line;
1547
1548 return line;
1549 }
1550
1551 KeyTyped = FALSE;
1552 ++cp->current_line;
1553 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1554 sourcing_lnum = wp->lnum;
1555 return vim_strsave(wp->line);
1556}
1557
1558/*
1559 * Store a line in "gap" so that a ":while" loop can execute it again.
1560 */
1561 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 garray_T *gap;
1564 char_u *line;
1565{
1566 if (ga_grow(gap, 1) == FAIL)
1567 return FAIL;
1568 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1569 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1570 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 return OK;
1572}
1573
1574/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 */
1577 static void
1578free_cmdlines(gap)
1579 garray_T *gap;
1580{
1581 while (gap->ga_len > 0)
1582 {
1583 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1584 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586}
1587#endif
1588
1589/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001590 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1591 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594getline_equal(fgetline, cookie, func)
1595 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001596 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 char_u *(*func) __ARGS((int, void *, int));
1598{
1599#ifdef FEAT_EVAL
1600 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001601 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaar89d40322006-08-29 15:30:07 +00001603 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001604 * function that's originally used to obtain the lines. This may be
1605 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001606 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 cp = (struct loop_cookie *)cookie;
1608 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 gp = cp->getline;
1611 cp = cp->cookie;
1612 }
1613 return gp == func;
1614#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#endif
1617}
1618
1619#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1620/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 * getline function. Otherwise return "cookie".
1623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001626 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629# ifdef FEAT_EVAL
1630 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001631 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar89d40322006-08-29 15:30:07 +00001633 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001634 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001637 cp = (struct loop_cookie *)cookie;
1638 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
1640 gp = cp->getline;
1641 cp = cp->cookie;
1642 }
1643 return cp;
1644# else
1645 return cookie;
1646# endif
1647}
1648#endif
1649
1650/*
1651 * Execute one Ex command.
1652 *
1653 * If 'sourcing' is TRUE, the command will be included in the error message.
1654 *
1655 * 1. skip comment lines and leading space
1656 * 2. handle command modifiers
1657 * 3. parse range
1658 * 4. parse command
1659 * 5. parse arguments
1660 * 6. switch on command name
1661 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 *
1664 * This function may be called recursively!
1665 */
1666#if (_MSC_VER == 1200)
1667/*
Bram Moolenaared203462004-06-16 11:19:22 +00001668 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001670 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671#endif
1672 static char_u *
1673do_one_cmd(cmdlinep, sourcing,
1674#ifdef FEAT_EVAL
1675 cstack,
1676#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 char_u **cmdlinep;
1679 int sourcing;
1680#ifdef FEAT_EVAL
1681 struct condstack *cstack;
1682#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001683 char_u *(*fgetline) __ARGS((int, void *, int));
1684 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 char_u *p;
1687 linenr_T lnum;
1688 long n;
1689 char_u *errormsg = NULL; /* error message */
1690 exarg_T ea; /* Ex command arguments */
1691 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001692 int save_msg_scroll = msg_scroll;
1693 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001695#ifdef HAVE_SANDBOX
1696 int did_sandbox = FALSE;
1697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 cmdmod_T save_cmdmod;
1699 int ni; /* set when Not Implemented */
1700
1701 vim_memset(&ea, 0, sizeof(ea));
1702 ea.line1 = 1;
1703 ea.line2 = 1;
1704#ifdef FEAT_EVAL
1705 ++ex_nesting_level;
1706#endif
1707
1708 /* when not editing the last file :q has to be typed twice */
1709 if (quitmore
1710#ifdef FEAT_EVAL
1711 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#endif
1714 )
1715 --quitmore;
1716
1717 /*
1718 * Reset browse, confirm, etc.. They are restored when returning, for
1719 * recursive calls.
1720 */
1721 save_cmdmod = cmdmod;
1722 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1723
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001724 /* "#!anything" is handled like a comment. */
1725 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1726 goto doend;
1727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 /*
1729 * Repeat until no more command modifiers are found.
1730 */
1731 ea.cmd = *cmdlinep;
1732 for (;;)
1733 {
1734/*
1735 * 1. skip comment lines and leading white space and colons
1736 */
1737 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1738 ++ea.cmd;
1739
1740 /* in ex mode, an empty line works like :+ */
1741 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001742 && (getline_equal(fgetline, cookie, getexmodeline)
1743 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001744 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 {
1746 ea.cmd = (char_u *)"+";
1747 ex_pressedreturn = TRUE;
1748 }
1749
1750 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001751 if (*ea.cmd == '"')
1752 goto doend;
1753 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001754 {
1755 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759/*
1760 * 2. handle command modifiers.
1761 */
1762 p = ea.cmd;
1763 if (VIM_ISDIGIT(*ea.cmd))
1764 p = skipwhite(skipdigits(ea.cmd));
1765 switch (*p)
1766 {
1767 /* When adding an entry, also modify cmd_exists(). */
1768 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1769 break;
1770#ifdef FEAT_WINDOWS
1771 cmdmod.split |= WSP_ABOVE;
1772#endif
1773 continue;
1774
1775 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1776 {
1777#ifdef FEAT_WINDOWS
1778 cmdmod.split |= WSP_BELOW;
1779#endif
1780 continue;
1781 }
1782 if (checkforcmd(&ea.cmd, "browse", 3))
1783 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001784#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 cmdmod.browse = TRUE;
1786#endif
1787 continue;
1788 }
1789 if (!checkforcmd(&ea.cmd, "botright", 2))
1790 break;
1791#ifdef FEAT_WINDOWS
1792 cmdmod.split |= WSP_BOT;
1793#endif
1794 continue;
1795
1796 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1797 break;
1798#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1799 cmdmod.confirm = TRUE;
1800#endif
1801 continue;
1802
1803 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1804 {
1805 cmdmod.keepmarks = TRUE;
1806 continue;
1807 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001808 if (checkforcmd(&ea.cmd, "keepalt", 5))
1809 {
1810 cmdmod.keepalt = TRUE;
1811 continue;
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1814 break;
1815 cmdmod.keepjumps = TRUE;
1816 continue;
1817
1818 /* ":hide" and ":hide | cmd" are not modifiers */
1819 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1820 || *p == NUL || ends_excmd(*p))
1821 break;
1822 ea.cmd = p;
1823 cmdmod.hide = TRUE;
1824 continue;
1825
1826 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1827 {
1828 cmdmod.lockmarks = TRUE;
1829 continue;
1830 }
1831
1832 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1833 break;
1834#ifdef FEAT_WINDOWS
1835 cmdmod.split |= WSP_ABOVE;
1836#endif
1837 continue;
1838
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001839 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1840 break;
1841#ifdef FEAT_AUTOCMD
1842 if (cmdmod.save_ei == NULL)
1843 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001844 /* Set 'eventignore' to "all". Restore the
1845 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001846 cmdmod.save_ei = vim_strsave(p_ei);
1847 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001848 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001849 }
1850#endif
1851 continue;
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1854 break;
1855#ifdef FEAT_WINDOWS
1856 cmdmod.split |= WSP_BELOW;
1857#endif
1858 continue;
1859
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001860 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1861 {
1862#ifdef HAVE_SANDBOX
1863 if (!did_sandbox)
1864 ++sandbox;
1865 did_sandbox = TRUE;
1866#endif
1867 continue;
1868 }
1869 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001871 if (save_msg_silent == -1)
1872 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1875 {
1876 /* ":silent!", but not "silent !cmd" */
1877 ea.cmd = skipwhite(ea.cmd + 1);
1878 ++emsg_silent;
1879 ++did_esilent;
1880 }
1881 continue;
1882
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001883 case 't': if (checkforcmd(&p, "tab", 3))
1884 {
1885#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001886 if (vim_isdigit(*ea.cmd))
1887 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1888 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001889 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001890 ea.cmd = p;
1891#endif
1892 continue;
1893 }
1894 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 break;
1896#ifdef FEAT_WINDOWS
1897 cmdmod.split |= WSP_TOP;
1898#endif
1899 continue;
1900
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001901 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1902 break;
1903 if (save_msg_silent == -1)
1904 save_msg_silent = msg_silent;
1905 msg_silent = 0;
1906 continue;
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1909 {
1910#ifdef FEAT_VERTSPLIT
1911 cmdmod.split |= WSP_VERT;
1912#endif
1913 continue;
1914 }
1915 if (!checkforcmd(&p, "verbose", 4))
1916 break;
1917 if (verbose_save < 0)
1918 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001919 if (vim_isdigit(*ea.cmd))
1920 p_verbose = atoi((char *)ea.cmd);
1921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 p_verbose = 1;
1923 ea.cmd = p;
1924 continue;
1925 }
1926 break;
1927 }
1928
1929#ifdef FEAT_EVAL
1930 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1931 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1932#else
1933 ea.skip = (if_level > 0);
1934#endif
1935
1936#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001937# ifdef FEAT_PROFILE
1938 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001939 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001940 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001941 if (getline_equal(fgetline, cookie, get_func_line))
1942 func_line_exec(getline_cookie(fgetline, cookie));
1943 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001944 script_line_exec();
1945 }
1946#endif
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 /* May go to debug mode. If this happens and the ">quit" debug command is
1949 * used, throw an interrupt exception and skip the next command. */
1950 dbg_check_breakpoint(&ea);
1951 if (!ea.skip && got_int)
1952 {
1953 ea.skip = TRUE;
1954 (void)do_intthrow(cstack);
1955 }
1956#endif
1957
1958/*
1959 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1960 *
1961 * where 'addr' is:
1962 *
1963 * % (entire file)
1964 * $ [+-NUM]
1965 * 'x [+-NUM] (where x denotes a currently defined mark)
1966 * . [+-NUM]
1967 * [+-NUM]..
1968 * NUM
1969 *
1970 * The ea.cmd pointer is updated to point to the first character following the
1971 * range spec. If an initial address is found, but no second, the upper bound
1972 * is equal to the lower.
1973 */
1974
1975 /* repeat for all ',' or ';' separated addresses */
1976 for (;;)
1977 {
1978 ea.line1 = ea.line2;
1979 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1980 ea.cmd = skipwhite(ea.cmd);
1981 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1982 if (ea.cmd == NULL) /* error detected */
1983 goto doend;
1984 if (lnum == MAXLNUM)
1985 {
1986 if (*ea.cmd == '%') /* '%' - all lines */
1987 {
1988 ++ea.cmd;
1989 ea.line1 = 1;
1990 ea.line2 = curbuf->b_ml.ml_line_count;
1991 ++ea.addr_count;
1992 }
1993 /* '*' - visual area */
1994 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1995 {
1996 pos_T *fp;
1997
1998 ++ea.cmd;
1999 if (!ea.skip)
2000 {
2001 fp = getmark('<', FALSE);
2002 if (check_mark(fp) == FAIL)
2003 goto doend;
2004 ea.line1 = fp->lnum;
2005 fp = getmark('>', FALSE);
2006 if (check_mark(fp) == FAIL)
2007 goto doend;
2008 ea.line2 = fp->lnum;
2009 ++ea.addr_count;
2010 }
2011 }
2012 }
2013 else
2014 ea.line2 = lnum;
2015 ea.addr_count++;
2016
2017 if (*ea.cmd == ';')
2018 {
2019 if (!ea.skip)
2020 curwin->w_cursor.lnum = ea.line2;
2021 }
2022 else if (*ea.cmd != ',')
2023 break;
2024 ++ea.cmd;
2025 }
2026
2027 /* One address given: set start and end lines */
2028 if (ea.addr_count == 1)
2029 {
2030 ea.line1 = ea.line2;
2031 /* ... but only implicit: really no address given */
2032 if (lnum == MAXLNUM)
2033 ea.addr_count = 0;
2034 }
2035
2036 /* Don't leave the cursor on an illegal line (caused by ';') */
2037 check_cursor_lnum();
2038
2039/*
2040 * 4. parse command
2041 */
2042
2043 /*
2044 * Skip ':' and any white space
2045 */
2046 ea.cmd = skipwhite(ea.cmd);
2047 while (*ea.cmd == ':')
2048 ea.cmd = skipwhite(ea.cmd + 1);
2049
2050 /*
2051 * If we got a line, but no command, then go to the line.
2052 * If we find a '|' or '\n' we set ea.nextcmd.
2053 */
2054 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2055 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2056 {
2057 /*
2058 * strange vi behaviour:
2059 * ":3" jumps to line 3
2060 * ":3|..." prints line 3
2061 * ":|" prints current line
2062 */
2063 if (ea.skip) /* skip this if inside :if */
2064 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002065 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067 ea.cmdidx = CMD_print;
2068 ea.argt = RANGE+COUNT+TRLBAR;
2069 if ((errormsg = invalid_range(&ea)) == NULL)
2070 {
2071 correct_range(&ea);
2072 ex_print(&ea);
2073 }
2074 }
2075 else if (ea.addr_count != 0)
2076 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002077 if (ea.line2 > curbuf->b_ml.ml_line_count)
2078 {
2079 /* With '-' in 'cpoptions' a line number past the file is an
2080 * error, otherwise put it at the end of the file. */
2081 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2082 ea.line2 = -1;
2083 else
2084 ea.line2 = curbuf->b_ml.ml_line_count;
2085 }
2086
2087 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002088 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 else
2090 {
2091 if (ea.line2 == 0)
2092 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 else
2094 curwin->w_cursor.lnum = ea.line2;
2095 beginline(BL_SOL | BL_FIX);
2096 }
2097 }
2098 goto doend;
2099 }
2100
2101 /* Find the command and let "p" point to after it. */
2102 p = find_command(&ea, NULL);
2103
2104#ifdef FEAT_USR_CMDS
2105 if (p == NULL)
2106 {
2107 if (!ea.skip)
2108 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2109 goto doend;
2110 }
2111 /* Check for wrong commands. */
2112 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2113 {
2114 errormsg = uc_fun_cmd();
2115 goto doend;
2116 }
2117#endif
2118 if (ea.cmdidx == CMD_SIZE)
2119 {
2120 if (!ea.skip)
2121 {
2122 STRCPY(IObuff, _("E492: Not an editor command"));
2123 if (!sourcing)
2124 {
2125 STRCAT(IObuff, ": ");
2126 STRNCAT(IObuff, *cmdlinep, 40);
2127 }
2128 errormsg = IObuff;
2129 }
2130 goto doend;
2131 }
2132
2133 ni = (
2134#ifdef FEAT_USR_CMDS
2135 !USER_CMDIDX(ea.cmdidx) &&
2136#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002137 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002138#ifdef HAVE_EX_SCRIPT_NI
2139 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2140#endif
2141 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143#ifndef FEAT_EVAL
2144 /*
2145 * When the expression evaluation is disabled, recognize the ":if" and
2146 * ":endif" commands and ignore everything in between it.
2147 */
2148 if (ea.cmdidx == CMD_if)
2149 ++if_level;
2150 if (if_level)
2151 {
2152 if (ea.cmdidx == CMD_endif)
2153 --if_level;
2154 goto doend;
2155 }
2156
2157#endif
2158
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002159 /* forced commands */
2160 if (*p == '!' && ea.cmdidx != CMD_substitute
2161 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 ++p;
2164 ea.forceit = TRUE;
2165 }
2166 else
2167 ea.forceit = FALSE;
2168
2169/*
2170 * 5. parse arguments
2171 */
2172#ifdef FEAT_USR_CMDS
2173 if (!USER_CMDIDX(ea.cmdidx))
2174#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002175 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 if (!ea.skip)
2178 {
2179#ifdef HAVE_SANDBOX
2180 if (sandbox != 0 && !(ea.argt & SBOXOK))
2181 {
2182 /* Command not allowed in sandbox. */
2183 errormsg = (char_u *)_(e_sandbox);
2184 goto doend;
2185 }
2186#endif
2187 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2188 {
2189 /* Command not allowed in non-'modifiable' buffer */
2190 errormsg = (char_u *)_(e_modifiable);
2191 goto doend;
2192 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002193
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002194 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195# ifdef FEAT_USR_CMDS
2196 && !USER_CMDIDX(ea.cmdidx)
2197# endif
2198 )
2199 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002200 /* Command not allowed when editing the command line. */
2201#ifdef FEAT_CMDWIN
2202 if (cmdwin_type != 0)
2203 errormsg = (char_u *)_(e_cmdwin);
2204 else
2205#endif
2206 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 goto doend;
2208 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002210 /* Disallow editing another buffer when "curbuf_lock" is set.
2211 * Do allow ":edit" (check for argument later).
2212 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002213 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002214 && ea.cmdidx != CMD_edit
2215 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002216# ifdef FEAT_USR_CMDS
2217 && !USER_CMDIDX(ea.cmdidx)
2218# endif
2219 && curbuf_locked())
2220 goto doend;
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2224 {
2225 /* no range allowed */
2226 errormsg = (char_u *)_(e_norange);
2227 goto doend;
2228 }
2229 }
2230
2231 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2232 {
2233 errormsg = (char_u *)_(e_nobang);
2234 goto doend;
2235 }
2236
2237 /*
2238 * Don't complain about the range if it is not used
2239 * (could happen if line_count is accidentally set to 0).
2240 */
2241 if (!ea.skip && !ni)
2242 {
2243 /*
2244 * If the range is backwards, ask for confirmation and, if given, swap
2245 * ea.line1 & ea.line2 so it's forwards again.
2246 * When global command is busy, don't ask, will fail below.
2247 */
2248 if (!global_busy && ea.line1 > ea.line2)
2249 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002250 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002252 if (sourcing || exmode_active)
2253 {
2254 errormsg = (char_u *)_("E493: Backwards range given");
2255 goto doend;
2256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if (ask_yesno((char_u *)
2258 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002259 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 lnum = ea.line1;
2262 ea.line1 = ea.line2;
2263 ea.line2 = lnum;
2264 }
2265 if ((errormsg = invalid_range(&ea)) != NULL)
2266 goto doend;
2267 }
2268
2269 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2270 ea.line2 = 1;
2271
2272 correct_range(&ea);
2273
2274#ifdef FEAT_FOLDING
2275 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2276 {
2277 /* Put the first line at the start of a closed fold, put the last line
2278 * at the end of a closed fold. */
2279 (void)hasFolding(ea.line1, &ea.line1, NULL);
2280 (void)hasFolding(ea.line2, NULL, &ea.line2);
2281 }
2282#endif
2283
2284#ifdef FEAT_QUICKFIX
2285 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002286 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 * option here, so things like % get expanded.
2288 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002289 p = replace_makeprg(&ea, p, cmdlinep);
2290 if (p == NULL)
2291 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292#endif
2293
2294 /*
2295 * Skip to start of argument.
2296 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2297 */
2298 if (ea.cmdidx == CMD_bang)
2299 ea.arg = p;
2300 else
2301 ea.arg = skipwhite(p);
2302
2303 /*
2304 * Check for "++opt=val" argument.
2305 * Must be first, allow ":w ++enc=utf8 !cmd"
2306 */
2307 if (ea.argt & ARGOPT)
2308 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2309 if (getargopt(&ea) == FAIL && !ni)
2310 {
2311 errormsg = (char_u *)_(e_invarg);
2312 goto doend;
2313 }
2314
2315 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2316 {
2317 if (*ea.arg == '>') /* append */
2318 {
2319 if (*++ea.arg != '>') /* typed wrong */
2320 {
2321 errormsg = (char_u *)_("E494: Use w or w>>");
2322 goto doend;
2323 }
2324 ea.arg = skipwhite(ea.arg + 1);
2325 ea.append = TRUE;
2326 }
2327 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2328 {
2329 ++ea.arg;
2330 ea.usefilter = TRUE;
2331 }
2332 }
2333
2334 if (ea.cmdidx == CMD_read)
2335 {
2336 if (ea.forceit)
2337 {
2338 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2339 ea.forceit = FALSE;
2340 }
2341 else if (*ea.arg == '!') /* :r !filter */
2342 {
2343 ++ea.arg;
2344 ea.usefilter = TRUE;
2345 }
2346 }
2347
2348 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2349 {
2350 ea.amount = 1;
2351 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2352 {
2353 ++ea.arg;
2354 ++ea.amount;
2355 }
2356 ea.arg = skipwhite(ea.arg);
2357 }
2358
2359 /*
2360 * Check for "+command" argument, before checking for next command.
2361 * Don't do this for ":read !cmd" and ":write !cmd".
2362 */
2363 if ((ea.argt & EDITCMD) && !ea.usefilter)
2364 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2365
2366 /*
2367 * Check for '|' to separate commands and '"' to start comments.
2368 * Don't do this for ":read !cmd" and ":write !cmd".
2369 */
2370 if ((ea.argt & TRLBAR) && !ea.usefilter)
2371 separate_nextcmd(&ea);
2372
2373 /*
2374 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002375 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2376 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002378 else if (ea.cmdidx == CMD_bang
2379 || ea.cmdidx == CMD_global
2380 || ea.cmdidx == CMD_vglobal
2381 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
2383 for (p = ea.arg; *p; ++p)
2384 {
2385 /* Remove one backslash before a newline, so that it's possible to
2386 * pass a newline to the shell and also a newline that is preceded
2387 * with a backslash. This makes it impossible to end a shell
2388 * command in a backslash, but that doesn't appear useful.
2389 * Halving the number of backslashes is incompatible with previous
2390 * versions. */
2391 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002392 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else if (*p == '\n')
2394 {
2395 ea.nextcmd = p + 1;
2396 *p = NUL;
2397 break;
2398 }
2399 }
2400 }
2401
2402 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2403 {
2404 ea.line1 = 1;
2405 ea.line2 = curbuf->b_ml.ml_line_count;
2406 }
2407
2408 /* accept numbered register only when no count allowed (:put) */
2409 if ( (ea.argt & REGSTR)
2410 && *ea.arg != NUL
2411#ifdef FEAT_USR_CMDS
2412 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2413 && USER_CMDIDX(ea.cmdidx)))
2414 /* Do not allow register = for user commands */
2415 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2416#else
2417 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2418#endif
2419 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2420 {
2421 ea.regname = *ea.arg++;
2422#ifdef FEAT_EVAL
2423 /* for '=' register: accept the rest of the line as an expression */
2424 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2425 {
2426 set_expr_line(vim_strsave(ea.arg));
2427 ea.arg += STRLEN(ea.arg);
2428 }
2429#endif
2430 ea.arg = skipwhite(ea.arg);
2431 }
2432
2433 /*
2434 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2435 * count, it's a buffer name.
2436 */
2437 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2438 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2439 || vim_iswhite(*p)))
2440 {
2441 n = getdigits(&ea.arg);
2442 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002443 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 errormsg = (char_u *)_(e_zerocount);
2446 goto doend;
2447 }
2448 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2449 {
2450 ea.line2 = n;
2451 if (ea.addr_count == 0)
2452 ea.addr_count = 1;
2453 }
2454 else
2455 {
2456 ea.line1 = ea.line2;
2457 ea.line2 += n - 1;
2458 ++ea.addr_count;
2459 /*
2460 * Be vi compatible: no error message for out of range.
2461 */
2462 if (ea.line2 > curbuf->b_ml.ml_line_count)
2463 ea.line2 = curbuf->b_ml.ml_line_count;
2464 }
2465 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002466
2467 /*
2468 * Check for flags: 'l', 'p' and '#'.
2469 */
2470 if (ea.argt & EXFLAGS)
2471 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002473 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002474 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 errormsg = (char_u *)_(e_trailing);
2477 goto doend;
2478 }
2479
2480 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2481 {
2482 errormsg = (char_u *)_(e_argreq);
2483 goto doend;
2484 }
2485
2486#ifdef FEAT_EVAL
2487 /*
2488 * Skip the command when it's not going to be executed.
2489 * The commands like :if, :endif, etc. always need to be executed.
2490 * Also make an exception for commands that handle a trailing command
2491 * themselves.
2492 */
2493 if (ea.skip)
2494 {
2495 switch (ea.cmdidx)
2496 {
2497 /* commands that need evaluation */
2498 case CMD_while:
2499 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002500 case CMD_for:
2501 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_if:
2503 case CMD_elseif:
2504 case CMD_else:
2505 case CMD_endif:
2506 case CMD_try:
2507 case CMD_catch:
2508 case CMD_finally:
2509 case CMD_endtry:
2510 case CMD_function:
2511 break;
2512
2513 /* Commands that handle '|' themselves. Check: A command should
2514 * either have the TRLBAR flag, appear in this list or appear in
2515 * the list at ":help :bar". */
2516 case CMD_aboveleft:
2517 case CMD_and:
2518 case CMD_belowright:
2519 case CMD_botright:
2520 case CMD_browse:
2521 case CMD_call:
2522 case CMD_confirm:
2523 case CMD_delfunction:
2524 case CMD_djump:
2525 case CMD_dlist:
2526 case CMD_dsearch:
2527 case CMD_dsplit:
2528 case CMD_echo:
2529 case CMD_echoerr:
2530 case CMD_echomsg:
2531 case CMD_echon:
2532 case CMD_execute:
2533 case CMD_help:
2534 case CMD_hide:
2535 case CMD_ijump:
2536 case CMD_ilist:
2537 case CMD_isearch:
2538 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002539 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 case CMD_keepjumps:
2541 case CMD_keepmarks:
2542 case CMD_leftabove:
2543 case CMD_let:
2544 case CMD_lockmarks:
2545 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002546 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 case CMD_perl:
2548 case CMD_psearch:
2549 case CMD_python:
2550 case CMD_return:
2551 case CMD_rightbelow:
2552 case CMD_ruby:
2553 case CMD_silent:
2554 case CMD_smagic:
2555 case CMD_snomagic:
2556 case CMD_substitute:
2557 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002558 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 case CMD_tcl:
2560 case CMD_throw:
2561 case CMD_tilde:
2562 case CMD_topleft:
2563 case CMD_unlet:
2564 case CMD_verbose:
2565 case CMD_vertical:
2566 break;
2567
2568 default: goto doend;
2569 }
2570 }
2571#endif
2572
2573 if (ea.argt & XFILE)
2574 {
2575 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2576 goto doend;
2577 }
2578
2579#ifdef FEAT_LISTCMDS
2580 /*
2581 * Accept buffer name. Cannot be used at the same time with a buffer
2582 * number. Don't do this for a user command.
2583 */
2584 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2585# ifdef FEAT_USR_CMDS
2586 && !USER_CMDIDX(ea.cmdidx)
2587# endif
2588 )
2589 {
2590 /*
2591 * :bdelete, :bwipeout and :bunload take several arguments, separated
2592 * by spaces: find next space (skipping over escaped characters).
2593 * The others take one argument: ignore trailing spaces.
2594 */
2595 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2596 || ea.cmdidx == CMD_bunload)
2597 p = skiptowhite_esc(ea.arg);
2598 else
2599 {
2600 p = ea.arg + STRLEN(ea.arg);
2601 while (p > ea.arg && vim_iswhite(p[-1]))
2602 --p;
2603 }
2604 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2605 if (ea.line2 < 0) /* failed */
2606 goto doend;
2607 ea.addr_count = 1;
2608 ea.arg = skipwhite(p);
2609 }
2610#endif
2611
2612/*
2613 * 6. switch on command name
2614 *
2615 * The "ea" structure holds the arguments that can be used.
2616 */
2617 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002618 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ea.cookie = cookie;
2620#ifdef FEAT_EVAL
2621 ea.cstack = cstack;
2622#endif
2623
2624#ifdef FEAT_USR_CMDS
2625 if (USER_CMDIDX(ea.cmdidx))
2626 {
2627 /*
2628 * Execute a user-defined command.
2629 */
2630 do_ucmd(&ea);
2631 }
2632 else
2633#endif
2634 {
2635 /*
2636 * Call the function to execute the command.
2637 */
2638 ea.errmsg = NULL;
2639 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2640 if (ea.errmsg != NULL)
2641 errormsg = (char_u *)_(ea.errmsg);
2642 }
2643
2644#ifdef FEAT_EVAL
2645 /*
2646 * If the command just executed called do_cmdline(), any throw or ":return"
2647 * or ":finish" encountered there must also check the cstack of the still
2648 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2649 * exception, or reanimate a returned function or finished script file and
2650 * return or finish it again.
2651 */
2652 if (need_rethrow)
2653 do_throw(cstack);
2654 else if (check_cstack)
2655 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 && current_func_returned())
2660 do_return(&ea, TRUE, FALSE, NULL);
2661 }
2662 need_rethrow = check_cstack = FALSE;
2663#endif
2664
2665doend:
2666 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2667 curwin->w_cursor.lnum = 1;
2668
2669 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2670 {
2671 if (sourcing)
2672 {
2673 if (errormsg != IObuff)
2674 {
2675 STRCPY(IObuff, errormsg);
2676 errormsg = IObuff;
2677 }
2678 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002679 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 }
2681 emsg(errormsg);
2682 }
2683#ifdef FEAT_EVAL
2684 do_errthrow(cstack,
2685 (ea.cmdidx != CMD_SIZE
2686# ifdef FEAT_USR_CMDS
2687 && !USER_CMDIDX(ea.cmdidx)
2688# endif
2689 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2690#endif
2691
2692 if (verbose_save >= 0)
2693 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002694#ifdef FEAT_AUTOCMD
2695 if (cmdmod.save_ei != NULL)
2696 {
2697 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002698 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2699 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002700 free_string_option(cmdmod.save_ei);
2701 }
2702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 cmdmod = save_cmdmod;
2705
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002706 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
2708 /* messages could be enabled for a serious error, need to check if the
2709 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002710 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002711 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 emsg_silent -= did_esilent;
2713 if (emsg_silent < 0)
2714 emsg_silent = 0;
2715 /* Restore msg_scroll, it's set by file I/O commands, even when no
2716 * message is actually displayed. */
2717 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002718
2719 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2720 * somewhere in the line. Put it back in the first column. */
2721 if (redirecting())
2722 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002725#ifdef HAVE_SANDBOX
2726 if (did_sandbox)
2727 --sandbox;
2728#endif
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2731 ea.nextcmd = NULL;
2732
2733#ifdef FEAT_EVAL
2734 --ex_nesting_level;
2735#endif
2736
2737 return ea.nextcmd;
2738}
2739#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002740 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#endif
2742
2743/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002744 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 * If there is a match advance "pp" to the argument and return TRUE.
2746 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002747 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002749 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 char *cmd; /* name of command */
2751 int len; /* required length */
2752{
2753 int i;
2754
2755 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002756 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 break;
2758 if (i >= len && !isalpha((*pp)[i]))
2759 {
2760 *pp = skipwhite(*pp + i);
2761 return TRUE;
2762 }
2763 return FALSE;
2764}
2765
2766/*
2767 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002768 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002770 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 * Returns NULL for an ambiguous user command.
2772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 static char_u *
2774find_command(eap, full)
2775 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002776 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 int len;
2779 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002780 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 /*
2783 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002784 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 * - the 'k' command can directly be followed by any character.
2786 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2787 * but :sre[wind] is another command, as are :scrip[tnames],
2788 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002789 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 */
2791 p = eap->cmd;
2792 if (*p == 'k')
2793 {
2794 eap->cmdidx = CMD_k;
2795 ++p;
2796 }
2797 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002798 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2799 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 || p[1] == 'g'
2801 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2802 || p[1] == 'I'
2803 || (p[1] == 'r' && p[2] != 'e')))
2804 {
2805 eap->cmdidx = CMD_substitute;
2806 ++p;
2807 }
2808 else
2809 {
2810 while (ASCII_ISALPHA(*p))
2811 ++p;
2812 /* check for non-alpha command */
2813 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2814 ++p;
2815 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002816 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2817 {
2818 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2819 * :delete with the 'l' flag. Same for 'p'. */
2820 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002821 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002822 break;
2823 if (i == len - 1)
2824 {
2825 --len;
2826 if (p[-1] == 'l')
2827 eap->flags |= EXFLAG_LIST;
2828 else
2829 eap->flags |= EXFLAG_PRINT;
2830 }
2831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 if (ASCII_ISLOWER(*eap->cmd))
2834 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2835 else
2836 eap->cmdidx = cmdidxs[26];
2837
2838 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2839 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2840 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2841 (size_t)len) == 0)
2842 {
2843#ifdef FEAT_EVAL
2844 if (full != NULL
2845 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2846 *full = TRUE;
2847#endif
2848 break;
2849 }
2850
2851#ifdef FEAT_USR_CMDS
2852 /* Look for a user defined command as a last resort */
2853 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2854 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002855 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 while (ASCII_ISALNUM(*p))
2857 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002858 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002861 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 eap->cmdidx = CMD_SIZE;
2863 }
2864
2865 return p;
2866}
2867
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002868#ifdef FEAT_USR_CMDS
2869/*
2870 * Search for a user command that matches "eap->cmd".
2871 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2872 * Return a pointer to just after the command.
2873 * Return NULL if there is no matching command.
2874 */
2875 static char_u *
2876find_ucmd(eap, p, full, xp, compl)
2877 exarg_T *eap;
2878 char_u *p; /* end of the command (possibly including count) */
2879 int *full; /* set to TRUE for a full match */
2880 expand_T *xp; /* used for completion, NULL otherwise */
2881 int *compl; /* completion flags or NULL */
2882{
2883 int len = (int)(p - eap->cmd);
2884 int j, k, matchlen = 0;
2885 ucmd_T *uc;
2886 int found = FALSE;
2887 int possible = FALSE;
2888 char_u *cp, *np; /* Point into typed cmd and test name */
2889 garray_T *gap;
2890 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2891 only full match global is accepted. */
2892
2893 /*
2894 * Look for buffer-local user commands first, then global ones.
2895 */
2896 gap = &curbuf->b_ucmds;
2897 for (;;)
2898 {
2899 for (j = 0; j < gap->ga_len; ++j)
2900 {
2901 uc = USER_CMD_GA(gap, j);
2902 cp = eap->cmd;
2903 np = uc->uc_name;
2904 k = 0;
2905 while (k < len && *np != NUL && *cp++ == *np++)
2906 k++;
2907 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2908 {
2909 /* If finding a second match, the command is ambiguous. But
2910 * not if a buffer-local command wasn't a full match and a
2911 * global command is a full match. */
2912 if (k == len && found && *np != NUL)
2913 {
2914 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002915 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002916 amb_local = TRUE;
2917 }
2918
2919 if (!found || (k == len && *np == NUL))
2920 {
2921 /* If we matched up to a digit, then there could
2922 * be another command including the digit that we
2923 * should use instead.
2924 */
2925 if (k == len)
2926 found = TRUE;
2927 else
2928 possible = TRUE;
2929
2930 if (gap == &ucmds)
2931 eap->cmdidx = CMD_USER;
2932 else
2933 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002934 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002935 eap->useridx = j;
2936
2937# ifdef FEAT_CMDL_COMPL
2938 if (compl != NULL)
2939 *compl = uc->uc_compl;
2940# ifdef FEAT_EVAL
2941 if (xp != NULL)
2942 {
2943 xp->xp_arg = uc->uc_compl_arg;
2944 xp->xp_scriptID = uc->uc_scriptID;
2945 }
2946# endif
2947# endif
2948 /* Do not search for further abbreviations
2949 * if this is an exact match. */
2950 matchlen = k;
2951 if (k == len && *np == NUL)
2952 {
2953 if (full != NULL)
2954 *full = TRUE;
2955 amb_local = FALSE;
2956 break;
2957 }
2958 }
2959 }
2960 }
2961
2962 /* Stop if we found a full match or searched all. */
2963 if (j < gap->ga_len || gap == &ucmds)
2964 break;
2965 gap = &ucmds;
2966 }
2967
2968 /* Only found ambiguous matches. */
2969 if (amb_local)
2970 {
2971 if (xp != NULL)
2972 xp->xp_context = EXPAND_UNSUCCESSFUL;
2973 return NULL;
2974 }
2975
2976 /* The match we found may be followed immediately by a number. Move "p"
2977 * back to point to it. */
2978 if (found || possible)
2979 return p + (matchlen - len);
2980 return p;
2981}
2982#endif
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00002985static struct cmdmod
2986{
2987 char *name;
2988 int minlen;
2989 int has_count; /* :123verbose :3tab */
2990} cmdmods[] = {
2991 {"aboveleft", 3, FALSE},
2992 {"belowright", 3, FALSE},
2993 {"botright", 2, FALSE},
2994 {"browse", 3, FALSE},
2995 {"confirm", 4, FALSE},
2996 {"hide", 3, FALSE},
2997 {"keepalt", 5, FALSE},
2998 {"keepjumps", 5, FALSE},
2999 {"keepmarks", 3, FALSE},
3000 {"leftabove", 5, FALSE},
3001 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003002 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003003 {"rightbelow", 6, FALSE},
3004 {"sandbox", 3, FALSE},
3005 {"silent", 3, FALSE},
3006 {"tab", 3, TRUE},
3007 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003008 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003009 {"verbose", 4, TRUE},
3010 {"vertical", 4, FALSE},
3011};
3012
3013/*
3014 * Return length of a command modifier (including optional count).
3015 * Return zero when it's not a modifier.
3016 */
3017 int
3018modifier_len(cmd)
3019 char_u *cmd;
3020{
3021 int i, j;
3022 char_u *p = cmd;
3023
3024 if (VIM_ISDIGIT(*cmd))
3025 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003026 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003027 {
3028 for (j = 0; p[j] != NUL; ++j)
3029 if (p[j] != cmdmods[i].name[j])
3030 break;
3031 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3032 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003033 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003034 }
3035 return 0;
3036}
3037
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038/*
3039 * Return > 0 if an Ex command "name" exists.
3040 * Return 2 if there is an exact match.
3041 * Return 3 if there is an ambiguous match.
3042 */
3043 int
3044cmd_exists(name)
3045 char_u *name;
3046{
3047 exarg_T ea;
3048 int full = FALSE;
3049 int i;
3050 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003051 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003054 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 {
3056 for (j = 0; name[j] != NUL; ++j)
3057 if (name[j] != cmdmods[i].name[j])
3058 break;
3059 if (name[j] == NUL && j >= cmdmods[i].minlen)
3060 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3061 }
3062
Bram Moolenaara9587612006-05-04 21:47:50 +00003063 /* Check built-in commands and user defined commands.
3064 * For ":2match" and ":3match" we need to skip the number. */
3065 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003067 p = find_command(&ea, &full);
3068 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003070 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3071 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003072 if (*skipwhite(p) != NUL)
3073 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3075}
3076#endif
3077
3078/*
3079 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3080 * we don't need/want deleted. Maybe this could be done better if we didn't
3081 * repeat all this stuff. The only problem is that they may not stay
3082 * perfectly compatible with each other, but then the command line syntax
3083 * probably won't change that much -- webb.
3084 */
3085 char_u *
3086set_one_cmd_context(xp, buff)
3087 expand_T *xp;
3088 char_u *buff; /* buffer for command string */
3089{
3090 char_u *p;
3091 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003092 int len = 0;
3093 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3095 int compl = EXPAND_NOTHING;
3096#endif
3097#ifdef FEAT_CMDL_COMPL
3098 int delim;
3099#endif
3100 int forceit = FALSE;
3101 int usefilter = FALSE; /* filter instead of file name */
3102
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003103 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 xp->xp_pattern = buff;
3105 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003106 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108/*
3109 * 2. skip comment lines and leading space, colons or bars
3110 */
3111 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3112 ;
3113 xp->xp_pattern = cmd;
3114
3115 if (*cmd == NUL)
3116 return NULL;
3117 if (*cmd == '"') /* ignore comment lines */
3118 {
3119 xp->xp_context = EXPAND_NOTHING;
3120 return NULL;
3121 }
3122
3123/*
3124 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3125 */
3126 cmd = skip_range(cmd, &xp->xp_context);
3127
3128/*
3129 * 4. parse command
3130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 xp->xp_pattern = cmd;
3132 if (*cmd == NUL)
3133 return NULL;
3134 if (*cmd == '"')
3135 {
3136 xp->xp_context = EXPAND_NOTHING;
3137 return NULL;
3138 }
3139
3140 if (*cmd == '|' || *cmd == '\n')
3141 return cmd + 1; /* There's another command */
3142
3143 /*
3144 * Isolate the command and search for it in the command table.
3145 * Exceptions:
3146 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003147 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3149 */
3150 if (*cmd == 'k' && cmd[1] != 'e')
3151 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 p = cmd + 1;
3154 }
3155 else
3156 {
3157 p = cmd;
3158 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3159 ++p;
3160 /* check for non-alpha command */
3161 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3162 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003163 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003165 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 xp->xp_context = EXPAND_UNSUCCESSFUL;
3168 return NULL;
3169 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003170 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003171 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3172 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3173 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 break;
3175
3176#ifdef FEAT_USR_CMDS
3177 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3179 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#endif
3181 }
3182
3183 /*
3184 * If the cursor is touching the command, and it ends in an alpha-numeric
3185 * character, complete the command name.
3186 */
3187 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3188 return NULL;
3189
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3193 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 p = cmd + 1;
3196 }
3197#ifdef FEAT_USR_CMDS
3198 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3199 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003200 ea.cmd = cmd;
3201 p = find_ucmd(&ea, p, NULL, xp,
3202# if defined(FEAT_CMDL_COMPL)
3203 &compl
3204# else
3205 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003208 if (p == NULL)
3209 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211#endif
3212 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
3215 /* Not still touching the command and it was an illegal one */
3216 xp->xp_context = EXPAND_UNSUCCESSFUL;
3217 return NULL;
3218 }
3219
3220 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3221
3222 if (*p == '!') /* forced commands */
3223 {
3224 forceit = TRUE;
3225 ++p;
3226 }
3227
3228/*
3229 * 5. parse arguments
3230 */
3231#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003234 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 arg = skipwhite(p);
3237
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 if (*arg == '>') /* append */
3241 {
3242 if (*++arg == '>')
3243 ++arg;
3244 arg = skipwhite(arg);
3245 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 ++arg;
3249 usefilter = TRUE;
3250 }
3251 }
3252
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
3255 usefilter = forceit; /* :r! filter if forced */
3256 if (*arg == '!') /* :r !filter */
3257 {
3258 ++arg;
3259 usefilter = TRUE;
3260 }
3261 }
3262
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 while (*arg == *cmd) /* allow any number of '>' or '<' */
3266 ++arg;
3267 arg = skipwhite(arg);
3268 }
3269
3270 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 /* Check if we're in the +command */
3274 p = arg + 1;
3275 arg = skip_cmd_arg(arg, FALSE);
3276
3277 /* Still touching the command after '+'? */
3278 if (*arg == NUL)
3279 return p;
3280
3281 /* Skip space(s) after +command to get to the real argument */
3282 arg = skipwhite(arg);
3283 }
3284
3285 /*
3286 * Check for '|' to separate commands and '"' to start comments.
3287 * Don't do this for ":read !cmd" and ":write !cmd".
3288 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
3291 p = arg;
3292 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 p += 2;
3295 while (*p)
3296 {
3297 if (*p == Ctrl_V)
3298 {
3299 if (p[1] != NUL)
3300 ++p;
3301 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 || *p == '|' || *p == '\n')
3304 {
3305 if (*(p - 1) != '\\')
3306 {
3307 if (*p == '|' || *p == '\n')
3308 return p + 1;
3309 return NULL; /* It's a comment */
3310 }
3311 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003312 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 }
3315
3316 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 vim_strchr((char_u *)"|\"", *arg) == NULL)
3319 return NULL;
3320
3321 /* Find start of last argument (argument just before cursor): */
3322 p = buff + STRLEN(buff);
3323 while (p != arg && *p != ' ' && *p != TAB)
3324 p--;
3325 if (*p == ' ' || *p == TAB)
3326 p++;
3327 xp->xp_pattern = p;
3328
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003331 int c;
3332 int in_quote = FALSE;
3333 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335 /*
3336 * Allow spaces within back-quotes to count as part of the argument
3337 * being expanded.
3338 */
3339 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003340 p = xp->xp_pattern;
3341 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003343#ifdef FEAT_MBYTE
3344 if (has_mbyte)
3345 c = mb_ptr2char(p);
3346 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003348 c = *p;
3349 if (c == '\\' && p[1] != NUL)
3350 ++p;
3351 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 if (!in_quote)
3354 {
3355 xp->xp_pattern = p;
3356 bow = p + 1;
3357 }
3358 in_quote = !in_quote;
3359 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003360 /* An argument can contain just about everything, except
3361 * characters that end the command and white space. */
3362 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003363#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003364 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003365#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003366 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003367 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003368 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003369 while (*p != NUL)
3370 {
3371#ifdef FEAT_MBYTE
3372 if (has_mbyte)
3373 c = mb_ptr2char(p);
3374 else
3375#endif
3376 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003377 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003378 break;
3379#ifdef FEAT_MBYTE
3380 if (has_mbyte)
3381 len = (*mb_ptr2len)(p);
3382 else
3383#endif
3384 len = 1;
3385 mb_ptr_adv(p);
3386 }
3387 if (in_quote)
3388 bow = p;
3389 else
3390 xp->xp_pattern = p;
3391 p -= len;
3392 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003393 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395
3396 /*
3397 * If we are still inside the quotes, and we passed a space, just
3398 * expand from there.
3399 */
3400 if (bow != NULL && in_quote)
3401 xp->xp_pattern = bow;
3402 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003403
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003404#ifndef BACKSLASH_IN_FILENAME
3405 /* For a shell command more chars need to be escaped. */
3406 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003407 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003408 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003409
3410 /* When still after the command name expand executables. */
3411 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003412 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003413 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 /* Check for environment variable */
3417 if (*xp->xp_pattern == '$'
3418#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3419 || *xp->xp_pattern == '%'
3420#endif
3421 )
3422 {
3423 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3424 if (!vim_isIDc(*p))
3425 break;
3426 if (*p == NUL)
3427 {
3428 xp->xp_context = EXPAND_ENV_VARS;
3429 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003430#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3431 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003432 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003433 compl = EXPAND_ENV_VARS;
3434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 }
3437 }
3438
3439/*
3440 * 6. switch on command name
3441 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 case CMD_cd:
3445 case CMD_chdir:
3446 case CMD_lcd:
3447 case CMD_lchdir:
3448 if (xp->xp_context == EXPAND_FILES)
3449 xp->xp_context = EXPAND_DIRECTORIES;
3450 break;
3451 case CMD_help:
3452 xp->xp_context = EXPAND_HELP;
3453 xp->xp_pattern = arg;
3454 break;
3455
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003456 /* Command modifiers: return the argument.
3457 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003459 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 case CMD_belowright:
3461 case CMD_botright:
3462 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003463 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003465 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 case CMD_folddoclosed:
3467 case CMD_folddoopen:
3468 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003469 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 case CMD_keepjumps:
3471 case CMD_keepmarks:
3472 case CMD_leftabove:
3473 case CMD_lockmarks:
3474 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003475 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003477 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case CMD_topleft:
3479 case CMD_verbose:
3480 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003481 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return arg;
3483
Bram Moolenaar4f688582007-07-24 12:34:30 +00003484#ifdef FEAT_CMDL_COMPL
3485# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 case CMD_match:
3487 if (*arg == NUL || !ends_excmd(*arg))
3488 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003489 /* also complete "None" */
3490 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 arg = skipwhite(skiptowhite(arg));
3492 if (*arg != NUL)
3493 {
3494 xp->xp_context = EXPAND_NOTHING;
3495 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3496 }
3497 }
3498 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501/*
3502 * All completion for the +cmdline_compl feature goes here.
3503 */
3504
3505# ifdef FEAT_USR_CMDS
3506 case CMD_command:
3507 /* Check for attributes */
3508 while (*arg == '-')
3509 {
3510 arg++; /* Skip "-" */
3511 p = skiptowhite(arg);
3512 if (*p == NUL)
3513 {
3514 /* Cursor is still in the attribute */
3515 p = vim_strchr(arg, '=');
3516 if (p == NULL)
3517 {
3518 /* No "=", so complete attribute names */
3519 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3520 xp->xp_pattern = arg;
3521 return NULL;
3522 }
3523
3524 /* For the -complete and -nargs attributes, we complete
3525 * their arguments as well.
3526 */
3527 if (STRNICMP(arg, "complete", p - arg) == 0)
3528 {
3529 xp->xp_context = EXPAND_USER_COMPLETE;
3530 xp->xp_pattern = p + 1;
3531 return NULL;
3532 }
3533 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3534 {
3535 xp->xp_context = EXPAND_USER_NARGS;
3536 xp->xp_pattern = p + 1;
3537 return NULL;
3538 }
3539 return NULL;
3540 }
3541 arg = skipwhite(p);
3542 }
3543
3544 /* After the attributes comes the new command name */
3545 p = skiptowhite(arg);
3546 if (*p == NUL)
3547 {
3548 xp->xp_context = EXPAND_USER_COMMANDS;
3549 xp->xp_pattern = arg;
3550 break;
3551 }
3552
3553 /* And finally comes a normal command */
3554 return skipwhite(p);
3555
3556 case CMD_delcommand:
3557 xp->xp_context = EXPAND_USER_COMMANDS;
3558 xp->xp_pattern = arg;
3559 break;
3560# endif
3561
3562 case CMD_global:
3563 case CMD_vglobal:
3564 delim = *arg; /* get the delimiter */
3565 if (delim)
3566 ++arg; /* skip delimiter if there is one */
3567
3568 while (arg[0] != NUL && arg[0] != delim)
3569 {
3570 if (arg[0] == '\\' && arg[1] != NUL)
3571 ++arg;
3572 ++arg;
3573 }
3574 if (arg[0] != NUL)
3575 return arg + 1;
3576 break;
3577 case CMD_and:
3578 case CMD_substitute:
3579 delim = *arg;
3580 if (delim)
3581 {
3582 /* skip "from" part */
3583 ++arg;
3584 arg = skip_regexp(arg, delim, p_magic, NULL);
3585 }
3586 /* skip "to" part */
3587 while (arg[0] != NUL && arg[0] != delim)
3588 {
3589 if (arg[0] == '\\' && arg[1] != NUL)
3590 ++arg;
3591 ++arg;
3592 }
3593 if (arg[0] != NUL) /* skip delimiter */
3594 ++arg;
3595 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3596 ++arg;
3597 if (arg[0] != NUL)
3598 return arg;
3599 break;
3600 case CMD_isearch:
3601 case CMD_dsearch:
3602 case CMD_ilist:
3603 case CMD_dlist:
3604 case CMD_ijump:
3605 case CMD_psearch:
3606 case CMD_djump:
3607 case CMD_isplit:
3608 case CMD_dsplit:
3609 arg = skipwhite(skipdigits(arg)); /* skip count */
3610 if (*arg == '/') /* Match regexp, not just whole words */
3611 {
3612 for (++arg; *arg && *arg != '/'; arg++)
3613 if (*arg == '\\' && arg[1] != NUL)
3614 arg++;
3615 if (*arg)
3616 {
3617 arg = skipwhite(arg + 1);
3618
3619 /* Check for trailing illegal characters */
3620 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3621 xp->xp_context = EXPAND_NOTHING;
3622 else
3623 return arg;
3624 }
3625 }
3626 break;
3627#ifdef FEAT_AUTOCMD
3628 case CMD_autocmd:
3629 return set_context_in_autocmd(xp, arg, FALSE);
3630
3631 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003632 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 return set_context_in_autocmd(xp, arg, TRUE);
3634#endif
3635 case CMD_set:
3636 set_context_in_set_cmd(xp, arg, 0);
3637 break;
3638 case CMD_setglobal:
3639 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3640 break;
3641 case CMD_setlocal:
3642 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3643 break;
3644 case CMD_tag:
3645 case CMD_stag:
3646 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003647 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 case CMD_tselect:
3649 case CMD_stselect:
3650 case CMD_ptselect:
3651 case CMD_tjump:
3652 case CMD_stjump:
3653 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003654 if (*p_wop != NUL)
3655 xp->xp_context = EXPAND_TAGS_LISTFILES;
3656 else
3657 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 xp->xp_pattern = arg;
3659 break;
3660 case CMD_augroup:
3661 xp->xp_context = EXPAND_AUGROUP;
3662 xp->xp_pattern = arg;
3663 break;
3664#ifdef FEAT_SYN_HL
3665 case CMD_syntax:
3666 set_context_in_syntax_cmd(xp, arg);
3667 break;
3668#endif
3669#ifdef FEAT_EVAL
3670 case CMD_let:
3671 case CMD_if:
3672 case CMD_elseif:
3673 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003674 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 case CMD_echo:
3676 case CMD_echon:
3677 case CMD_execute:
3678 case CMD_echomsg:
3679 case CMD_echoerr:
3680 case CMD_call:
3681 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003682 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 break;
3684
3685 case CMD_unlet:
3686 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3687 arg = xp->xp_pattern + 1;
3688 xp->xp_context = EXPAND_USER_VARS;
3689 xp->xp_pattern = arg;
3690 break;
3691
3692 case CMD_function:
3693 case CMD_delfunction:
3694 xp->xp_context = EXPAND_USER_FUNC;
3695 xp->xp_pattern = arg;
3696 break;
3697
3698 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003699 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 break;
3701#endif
3702 case CMD_highlight:
3703 set_context_in_highlight_cmd(xp, arg);
3704 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003705#ifdef FEAT_CSCOPE
3706 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003707 case CMD_lcscope:
3708 case CMD_scscope:
3709 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003710 break;
3711#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003712#ifdef FEAT_SIGNS
3713 case CMD_sign:
3714 set_context_in_sign_cmd(xp, arg);
3715 break;
3716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717#ifdef FEAT_LISTCMDS
3718 case CMD_bdelete:
3719 case CMD_bwipeout:
3720 case CMD_bunload:
3721 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3722 arg = xp->xp_pattern + 1;
3723 /*FALLTHROUGH*/
3724 case CMD_buffer:
3725 case CMD_sbuffer:
3726 case CMD_checktime:
3727 xp->xp_context = EXPAND_BUFFERS;
3728 xp->xp_pattern = arg;
3729 break;
3730#endif
3731#ifdef FEAT_USR_CMDS
3732 case CMD_USER:
3733 case CMD_USER_BUF:
3734 if (compl != EXPAND_NOTHING)
3735 {
3736 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003737 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739# ifdef FEAT_MENU
3740 if (compl == EXPAND_MENUS)
3741 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3742# endif
3743 if (compl == EXPAND_COMMANDS)
3744 return arg;
3745 if (compl == EXPAND_MAPPINGS)
3746 return set_context_in_map_cmd(xp, (char_u *)"map",
3747 arg, forceit, FALSE, FALSE, CMD_map);
3748 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3749 arg = xp->xp_pattern + 1;
3750 xp->xp_pattern = arg;
3751 }
3752 xp->xp_context = compl;
3753 }
3754 break;
3755#endif
3756 case CMD_map: case CMD_noremap:
3757 case CMD_nmap: case CMD_nnoremap:
3758 case CMD_vmap: case CMD_vnoremap:
3759 case CMD_omap: case CMD_onoremap:
3760 case CMD_imap: case CMD_inoremap:
3761 case CMD_cmap: case CMD_cnoremap:
3762 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003763 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 case CMD_unmap:
3765 case CMD_nunmap:
3766 case CMD_vunmap:
3767 case CMD_ounmap:
3768 case CMD_iunmap:
3769 case CMD_cunmap:
3770 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003771 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case CMD_abbreviate: case CMD_noreabbrev:
3773 case CMD_cabbrev: case CMD_cnoreabbrev:
3774 case CMD_iabbrev: case CMD_inoreabbrev:
3775 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003776 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 case CMD_unabbreviate:
3778 case CMD_cunabbrev:
3779 case CMD_iunabbrev:
3780 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003781 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#ifdef FEAT_MENU
3783 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3784 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3785 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3786 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3787 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3788 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3789 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3790 case CMD_tmenu: case CMD_tunmenu:
3791 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3792 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3793#endif
3794
3795 case CMD_colorscheme:
3796 xp->xp_context = EXPAND_COLORS;
3797 xp->xp_pattern = arg;
3798 break;
3799
3800 case CMD_compiler:
3801 xp->xp_context = EXPAND_COMPILER;
3802 xp->xp_pattern = arg;
3803 break;
3804
3805#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3806 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3807 case CMD_language:
3808 if (*skiptowhite(arg) == NUL)
3809 {
3810 xp->xp_context = EXPAND_LANGUAGE;
3811 xp->xp_pattern = arg;
3812 }
3813 else
3814 xp->xp_context = EXPAND_NOTHING;
3815 break;
3816#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003817#if defined(FEAT_PROFILE)
3818 case CMD_profile:
3819 set_context_in_profile_cmd(xp, arg);
3820 break;
3821#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003822 case CMD_behave:
3823 xp->xp_context = EXPAND_BEHAVE;
3824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826#endif /* FEAT_CMDL_COMPL */
3827
3828 default:
3829 break;
3830 }
3831 return NULL;
3832}
3833
3834/*
3835 * skip a range specifier of the form: addr [,addr] [;addr] ..
3836 *
3837 * Backslashed delimiters after / or ? will be skipped, and commands will
3838 * not be expanded between /'s and ?'s or after "'".
3839 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003840 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * Returns the "cmd" pointer advanced to beyond the range.
3842 */
3843 char_u *
3844skip_range(cmd, ctx)
3845 char_u *cmd;
3846 int *ctx; /* pointer to xp_context or NULL */
3847{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003848 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
Bram Moolenaardf177f62005-02-22 08:39:57 +00003850 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 if (*cmd == '\'')
3853 {
3854 if (*++cmd == NUL && ctx != NULL)
3855 *ctx = EXPAND_NOTHING;
3856 }
3857 else if (*cmd == '/' || *cmd == '?')
3858 {
3859 delim = *cmd++;
3860 while (*cmd != NUL && *cmd != delim)
3861 if (*cmd++ == '\\' && *cmd != NUL)
3862 ++cmd;
3863 if (*cmd == NUL && ctx != NULL)
3864 *ctx = EXPAND_NOTHING;
3865 }
3866 if (*cmd != NUL)
3867 ++cmd;
3868 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003869
3870 /* Skip ":" and white space. */
3871 while (*cmd == ':')
3872 cmd = skipwhite(cmd + 1);
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return cmd;
3875}
3876
3877/*
3878 * get a single EX address
3879 *
3880 * Set ptr to the next character after the part that was interpreted.
3881 * Set ptr to NULL when an error is encountered.
3882 *
3883 * Return MAXLNUM when no Ex address was found.
3884 */
3885 static linenr_T
3886get_address(ptr, skip, to_other_file)
3887 char_u **ptr;
3888 int skip; /* only skip the address, don't use it */
3889 int to_other_file; /* flag: may jump to other file */
3890{
3891 int c;
3892 int i;
3893 long n;
3894 char_u *cmd;
3895 pos_T pos;
3896 pos_T *fp;
3897 linenr_T lnum;
3898
3899 cmd = skipwhite(*ptr);
3900 lnum = MAXLNUM;
3901 do
3902 {
3903 switch (*cmd)
3904 {
3905 case '.': /* '.' - Cursor position */
3906 ++cmd;
3907 lnum = curwin->w_cursor.lnum;
3908 break;
3909
3910 case '$': /* '$' - last line */
3911 ++cmd;
3912 lnum = curbuf->b_ml.ml_line_count;
3913 break;
3914
3915 case '\'': /* ''' - mark */
3916 if (*++cmd == NUL)
3917 {
3918 cmd = NULL;
3919 goto error;
3920 }
3921 if (skip)
3922 ++cmd;
3923 else
3924 {
3925 /* Only accept a mark in another file when it is
3926 * used by itself: ":'M". */
3927 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3928 ++cmd;
3929 if (fp == (pos_T *)-1)
3930 /* Jumped to another file. */
3931 lnum = curwin->w_cursor.lnum;
3932 else
3933 {
3934 if (check_mark(fp) == FAIL)
3935 {
3936 cmd = NULL;
3937 goto error;
3938 }
3939 lnum = fp->lnum;
3940 }
3941 }
3942 break;
3943
3944 case '/':
3945 case '?': /* '/' or '?' - search */
3946 c = *cmd++;
3947 if (skip) /* skip "/pat/" */
3948 {
3949 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3950 if (*cmd == c)
3951 ++cmd;
3952 }
3953 else
3954 {
3955 pos = curwin->w_cursor; /* save curwin->w_cursor */
3956 /*
3957 * When '/' or '?' follows another address, start
3958 * from there.
3959 */
3960 if (lnum != MAXLNUM)
3961 curwin->w_cursor.lnum = lnum;
3962 /*
3963 * Start a forward search at the end of the line.
3964 * Start a backward search at the start of the line.
3965 * This makes sure we never match in the current
3966 * line, and can match anywhere in the
3967 * next/previous line.
3968 */
3969 if (c == '/')
3970 curwin->w_cursor.col = MAXCOL;
3971 else
3972 curwin->w_cursor.col = 0;
3973 searchcmdlen = 0;
3974 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003975 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
3977 curwin->w_cursor = pos;
3978 cmd = NULL;
3979 goto error;
3980 }
3981 lnum = curwin->w_cursor.lnum;
3982 curwin->w_cursor = pos;
3983 /* adjust command string pointer */
3984 cmd += searchcmdlen;
3985 }
3986 break;
3987
3988 case '\\': /* "\?", "\/" or "\&", repeat search */
3989 ++cmd;
3990 if (*cmd == '&')
3991 i = RE_SUBST;
3992 else if (*cmd == '?' || *cmd == '/')
3993 i = RE_SEARCH;
3994 else
3995 {
3996 EMSG(_(e_backslash));
3997 cmd = NULL;
3998 goto error;
3999 }
4000
4001 if (!skip)
4002 {
4003 /*
4004 * When search follows another address, start from
4005 * there.
4006 */
4007 if (lnum != MAXLNUM)
4008 pos.lnum = lnum;
4009 else
4010 pos.lnum = curwin->w_cursor.lnum;
4011
4012 /*
4013 * Start the search just like for the above
4014 * do_search().
4015 */
4016 if (*cmd != '?')
4017 pos.col = MAXCOL;
4018 else
4019 pos.col = 0;
4020 if (searchit(curwin, curbuf, &pos,
4021 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004022 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004023 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 lnum = pos.lnum;
4025 else
4026 {
4027 cmd = NULL;
4028 goto error;
4029 }
4030 }
4031 ++cmd;
4032 break;
4033
4034 default:
4035 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4036 lnum = getdigits(&cmd);
4037 }
4038
4039 for (;;)
4040 {
4041 cmd = skipwhite(cmd);
4042 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4043 break;
4044
4045 if (lnum == MAXLNUM)
4046 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4047 if (VIM_ISDIGIT(*cmd))
4048 i = '+'; /* "number" is same as "+number" */
4049 else
4050 i = *cmd++;
4051 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4052 n = 1;
4053 else
4054 n = getdigits(&cmd);
4055 if (i == '-')
4056 lnum -= n;
4057 else
4058 lnum += n;
4059 }
4060 } while (*cmd == '/' || *cmd == '?');
4061
4062error:
4063 *ptr = cmd;
4064 return lnum;
4065}
4066
4067/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004068 * Get flags from an Ex command argument.
4069 */
4070 static void
4071get_flags(eap)
4072 exarg_T *eap;
4073{
4074 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4075 {
4076 if (*eap->arg == 'l')
4077 eap->flags |= EXFLAG_LIST;
4078 else if (*eap->arg == 'p')
4079 eap->flags |= EXFLAG_PRINT;
4080 else
4081 eap->flags |= EXFLAG_NR;
4082 eap->arg = skipwhite(eap->arg + 1);
4083 }
4084}
4085
4086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Function called for command which is Not Implemented. NI!
4088 */
4089 void
4090ex_ni(eap)
4091 exarg_T *eap;
4092{
4093 if (!eap->skip)
4094 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4095}
4096
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004097#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098/*
4099 * Function called for script command which is Not Implemented. NI!
4100 * Skips over ":perl <<EOF" constructs.
4101 */
4102 static void
4103ex_script_ni(eap)
4104 exarg_T *eap;
4105{
4106 if (!eap->skip)
4107 ex_ni(eap);
4108 else
4109 vim_free(script_get(eap, eap->arg));
4110}
4111#endif
4112
4113/*
4114 * Check range in Ex command for validity.
4115 * Return NULL when valid, error message when invalid.
4116 */
4117 static char_u *
4118invalid_range(eap)
4119 exarg_T *eap;
4120{
4121 if ( eap->line1 < 0
4122 || eap->line2 < 0
4123 || eap->line1 > eap->line2
4124 || ((eap->argt & RANGE)
4125 && !(eap->argt & NOTADR)
4126 && eap->line2 > curbuf->b_ml.ml_line_count
4127#ifdef FEAT_DIFF
4128 + (eap->cmdidx == CMD_diffget)
4129#endif
4130 ))
4131 return (char_u *)_(e_invrange);
4132 return NULL;
4133}
4134
4135/*
4136 * Correct the range for zero line number, if required.
4137 */
4138 static void
4139correct_range(eap)
4140 exarg_T *eap;
4141{
4142 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4143 {
4144 if (eap->line1 == 0)
4145 eap->line1 = 1;
4146 if (eap->line2 == 0)
4147 eap->line2 = 1;
4148 }
4149}
4150
Bram Moolenaar748bf032005-02-02 23:04:36 +00004151#ifdef FEAT_QUICKFIX
4152static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4153
4154/*
4155 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4156 * pattern. Otherwise return eap->arg.
4157 */
4158 static char_u *
4159skip_grep_pat(eap)
4160 exarg_T *eap;
4161{
4162 char_u *p = eap->arg;
4163
Bram Moolenaara37420f2006-02-04 22:37:47 +00004164 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4165 || eap->cmdidx == CMD_vimgrepadd
4166 || eap->cmdidx == CMD_lvimgrepadd
4167 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004168 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004169 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004170 if (p == NULL)
4171 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004172 }
4173 return p;
4174}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004175
4176/*
4177 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4178 * in the command line, so that things like % get expanded.
4179 */
4180 static char_u *
4181replace_makeprg(eap, p, cmdlinep)
4182 exarg_T *eap;
4183 char_u *p;
4184 char_u **cmdlinep;
4185{
4186 char_u *new_cmdline;
4187 char_u *program;
4188 char_u *pos;
4189 char_u *ptr;
4190 int len;
4191 int i;
4192
4193 /*
4194 * Don't do it when ":vimgrep" is used for ":grep".
4195 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004196 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4197 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4198 || eap->cmdidx == CMD_grepadd
4199 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004200 && !grep_internal(eap->cmdidx))
4201 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004202 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4203 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004204 {
4205 if (*curbuf->b_p_gp == NUL)
4206 program = p_gp;
4207 else
4208 program = curbuf->b_p_gp;
4209 }
4210 else
4211 {
4212 if (*curbuf->b_p_mp == NUL)
4213 program = p_mp;
4214 else
4215 program = curbuf->b_p_mp;
4216 }
4217
4218 p = skipwhite(p);
4219
4220 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4221 {
4222 /* replace $* by given arguments */
4223 i = 1;
4224 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4225 ++i;
4226 len = (int)STRLEN(p);
4227 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4228 if (new_cmdline == NULL)
4229 return NULL; /* out of memory */
4230 ptr = new_cmdline;
4231 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4232 {
4233 i = (int)(pos - program);
4234 STRNCPY(ptr, program, i);
4235 STRCPY(ptr += i, p);
4236 ptr += len;
4237 program = pos + 2;
4238 }
4239 STRCPY(ptr, program);
4240 }
4241 else
4242 {
4243 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4244 if (new_cmdline == NULL)
4245 return NULL; /* out of memory */
4246 STRCPY(new_cmdline, program);
4247 STRCAT(new_cmdline, " ");
4248 STRCAT(new_cmdline, p);
4249 }
4250 msg_make(p);
4251
4252 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4253 vim_free(*cmdlinep);
4254 *cmdlinep = new_cmdline;
4255 p = new_cmdline;
4256 }
4257 return p;
4258}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004259#endif
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261/*
4262 * Expand file name in Ex command argument.
4263 * Return FAIL for failure, OK otherwise.
4264 */
4265 int
4266expand_filename(eap, cmdlinep, errormsgp)
4267 exarg_T *eap;
4268 char_u **cmdlinep;
4269 char_u **errormsgp;
4270{
4271 int has_wildcards; /* need to expand wildcards */
4272 char_u *repl;
4273 int srclen;
4274 char_u *p;
4275 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004276 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar748bf032005-02-02 23:04:36 +00004278#ifdef FEAT_QUICKFIX
4279 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4280 p = skip_grep_pat(eap);
4281#else
4282 p = eap->arg;
4283#endif
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 /*
4286 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4287 * the file name contains a wildcard it should not cause expanding.
4288 * (it will be expanded anyway if there is a wildcard before replacing).
4289 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004290 has_wildcards = mch_has_wildcard(p);
4291 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004293#ifdef FEAT_EVAL
4294 /* Skip over `=expr`, wildcards in it are not expanded. */
4295 if (p[0] == '`' && p[1] == '=')
4296 {
4297 p += 2;
4298 (void)skip_expr(&p);
4299 if (*p == '`')
4300 ++p;
4301 continue;
4302 }
4303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 /*
4305 * Quick check if this cannot be the start of a special string.
4306 * Also removes backslash before '%', '#' and '<'.
4307 */
4308 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4309 {
4310 ++p;
4311 continue;
4312 }
4313
4314 /*
4315 * Try to find a match at this position.
4316 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004317 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4318 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (*errormsgp != NULL) /* error detected */
4320 return FAIL;
4321 if (repl == NULL) /* no match found */
4322 {
4323 p += srclen;
4324 continue;
4325 }
4326
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004327 /* Wildcards won't be expanded below, the replacement is taken
4328 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4329 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4330 {
4331 char_u *l = repl;
4332
4333 repl = expand_env_save(repl);
4334 vim_free(l);
4335 }
4336
Bram Moolenaar63b92542007-03-27 14:57:09 +00004337 /* Need to escape white space et al. with a backslash.
4338 * Don't do this for:
4339 * - replacement that already has been escaped: "##"
4340 * - shell commands (may have to use quotes instead).
4341 * - non-unix systems when there is a single argument (spaces don't
4342 * separate arguments then).
4343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004345 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 && eap->cmdidx != CMD_bang
4347 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004348 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004350 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004352 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#ifndef UNIX
4354 && !(eap->argt & NOSPC)
4355#endif
4356 )
4357 {
4358 char_u *l;
4359#ifdef BACKSLASH_IN_FILENAME
4360 /* Don't escape a backslash here, because rem_backslash() doesn't
4361 * remove it later. */
4362 static char_u *nobslash = (char_u *)" \t\"|";
4363# define ESCAPE_CHARS nobslash
4364#else
4365# define ESCAPE_CHARS escape_chars
4366#endif
4367
4368 for (l = repl; *l; ++l)
4369 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4370 {
4371 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4372 if (l != NULL)
4373 {
4374 vim_free(repl);
4375 repl = l;
4376 }
4377 break;
4378 }
4379 }
4380
4381 /* For a shell command a '!' must be escaped. */
4382 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004383 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 char_u *l;
4386
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004387 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (l != NULL)
4389 {
4390 vim_free(repl);
4391 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004392 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (strstr((char *)p_sh, "sh") != NULL)
4394 {
4395 l = vim_strsave_escaped(repl, (char_u *)"!");
4396 if (l != NULL)
4397 {
4398 vim_free(repl);
4399 repl = l;
4400 }
4401 }
4402 }
4403 }
4404
4405 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4406 vim_free(repl);
4407 if (p == NULL)
4408 return FAIL;
4409 }
4410
4411 /*
4412 * One file argument: Expand wildcards.
4413 * Don't do this with ":r !command" or ":w !command".
4414 */
4415 if ((eap->argt & NOSPC) && !eap->usefilter)
4416 {
4417 /*
4418 * May do this twice:
4419 * 1. Replace environment variables.
4420 * 2. Replace any other wildcards, remove backslashes.
4421 */
4422 for (n = 1; n <= 2; ++n)
4423 {
4424 if (n == 2)
4425 {
4426#ifdef UNIX
4427 /*
4428 * Only for Unix we check for more than one file name.
4429 * For other systems spaces are considered to be part
4430 * of the file name.
4431 * Only check here if there is no wildcard, otherwise
4432 * ExpandOne() will check for errors. This allows
4433 * ":e `ls ve*.c`" on Unix.
4434 */
4435 if (!has_wildcards)
4436 for (p = eap->arg; *p; ++p)
4437 {
4438 /* skip escaped characters */
4439 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4440 ++p;
4441 else if (vim_iswhite(*p))
4442 {
4443 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4444 return FAIL;
4445 }
4446 }
4447#endif
4448
4449 /*
4450 * Halve the number of backslashes (this is Vi compatible).
4451 * For Unix and OS/2, when wildcards are expanded, this is
4452 * done by ExpandOne() below.
4453 */
4454#if defined(UNIX) || defined(OS2)
4455 if (!has_wildcards)
4456#endif
4457 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
4460 if (has_wildcards)
4461 {
4462 if (n == 1)
4463 {
4464 /*
4465 * First loop: May expand environment variables. This
4466 * can be done much faster with expand_env() than with
4467 * something else (e.g., calling a shell).
4468 * After expanding environment variables, check again
4469 * if there are still wildcards present.
4470 */
4471 if (vim_strchr(eap->arg, '$') != NULL
4472 || vim_strchr(eap->arg, '~') != NULL)
4473 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004474 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004475 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 has_wildcards = mch_has_wildcard(NameBuff);
4477 p = NameBuff;
4478 }
4479 else
4480 p = NULL;
4481 }
4482 else /* n == 2 */
4483 {
4484 expand_T xpc;
4485
4486 ExpandInit(&xpc);
4487 xpc.xp_context = EXPAND_FILES;
4488 p = ExpandOne(&xpc, eap->arg, NULL,
4489 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4490 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (p == NULL)
4492 return FAIL;
4493 }
4494 if (p != NULL)
4495 {
4496 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4497 p, cmdlinep);
4498 if (n == 2) /* p came from ExpandOne() */
4499 vim_free(p);
4500 }
4501 }
4502 }
4503 }
4504 return OK;
4505}
4506
4507/*
4508 * Replace part of the command line, keeping eap->cmd, eap->arg and
4509 * eap->nextcmd correct.
4510 * "src" points to the part that is to be replaced, of length "srclen".
4511 * "repl" is the replacement string.
4512 * Returns a pointer to the character after the replaced string.
4513 * Returns NULL for failure.
4514 */
4515 static char_u *
4516repl_cmdline(eap, src, srclen, repl, cmdlinep)
4517 exarg_T *eap;
4518 char_u *src;
4519 int srclen;
4520 char_u *repl;
4521 char_u **cmdlinep;
4522{
4523 int len;
4524 int i;
4525 char_u *new_cmdline;
4526
4527 /*
4528 * The new command line is build in new_cmdline[].
4529 * First allocate it.
4530 * Careful: a "+cmd" argument may have been NUL terminated.
4531 */
4532 len = (int)STRLEN(repl);
4533 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004534 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4536 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4537 return NULL; /* out of memory! */
4538
4539 /*
4540 * Copy the stuff before the expanded part.
4541 * Copy the expanded stuff.
4542 * Copy what came after the expanded part.
4543 * Copy the next commands, if there are any.
4544 */
4545 i = (int)(src - *cmdlinep); /* length of part before match */
4546 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 mch_memmove(new_cmdline + i, repl, (size_t)len);
4549 i += len; /* remember the end of the string */
4550 STRCPY(new_cmdline + i, src + srclen);
4551 src = new_cmdline + i; /* remember where to continue */
4552
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004553 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
4555 i = (int)STRLEN(new_cmdline) + 1;
4556 STRCPY(new_cmdline + i, eap->nextcmd);
4557 eap->nextcmd = new_cmdline + i;
4558 }
4559 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4560 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4561 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4562 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4563 vim_free(*cmdlinep);
4564 *cmdlinep = new_cmdline;
4565
4566 return src;
4567}
4568
4569/*
4570 * Check for '|' to separate commands and '"' to start comments.
4571 */
4572 void
4573separate_nextcmd(eap)
4574 exarg_T *eap;
4575{
4576 char_u *p;
4577
Bram Moolenaar86b68352004-12-27 21:59:20 +00004578#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004579 p = skip_grep_pat(eap);
4580#else
4581 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004582#endif
4583
4584 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 if (*p == Ctrl_V)
4587 {
4588 if (eap->argt & (USECTRLV | XFILE))
4589 ++p; /* skip CTRL-V and next char */
4590 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004591 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004592 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (*p == NUL) /* stop at NUL after CTRL-V */
4594 break;
4595 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004596
4597#ifdef FEAT_EVAL
4598 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004599 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004600 {
4601 p += 2;
4602 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004603 }
4604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /* Check for '"': start of comment or '|': next command */
4607 /* :@" and :*" do not start a comment!
4608 * :redir @" doesn't either. */
4609 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4610 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4611 || p != eap->arg)
4612 && (eap->cmdidx != CMD_redir
4613 || p != eap->arg + 1 || p[-1] != '@'))
4614 || *p == '|' || *p == '\n')
4615 {
4616 /*
4617 * We remove the '\' before the '|', unless USECTRLV is used
4618 * AND 'b' is present in 'cpoptions'.
4619 */
4620 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4621 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4622 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004623 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 --p;
4625 }
4626 else
4627 {
4628 eap->nextcmd = check_nextcmd(p);
4629 *p = NUL;
4630 break;
4631 }
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4636 del_trailing_spaces(eap->arg);
4637}
4638
4639/*
4640 * get + command from ex argument
4641 */
4642 static char_u *
4643getargcmd(argp)
4644 char_u **argp;
4645{
4646 char_u *arg = *argp;
4647 char_u *command = NULL;
4648
4649 if (*arg == '+') /* +[command] */
4650 {
4651 ++arg;
4652 if (vim_isspace(*arg))
4653 command = dollar_command;
4654 else
4655 {
4656 command = arg;
4657 arg = skip_cmd_arg(command, TRUE);
4658 if (*arg != NUL)
4659 *arg++ = NUL; /* terminate command with NUL */
4660 }
4661
4662 arg = skipwhite(arg); /* skip over spaces */
4663 *argp = arg;
4664 }
4665 return command;
4666}
4667
4668/*
4669 * Find end of "+command" argument. Skip over "\ " and "\\".
4670 */
4671 static char_u *
4672skip_cmd_arg(p, rembs)
4673 char_u *p;
4674 int rembs; /* TRUE to halve the number of backslashes */
4675{
4676 while (*p && !vim_isspace(*p))
4677 {
4678 if (*p == '\\' && p[1] != NUL)
4679 {
4680 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004681 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
4683 ++p;
4684 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004685 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 return p;
4688}
4689
4690/*
4691 * Get "++opt=arg" argument.
4692 * Return FAIL or OK.
4693 */
4694 static int
4695getargopt(eap)
4696 exarg_T *eap;
4697{
4698 char_u *arg = eap->arg + 2;
4699 int *pp = NULL;
4700#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004701 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 char_u *p;
4703#endif
4704
4705 /* ":edit ++[no]bin[ary] file" */
4706 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4707 {
4708 if (*arg == 'n')
4709 {
4710 arg += 2;
4711 eap->force_bin = FORCE_NOBIN;
4712 }
4713 else
4714 eap->force_bin = FORCE_BIN;
4715 if (!checkforcmd(&arg, "binary", 3))
4716 return FAIL;
4717 eap->arg = skipwhite(arg);
4718 return OK;
4719 }
4720
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004721 /* ":read ++edit file" */
4722 if (STRNCMP(arg, "edit", 4) == 0)
4723 {
4724 eap->read_edit = TRUE;
4725 eap->arg = skipwhite(arg + 4);
4726 return OK;
4727 }
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (STRNCMP(arg, "ff", 2) == 0)
4730 {
4731 arg += 2;
4732 pp = &eap->force_ff;
4733 }
4734 else if (STRNCMP(arg, "fileformat", 10) == 0)
4735 {
4736 arg += 10;
4737 pp = &eap->force_ff;
4738 }
4739#ifdef FEAT_MBYTE
4740 else if (STRNCMP(arg, "enc", 3) == 0)
4741 {
4742 arg += 3;
4743 pp = &eap->force_enc;
4744 }
4745 else if (STRNCMP(arg, "encoding", 8) == 0)
4746 {
4747 arg += 8;
4748 pp = &eap->force_enc;
4749 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004750 else if (STRNCMP(arg, "bad", 3) == 0)
4751 {
4752 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004753 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#endif
4756
4757 if (pp == NULL || *arg != '=')
4758 return FAIL;
4759
4760 ++arg;
4761 *pp = (int)(arg - eap->cmd);
4762 arg = skip_cmd_arg(arg, FALSE);
4763 eap->arg = skipwhite(arg);
4764 *arg = NUL;
4765
4766#ifdef FEAT_MBYTE
4767 if (pp == &eap->force_ff)
4768 {
4769#endif
4770 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4771 return FAIL;
4772#ifdef FEAT_MBYTE
4773 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004774 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 /* Make 'fileencoding' lower case. */
4777 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4778 *p = TOLOWER_ASC(*p);
4779 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004780 else
4781 {
4782 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4783 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004784 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004785 if (STRICMP(p, "keep") == 0)
4786 eap->bad_char = BAD_KEEP;
4787 else if (STRICMP(p, "drop") == 0)
4788 eap->bad_char = BAD_DROP;
4789 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4790 eap->bad_char = *p;
4791 else
4792 return FAIL;
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794#endif
4795
4796 return OK;
4797}
4798
4799/*
4800 * ":abbreviate" and friends.
4801 */
4802 static void
4803ex_abbreviate(eap)
4804 exarg_T *eap;
4805{
4806 do_exmap(eap, TRUE); /* almost the same as mapping */
4807}
4808
4809/*
4810 * ":map" and friends.
4811 */
4812 static void
4813ex_map(eap)
4814 exarg_T *eap;
4815{
4816 /*
4817 * If we are sourcing .exrc or .vimrc in current directory we
4818 * print the mappings for security reasons.
4819 */
4820 if (secure)
4821 {
4822 secure = 2;
4823 msg_outtrans(eap->cmd);
4824 msg_putchar('\n');
4825 }
4826 do_exmap(eap, FALSE);
4827}
4828
4829/*
4830 * ":unmap" and friends.
4831 */
4832 static void
4833ex_unmap(eap)
4834 exarg_T *eap;
4835{
4836 do_exmap(eap, FALSE);
4837}
4838
4839/*
4840 * ":mapclear" and friends.
4841 */
4842 static void
4843ex_mapclear(eap)
4844 exarg_T *eap;
4845{
4846 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4847}
4848
4849/*
4850 * ":abclear" and friends.
4851 */
4852 static void
4853ex_abclear(eap)
4854 exarg_T *eap;
4855{
4856 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4857}
4858
4859#ifdef FEAT_AUTOCMD
4860 static void
4861ex_autocmd(eap)
4862 exarg_T *eap;
4863{
4864 /*
4865 * Disallow auto commands from .exrc and .vimrc in current
4866 * directory for security reasons.
4867 */
4868 if (secure)
4869 {
4870 secure = 2;
4871 eap->errmsg = e_curdir;
4872 }
4873 else if (eap->cmdidx == CMD_autocmd)
4874 do_autocmd(eap->arg, eap->forceit);
4875 else
4876 do_augroup(eap->arg, eap->forceit);
4877}
4878
4879/*
4880 * ":doautocmd": Apply the automatic commands to the current buffer.
4881 */
4882 static void
4883ex_doautocmd(eap)
4884 exarg_T *eap;
4885{
4886 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004887 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888}
4889#endif
4890
4891#ifdef FEAT_LISTCMDS
4892/*
4893 * :[N]bunload[!] [N] [bufname] unload buffer
4894 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4895 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4896 */
4897 static void
4898ex_bunload(eap)
4899 exarg_T *eap;
4900{
4901 eap->errmsg = do_bufdel(
4902 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4903 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4904 : DOBUF_UNLOAD, eap->arg,
4905 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4906}
4907
4908/*
4909 * :[N]buffer [N] to buffer N
4910 * :[N]sbuffer [N] to buffer N
4911 */
4912 static void
4913ex_buffer(eap)
4914 exarg_T *eap;
4915{
4916 if (*eap->arg)
4917 eap->errmsg = e_trailing;
4918 else
4919 {
4920 if (eap->addr_count == 0) /* default is current buffer */
4921 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4922 else
4923 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4924 }
4925}
4926
4927/*
4928 * :[N]bmodified [N] to next mod. buffer
4929 * :[N]sbmodified [N] to next mod. buffer
4930 */
4931 static void
4932ex_bmodified(eap)
4933 exarg_T *eap;
4934{
4935 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4936}
4937
4938/*
4939 * :[N]bnext [N] to next buffer
4940 * :[N]sbnext [N] split and to next buffer
4941 */
4942 static void
4943ex_bnext(eap)
4944 exarg_T *eap;
4945{
4946 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4947}
4948
4949/*
4950 * :[N]bNext [N] to previous buffer
4951 * :[N]bprevious [N] to previous buffer
4952 * :[N]sbNext [N] split and to previous buffer
4953 * :[N]sbprevious [N] split and to previous buffer
4954 */
4955 static void
4956ex_bprevious(eap)
4957 exarg_T *eap;
4958{
4959 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4960}
4961
4962/*
4963 * :brewind to first buffer
4964 * :bfirst to first buffer
4965 * :sbrewind split and to first buffer
4966 * :sbfirst split and to first buffer
4967 */
4968 static void
4969ex_brewind(eap)
4970 exarg_T *eap;
4971{
4972 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4973}
4974
4975/*
4976 * :blast to last buffer
4977 * :sblast split and to last buffer
4978 */
4979 static void
4980ex_blast(eap)
4981 exarg_T *eap;
4982{
4983 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4984}
4985#endif
4986
4987 int
4988ends_excmd(c)
4989 int c;
4990{
4991 return (c == NUL || c == '|' || c == '"' || c == '\n');
4992}
4993
4994#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4995 || defined(PROTO)
4996/*
4997 * Return the next command, after the first '|' or '\n'.
4998 * Return NULL if not found.
4999 */
5000 char_u *
5001find_nextcmd(p)
5002 char_u *p;
5003{
5004 while (*p != '|' && *p != '\n')
5005 {
5006 if (*p == NUL)
5007 return NULL;
5008 ++p;
5009 }
5010 return (p + 1);
5011}
5012#endif
5013
5014/*
5015 * Check if *p is a separator between Ex commands.
5016 * Return NULL if it isn't, (p + 1) if it is.
5017 */
5018 char_u *
5019check_nextcmd(p)
5020 char_u *p;
5021{
5022 p = skipwhite(p);
5023 if (*p == '|' || *p == '\n')
5024 return (p + 1);
5025 else
5026 return NULL;
5027}
5028
5029/*
5030 * - if there are more files to edit
5031 * - and this is the last window
5032 * - and forceit not used
5033 * - and not repeated twice on a row
5034 * return FAIL and give error message if 'message' TRUE
5035 * return OK otherwise
5036 */
5037 static int
5038check_more(message, forceit)
5039 int message; /* when FALSE check only, no messages */
5040 int forceit;
5041{
5042 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5043
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005044 if (!forceit && only_one_window()
5045 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 if (message)
5048 {
5049#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5050 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5051 {
5052 char_u buff[IOSIZE];
5053
5054 if (n == 1)
5055 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5056 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005057 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 _("%d more files to edit. Quit anyway?"), n);
5059 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5060 return OK;
5061 return FAIL;
5062 }
5063#endif
5064 if (n == 1)
5065 EMSG(_("E173: 1 more file to edit"));
5066 else
5067 EMSGN(_("E173: %ld more files to edit"), n);
5068 quitmore = 2; /* next try to quit is allowed */
5069 }
5070 return FAIL;
5071 }
5072 return OK;
5073}
5074
5075#ifdef FEAT_CMDL_COMPL
5076/*
5077 * Function given to ExpandGeneric() to obtain the list of command names.
5078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *
5080get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005081 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 int idx;
5083{
5084 if (idx >= (int)CMD_SIZE)
5085# ifdef FEAT_USR_CMDS
5086 return get_user_command_name(idx);
5087# else
5088 return NULL;
5089# endif
5090 return cmdnames[idx].cmd_name;
5091}
5092#endif
5093
5094#if defined(FEAT_USR_CMDS) || defined(PROTO)
5095static 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));
5096static void uc_list __ARGS((char_u *name, size_t name_len));
5097static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5098static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5099static 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));
5100
5101 static int
5102uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5103 char_u *name;
5104 size_t name_len;
5105 char_u *rep;
5106 long argt;
5107 long def;
5108 int flags;
5109 int compl;
5110 char_u *compl_arg;
5111 int force;
5112{
5113 ucmd_T *cmd = NULL;
5114 char_u *p;
5115 int i;
5116 int cmp = 1;
5117 char_u *rep_buf = NULL;
5118 garray_T *gap;
5119
Bram Moolenaar9c102382006-05-03 21:26:49 +00005120 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (rep_buf == NULL)
5122 {
5123 /* Can't replace termcodes - try using the string as is */
5124 rep_buf = vim_strsave(rep);
5125
5126 /* Give up if out of memory */
5127 if (rep_buf == NULL)
5128 return FAIL;
5129 }
5130
5131 /* get address of growarray: global or in curbuf */
5132 if (flags & UC_BUFFER)
5133 {
5134 gap = &curbuf->b_ucmds;
5135 if (gap->ga_itemsize == 0)
5136 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5137 }
5138 else
5139 gap = &ucmds;
5140
5141 /* Search for the command in the already defined commands. */
5142 for (i = 0; i < gap->ga_len; ++i)
5143 {
5144 size_t len;
5145
5146 cmd = USER_CMD_GA(gap, i);
5147 len = STRLEN(cmd->uc_name);
5148 cmp = STRNCMP(name, cmd->uc_name, name_len);
5149 if (cmp == 0)
5150 {
5151 if (name_len < len)
5152 cmp = -1;
5153 else if (name_len > len)
5154 cmp = 1;
5155 }
5156
5157 if (cmp == 0)
5158 {
5159 if (!force)
5160 {
5161 EMSG(_("E174: Command already exists: add ! to replace it"));
5162 goto fail;
5163 }
5164
5165 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005166 cmd->uc_rep = NULL;
5167#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5168 vim_free(cmd->uc_compl_arg);
5169 cmd->uc_compl_arg = NULL;
5170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172 }
5173
5174 /* Stop as soon as we pass the name to add */
5175 if (cmp < 0)
5176 break;
5177 }
5178
5179 /* Extend the array unless we're replacing an existing command */
5180 if (cmp != 0)
5181 {
5182 if (ga_grow(gap, 1) != OK)
5183 goto fail;
5184 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5185 goto fail;
5186
5187 cmd = USER_CMD_GA(gap, i);
5188 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5189
5190 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192 cmd->uc_name = p;
5193 }
5194
5195 cmd->uc_rep = rep_buf;
5196 cmd->uc_argt = argt;
5197 cmd->uc_def = def;
5198 cmd->uc_compl = compl;
5199#ifdef FEAT_EVAL
5200 cmd->uc_scriptID = current_SID;
5201# ifdef FEAT_CMDL_COMPL
5202 cmd->uc_compl_arg = compl_arg;
5203# endif
5204#endif
5205
5206 return OK;
5207
5208fail:
5209 vim_free(rep_buf);
5210#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5211 vim_free(compl_arg);
5212#endif
5213 return FAIL;
5214}
5215
5216/*
5217 * List of names for completion for ":command" with the EXPAND_ flag.
5218 * Must be alphabetical for completion.
5219 */
5220static struct
5221{
5222 int expand;
5223 char *name;
5224} command_complete[] =
5225{
5226 {EXPAND_AUGROUP, "augroup"},
5227 {EXPAND_BUFFERS, "buffer"},
5228 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005229#if defined(FEAT_CSCOPE)
5230 {EXPAND_CSCOPE, "cscope"},
5231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5233 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005234 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
5236 {EXPAND_DIRECTORIES, "dir"},
5237 {EXPAND_ENV_VARS, "environment"},
5238 {EXPAND_EVENTS, "event"},
5239 {EXPAND_EXPRESSION, "expression"},
5240 {EXPAND_FILES, "file"},
5241 {EXPAND_FUNCTIONS, "function"},
5242 {EXPAND_HELP, "help"},
5243 {EXPAND_HIGHLIGHT, "highlight"},
5244 {EXPAND_MAPPINGS, "mapping"},
5245 {EXPAND_MENUS, "menu"},
5246 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005247 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005248#if defined(FEAT_SIGNS)
5249 {EXPAND_SIGN, "sign"},
5250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {EXPAND_TAGS, "tag"},
5252 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5253 {EXPAND_USER_VARS, "var"},
5254 {0, NULL}
5255};
5256
5257 static void
5258uc_list(name, name_len)
5259 char_u *name;
5260 size_t name_len;
5261{
5262 int i, j;
5263 int found = FALSE;
5264 ucmd_T *cmd;
5265 int len;
5266 long a;
5267 garray_T *gap;
5268
5269 gap = &curbuf->b_ucmds;
5270 for (;;)
5271 {
5272 for (i = 0; i < gap->ga_len; ++i)
5273 {
5274 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005275 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /* Skip commands which don't match the requested prefix */
5278 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5279 continue;
5280
5281 /* Put out the title first time */
5282 if (!found)
5283 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5284 found = TRUE;
5285 msg_putchar('\n');
5286 if (got_int)
5287 break;
5288
5289 /* Special cases */
5290 msg_putchar(a & BANG ? '!' : ' ');
5291 msg_putchar(a & REGSTR ? '"' : ' ');
5292 msg_putchar(gap != &ucmds ? 'b' : ' ');
5293 msg_putchar(' ');
5294
5295 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5296 len = (int)STRLEN(cmd->uc_name) + 4;
5297
5298 do {
5299 msg_putchar(' ');
5300 ++len;
5301 } while (len < 16);
5302
5303 len = 0;
5304
5305 /* Arguments */
5306 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5307 {
5308 case 0: IObuff[len++] = '0'; break;
5309 case (EXTRA): IObuff[len++] = '*'; break;
5310 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5311 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5312 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5313 }
5314
5315 do {
5316 IObuff[len++] = ' ';
5317 } while (len < 5);
5318
5319 /* Range */
5320 if (a & (RANGE|COUNT))
5321 {
5322 if (a & COUNT)
5323 {
5324 /* -count=N */
5325 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5326 len += (int)STRLEN(IObuff + len);
5327 }
5328 else if (a & DFLALL)
5329 IObuff[len++] = '%';
5330 else if (cmd->uc_def >= 0)
5331 {
5332 /* -range=N */
5333 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5334 len += (int)STRLEN(IObuff + len);
5335 }
5336 else
5337 IObuff[len++] = '.';
5338 }
5339
5340 do {
5341 IObuff[len++] = ' ';
5342 } while (len < 11);
5343
5344 /* Completion */
5345 for (j = 0; command_complete[j].expand != 0; ++j)
5346 if (command_complete[j].expand == cmd->uc_compl)
5347 {
5348 STRCPY(IObuff + len, command_complete[j].name);
5349 len += (int)STRLEN(IObuff + len);
5350 break;
5351 }
5352
5353 do {
5354 IObuff[len++] = ' ';
5355 } while (len < 21);
5356
5357 IObuff[len] = '\0';
5358 msg_outtrans(IObuff);
5359
5360 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005361#ifdef FEAT_EVAL
5362 if (p_verbose > 0)
5363 last_set_msg(cmd->uc_scriptID);
5364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 out_flush();
5366 ui_breakcheck();
5367 if (got_int)
5368 break;
5369 }
5370 if (gap == &ucmds || i < gap->ga_len)
5371 break;
5372 gap = &ucmds;
5373 }
5374
5375 if (!found)
5376 MSG(_("No user-defined commands found"));
5377}
5378
5379 static char_u *
5380uc_fun_cmd()
5381{
5382 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5383 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5384 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5385 0xb9, 0x7f, 0};
5386 int i;
5387
5388 for (i = 0; fcmd[i]; ++i)
5389 IObuff[i] = fcmd[i] - 0x40;
5390 IObuff[i] = 0;
5391 return IObuff;
5392}
5393
5394 static int
5395uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5396 char_u *attr;
5397 size_t len;
5398 long *argt;
5399 long *def;
5400 int *flags;
5401 int *compl;
5402 char_u **compl_arg;
5403{
5404 char_u *p;
5405
5406 if (len == 0)
5407 {
5408 EMSG(_("E175: No attribute specified"));
5409 return FAIL;
5410 }
5411
5412 /* First, try the simple attributes (no arguments) */
5413 if (STRNICMP(attr, "bang", len) == 0)
5414 *argt |= BANG;
5415 else if (STRNICMP(attr, "buffer", len) == 0)
5416 *flags |= UC_BUFFER;
5417 else if (STRNICMP(attr, "register", len) == 0)
5418 *argt |= REGSTR;
5419 else if (STRNICMP(attr, "bar", len) == 0)
5420 *argt |= TRLBAR;
5421 else
5422 {
5423 int i;
5424 char_u *val = NULL;
5425 size_t vallen = 0;
5426 size_t attrlen = len;
5427
5428 /* Look for the attribute name - which is the part before any '=' */
5429 for (i = 0; i < (int)len; ++i)
5430 {
5431 if (attr[i] == '=')
5432 {
5433 val = &attr[i + 1];
5434 vallen = len - i - 1;
5435 attrlen = i;
5436 break;
5437 }
5438 }
5439
5440 if (STRNICMP(attr, "nargs", attrlen) == 0)
5441 {
5442 if (vallen == 1)
5443 {
5444 if (*val == '0')
5445 /* Do nothing - this is the default */;
5446 else if (*val == '1')
5447 *argt |= (EXTRA | NOSPC | NEEDARG);
5448 else if (*val == '*')
5449 *argt |= EXTRA;
5450 else if (*val == '?')
5451 *argt |= (EXTRA | NOSPC);
5452 else if (*val == '+')
5453 *argt |= (EXTRA | NEEDARG);
5454 else
5455 goto wrong_nargs;
5456 }
5457 else
5458 {
5459wrong_nargs:
5460 EMSG(_("E176: Invalid number of arguments"));
5461 return FAIL;
5462 }
5463 }
5464 else if (STRNICMP(attr, "range", attrlen) == 0)
5465 {
5466 *argt |= RANGE;
5467 if (vallen == 1 && *val == '%')
5468 *argt |= DFLALL;
5469 else if (val != NULL)
5470 {
5471 p = val;
5472 if (*def >= 0)
5473 {
5474two_count:
5475 EMSG(_("E177: Count cannot be specified twice"));
5476 return FAIL;
5477 }
5478
5479 *def = getdigits(&p);
5480 *argt |= (ZEROR | NOTADR);
5481
5482 if (p != val + vallen || vallen == 0)
5483 {
5484invalid_count:
5485 EMSG(_("E178: Invalid default value for count"));
5486 return FAIL;
5487 }
5488 }
5489 }
5490 else if (STRNICMP(attr, "count", attrlen) == 0)
5491 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005492 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
5494 if (val != NULL)
5495 {
5496 p = val;
5497 if (*def >= 0)
5498 goto two_count;
5499
5500 *def = getdigits(&p);
5501
5502 if (p != val + vallen)
5503 goto invalid_count;
5504 }
5505
5506 if (*def < 0)
5507 *def = 0;
5508 }
5509 else if (STRNICMP(attr, "complete", attrlen) == 0)
5510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 if (val == NULL)
5512 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005513 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 return FAIL;
5515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005517 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5518 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
5521 else
5522 {
5523 char_u ch = attr[len];
5524 attr[len] = '\0';
5525 EMSG2(_("E181: Invalid attribute: %s"), attr);
5526 attr[len] = ch;
5527 return FAIL;
5528 }
5529 }
5530
5531 return OK;
5532}
5533
Bram Moolenaara850a712009-01-28 14:42:59 +00005534/*
5535 * ":command ..."
5536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 static void
5538ex_command(eap)
5539 exarg_T *eap;
5540{
5541 char_u *name;
5542 char_u *end;
5543 char_u *p;
5544 long argt = 0;
5545 long def = -1;
5546 int flags = 0;
5547 int compl = EXPAND_NOTHING;
5548 char_u *compl_arg = NULL;
5549 int has_attr = (eap->arg[0] == '-');
5550
5551 p = eap->arg;
5552
5553 /* Check for attributes */
5554 while (*p == '-')
5555 {
5556 ++p;
5557 end = skiptowhite(p);
5558 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5559 == FAIL)
5560 return;
5561 p = skipwhite(end);
5562 }
5563
5564 /* Get the name (if any) and skip to the following argument */
5565 name = p;
5566 if (ASCII_ISALPHA(*p))
5567 while (ASCII_ISALNUM(*p))
5568 ++p;
5569 if (!ends_excmd(*p) && !vim_iswhite(*p))
5570 {
5571 EMSG(_("E182: Invalid command name"));
5572 return;
5573 }
5574 end = p;
5575
5576 /* If there is nothing after the name, and no attributes were specified,
5577 * we are listing commands
5578 */
5579 p = skipwhite(end);
5580 if (!has_attr && ends_excmd(*p))
5581 {
5582 uc_list(name, end - name);
5583 }
5584 else if (!ASCII_ISUPPER(*name))
5585 {
5586 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5587 return;
5588 }
5589 else
5590 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5591 eap->forceit);
5592}
5593
5594/*
5595 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 * Clear all user commands, global and for current buffer.
5597 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005598 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005600 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602 uc_clear(&ucmds);
5603 uc_clear(&curbuf->b_ucmds);
5604}
5605
5606/*
5607 * Clear all user commands for "gap".
5608 */
5609 void
5610uc_clear(gap)
5611 garray_T *gap;
5612{
5613 int i;
5614 ucmd_T *cmd;
5615
5616 for (i = 0; i < gap->ga_len; ++i)
5617 {
5618 cmd = USER_CMD_GA(gap, i);
5619 vim_free(cmd->uc_name);
5620 vim_free(cmd->uc_rep);
5621# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5622 vim_free(cmd->uc_compl_arg);
5623# endif
5624 }
5625 ga_clear(gap);
5626}
5627
5628 static void
5629ex_delcommand(eap)
5630 exarg_T *eap;
5631{
5632 int i = 0;
5633 ucmd_T *cmd = NULL;
5634 int cmp = -1;
5635 garray_T *gap;
5636
5637 gap = &curbuf->b_ucmds;
5638 for (;;)
5639 {
5640 for (i = 0; i < gap->ga_len; ++i)
5641 {
5642 cmd = USER_CMD_GA(gap, i);
5643 cmp = STRCMP(eap->arg, cmd->uc_name);
5644 if (cmp <= 0)
5645 break;
5646 }
5647 if (gap == &ucmds || cmp == 0)
5648 break;
5649 gap = &ucmds;
5650 }
5651
5652 if (cmp != 0)
5653 {
5654 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5655 return;
5656 }
5657
5658 vim_free(cmd->uc_name);
5659 vim_free(cmd->uc_rep);
5660# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5661 vim_free(cmd->uc_compl_arg);
5662# endif
5663
5664 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 if (i < gap->ga_len)
5667 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5668}
5669
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005670/*
5671 * split and quote args for <f-args>
5672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 static char_u *
5674uc_split_args(arg, lenp)
5675 char_u *arg;
5676 size_t *lenp;
5677{
5678 char_u *buf;
5679 char_u *p;
5680 char_u *q;
5681 int len;
5682
5683 /* Precalculate length */
5684 p = arg;
5685 len = 2; /* Initial and final quotes */
5686
5687 while (*p)
5688 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005689 if (p[0] == '\\' && p[1] == '\\')
5690 {
5691 len += 2;
5692 p += 2;
5693 }
5694 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
5696 len += 1;
5697 p += 2;
5698 }
5699 else if (*p == '\\' || *p == '"')
5700 {
5701 len += 2;
5702 p += 1;
5703 }
5704 else if (vim_iswhite(*p))
5705 {
5706 p = skipwhite(p);
5707 if (*p == NUL)
5708 break;
5709 len += 3; /* "," */
5710 }
5711 else
5712 {
5713 ++len;
5714 ++p;
5715 }
5716 }
5717
5718 buf = alloc(len + 1);
5719 if (buf == NULL)
5720 {
5721 *lenp = 0;
5722 return buf;
5723 }
5724
5725 p = arg;
5726 q = buf;
5727 *q++ = '"';
5728 while (*p)
5729 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005730 if (p[0] == '\\' && p[1] == '\\')
5731 {
5732 *q++ = '\\';
5733 *q++ = '\\';
5734 p += 2;
5735 }
5736 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
5738 *q++ = p[1];
5739 p += 2;
5740 }
5741 else if (*p == '\\' || *p == '"')
5742 {
5743 *q++ = '\\';
5744 *q++ = *p++;
5745 }
5746 else if (vim_iswhite(*p))
5747 {
5748 p = skipwhite(p);
5749 if (*p == NUL)
5750 break;
5751 *q++ = '"';
5752 *q++ = ',';
5753 *q++ = '"';
5754 }
5755 else
5756 {
5757 *q++ = *p++;
5758 }
5759 }
5760 *q++ = '"';
5761 *q = 0;
5762
5763 *lenp = len;
5764 return buf;
5765}
5766
5767/*
5768 * Check for a <> code in a user command.
5769 * "code" points to the '<'. "len" the length of the <> (inclusive).
5770 * "buf" is where the result is to be added.
5771 * "split_buf" points to a buffer used for splitting, caller should free it.
5772 * "split_len" is the length of what "split_buf" contains.
5773 * Returns the length of the replacement, which has been added to "buf".
5774 * Returns -1 if there was no match, and only the "<" has been copied.
5775 */
5776 static size_t
5777uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5778 char_u *code;
5779 size_t len;
5780 char_u *buf;
5781 ucmd_T *cmd; /* the user command we're expanding */
5782 exarg_T *eap; /* ex arguments */
5783 char_u **split_buf;
5784 size_t *split_len;
5785{
5786 size_t result = 0;
5787 char_u *p = code + 1;
5788 size_t l = len - 2;
5789 int quote = 0;
5790 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5791 ct_LT, ct_NONE } type = ct_NONE;
5792
5793 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5794 {
5795 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5796 p += 2;
5797 l -= 2;
5798 }
5799
Bram Moolenaar371d5402006-03-20 21:47:49 +00005800 ++l;
5801 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005803 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005805 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005807 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005809 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005811 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005813 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005815 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 type = ct_REGISTER;
5817
5818 switch (type)
5819 {
5820 case ct_ARGS:
5821 /* Simple case first */
5822 if (*eap->arg == NUL)
5823 {
5824 if (quote == 1)
5825 {
5826 result = 2;
5827 if (buf != NULL)
5828 STRCPY(buf, "''");
5829 }
5830 else
5831 result = 0;
5832 break;
5833 }
5834
5835 /* When specified there is a single argument don't split it.
5836 * Works for ":Cmd %" when % is "a b c". */
5837 if ((eap->argt & NOSPC) && quote == 2)
5838 quote = 1;
5839
5840 switch (quote)
5841 {
5842 case 0: /* No quoting, no splitting */
5843 result = STRLEN(eap->arg);
5844 if (buf != NULL)
5845 STRCPY(buf, eap->arg);
5846 break;
5847 case 1: /* Quote, but don't split */
5848 result = STRLEN(eap->arg) + 2;
5849 for (p = eap->arg; *p; ++p)
5850 {
5851 if (*p == '\\' || *p == '"')
5852 ++result;
5853 }
5854
5855 if (buf != NULL)
5856 {
5857 *buf++ = '"';
5858 for (p = eap->arg; *p; ++p)
5859 {
5860 if (*p == '\\' || *p == '"')
5861 *buf++ = '\\';
5862 *buf++ = *p;
5863 }
5864 *buf = '"';
5865 }
5866
5867 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005868 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 /* This is hard, so only do it once, and cache the result */
5870 if (*split_buf == NULL)
5871 *split_buf = uc_split_args(eap->arg, split_len);
5872
5873 result = *split_len;
5874 if (buf != NULL && result != 0)
5875 STRCPY(buf, *split_buf);
5876
5877 break;
5878 }
5879 break;
5880
5881 case ct_BANG:
5882 result = eap->forceit ? 1 : 0;
5883 if (quote)
5884 result += 2;
5885 if (buf != NULL)
5886 {
5887 if (quote)
5888 *buf++ = '"';
5889 if (eap->forceit)
5890 *buf++ = '!';
5891 if (quote)
5892 *buf = '"';
5893 }
5894 break;
5895
5896 case ct_LINE1:
5897 case ct_LINE2:
5898 case ct_COUNT:
5899 {
5900 char num_buf[20];
5901 long num = (type == ct_LINE1) ? eap->line1 :
5902 (type == ct_LINE2) ? eap->line2 :
5903 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5904 size_t num_len;
5905
5906 sprintf(num_buf, "%ld", num);
5907 num_len = STRLEN(num_buf);
5908 result = num_len;
5909
5910 if (quote)
5911 result += 2;
5912
5913 if (buf != NULL)
5914 {
5915 if (quote)
5916 *buf++ = '"';
5917 STRCPY(buf, num_buf);
5918 buf += num_len;
5919 if (quote)
5920 *buf = '"';
5921 }
5922
5923 break;
5924 }
5925
5926 case ct_REGISTER:
5927 result = eap->regname ? 1 : 0;
5928 if (quote)
5929 result += 2;
5930 if (buf != NULL)
5931 {
5932 if (quote)
5933 *buf++ = '\'';
5934 if (eap->regname)
5935 *buf++ = eap->regname;
5936 if (quote)
5937 *buf = '\'';
5938 }
5939 break;
5940
5941 case ct_LT:
5942 result = 1;
5943 if (buf != NULL)
5944 *buf = '<';
5945 break;
5946
5947 default:
5948 /* Not recognized: just copy the '<' and return -1. */
5949 result = (size_t)-1;
5950 if (buf != NULL)
5951 *buf = '<';
5952 break;
5953 }
5954
5955 return result;
5956}
5957
5958 static void
5959do_ucmd(eap)
5960 exarg_T *eap;
5961{
5962 char_u *buf;
5963 char_u *p;
5964 char_u *q;
5965
5966 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005967 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005968 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 size_t len, totlen;
5970
5971 size_t split_len = 0;
5972 char_u *split_buf = NULL;
5973 ucmd_T *cmd;
5974#ifdef FEAT_EVAL
5975 scid_T save_current_SID = current_SID;
5976#endif
5977
5978 if (eap->cmdidx == CMD_USER)
5979 cmd = USER_CMD(eap->useridx);
5980 else
5981 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5982
5983 /*
5984 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00005985 * First round: "buf" is NULL, compute length, allocate "buf".
5986 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 */
5988 buf = NULL;
5989 for (;;)
5990 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005991 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005992 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00005994
5995 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005997 start = vim_strchr(p, '<');
5998 if (start != NULL)
5999 end = vim_strchr(start + 1, '>');
6000 if (buf != NULL)
6001 {
6002 ksp = vim_strchr(p, K_SPECIAL);
6003 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6004 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6005# ifdef FEAT_GUI
6006 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6007# endif
6008 ))
6009 {
6010 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6011 * KS_SPECIAL KE_FILLER, like for mappings, but
6012 * do_cmdline() doesn't handle that, so convert it back.
6013 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6014 len = ksp - p;
6015 if (len > 0)
6016 {
6017 mch_memmove(q, p, len);
6018 q += len;
6019 }
6020 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6021 p = ksp + 3;
6022 continue;
6023 }
6024 }
6025
6026 /* break if there no <item> is found */
6027 if (start == NULL || end == NULL)
6028 break;
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 /* Include the '>' */
6031 ++end;
6032
6033 /* Take everything up to the '<' */
6034 len = start - p;
6035 if (buf == NULL)
6036 totlen += len;
6037 else
6038 {
6039 mch_memmove(q, p, len);
6040 q += len;
6041 }
6042
6043 len = uc_check_code(start, end - start, q, cmd, eap,
6044 &split_buf, &split_len);
6045 if (len == (size_t)-1)
6046 {
6047 /* no match, continue after '<' */
6048 p = start + 1;
6049 len = 1;
6050 }
6051 else
6052 p = end;
6053 if (buf == NULL)
6054 totlen += len;
6055 else
6056 q += len;
6057 }
6058 if (buf != NULL) /* second time here, finished */
6059 {
6060 STRCPY(q, p);
6061 break;
6062 }
6063
6064 totlen += STRLEN(p); /* Add on the trailing characters */
6065 buf = alloc((unsigned)(totlen + 1));
6066 if (buf == NULL)
6067 {
6068 vim_free(split_buf);
6069 return;
6070 }
6071 }
6072
6073#ifdef FEAT_EVAL
6074 current_SID = cmd->uc_scriptID;
6075#endif
6076 (void)do_cmdline(buf, eap->getline, eap->cookie,
6077 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6078#ifdef FEAT_EVAL
6079 current_SID = save_current_SID;
6080#endif
6081 vim_free(buf);
6082 vim_free(split_buf);
6083}
6084
6085# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6086 static char_u *
6087get_user_command_name(idx)
6088 int idx;
6089{
6090 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6091}
6092
6093/*
6094 * Function given to ExpandGeneric() to obtain the list of user command names.
6095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 char_u *
6097get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006098 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 int idx;
6100{
6101 if (idx < curbuf->b_ucmds.ga_len)
6102 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6103 idx -= curbuf->b_ucmds.ga_len;
6104 if (idx < ucmds.ga_len)
6105 return USER_CMD(idx)->uc_name;
6106 return NULL;
6107}
6108
6109/*
6110 * Function given to ExpandGeneric() to obtain the list of user command
6111 * attributes.
6112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 char_u *
6114get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006115 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int idx;
6117{
6118 static char *user_cmd_flags[] =
6119 {"bang", "bar", "buffer", "complete", "count",
6120 "nargs", "range", "register"};
6121
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006122 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 return NULL;
6124 return (char_u *)user_cmd_flags[idx];
6125}
6126
6127/*
6128 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 char_u *
6131get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006132 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 int idx;
6134{
6135 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6136
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006137 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 return NULL;
6139 return (char_u *)user_cmd_nargs[idx];
6140}
6141
6142/*
6143 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 char_u *
6146get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006147 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 int idx;
6149{
6150 return (char_u *)command_complete[idx].name;
6151}
6152# endif /* FEAT_CMDL_COMPL */
6153
6154#endif /* FEAT_USR_CMDS */
6155
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006156#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6157/*
6158 * Parse a completion argument "value[vallen]".
6159 * The detected completion goes in "*complp", argument type in "*argt".
6160 * When there is an argument, for function and user defined completion, it's
6161 * copied to allocated memory and stored in "*compl_arg".
6162 * Returns FAIL if something is wrong.
6163 */
6164 int
6165parse_compl_arg(value, vallen, complp, argt, compl_arg)
6166 char_u *value;
6167 int vallen;
6168 int *complp;
6169 long *argt;
6170 char_u **compl_arg;
6171{
6172 char_u *arg = NULL;
6173 size_t arglen = 0;
6174 int i;
6175 int valend = vallen;
6176
6177 /* Look for any argument part - which is the part after any ',' */
6178 for (i = 0; i < vallen; ++i)
6179 {
6180 if (value[i] == ',')
6181 {
6182 arg = &value[i + 1];
6183 arglen = vallen - i - 1;
6184 valend = i;
6185 break;
6186 }
6187 }
6188
6189 for (i = 0; command_complete[i].expand != 0; ++i)
6190 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006191 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006192 && STRNCMP(value, command_complete[i].name, valend) == 0)
6193 {
6194 *complp = command_complete[i].expand;
6195 if (command_complete[i].expand == EXPAND_BUFFERS)
6196 *argt |= BUFNAME;
6197 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6198 || command_complete[i].expand == EXPAND_FILES)
6199 *argt |= XFILE;
6200 break;
6201 }
6202 }
6203
6204 if (command_complete[i].expand == 0)
6205 {
6206 EMSG2(_("E180: Invalid complete value: %s"), value);
6207 return FAIL;
6208 }
6209
6210# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6211 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6212 && arg != NULL)
6213# else
6214 if (arg != NULL)
6215# endif
6216 {
6217 EMSG(_("E468: Completion argument only allowed for custom completion"));
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 {
6225 EMSG(_("E467: Custom completion requires a function argument"));
6226 return FAIL;
6227 }
6228
6229 if (arg != NULL)
6230 *compl_arg = vim_strnsave(arg, (int)arglen);
6231# endif
6232 return OK;
6233}
6234#endif
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 static void
6237ex_colorscheme(eap)
6238 exarg_T *eap;
6239{
Bram Moolenaare6850792010-05-14 15:28:44 +02006240 if (*eap->arg == NUL)
6241 {
6242#ifdef FEAT_EVAL
6243 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6244 char_u *p = NULL;
6245
6246 if (expr != NULL)
6247 {
6248 ++emsg_off;
6249 p = eval_to_string(expr, NULL, FALSE);
6250 --emsg_off;
6251 vim_free(expr);
6252 }
6253 if (p != NULL)
6254 {
6255 MSG(p);
6256 vim_free(p);
6257 }
6258 else
6259 MSG("default");
6260#else
6261 MSG(_("unknown"));
6262#endif
6263 }
6264 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6266}
6267
6268 static void
6269ex_highlight(eap)
6270 exarg_T *eap;
6271{
6272 if (*eap->arg == NUL && eap->cmd[2] == '!')
6273 MSG(_("Greetings, Vim user!"));
6274 do_highlight(eap->arg, eap->forceit, FALSE);
6275}
6276
6277
6278/*
6279 * Call this function if we thought we were going to exit, but we won't
6280 * (because of an error). May need to restore the terminal mode.
6281 */
6282 void
6283not_exiting()
6284{
6285 exiting = FALSE;
6286 settmode(TMODE_RAW);
6287}
6288
6289/*
6290 * ":quit": quit current window, quit Vim if closed the last window.
6291 */
6292 static void
6293ex_quit(eap)
6294 exarg_T *eap;
6295{
6296#ifdef FEAT_CMDWIN
6297 if (cmdwin_type != 0)
6298 {
6299 cmdwin_result = Ctrl_C;
6300 return;
6301 }
6302#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006303 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006304 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006305 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006306 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006307 return;
6308 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006309#ifdef FEAT_AUTOCMD
6310 if (curbuf_locked())
6311 return;
6312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314#ifdef FEAT_NETBEANS_INTG
6315 netbeansForcedQuit = eap->forceit;
6316#endif
6317
6318 /*
6319 * If there are more files or windows we won't exit.
6320 */
6321 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6322 exiting = TRUE;
6323 if ((!P_HID(curbuf)
6324 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6325 || check_more(TRUE, eap->forceit) == FAIL
6326 || (only_one_window() && check_changed_any(eap->forceit)))
6327 {
6328 not_exiting();
6329 }
6330 else
6331 {
6332#ifdef FEAT_WINDOWS
6333 if (only_one_window()) /* quit last window */
6334#endif
6335 getout(0);
6336#ifdef FEAT_WINDOWS
6337# ifdef FEAT_GUI
6338 need_mouse_correct = TRUE;
6339# endif
6340 /* close window; may free buffer */
6341 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6342#endif
6343 }
6344}
6345
6346/*
6347 * ":cquit".
6348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 static void
6350ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006351 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 getout(1); /* this does not always pass on the exit code to the Manx
6354 compiler. why? */
6355}
6356
6357/*
6358 * ":qall": try to quit all windows
6359 */
6360 static void
6361ex_quit_all(eap)
6362 exarg_T *eap;
6363{
6364# ifdef FEAT_CMDWIN
6365 if (cmdwin_type != 0)
6366 {
6367 if (eap->forceit)
6368 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6369 else
6370 cmdwin_result = K_XF2;
6371 return;
6372 }
6373# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006374
6375 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006376 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006377 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006378 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006379 return;
6380 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006381#ifdef FEAT_AUTOCMD
6382 if (curbuf_locked())
6383 return;
6384#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 exiting = TRUE;
6387 if (eap->forceit || !check_changed_any(FALSE))
6388 getout(0);
6389 not_exiting();
6390}
6391
Bram Moolenaard9967712006-03-11 21:18:15 +00006392#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393/*
6394 * ":close": close current window, unless it is the last one
6395 */
6396 static void
6397ex_close(eap)
6398 exarg_T *eap;
6399{
6400# ifdef FEAT_CMDWIN
6401 if (cmdwin_type != 0)
6402 cmdwin_result = K_IGNORE;
6403 else
6404# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006405 if (!text_locked()
6406#ifdef FEAT_AUTOCMD
6407 && !curbuf_locked()
6408#endif
6409 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006410 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411}
6412
Bram Moolenaard9967712006-03-11 21:18:15 +00006413# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414/*
6415 * ":pclose": Close any preview window.
6416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006418ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006420{
6421 win_T *win;
6422
6423 for (win = firstwin; win != NULL; win = win->w_next)
6424 if (win->w_p_pvw)
6425 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006426 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006427 break;
6428 }
6429}
Bram Moolenaard9967712006-03-11 21:18:15 +00006430# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431
Bram Moolenaarf740b292006-02-16 22:11:02 +00006432/*
6433 * Close window "win" and take care of handling closing the last window for a
6434 * modified buffer.
6435 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006437ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006440 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 int need_hide;
6443 buf_T *buf = win->w_buffer;
6444
6445 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006448# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if ((p_confirm || cmdmod.confirm) && p_write)
6450 {
6451 dialog_changed(buf, FALSE);
6452 if (buf_valid(buf) && bufIsChanged(buf))
6453 return;
6454 need_hide = FALSE;
6455 }
6456 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
6459 EMSG(_(e_nowrtmsg));
6460 return;
6461 }
6462 }
6463
Bram Moolenaard9967712006-03-11 21:18:15 +00006464# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006466# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006467
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006469 if (tp == NULL)
6470 win_close(win, !need_hide && !P_HID(buf));
6471 else
6472 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006476 * ":tabclose": close current tab page, unless it is the last one.
6477 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 */
6479 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 exarg_T *eap;
6482{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006483 tabpage_T *tp;
6484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006485# ifdef FEAT_CMDWIN
6486 if (cmdwin_type != 0)
6487 cmdwin_result = K_IGNORE;
6488 else
6489# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006490 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006491 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006494 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006496 tp = find_tabpage((int)eap->line2);
6497 if (tp == NULL)
6498 {
6499 beep_flush();
6500 return;
6501 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006502 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006503 {
6504 tabpage_close_other(tp, eap->forceit);
6505 return;
6506 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006508 if (!text_locked()
6509#ifdef FEAT_AUTOCMD
6510 && !curbuf_locked()
6511#endif
6512 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006513 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006515}
6516
6517/*
6518 * ":tabonly": close all tab pages except the current one
6519 */
6520 static void
6521ex_tabonly(eap)
6522 exarg_T *eap;
6523{
6524 tabpage_T *tp;
6525 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006526
6527# ifdef FEAT_CMDWIN
6528 if (cmdwin_type != 0)
6529 cmdwin_result = K_IGNORE;
6530 else
6531# endif
6532 if (first_tabpage->tp_next == NULL)
6533 MSG(_("Already only one tab page"));
6534 else
6535 {
6536 /* Repeat this up to a 1000 times, because autocommands may mess
6537 * up the lists. */
6538 for (done = 0; done < 1000; ++done)
6539 {
6540 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6541 if (tp->tp_topframe != topframe)
6542 {
6543 tabpage_close_other(tp, eap->forceit);
6544 /* if we failed to close it quit */
6545 if (valid_tabpage(tp))
6546 done = 1000;
6547 /* start over, "tp" is now invalid */
6548 break;
6549 }
6550 if (first_tabpage->tp_next == NULL)
6551 break;
6552 }
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006557 * Close the current tab page.
6558 */
6559 void
6560tabpage_close(forceit)
6561 int forceit;
6562{
6563 /* First close all the windows but the current one. If that worked then
6564 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006565 if (lastwin != firstwin)
6566 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567 if (lastwin == firstwin)
6568 ex_win_close(forceit, curwin, NULL);
6569# ifdef FEAT_GUI
6570 need_mouse_correct = TRUE;
6571# endif
6572}
6573
6574/*
6575 * Close tab page "tp", which is not the current tab page.
6576 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006577 * Also takes care of the tab pages line disappearing when closing the
6578 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006579 */
6580 void
6581tabpage_close_other(tp, forceit)
6582 tabpage_T *tp;
6583 int forceit;
6584{
6585 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006586 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006587 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006588
6589 /* Limit to 1000 windows, autocommands may add a window while we close
6590 * one. OK, so I'm paranoid... */
6591 while (++done < 1000)
6592 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006593 wp = tp->tp_firstwin;
6594 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006595
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006596 /* Autocommands may delete the tab page under our fingers and we may
6597 * fail to close a window with a modified buffer. */
6598 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006599 break;
6600 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006601
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006602 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006603 if (h != tabline_height())
6604 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006605}
6606
6607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 * ":only".
6609 */
6610 static void
6611ex_only(eap)
6612 exarg_T *eap;
6613{
6614# ifdef FEAT_GUI
6615 need_mouse_correct = TRUE;
6616# endif
6617 close_others(TRUE, eap->forceit);
6618}
6619
6620/*
6621 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006622 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006624 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625ex_all(eap)
6626 exarg_T *eap;
6627{
6628 if (eap->addr_count == 0)
6629 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006630 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631}
6632#endif /* FEAT_WINDOWS */
6633
6634 static void
6635ex_hide(eap)
6636 exarg_T *eap;
6637{
6638 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6639 eap->errmsg = e_invarg;
6640 else
6641 {
6642 /* ":hide" or ":hide | cmd": hide current window */
6643 eap->nextcmd = check_nextcmd(eap->arg);
6644#ifdef FEAT_WINDOWS
6645 if (!eap->skip)
6646 {
6647# ifdef FEAT_GUI
6648 need_mouse_correct = TRUE;
6649# endif
6650 win_close(curwin, FALSE); /* don't free buffer */
6651 }
6652#endif
6653 }
6654}
6655
6656/*
6657 * ":stop" and ":suspend": Suspend Vim.
6658 */
6659 static void
6660ex_stop(eap)
6661 exarg_T *eap;
6662{
6663 /*
6664 * Disallow suspending for "rvim".
6665 */
6666 if (!check_restricted()
6667#ifdef WIN3264
6668 /*
6669 * Check if external commands are allowed now.
6670 */
6671 && can_end_termcap_mode(TRUE)
6672#endif
6673 )
6674 {
6675 if (!eap->forceit)
6676 autowrite_all();
6677 windgoto((int)Rows - 1, 0);
6678 out_char('\n');
6679 out_flush();
6680 stoptermcap();
6681 out_flush(); /* needed for SUN to restore xterm buffer */
6682#ifdef FEAT_TITLE
6683 mch_restore_title(3); /* restore window titles */
6684#endif
6685 ui_suspend(); /* call machine specific function */
6686#ifdef FEAT_TITLE
6687 maketitle();
6688 resettitle(); /* force updating the title */
6689#endif
6690 starttermcap();
6691 scroll_start(); /* scroll screen before redrawing */
6692 redraw_later_clear();
6693 shell_resized(); /* may have resized window */
6694 }
6695}
6696
6697/*
6698 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6699 */
6700 static void
6701ex_exit(eap)
6702 exarg_T *eap;
6703{
6704#ifdef FEAT_CMDWIN
6705 if (cmdwin_type != 0)
6706 {
6707 cmdwin_result = Ctrl_C;
6708 return;
6709 }
6710#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006711 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006712 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006713 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006714 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006715 return;
6716 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006717#ifdef FEAT_AUTOCMD
6718 if (curbuf_locked())
6719 return;
6720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 /*
6723 * if more files or windows we won't exit
6724 */
6725 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6726 exiting = TRUE;
6727 if ( ((eap->cmdidx == CMD_wq
6728 || curbufIsChanged())
6729 && do_write(eap) == FAIL)
6730 || check_more(TRUE, eap->forceit) == FAIL
6731 || (only_one_window() && check_changed_any(eap->forceit)))
6732 {
6733 not_exiting();
6734 }
6735 else
6736 {
6737#ifdef FEAT_WINDOWS
6738 if (only_one_window()) /* quit last window, exit Vim */
6739#endif
6740 getout(0);
6741#ifdef FEAT_WINDOWS
6742# ifdef FEAT_GUI
6743 need_mouse_correct = TRUE;
6744# endif
6745 /* quit current window, may free buffer */
6746 win_close(curwin, !P_HID(curwin->w_buffer));
6747#endif
6748 }
6749}
6750
6751/*
6752 * ":print", ":list", ":number".
6753 */
6754 static void
6755ex_print(eap)
6756 exarg_T *eap;
6757{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006758 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6759 EMSG(_(e_emptybuf));
6760 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006762 for ( ;!got_int; ui_breakcheck())
6763 {
6764 print_line(eap->line1,
6765 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6766 || (eap->flags & EXFLAG_NR)),
6767 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6768 if (++eap->line1 > eap->line2)
6769 break;
6770 out_flush(); /* show one line at a time */
6771 }
6772 setpcmark();
6773 /* put cursor at last line */
6774 curwin->w_cursor.lnum = eap->line2;
6775 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780
6781#ifdef FEAT_BYTEOFF
6782 static void
6783ex_goto(eap)
6784 exarg_T *eap;
6785{
6786 goto_byte(eap->line2);
6787}
6788#endif
6789
6790/*
6791 * ":shell".
6792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 static void
6794ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006795 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 do_shell(NULL, 0);
6798}
6799
6800#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6801 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006802 || defined(FEAT_GUI_MSWIN) \
6803 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 || defined(PROTO)
6805
6806/*
6807 * Handle a file drop. The code is here because a drop is *nearly* like an
6808 * :args command, but not quite (we have a list of exact filenames, so we
6809 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6810 * code is very similar to :args and hence needs access to a lot of the static
6811 * functions in this file.
6812 *
6813 * The list should be allocated using alloc(), as should each item in the
6814 * list. This function takes over responsibility for freeing the list.
6815 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006816 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6818 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6819 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6820 * file functionality is (currently) not in EMX this is not presently a
6821 * problem.
6822 */
6823 void
6824handle_drop(filec, filev, split)
6825 int filec; /* the number of files dropped */
6826 char_u **filev; /* the list of files dropped */
6827 int split; /* force splitting the window */
6828{
6829 exarg_T ea;
6830 int save_msg_scroll = msg_scroll;
6831
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006832 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006833 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006835#ifdef FEAT_AUTOCMD
6836 if (curbuf_locked())
6837 return;
6838#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006839 /* When the screen is being updated we should not change buffers and
6840 * windows structures, it may cause freed memory to be used. */
6841 if (updating_screen)
6842 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
6844 /* Check whether the current buffer is changed. If so, we will need
6845 * to split the current window or data could be lost.
6846 * We don't need to check if the 'hidden' option is set, as in this
6847 * case the buffer won't be lost.
6848 */
6849 if (!P_HID(curbuf) && !split)
6850 {
6851 ++emsg_off;
6852 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6853 --emsg_off;
6854 }
6855 if (split)
6856 {
6857# ifdef FEAT_WINDOWS
6858 if (win_split(0, 0) == FAIL)
6859 return;
6860# ifdef FEAT_SCROLLBIND
6861 curwin->w_p_scb = FALSE;
6862# endif
6863
6864 /* When splitting the window, create a new alist. Otherwise the
6865 * existing one is overwritten. */
6866 alist_unlink(curwin->w_alist);
6867 alist_new();
6868# else
6869 return; /* can't split, always fail */
6870# endif
6871 }
6872
6873 /*
6874 * Set up the new argument list.
6875 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006876 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878 /*
6879 * Move to the first file.
6880 */
6881 /* Fake up a minimal "next" command for do_argfile() */
6882 vim_memset(&ea, 0, sizeof(ea));
6883 ea.cmd = (char_u *)"next";
6884 do_argfile(&ea, 0);
6885
6886 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6887 * mode that is not needed here. */
6888 need_start_insertmode = FALSE;
6889
6890 /* Restore msg_scroll, otherwise a following command may cause scrolling
6891 * unexpectedly. The screen will be redrawn by the caller, thus
6892 * msg_scroll being set by displaying a message is irrelevant. */
6893 msg_scroll = save_msg_scroll;
6894}
6895#endif
6896
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897/*
6898 * Clear an argument list: free all file names and reset it to zero entries.
6899 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006900 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901alist_clear(al)
6902 alist_T *al;
6903{
6904 while (--al->al_ga.ga_len >= 0)
6905 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6906 ga_clear(&al->al_ga);
6907}
6908
6909/*
6910 * Init an argument list.
6911 */
6912 void
6913alist_init(al)
6914 alist_T *al;
6915{
6916 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6917}
6918
6919#if defined(FEAT_WINDOWS) || defined(PROTO)
6920
6921/*
6922 * Remove a reference from an argument list.
6923 * Ignored when the argument list is the global one.
6924 * If the argument list is no longer used by any window, free it.
6925 */
6926 void
6927alist_unlink(al)
6928 alist_T *al;
6929{
6930 if (al != &global_alist && --al->al_refcount <= 0)
6931 {
6932 alist_clear(al);
6933 vim_free(al);
6934 }
6935}
6936
6937# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6938/*
6939 * Create a new argument list and use it for the current window.
6940 */
6941 void
6942alist_new()
6943{
6944 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6945 if (curwin->w_alist == NULL)
6946 {
6947 curwin->w_alist = &global_alist;
6948 ++global_alist.al_refcount;
6949 }
6950 else
6951 {
6952 curwin->w_alist->al_refcount = 1;
6953 alist_init(curwin->w_alist);
6954 }
6955}
6956# endif
6957#endif
6958
6959#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6960/*
6961 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006962 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6963 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 */
6965 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006966alist_expand(fnum_list, fnum_len)
6967 int *fnum_list;
6968 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006971 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 char_u **new_arg_files;
6973 int new_arg_file_count;
6974 char_u *save_p_su = p_su;
6975 int i;
6976
6977 /* Don't use 'suffixes' here. This should work like the shell did the
6978 * expansion. Also, the vimrc file isn't read yet, thus the user
6979 * can't set the options. */
6980 p_su = empty_option;
6981 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6982 if (old_arg_files != NULL)
6983 {
6984 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006985 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6986 old_arg_count = GARGCOUNT;
6987 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 &new_arg_file_count, &new_arg_files,
6989 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6990 && new_arg_file_count > 0)
6991 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006992 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6993 TRUE, fnum_list, fnum_len);
6994 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997 p_su = save_p_su;
6998}
6999#endif
7000
7001/*
7002 * Set the argument list for the current window.
7003 * Takes over the allocated files[] and the allocated fnames in it.
7004 */
7005 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007006alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 alist_T *al;
7008 int count;
7009 char_u **files;
7010 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007011 int *fnum_list;
7012 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
7014 int i;
7015
7016 alist_clear(al);
7017 if (ga_grow(&al->al_ga, count) == OK)
7018 {
7019 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007020 {
7021 if (got_int)
7022 {
7023 /* When adding many buffers this can take a long time. Allow
7024 * interrupting here. */
7025 while (i < count)
7026 vim_free(files[i++]);
7027 break;
7028 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007029
7030 /* May set buffer name of a buffer previously used for the
7031 * argument list, so that it's re-used by alist_add. */
7032 if (fnum_list != NULL && i < fnum_len)
7033 buf_set_name(fnum_list[i], files[i]);
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007036 ui_breakcheck();
7037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 vim_free(files);
7039 }
7040 else
7041 FreeWild(count, files);
7042#ifdef FEAT_WINDOWS
7043 if (al == &global_alist)
7044#endif
7045 arg_had_last = FALSE;
7046}
7047
7048/*
7049 * Add file "fname" to argument list "al".
7050 * "fname" must have been allocated and "al" must have been checked for room.
7051 */
7052 void
7053alist_add(al, fname, set_fnum)
7054 alist_T *al;
7055 char_u *fname;
7056 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7057{
7058 if (fname == NULL) /* don't add NULL file names */
7059 return;
7060#ifdef BACKSLASH_IN_FILENAME
7061 slash_adjust(fname);
7062#endif
7063 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7064 if (set_fnum > 0)
7065 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7066 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7067 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068}
7069
7070#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7071/*
7072 * Adjust slashes in file names. Called after 'shellslash' was set.
7073 */
7074 void
7075alist_slash_adjust()
7076{
7077 int i;
7078# ifdef FEAT_WINDOWS
7079 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007080 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081# endif
7082
7083 for (i = 0; i < GARGCOUNT; ++i)
7084 if (GARGLIST[i].ae_fname != NULL)
7085 slash_adjust(GARGLIST[i].ae_fname);
7086# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007087 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (wp->w_alist != &global_alist)
7089 for (i = 0; i < WARGCOUNT(wp); ++i)
7090 if (WARGLIST(wp)[i].ae_fname != NULL)
7091 slash_adjust(WARGLIST(wp)[i].ae_fname);
7092# endif
7093}
7094#endif
7095
7096/*
7097 * ":preserve".
7098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 static void
7100ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007101 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007103 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 ml_preserve(curbuf, TRUE);
7105}
7106
7107/*
7108 * ":recover".
7109 */
7110 static void
7111ex_recover(eap)
7112 exarg_T *eap;
7113{
7114 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7115 recoverymode = TRUE;
7116 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7117 && (*eap->arg == NUL
7118 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7119 ml_recover();
7120 recoverymode = FALSE;
7121}
7122
7123/*
7124 * Command modifier used in a wrong way.
7125 */
7126 static void
7127ex_wrongmodifier(eap)
7128 exarg_T *eap;
7129{
7130 eap->errmsg = e_invcmd;
7131}
7132
7133#ifdef FEAT_WINDOWS
7134/*
7135 * :sview [+command] file split window with new file, read-only
7136 * :split [[+command] file] split window with current or new file
7137 * :vsplit [[+command] file] split window vertically with current or new file
7138 * :new [[+command] file] split window with no or new file
7139 * :vnew [[+command] file] split vertically window with no or new file
7140 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007141 *
7142 * :tabedit open new Tab page with empty window
7143 * :tabedit [+command] file open new Tab page and edit "file"
7144 * :tabnew [[+command] file] just like :tabedit
7145 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 */
7147 void
7148ex_splitview(eap)
7149 exarg_T *eap;
7150{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007151 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007152# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007154# endif
7155# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007159# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7161 {
7162 ex_ni(eap);
7163 return;
7164 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007167# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007173 * quickfix windows. But it's OK when doing ":tab split". */
7174 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176 if (eap->cmdidx == CMD_split)
7177 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007178# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (eap->cmdidx == CMD_vsplit)
7180 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007185# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007186 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7189 FNAME_MESS, TRUE, curbuf->b_ffname);
7190 if (fname == NULL)
7191 goto theend;
7192 eap->arg = fname;
7193 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007194# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 && eap->cmdidx != CMD_new)
7204 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007205# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007206 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007207# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007208 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007209# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007210 au_has_group((char_u *)"FileExplorer"))
7211 {
7212 /* No browsing supported but we do have the file explorer:
7213 * Edit the directory. */
7214 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7215 eap->arg = (char_u *)".";
7216 }
7217 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007218# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007219 {
7220 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007222 if (fname == NULL)
7223 goto theend;
7224 eap->arg = fname;
7225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007230 /*
7231 * Either open new tab page or split the window.
7232 */
7233 if (eap->cmdidx == CMD_tabedit
7234 || eap->cmdidx == CMD_tabfind
7235 || eap->cmdidx == CMD_tabnew)
7236 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007237 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7238 : eap->addr_count == 0 ? 0
7239 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007240 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007241 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007242
7243 /* set the alternate buffer for the window we came from */
7244 if (curwin != old_curwin
7245 && win_valid(old_curwin)
7246 && old_curwin->w_buffer != curbuf
7247 && !cmdmod.keepalt)
7248 old_curwin->w_alt_fnum = curbuf->b_fnum;
7249 }
7250 }
7251 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7253 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007254# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* Reset 'scrollbind' when editing another file, but keep it when
7256 * doing ":split" without arguments. */
7257 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007258# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 )
7262 curwin->w_p_scb = FALSE;
7263 else
7264 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 do_exedit(eap, old_curwin);
7267 }
7268
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007269# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274theend:
7275 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007278
7279/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007280 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007281 */
7282 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007283tabpage_new()
7284{
7285 exarg_T ea;
7286
7287 vim_memset(&ea, 0, sizeof(ea));
7288 ea.cmdidx = CMD_tabnew;
7289 ea.cmd = (char_u *)"tabn";
7290 ea.arg = (char_u *)"";
7291 ex_splitview(&ea);
7292}
7293
7294/*
7295 * :tabnext command
7296 */
7297 static void
7298ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007299 exarg_T *eap;
7300{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007301 switch (eap->cmdidx)
7302 {
7303 case CMD_tabfirst:
7304 case CMD_tabrewind:
7305 goto_tabpage(1);
7306 break;
7307 case CMD_tablast:
7308 goto_tabpage(9999);
7309 break;
7310 case CMD_tabprevious:
7311 case CMD_tabNext:
7312 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7313 break;
7314 default: /* CMD_tabnext */
7315 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7316 break;
7317 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007318}
7319
7320/*
7321 * :tabmove command
7322 */
7323 static void
7324ex_tabmove(eap)
7325 exarg_T *eap;
7326{
7327 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007328}
7329
7330/*
7331 * :tabs command: List tabs and their contents.
7332 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007333 static void
7334ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007335 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007336{
7337 tabpage_T *tp;
7338 win_T *wp;
7339 int tabcount = 1;
7340
7341 msg_start();
7342 msg_scroll = TRUE;
7343 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7344 {
7345 msg_putchar('\n');
7346 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7347 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7348 out_flush(); /* output one line at a time */
7349 ui_breakcheck();
7350
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007351 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007352 wp = firstwin;
7353 else
7354 wp = tp->tp_firstwin;
7355 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7356 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007357 msg_putchar('\n');
7358 msg_putchar(wp == curwin ? '>' : ' ');
7359 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007360 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7361 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007362 if (buf_spname(wp->w_buffer) != NULL)
7363 STRCPY(IObuff, buf_spname(wp->w_buffer));
7364 else
7365 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7366 IObuff, IOSIZE, TRUE);
7367 msg_outtrans(IObuff);
7368 out_flush(); /* output one line at a time */
7369 ui_breakcheck();
7370 }
7371 }
7372}
7373
7374#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376/*
7377 * ":mode": Set screen mode.
7378 * If no argument given, just get the screen size and redraw.
7379 */
7380 static void
7381ex_mode(eap)
7382 exarg_T *eap;
7383{
7384 if (*eap->arg == NUL)
7385 shell_resized();
7386 else
7387 mch_screenmode(eap->arg);
7388}
7389
7390#ifdef FEAT_WINDOWS
7391/*
7392 * ":resize".
7393 * set, increment or decrement current window height
7394 */
7395 static void
7396ex_resize(eap)
7397 exarg_T *eap;
7398{
7399 int n;
7400 win_T *wp = curwin;
7401
7402 if (eap->addr_count > 0)
7403 {
7404 n = eap->line2;
7405 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7406 ;
7407 }
7408
7409#ifdef FEAT_GUI
7410 need_mouse_correct = TRUE;
7411#endif
7412 n = atol((char *)eap->arg);
7413#ifdef FEAT_VERTSPLIT
7414 if (cmdmod.split & WSP_VERT)
7415 {
7416 if (*eap->arg == '-' || *eap->arg == '+')
7417 n += W_WIDTH(curwin);
7418 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7419 n = 9999;
7420 win_setwidth_win((int)n, wp);
7421 }
7422 else
7423#endif
7424 {
7425 if (*eap->arg == '-' || *eap->arg == '+')
7426 n += curwin->w_height;
7427 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7428 n = 9999;
7429 win_setheight_win((int)n, wp);
7430 }
7431}
7432#endif
7433
7434/*
7435 * ":find [+command] <file>" command.
7436 */
7437 static void
7438ex_find(eap)
7439 exarg_T *eap;
7440{
7441#ifdef FEAT_SEARCHPATH
7442 char_u *fname;
7443 int count;
7444
7445 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7446 TRUE, curbuf->b_ffname);
7447 if (eap->addr_count > 0)
7448 {
7449 /* Repeat finding the file "count" times. This matters when it
7450 * appears several times in the path. */
7451 count = eap->line2;
7452 while (fname != NULL && --count > 0)
7453 {
7454 vim_free(fname);
7455 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7456 FALSE, curbuf->b_ffname);
7457 }
7458 }
7459
7460 if (fname != NULL)
7461 {
7462 eap->arg = fname;
7463#endif
7464 do_exedit(eap, NULL);
7465#ifdef FEAT_SEARCHPATH
7466 vim_free(fname);
7467 }
7468#endif
7469}
7470
7471/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007472 * ":open" simulation: for now just work like ":visual".
7473 */
7474 static void
7475ex_open(eap)
7476 exarg_T *eap;
7477{
7478 regmatch_T regmatch;
7479 char_u *p;
7480
7481 curwin->w_cursor.lnum = eap->line2;
7482 beginline(BL_SOL | BL_FIX);
7483 if (*eap->arg == '/')
7484 {
7485 /* ":open /pattern/": put cursor in column found with pattern */
7486 ++eap->arg;
7487 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7488 *p = NUL;
7489 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7490 if (regmatch.regprog != NULL)
7491 {
7492 regmatch.rm_ic = p_ic;
7493 p = ml_get_curline();
7494 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007495 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007496 else
7497 EMSG(_(e_nomatch));
7498 vim_free(regmatch.regprog);
7499 }
7500 /* Move to the NUL, ignore any other arguments. */
7501 eap->arg += STRLEN(eap->arg);
7502 }
7503 check_cursor();
7504
7505 eap->cmdidx = CMD_visual;
7506 do_exedit(eap, NULL);
7507}
7508
7509/*
7510 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 */
7512 static void
7513ex_edit(eap)
7514 exarg_T *eap;
7515{
7516 do_exedit(eap, NULL);
7517}
7518
7519/*
7520 * ":edit <file>" command and alikes.
7521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 void
7523do_exedit(eap, old_curwin)
7524 exarg_T *eap;
7525 win_T *old_curwin; /* curwin before doing a split or NULL */
7526{
7527 int n;
7528#ifdef FEAT_WINDOWS
7529 int need_hide;
7530#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 /*
7534 * ":vi" command ends Ex mode.
7535 */
7536 if (exmode_active && (eap->cmdidx == CMD_visual
7537 || eap->cmdidx == CMD_view))
7538 {
7539 exmode_active = FALSE;
7540 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007541 {
7542 /* Special case: ":global/pat/visual\NLvi-commands" */
7543 if (global_busy)
7544 {
7545 int rd = RedrawingDisabled;
7546 int nwr = no_wait_return;
7547 int ms = msg_scroll;
7548#ifdef FEAT_GUI
7549 int he = hold_gui_events;
7550#endif
7551
7552 if (eap->nextcmd != NULL)
7553 {
7554 stuffReadbuff(eap->nextcmd);
7555 eap->nextcmd = NULL;
7556 }
7557
7558 if (exmode_was != EXMODE_VIM)
7559 settmode(TMODE_RAW);
7560 RedrawingDisabled = 0;
7561 no_wait_return = 0;
7562 need_wait_return = FALSE;
7563 msg_scroll = 0;
7564#ifdef FEAT_GUI
7565 hold_gui_events = 0;
7566#endif
7567 must_redraw = CLEAR;
7568
7569 main_loop(FALSE, TRUE);
7570
7571 RedrawingDisabled = rd;
7572 no_wait_return = nwr;
7573 msg_scroll = ms;
7574#ifdef FEAT_GUI
7575 hold_gui_events = he;
7576#endif
7577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
7581
7582 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007583 || eap->cmdidx == CMD_tabnew
7584 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585#ifdef FEAT_VERTSPLIT
7586 || eap->cmdidx == CMD_vnew
7587#endif
7588 ) && *eap->arg == NUL)
7589 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007590 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 setpcmark();
7592 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007593 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7594 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 else if ((eap->cmdidx != CMD_split
7597#ifdef FEAT_VERTSPLIT
7598 && eap->cmdidx != CMD_vsplit
7599#endif
7600 )
7601 || *eap->arg != NUL
7602#ifdef FEAT_BROWSE
7603 || cmdmod.browse
7604#endif
7605 )
7606 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007607#ifdef FEAT_AUTOCMD
7608 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7609 * can bring us here, others are stopped earlier. */
7610 if (*eap->arg != NUL && curbuf_locked())
7611 return;
7612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 n = readonlymode;
7614 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7615 readonlymode = TRUE;
7616 else if (eap->cmdidx == CMD_enew)
7617 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7618 empty buffer */
7619 setpcmark();
7620 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7621 NULL, eap,
7622 /* ":edit" goes to first line if Vi compatible */
7623 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7624 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7625 ? ECMD_ONE : eap->do_ecmd_lnum,
7626 (P_HID(curbuf) ? ECMD_HIDE : 0)
7627 + (eap->forceit ? ECMD_FORCEIT : 0)
7628#ifdef FEAT_LISTCMDS
7629 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7630#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007631 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
7633 /* Editing the file failed. If the window was split, close it. */
7634#ifdef FEAT_WINDOWS
7635 if (old_curwin != NULL)
7636 {
7637 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7638 if (!need_hide || P_HID(curbuf))
7639 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007640# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7641 cleanup_T cs;
7642
7643 /* Reset the error/interrupt/exception state here so that
7644 * aborting() returns FALSE when closing a window. */
7645 enter_cleanup(&cs);
7646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647# ifdef FEAT_GUI
7648 need_mouse_correct = TRUE;
7649# endif
7650 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007651
7652# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7653 /* Restore the error/interrupt/exception state if not
7654 * discarded by a new aborting error, interrupt, or
7655 * uncaught exception. */
7656 leave_cleanup(&cs);
7657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659 }
7660#endif
7661 }
7662 else if (readonlymode && curbuf->b_nwindows == 1)
7663 {
7664 /* When editing an already visited buffer, 'readonly' won't be set
7665 * but the previous value is kept. With ":view" and ":sview" we
7666 * want the file to be readonly, except when another window is
7667 * editing the same buffer. */
7668 curbuf->b_p_ro = TRUE;
7669 }
7670 readonlymode = n;
7671 }
7672 else
7673 {
7674 if (eap->do_ecmd_cmd != NULL)
7675 do_cmdline_cmd(eap->do_ecmd_cmd);
7676#ifdef FEAT_TITLE
7677 n = curwin->w_arg_idx_invalid;
7678#endif
7679 check_arg_idx(curwin);
7680#ifdef FEAT_TITLE
7681 if (n != curwin->w_arg_idx_invalid)
7682 maketitle();
7683#endif
7684 }
7685
7686#ifdef FEAT_WINDOWS
7687 /*
7688 * if ":split file" worked, set alternate file name in old window to new
7689 * file
7690 */
7691 if (old_curwin != NULL
7692 && *eap->arg != NUL
7693 && curwin != old_curwin
7694 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007695 && old_curwin->w_buffer != curbuf
7696 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 old_curwin->w_alt_fnum = curbuf->b_fnum;
7698#endif
7699
7700 ex_no_reprint = TRUE;
7701}
7702
7703#ifndef FEAT_GUI
7704/*
7705 * ":gui" and ":gvim" when there is no GUI.
7706 */
7707 static void
7708ex_nogui(eap)
7709 exarg_T *eap;
7710{
7711 eap->errmsg = e_nogvim;
7712}
7713#endif
7714
7715#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7716 static void
7717ex_tearoff(eap)
7718 exarg_T *eap;
7719{
7720 gui_make_tearoff(eap->arg);
7721}
7722#endif
7723
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007724#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 static void
7726ex_popup(eap)
7727 exarg_T *eap;
7728{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007729 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730}
7731#endif
7732
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 static void
7734ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007735 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7738 MSG(_("No swap file"));
7739 else
7740 msg(curbuf->b_ml.ml_mfp->mf_fname);
7741}
7742
7743/*
7744 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7745 * offset.
7746 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 static void
7749ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007750 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752#ifdef FEAT_SCROLLBIND
7753 win_T *wp;
7754 long topline;
7755 long y;
7756 linenr_T old_linenr = curwin->w_cursor.lnum;
7757
7758 setpcmark();
7759
7760 /*
7761 * determine max topline
7762 */
7763 if (curwin->w_p_scb)
7764 {
7765 topline = curwin->w_topline;
7766 for (wp = firstwin; wp; wp = wp->w_next)
7767 {
7768 if (wp->w_p_scb && wp->w_buffer)
7769 {
7770 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7771 if (topline > y)
7772 topline = y;
7773 }
7774 }
7775 if (topline < 1)
7776 topline = 1;
7777 }
7778 else
7779 {
7780 topline = 1;
7781 }
7782
7783
7784 /*
7785 * set all scrollbind windows to the same topline
7786 */
7787 wp = curwin;
7788 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7789 {
7790 if (curwin->w_p_scb)
7791 {
7792 y = topline - curwin->w_topline;
7793 if (y > 0)
7794 scrollup(y, TRUE);
7795 else
7796 scrolldown(-y, TRUE);
7797 curwin->w_scbind_pos = topline;
7798 redraw_later(VALID);
7799 cursor_correct();
7800#ifdef FEAT_WINDOWS
7801 curwin->w_redr_status = TRUE;
7802#endif
7803 }
7804 }
7805 curwin = wp;
7806 if (curwin->w_p_scb)
7807 {
7808 did_syncbind = TRUE;
7809 checkpcmark();
7810 if (old_linenr != curwin->w_cursor.lnum)
7811 {
7812 char_u ctrl_o[2];
7813
7814 ctrl_o[0] = Ctrl_O;
7815 ctrl_o[1] = 0;
7816 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7817 }
7818 }
7819#endif
7820}
7821
7822
7823 static void
7824ex_read(eap)
7825 exarg_T *eap;
7826{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007827 int i;
7828 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7829 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830
7831 if (eap->usefilter) /* :r!cmd */
7832 do_bang(1, eap, FALSE, FALSE, TRUE);
7833 else
7834 {
7835 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7836 return;
7837
7838#ifdef FEAT_BROWSE
7839 if (cmdmod.browse)
7840 {
7841 char_u *browseFile;
7842
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007843 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 NULL, NULL, NULL, curbuf);
7845 if (browseFile != NULL)
7846 {
7847 i = readfile(browseFile, NULL,
7848 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7849 vim_free(browseFile);
7850 }
7851 else
7852 i = OK;
7853 }
7854 else
7855#endif
7856 if (*eap->arg == NUL)
7857 {
7858 if (check_fname() == FAIL) /* check for no file name */
7859 return;
7860 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7861 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7862 }
7863 else
7864 {
7865 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7866 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7867 i = readfile(eap->arg, NULL,
7868 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7869
7870 }
7871 if (i == FAIL)
7872 {
7873#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7874 if (!aborting())
7875#endif
7876 EMSG2(_(e_notopen), eap->arg);
7877 }
7878 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007879 {
7880 if (empty && exmode_active)
7881 {
7882 /* Delete the empty line that remains. Historically ex does
7883 * this but vi doesn't. */
7884 if (eap->line2 == 0)
7885 lnum = curbuf->b_ml.ml_line_count;
7886 else
7887 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007888 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007889 {
7890 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007891 if (curwin->w_cursor.lnum > 1
7892 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007893 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007894 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007895 }
7896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900}
7901
Bram Moolenaarea408852005-06-25 22:49:46 +00007902static char_u *prev_dir = NULL;
7903
7904#if defined(EXITFREE) || defined(PROTO)
7905 void
7906free_cd_dir()
7907{
7908 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007909 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007910
7911 vim_free(globaldir);
7912 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007913}
7914#endif
7915
7916
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917/*
7918 * ":cd", ":lcd", ":chdir" and ":lchdir".
7919 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007920 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921ex_cd(eap)
7922 exarg_T *eap;
7923{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 char_u *new_dir;
7925 char_u *tofree;
7926
7927 new_dir = eap->arg;
7928#if !defined(UNIX) && !defined(VMS)
7929 /* for non-UNIX ":cd" means: print current directory */
7930 if (*new_dir == NUL)
7931 ex_pwd(NULL);
7932 else
7933#endif
7934 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007935#ifdef FEAT_AUTOCMD
7936 if (allbuf_locked())
7937 return;
7938#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007939 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7940 && !eap->forceit)
7941 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007942 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007943 return;
7944 }
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 /* ":cd -": Change to previous directory */
7947 if (STRCMP(new_dir, "-") == 0)
7948 {
7949 if (prev_dir == NULL)
7950 {
7951 EMSG(_("E186: No previous directory"));
7952 return;
7953 }
7954 new_dir = prev_dir;
7955 }
7956
7957 /* Save current directory for next ":cd -" */
7958 tofree = prev_dir;
7959 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7960 prev_dir = vim_strsave(NameBuff);
7961 else
7962 prev_dir = NULL;
7963
7964#if defined(UNIX) || defined(VMS)
7965 /* for UNIX ":cd" means: go to home directory */
7966 if (*new_dir == NUL)
7967 {
7968 /* use NameBuff for home directory name */
7969# ifdef VMS
7970 char_u *p;
7971
7972 p = mch_getenv((char_u *)"SYS$LOGIN");
7973 if (p == NULL || *p == NUL) /* empty is the same as not set */
7974 NameBuff[0] = NUL;
7975 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007976 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977# else
7978 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7979# endif
7980 new_dir = NameBuff;
7981 }
7982#endif
7983 if (new_dir == NULL || vim_chdir(new_dir))
7984 EMSG(_(e_failed));
7985 else
7986 {
7987 vim_free(curwin->w_localdir);
7988 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7989 {
7990 /* If still in global directory, need to remember current
7991 * directory as global directory. */
7992 if (globaldir == NULL && prev_dir != NULL)
7993 globaldir = vim_strsave(prev_dir);
7994 /* Remember this local directory for the window. */
7995 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7996 curwin->w_localdir = vim_strsave(NameBuff);
7997 }
7998 else
7999 {
8000 /* We are now in the global directory, no need to remember its
8001 * name. */
8002 vim_free(globaldir);
8003 globaldir = NULL;
8004 curwin->w_localdir = NULL;
8005 }
8006
8007 shorten_fnames(TRUE);
8008
8009 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008010 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 ex_pwd(eap);
8012 }
8013 vim_free(tofree);
8014 }
8015}
8016
8017/*
8018 * ":pwd".
8019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 static void
8021ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008022 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8025 {
8026#ifdef BACKSLASH_IN_FILENAME
8027 slash_adjust(NameBuff);
8028#endif
8029 msg(NameBuff);
8030 }
8031 else
8032 EMSG(_("E187: Unknown"));
8033}
8034
8035/*
8036 * ":=".
8037 */
8038 static void
8039ex_equal(eap)
8040 exarg_T *eap;
8041{
8042 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008043 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045
8046 static void
8047ex_sleep(eap)
8048 exarg_T *eap;
8049{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008050 int n;
8051 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
8053 if (cursor_valid())
8054 {
8055 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8056 if (n >= 0)
8057 windgoto((int)n, curwin->w_wcol);
8058 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008059
8060 len = eap->line2;
8061 switch (*eap->arg)
8062 {
8063 case 'm': break;
8064 case NUL: len *= 1000L; break;
8065 default: EMSG2(_(e_invarg2), eap->arg); return;
8066 }
8067 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
8070/*
8071 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8072 */
8073 void
8074do_sleep(msec)
8075 long msec;
8076{
8077 long done;
8078
8079 cursor_on();
8080 out_flush();
8081 for (done = 0; !got_int && done < msec; done += 1000L)
8082 {
8083 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8084 ui_breakcheck();
8085 }
8086}
8087
8088 static void
8089do_exmap(eap, isabbrev)
8090 exarg_T *eap;
8091 int isabbrev;
8092{
8093 int mode;
8094 char_u *cmdp;
8095
8096 cmdp = eap->cmd;
8097 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8098
8099 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8100 eap->arg, mode, isabbrev))
8101 {
8102 case 1: EMSG(_(e_invarg));
8103 break;
8104 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8105 break;
8106 }
8107}
8108
8109/*
8110 * ":winsize" command (obsolete).
8111 */
8112 static void
8113ex_winsize(eap)
8114 exarg_T *eap;
8115{
8116 int w, h;
8117 char_u *arg = eap->arg;
8118 char_u *p;
8119
8120 w = getdigits(&arg);
8121 arg = skipwhite(arg);
8122 p = arg;
8123 h = getdigits(&arg);
8124 if (*p != NUL && *arg == NUL)
8125 set_shellsize(w, h, TRUE);
8126 else
8127 EMSG(_("E465: :winsize requires two number arguments"));
8128}
8129
8130#ifdef FEAT_WINDOWS
8131 static void
8132ex_wincmd(eap)
8133 exarg_T *eap;
8134{
8135 int xchar = NUL;
8136 char_u *p;
8137
8138 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8139 {
8140 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8141 if (eap->arg[1] == NUL)
8142 {
8143 EMSG(_(e_invarg));
8144 return;
8145 }
8146 xchar = eap->arg[1];
8147 p = eap->arg + 2;
8148 }
8149 else
8150 p = eap->arg + 1;
8151
8152 eap->nextcmd = check_nextcmd(p);
8153 p = skipwhite(p);
8154 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8155 EMSG(_(e_invarg));
8156 else
8157 {
8158 /* Pass flags on for ":vertical wincmd ]". */
8159 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008160 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8162 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008163 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165}
8166#endif
8167
Bram Moolenaar843ee412004-06-30 16:16:41 +00008168#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169/*
8170 * ":winpos".
8171 */
8172 static void
8173ex_winpos(eap)
8174 exarg_T *eap;
8175{
8176 int x, y;
8177 char_u *arg = eap->arg;
8178 char_u *p;
8179
8180 if (*arg == NUL)
8181 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008182# if defined(FEAT_GUI) || defined(MSWIN)
8183# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008185# else
8186 if (mch_get_winpos(&x, &y) != FAIL)
8187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {
8189 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8190 msg(IObuff);
8191 }
8192 else
8193# endif
8194 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8195 }
8196 else
8197 {
8198 x = getdigits(&arg);
8199 arg = skipwhite(arg);
8200 p = arg;
8201 y = getdigits(&arg);
8202 if (*p == NUL || *arg != NUL)
8203 {
8204 EMSG(_("E466: :winpos requires two number arguments"));
8205 return;
8206 }
8207# ifdef FEAT_GUI
8208 if (gui.in_use)
8209 gui_mch_set_winpos(x, y);
8210 else if (gui.starting)
8211 {
8212 /* Remember the coordinates for when the window is opened. */
8213 gui_win_x = x;
8214 gui_win_y = y;
8215 }
8216# ifdef HAVE_TGETENT
8217 else
8218# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008219# else
8220# ifdef MSWIN
8221 mch_set_winpos(x, y);
8222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223# endif
8224# ifdef HAVE_TGETENT
8225 if (*T_CWP)
8226 term_set_winpos(x, y);
8227# endif
8228 }
8229}
8230#endif
8231
8232/*
8233 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8234 */
8235 static void
8236ex_operators(eap)
8237 exarg_T *eap;
8238{
8239 oparg_T oa;
8240
8241 clear_oparg(&oa);
8242 oa.regname = eap->regname;
8243 oa.start.lnum = eap->line1;
8244 oa.end.lnum = eap->line2;
8245 oa.line_count = eap->line2 - eap->line1 + 1;
8246 oa.motion_type = MLINE;
8247#ifdef FEAT_VIRTUALEDIT
8248 virtual_op = FALSE;
8249#endif
8250 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8251 {
8252 setpcmark();
8253 curwin->w_cursor.lnum = eap->line1;
8254 beginline(BL_SOL | BL_FIX);
8255 }
8256
8257 switch (eap->cmdidx)
8258 {
8259 case CMD_delete:
8260 oa.op_type = OP_DELETE;
8261 op_delete(&oa);
8262 break;
8263
8264 case CMD_yank:
8265 oa.op_type = OP_YANK;
8266 (void)op_yank(&oa, FALSE, TRUE);
8267 break;
8268
8269 default: /* CMD_rshift or CMD_lshift */
8270 if ((eap->cmdidx == CMD_rshift)
8271#ifdef FEAT_RIGHTLEFT
8272 ^ curwin->w_p_rl
8273#endif
8274 )
8275 oa.op_type = OP_RSHIFT;
8276 else
8277 oa.op_type = OP_LSHIFT;
8278 op_shift(&oa, FALSE, eap->amount);
8279 break;
8280 }
8281#ifdef FEAT_VIRTUALEDIT
8282 virtual_op = MAYBE;
8283#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008284 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285}
8286
8287/*
8288 * ":put".
8289 */
8290 static void
8291ex_put(eap)
8292 exarg_T *eap;
8293{
8294 /* ":0put" works like ":1put!". */
8295 if (eap->line2 == 0)
8296 {
8297 eap->line2 = 1;
8298 eap->forceit = TRUE;
8299 }
8300 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008301 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8302 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303}
8304
8305/*
8306 * Handle ":copy" and ":move".
8307 */
8308 static void
8309ex_copymove(eap)
8310 exarg_T *eap;
8311{
8312 long n;
8313
8314 n = get_address(&eap->arg, FALSE, FALSE);
8315 if (eap->arg == NULL) /* error detected */
8316 {
8317 eap->nextcmd = NULL;
8318 return;
8319 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008320 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322 /*
8323 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8324 */
8325 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8326 {
8327 EMSG(_(e_invaddr));
8328 return;
8329 }
8330
8331 if (eap->cmdidx == CMD_move)
8332 {
8333 if (do_move(eap->line1, eap->line2, n) == FAIL)
8334 return;
8335 }
8336 else
8337 ex_copy(eap->line1, eap->line2, n);
8338 u_clearline();
8339 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008340 ex_may_print(eap);
8341}
8342
8343/*
8344 * Print the current line if flags were given to the Ex command.
8345 */
8346 static void
8347ex_may_print(eap)
8348 exarg_T *eap;
8349{
8350 if (eap->flags != 0)
8351 {
8352 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8353 (eap->flags & EXFLAG_LIST));
8354 ex_no_reprint = TRUE;
8355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356}
8357
8358/*
8359 * ":smagic" and ":snomagic".
8360 */
8361 static void
8362ex_submagic(eap)
8363 exarg_T *eap;
8364{
8365 int magic_save = p_magic;
8366
8367 p_magic = (eap->cmdidx == CMD_smagic);
8368 do_sub(eap);
8369 p_magic = magic_save;
8370}
8371
8372/*
8373 * ":join".
8374 */
8375 static void
8376ex_join(eap)
8377 exarg_T *eap;
8378{
8379 curwin->w_cursor.lnum = eap->line1;
8380 if (eap->line1 == eap->line2)
8381 {
8382 if (eap->addr_count >= 2) /* :2,2join does nothing */
8383 return;
8384 if (eap->line2 == curbuf->b_ml.ml_line_count)
8385 {
8386 beep_flush();
8387 return;
8388 }
8389 ++eap->line2;
8390 }
8391 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8392 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008393 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394}
8395
8396/*
8397 * ":[addr]@r" or ":[addr]*r": execute register
8398 */
8399 static void
8400ex_at(eap)
8401 exarg_T *eap;
8402{
8403 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008404 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
8406 curwin->w_cursor.lnum = eap->line2;
8407
8408#ifdef USE_ON_FLY_SCROLL
8409 dont_scroll = TRUE; /* disallow scrolling here */
8410#endif
8411
8412 /* get the register name. No name means to use the previous one */
8413 c = *eap->arg;
8414 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8415 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008416 /* Put the register in the typeahead buffer with the "silent" flag. */
8417 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8418 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 else
8423 {
8424 int save_efr = exec_from_reg;
8425
8426 exec_from_reg = TRUE;
8427
8428 /*
8429 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008430 * Continue until the stuff buffer is empty and all added characters
8431 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008433 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8435
8436 exec_from_reg = save_efr;
8437 }
8438}
8439
8440/*
8441 * ":!".
8442 */
8443 static void
8444ex_bang(eap)
8445 exarg_T *eap;
8446{
8447 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8448}
8449
8450/*
8451 * ":undo".
8452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 static void
8454ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008455 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008457 if (eap->addr_count == 1) /* :undo 123 */
8458 undo_time(eap->line2, FALSE, TRUE);
8459 else
8460 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461}
8462
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008463#ifdef FEAT_PERSISTENT_UNDO
8464 void
8465ex_wundo(eap)
8466 exarg_T *eap;
8467{
8468 char_u hash[UNDO_HASH_SIZE];
8469
8470 u_compute_hash(hash);
8471 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8472}
8473
8474 void
8475ex_rundo(eap)
8476 exarg_T *eap;
8477{
8478 char_u hash[UNDO_HASH_SIZE];
8479
8480 u_compute_hash(hash);
8481 u_read_undo(eap->arg, hash);
8482}
8483#endif
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485/*
8486 * ":redo".
8487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 static void
8489ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008490 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
8492 u_redo(1);
8493}
8494
8495/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008496 * ":earlier" and ":later".
8497 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008498 static void
8499ex_later(eap)
8500 exarg_T *eap;
8501{
8502 long count = 0;
8503 int sec = FALSE;
8504 char_u *p = eap->arg;
8505
8506 if (*p == NUL)
8507 count = 1;
8508 else if (isdigit(*p))
8509 {
8510 count = getdigits(&p);
8511 switch (*p)
8512 {
8513 case 's': ++p; sec = TRUE; break;
8514 case 'm': ++p; sec = TRUE; count *= 60; break;
8515 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8516 }
8517 }
8518
8519 if (*p != NUL)
8520 EMSG2(_(e_invarg2), eap->arg);
8521 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008522 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008523}
8524
8525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 * ":redir": start/stop redirection.
8527 */
8528 static void
8529ex_redir(eap)
8530 exarg_T *eap;
8531{
8532 char *mode;
8533 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008534 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
8536 if (STRICMP(eap->arg, "END") == 0)
8537 close_redir();
8538 else
8539 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008540 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008542 ++arg;
8543 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008545 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 mode = "a";
8547 }
8548 else
8549 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008550 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551
8552 close_redir();
8553
8554 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008555 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 if (fname == NULL)
8557 return;
8558#ifdef FEAT_BROWSE
8559 if (cmdmod.browse)
8560 {
8561 char_u *browseFile;
8562
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008563 browseFile = do_browse(BROWSE_SAVE,
8564 (char_u *)_("Save Redirection"),
8565 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (browseFile == NULL)
8567 return; /* operation cancelled */
8568 vim_free(fname);
8569 fname = browseFile;
8570 eap->forceit = TRUE; /* since dialog already asked */
8571 }
8572#endif
8573
8574 redir_fd = open_exfile(fname, eap->forceit, mode);
8575 vim_free(fname);
8576 }
8577#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008578 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 {
8580 /* redirect to a register a-z (resp. A-Z for appending) */
8581 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008582 ++arg;
8583 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008585 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008586 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008588 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008590 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008591 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008592 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008593 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008595 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008596 if (*arg == '>')
8597 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008598 /* Make register empty when not using @A-@Z and the
8599 * command is valid. */
8600 if (*arg == NUL && !isupper(redir_reg))
8601 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008603 }
8604 if (*arg != NUL)
8605 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008606 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008607 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008608 }
8609 }
8610 else if (*arg == '=' && arg[1] == '>')
8611 {
8612 int append;
8613
8614 /* redirect to a variable */
8615 close_redir();
8616 arg += 2;
8617
8618 if (*arg == '>')
8619 {
8620 ++arg;
8621 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
8623 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008624 append = FALSE;
8625
8626 if (var_redir_start(skipwhite(arg), append) == OK)
8627 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629#endif
8630
8631 /* TODO: redirect to a buffer */
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008634 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008636
8637 /* Make sure redirection is not off. Can happen for cmdline completion
8638 * that indirectly invokes a command to catch its output. */
8639 if (redir_fd != NULL
8640#ifdef FEAT_EVAL
8641 || redir_reg || redir_vname
8642#endif
8643 )
8644 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645}
8646
8647/*
8648 * ":redraw": force redraw
8649 */
8650 static void
8651ex_redraw(eap)
8652 exarg_T *eap;
8653{
8654 int r = RedrawingDisabled;
8655 int p = p_lz;
8656
8657 RedrawingDisabled = 0;
8658 p_lz = FALSE;
8659 update_topline();
8660 update_screen(eap->forceit ? CLEAR :
8661#ifdef FEAT_VISUAL
8662 VIsual_active ? INVERTED :
8663#endif
8664 0);
8665#ifdef FEAT_TITLE
8666 if (need_maketitle)
8667 maketitle();
8668#endif
8669 RedrawingDisabled = r;
8670 p_lz = p;
8671
8672 /* Reset msg_didout, so that a message that's there is overwritten. */
8673 msg_didout = FALSE;
8674 msg_col = 0;
8675
8676 out_flush();
8677}
8678
8679/*
8680 * ":redrawstatus": force redraw of status line(s)
8681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 static void
8683ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008684 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686#if defined(FEAT_WINDOWS)
8687 int r = RedrawingDisabled;
8688 int p = p_lz;
8689
8690 RedrawingDisabled = 0;
8691 p_lz = FALSE;
8692 if (eap->forceit)
8693 status_redraw_all();
8694 else
8695 status_redraw_curbuf();
8696 update_screen(
8697# ifdef FEAT_VISUAL
8698 VIsual_active ? INVERTED :
8699# endif
8700 0);
8701 RedrawingDisabled = r;
8702 p_lz = p;
8703 out_flush();
8704#endif
8705}
8706
8707 static void
8708close_redir()
8709{
8710 if (redir_fd != NULL)
8711 {
8712 fclose(redir_fd);
8713 redir_fd = NULL;
8714 }
8715#ifdef FEAT_EVAL
8716 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008717 if (redir_vname)
8718 {
8719 var_redir_stop();
8720 redir_vname = 0;
8721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722#endif
8723}
8724
8725#if defined(FEAT_SESSION) && defined(USE_CRNL)
8726# define MKSESSION_NL
8727static int mksession_nl = FALSE; /* use NL only in put_eol() */
8728#endif
8729
8730/*
8731 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8732 */
8733 static void
8734ex_mkrc(eap)
8735 exarg_T *eap;
8736{
8737 FILE *fd;
8738 int failed = FALSE;
8739 char_u *fname;
8740#ifdef FEAT_BROWSE
8741 char_u *browseFile = NULL;
8742#endif
8743#ifdef FEAT_SESSION
8744 int view_session = FALSE;
8745 int using_vdir = FALSE; /* using 'viewdir'? */
8746 char_u *viewFile = NULL;
8747 unsigned *flagp;
8748#endif
8749
8750 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8751 {
8752#ifdef FEAT_SESSION
8753 view_session = TRUE;
8754#else
8755 ex_ni(eap);
8756 return;
8757#endif
8758 }
8759
8760#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008761 /* Use the short file name until ":lcd" is used. We also don't use the
8762 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008763 did_lcd = FALSE;
8764
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8766 if (eap->cmdidx == CMD_mkview
8767 && (*eap->arg == NUL
8768 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8769 {
8770 eap->forceit = TRUE;
8771 fname = get_view_file(*eap->arg);
8772 if (fname == NULL)
8773 return;
8774 viewFile = fname;
8775 using_vdir = TRUE;
8776 }
8777 else
8778#endif
8779 if (*eap->arg != NUL)
8780 fname = eap->arg;
8781 else if (eap->cmdidx == CMD_mkvimrc)
8782 fname = (char_u *)VIMRC_FILE;
8783#ifdef FEAT_SESSION
8784 else if (eap->cmdidx == CMD_mksession)
8785 fname = (char_u *)SESSION_FILE;
8786#endif
8787 else
8788 fname = (char_u *)EXRC_FILE;
8789
8790#ifdef FEAT_BROWSE
8791 if (cmdmod.browse)
8792 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008793 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794# ifdef FEAT_SESSION
8795 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8796 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8797# endif
8798 (char_u *)_("Save Setup"),
8799 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8800 if (browseFile == NULL)
8801 goto theend;
8802 fname = browseFile;
8803 eap->forceit = TRUE; /* since dialog already asked */
8804 }
8805#endif
8806
8807#if defined(FEAT_SESSION) && defined(vim_mkdir)
8808 /* When using 'viewdir' may have to create the directory. */
8809 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008810 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811#endif
8812
8813 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8814 if (fd != NULL)
8815 {
8816#ifdef FEAT_SESSION
8817 if (eap->cmdidx == CMD_mkview)
8818 flagp = &vop_flags;
8819 else
8820 flagp = &ssop_flags;
8821#endif
8822
8823#ifdef MKSESSION_NL
8824 /* "unix" in 'sessionoptions': use NL line separator */
8825 if (view_session && (*flagp & SSOP_UNIX))
8826 mksession_nl = TRUE;
8827#endif
8828
8829 /* Write the version command for :mkvimrc */
8830 if (eap->cmdidx == CMD_mkvimrc)
8831 (void)put_line(fd, "version 6.0");
8832
8833#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008834 if (eap->cmdidx == CMD_mksession)
8835 {
8836 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8837 failed = TRUE;
8838 }
8839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (eap->cmdidx != CMD_mkview)
8841#endif
8842 {
8843 /* Write setting 'compatible' first, because it has side effects.
8844 * For that same reason only do it when needed. */
8845 if (p_cp)
8846 (void)put_line(fd, "if !&cp | set cp | endif");
8847 else
8848 (void)put_line(fd, "if &cp | set nocp | endif");
8849 }
8850
8851#ifdef FEAT_SESSION
8852 if (!view_session
8853 || (eap->cmdidx == CMD_mksession
8854 && (*flagp & SSOP_OPTIONS)))
8855#endif
8856 failed |= (makemap(fd, NULL) == FAIL
8857 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8858
8859#ifdef FEAT_SESSION
8860 if (!failed && view_session)
8861 {
8862 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8863 failed = TRUE;
8864 if (eap->cmdidx == CMD_mksession)
8865 {
8866 char_u dirnow[MAXPATHL]; /* current directory */
8867
8868 /*
8869 * Change to session file's dir.
8870 */
8871 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8872 || mch_chdir((char *)dirnow) != 0)
8873 *dirnow = NUL;
8874 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8875 {
8876 if (vim_chdirfile(fname) == OK)
8877 shorten_fnames(TRUE);
8878 }
8879 else if (*dirnow != NUL
8880 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8881 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008882 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008883 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885
8886 failed |= (makeopens(fd, dirnow) == FAIL);
8887
8888 /* restore original dir */
8889 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8890 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8891 {
8892 if (mch_chdir((char *)dirnow) != 0)
8893 EMSG(_(e_prev_dir));
8894 shorten_fnames(TRUE);
8895 }
8896 }
8897 else
8898 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008899 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8900 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
8902 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8903 == FAIL)
8904 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008905 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8906 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008907 if (eap->cmdidx == CMD_mksession)
8908 {
8909 if (put_line(fd, "unlet SessionLoad") == FAIL)
8910 failed = TRUE;
8911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008914 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8915 failed = TRUE;
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 failed |= fclose(fd);
8918
8919 if (failed)
8920 EMSG(_(e_write));
8921#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8922 else if (eap->cmdidx == CMD_mksession)
8923 {
8924 /* successful session write - set this_session var */
8925 char_u tbuf[MAXPATHL];
8926
8927 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8928 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8929 }
8930#endif
8931#ifdef MKSESSION_NL
8932 mksession_nl = FALSE;
8933#endif
8934 }
8935
8936#ifdef FEAT_BROWSE
8937theend:
8938 vim_free(browseFile);
8939#endif
8940#ifdef FEAT_SESSION
8941 vim_free(viewFile);
8942#endif
8943}
8944
Bram Moolenaardf177f62005-02-22 08:39:57 +00008945#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8946 || defined(PROTO)
8947 int
8948vim_mkdir_emsg(name, prot)
8949 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008950 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008951{
8952 if (vim_mkdir(name, prot) != 0)
8953 {
8954 EMSG2(_("E739: Cannot create directory: %s"), name);
8955 return FAIL;
8956 }
8957 return OK;
8958}
8959#endif
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961/*
8962 * Open a file for writing for an Ex command, with some checks.
8963 * Return file descriptor, or NULL on failure.
8964 */
8965 FILE *
8966open_exfile(fname, forceit, mode)
8967 char_u *fname;
8968 int forceit;
8969 char *mode; /* "w" for create new file or "a" for append */
8970{
8971 FILE *fd;
8972
8973#ifdef UNIX
8974 /* with Unix it is possible to open a directory */
8975 if (mch_isdir(fname))
8976 {
8977 EMSG2(_(e_isadir2), fname);
8978 return NULL;
8979 }
8980#endif
8981 if (!forceit && *mode != 'a' && vim_fexists(fname))
8982 {
8983 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8984 return NULL;
8985 }
8986
8987 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8988 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8989
8990 return fd;
8991}
8992
8993/*
8994 * ":mark" and ":k".
8995 */
8996 static void
8997ex_mark(eap)
8998 exarg_T *eap;
8999{
9000 pos_T pos;
9001
9002 if (*eap->arg == NUL) /* No argument? */
9003 EMSG(_(e_argreq));
9004 else if (eap->arg[1] != NUL) /* more than one character? */
9005 EMSG(_(e_trailing));
9006 else
9007 {
9008 pos = curwin->w_cursor; /* save curwin->w_cursor */
9009 curwin->w_cursor.lnum = eap->line2;
9010 beginline(BL_WHITE | BL_FIX);
9011 if (setmark(*eap->arg) == FAIL) /* set mark */
9012 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9013 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9014 }
9015}
9016
9017/*
9018 * Update w_topline, w_leftcol and the cursor position.
9019 */
9020 void
9021update_topline_cursor()
9022{
9023 check_cursor(); /* put cursor on valid line */
9024 update_topline();
9025 if (!curwin->w_p_wrap)
9026 validate_cursor();
9027 update_curswant();
9028}
9029
9030#ifdef FEAT_EX_EXTRA
9031/*
9032 * ":normal[!] {commands}": Execute normal mode commands.
9033 */
9034 static void
9035ex_normal(eap)
9036 exarg_T *eap;
9037{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 int save_msg_scroll = msg_scroll;
9039 int save_restart_edit = restart_edit;
9040 int save_msg_didout = msg_didout;
9041 int save_State = State;
9042 tasave_T tabuf;
9043 int save_insertmode = p_im;
9044 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009045 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046#ifdef FEAT_MBYTE
9047 char_u *arg = NULL;
9048 int l;
9049 char_u *p;
9050#endif
9051
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009052 if (ex_normal_lock > 0)
9053 {
9054 EMSG(_(e_secure));
9055 return;
9056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (ex_normal_busy >= p_mmd)
9058 {
9059 EMSG(_("E192: Recursive use of :normal too deep"));
9060 return;
9061 }
9062 ++ex_normal_busy;
9063
9064 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9065 restart_edit = 0; /* don't go to Insert mode */
9066 p_im = FALSE; /* don't use 'insertmode' */
9067
9068#ifdef FEAT_MBYTE
9069 /*
9070 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9071 * this for the K_SPECIAL leading byte, otherwise special keys will not
9072 * work.
9073 */
9074 if (has_mbyte)
9075 {
9076 int len = 0;
9077
9078 /* Count the number of characters to be escaped. */
9079 for (p = eap->arg; *p != NUL; ++p)
9080 {
9081# ifdef FEAT_GUI
9082 if (*p == CSI) /* leadbyte CSI */
9083 len += 2;
9084# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009085 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9087# ifdef FEAT_GUI
9088 || *p == CSI
9089# endif
9090 )
9091 len += 2;
9092 }
9093 if (len > 0)
9094 {
9095 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9096 if (arg != NULL)
9097 {
9098 len = 0;
9099 for (p = eap->arg; *p != NUL; ++p)
9100 {
9101 arg[len++] = *p;
9102# ifdef FEAT_GUI
9103 if (*p == CSI)
9104 {
9105 arg[len++] = KS_EXTRA;
9106 arg[len++] = (int)KE_CSI;
9107 }
9108# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009109 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 {
9111 arg[len++] = *++p;
9112 if (*p == K_SPECIAL)
9113 {
9114 arg[len++] = KS_SPECIAL;
9115 arg[len++] = KE_FILLER;
9116 }
9117# ifdef FEAT_GUI
9118 else if (*p == CSI)
9119 {
9120 arg[len++] = KS_EXTRA;
9121 arg[len++] = (int)KE_CSI;
9122 }
9123# endif
9124 }
9125 arg[len] = NUL;
9126 }
9127 }
9128 }
9129 }
9130#endif
9131
9132 /*
9133 * Save the current typeahead. This is required to allow using ":normal"
9134 * from an event handler and makes sure we don't hang when the argument
9135 * ends with half a command.
9136 */
9137 save_typeahead(&tabuf);
9138 if (tabuf.typebuf_valid)
9139 {
9140 /*
9141 * Repeat the :normal command for each line in the range. When no
9142 * range given, execute it just once, without positioning the cursor
9143 * first.
9144 */
9145 do
9146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 if (eap->addr_count != 0)
9148 {
9149 curwin->w_cursor.lnum = eap->line1++;
9150 curwin->w_cursor.col = 0;
9151 }
9152
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009153 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#ifdef FEAT_MBYTE
9155 arg != NULL ? arg :
9156#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009157 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 }
9159 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9160 }
9161
9162 /* Might not return to the main loop when in an event handler. */
9163 update_topline_cursor();
9164
9165 /* Restore the previous typeahead. */
9166 restore_typeahead(&tabuf);
9167
9168 --ex_normal_busy;
9169 msg_scroll = save_msg_scroll;
9170 restart_edit = save_restart_edit;
9171 p_im = save_insertmode;
9172 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009173 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9175
9176 /* Restore the state (needed when called from a function executed for
9177 * 'indentexpr'). */
9178 State = save_State;
9179#ifdef FEAT_MBYTE
9180 vim_free(arg);
9181#endif
9182}
9183
9184/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009185 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 */
9187 static void
9188ex_startinsert(eap)
9189 exarg_T *eap;
9190{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009191 if (eap->forceit)
9192 {
9193 coladvance((colnr_T)MAXCOL);
9194 curwin->w_curswant = MAXCOL;
9195 curwin->w_set_curswant = FALSE;
9196 }
9197
Bram Moolenaara40c5002005-01-09 21:16:21 +00009198 /* Ignore the command when already in Insert mode. Inserting an
9199 * expression register that invokes a function can do this. */
9200 if (State & INSERT)
9201 return;
9202
Bram Moolenaar64969662005-12-14 21:59:55 +00009203 if (eap->cmdidx == CMD_startinsert)
9204 restart_edit = 'a';
9205 else if (eap->cmdidx == CMD_startreplace)
9206 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009208 restart_edit = 'V';
9209
9210 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009212 if (eap->cmdidx == CMD_startinsert)
9213 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 curwin->w_curswant = 0; /* avoid MAXCOL */
9215 }
9216}
9217
9218/*
9219 * ":stopinsert"
9220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 static void
9222ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009223 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 restart_edit = 0;
9226 stop_insert_mode = TRUE;
9227}
9228#endif
9229
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009230#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9231/*
9232 * Execute normal mode command "cmd".
9233 * "remap" can be REMAP_NONE or REMAP_YES.
9234 */
9235 void
9236exec_normal_cmd(cmd, remap, silent)
9237 char_u *cmd;
9238 int remap;
9239 int silent;
9240{
9241 oparg_T oa;
9242
9243 /*
9244 * Stuff the argument into the typeahead buffer.
9245 * Execute normal_cmd() until there is no typeahead left.
9246 */
9247 clear_oparg(&oa);
9248 finish_op = FALSE;
9249 ins_typebuf(cmd, remap, 0, TRUE, silent);
9250 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9251 && !got_int)
9252 {
9253 update_topline_cursor();
9254 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9255 }
9256}
9257#endif
9258
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259#ifdef FEAT_FIND_ID
9260 static void
9261ex_checkpath(eap)
9262 exarg_T *eap;
9263{
9264 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9265 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9266 (linenr_T)1, (linenr_T)MAXLNUM);
9267}
9268
9269#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9270/*
9271 * ":psearch"
9272 */
9273 static void
9274ex_psearch(eap)
9275 exarg_T *eap;
9276{
9277 g_do_tagpreview = p_pvh;
9278 ex_findpat(eap);
9279 g_do_tagpreview = 0;
9280}
9281#endif
9282
9283 static void
9284ex_findpat(eap)
9285 exarg_T *eap;
9286{
9287 int whole = TRUE;
9288 long n;
9289 char_u *p;
9290 int action;
9291
9292 switch (cmdnames[eap->cmdidx].cmd_name[2])
9293 {
9294 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9295 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9296 action = ACTION_GOTO;
9297 else
9298 action = ACTION_SHOW;
9299 break;
9300 case 'i': /* ":ilist" and ":dlist" */
9301 action = ACTION_SHOW_ALL;
9302 break;
9303 case 'u': /* ":ijump" and ":djump" */
9304 action = ACTION_GOTO;
9305 break;
9306 default: /* ":isplit" and ":dsplit" */
9307 action = ACTION_SPLIT;
9308 break;
9309 }
9310
9311 n = 1;
9312 if (vim_isdigit(*eap->arg)) /* get count */
9313 {
9314 n = getdigits(&eap->arg);
9315 eap->arg = skipwhite(eap->arg);
9316 }
9317 if (*eap->arg == '/') /* Match regexp, not just whole words */
9318 {
9319 whole = FALSE;
9320 ++eap->arg;
9321 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9322 if (*p)
9323 {
9324 *p++ = NUL;
9325 p = skipwhite(p);
9326
9327 /* Check for trailing illegal characters */
9328 if (!ends_excmd(*p))
9329 eap->errmsg = e_trailing;
9330 else
9331 eap->nextcmd = check_nextcmd(p);
9332 }
9333 }
9334 if (!eap->skip)
9335 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9336 whole, !eap->forceit,
9337 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9338 n, action, eap->line1, eap->line2);
9339}
9340#endif
9341
9342#ifdef FEAT_WINDOWS
9343
9344# ifdef FEAT_QUICKFIX
9345/*
9346 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9347 */
9348 static void
9349ex_ptag(eap)
9350 exarg_T *eap;
9351{
9352 g_do_tagpreview = p_pvh;
9353 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9354}
9355
9356/*
9357 * ":pedit"
9358 */
9359 static void
9360ex_pedit(eap)
9361 exarg_T *eap;
9362{
9363 win_T *curwin_save = curwin;
9364
9365 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009366 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 keep_help_flag = curwin_save->w_buffer->b_help;
9368 do_exedit(eap, NULL);
9369 keep_help_flag = FALSE;
9370 if (curwin != curwin_save && win_valid(curwin_save))
9371 {
9372 /* Return cursor to where we were */
9373 validate_cursor();
9374 redraw_later(VALID);
9375 win_enter(curwin_save, TRUE);
9376 }
9377 g_do_tagpreview = 0;
9378}
9379# endif
9380
9381/*
9382 * ":stag", ":stselect" and ":stjump".
9383 */
9384 static void
9385ex_stag(eap)
9386 exarg_T *eap;
9387{
9388 postponed_split = -1;
9389 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009390 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9392 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009393 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394}
9395#endif
9396
9397/*
9398 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9399 */
9400 static void
9401ex_tag(eap)
9402 exarg_T *eap;
9403{
9404 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9405}
9406
9407 static void
9408ex_tag_cmd(eap, name)
9409 exarg_T *eap;
9410 char_u *name;
9411{
9412 int cmd;
9413
9414 switch (name[1])
9415 {
9416 case 'j': cmd = DT_JUMP; /* ":tjump" */
9417 break;
9418 case 's': cmd = DT_SELECT; /* ":tselect" */
9419 break;
9420 case 'p': cmd = DT_PREV; /* ":tprevious" */
9421 break;
9422 case 'N': cmd = DT_PREV; /* ":tNext" */
9423 break;
9424 case 'n': cmd = DT_NEXT; /* ":tnext" */
9425 break;
9426 case 'o': cmd = DT_POP; /* ":pop" */
9427 break;
9428 case 'f': /* ":tfirst" */
9429 case 'r': cmd = DT_FIRST; /* ":trewind" */
9430 break;
9431 case 'l': cmd = DT_LAST; /* ":tlast" */
9432 break;
9433 default: /* ":tag" */
9434#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009435 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
9437 do_cstag(eap);
9438 return;
9439 }
9440#endif
9441 cmd = DT_TAG;
9442 break;
9443 }
9444
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009445 if (name[0] == 'l')
9446 {
9447#ifndef FEAT_QUICKFIX
9448 ex_ni(eap);
9449 return;
9450#else
9451 cmd = DT_LTAG;
9452#endif
9453 }
9454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9456 eap->forceit, TRUE);
9457}
9458
9459/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009460 * Check "str" for starting with a special cmdline variable.
9461 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9462 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9463 */
9464 int
9465find_cmdline_var(src, usedlen)
9466 char_u *src;
9467 int *usedlen;
9468{
9469 int len;
9470 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009471 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009472 "%",
9473#define SPEC_PERC 0
9474 "#",
9475#define SPEC_HASH 1
9476 "<cword>", /* cursor word */
9477#define SPEC_CWORD 2
9478 "<cWORD>", /* cursor WORD */
9479#define SPEC_CCWORD 3
9480 "<cfile>", /* cursor path name */
9481#define SPEC_CFILE 4
9482 "<sfile>", /* ":so" file name */
9483#define SPEC_SFILE 5
9484#ifdef FEAT_AUTOCMD
9485 "<afile>", /* autocommand file name */
9486# define SPEC_AFILE 6
9487 "<abuf>", /* autocommand buffer number */
9488# define SPEC_ABUF 7
9489 "<amatch>", /* autocommand match name */
9490# define SPEC_AMATCH 8
9491#endif
9492#ifdef FEAT_CLIENTSERVER
9493 "<client>"
9494# define SPEC_CLIENT 9
9495#endif
9496 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009497
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009498 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009499 {
9500 len = (int)STRLEN(spec_str[i]);
9501 if (STRNCMP(src, spec_str[i], len) == 0)
9502 {
9503 *usedlen = len;
9504 return i;
9505 }
9506 }
9507 return -1;
9508}
9509
9510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 * Evaluate cmdline variables.
9512 *
9513 * change '%' to curbuf->b_ffname
9514 * '#' to curwin->w_altfile
9515 * '<cword>' to word under the cursor
9516 * '<cWORD>' to WORD under the cursor
9517 * '<cfile>' to path name under the cursor
9518 * '<sfile>' to sourced file name
9519 * '<afile>' to file name for autocommand
9520 * '<abuf>' to buffer number for autocommand
9521 * '<amatch>' to matching name for autocommand
9522 *
9523 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9524 * "" for error without a message) and NULL is returned.
9525 * Returns an allocated string if a valid match was found.
9526 * Returns NULL if no match was found. "usedlen" then still contains the
9527 * number of characters to skip.
9528 */
9529 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009530eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009532 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 int *usedlen; /* characters after src that are used */
9534 linenr_T *lnump; /* line number for :e command, or NULL */
9535 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009536 int *escaped; /* return value has escaped white space (can
9537 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 int i;
9540 char_u *s;
9541 char_u *result;
9542 char_u *resultbuf = NULL;
9543 int resultlen;
9544 buf_T *buf;
9545 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9546 int spec_idx;
9547#ifdef FEAT_MODIFY_FNAME
9548 int skip_mod = FALSE;
9549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
9551#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9552 char_u strbuf[30];
9553#endif
9554
9555 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009556 if (escaped != NULL)
9557 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
9559 /*
9560 * Check if there is something to do.
9561 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009562 spec_idx = find_cmdline_var(src, usedlen);
9563 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 {
9565 *usedlen = 1;
9566 return NULL;
9567 }
9568
9569 /*
9570 * Skip when preceded with a backslash "\%" and "\#".
9571 * Note: In "\\%" the % is also not recognized!
9572 */
9573 if (src > srcstart && src[-1] == '\\')
9574 {
9575 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009576 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 return NULL;
9578 }
9579
9580 /*
9581 * word or WORD under cursor
9582 */
9583 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9584 {
9585 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9586 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9587 if (resultlen == 0)
9588 {
9589 *errormsg = (char_u *)"";
9590 return NULL;
9591 }
9592 }
9593
9594 /*
9595 * '#': Alternate file name
9596 * '%': Current file name
9597 * File name under the cursor
9598 * File name for autocommand
9599 * and following modifiers
9600 */
9601 else
9602 {
9603 switch (spec_idx)
9604 {
9605 case SPEC_PERC: /* '%': current file */
9606 if (curbuf->b_fname == NULL)
9607 {
9608 result = (char_u *)"";
9609 valid = 0; /* Must have ":p:h" to be valid */
9610 }
9611 else
9612#ifdef RISCOS
9613 /* Always use the full path for RISC OS if possible. */
9614 result = curbuf->b_ffname;
9615 if (result == NULL)
9616 result = curbuf->b_fname;
9617#else
9618 result = curbuf->b_fname;
9619#endif
9620 break;
9621
9622 case SPEC_HASH: /* '#' or "#99": alternate file */
9623 if (src[1] == '#') /* "##": the argument list */
9624 {
9625 result = arg_all();
9626 resultbuf = result;
9627 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009628 if (escaped != NULL)
9629 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630#ifdef FEAT_MODIFY_FNAME
9631 skip_mod = TRUE;
9632#endif
9633 break;
9634 }
9635 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009636 if (*s == '<') /* "#<99" uses v:oldfiles */
9637 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 i = (int)getdigits(&s);
9639 *usedlen = (int)(s - src); /* length of what we expand */
9640
Bram Moolenaard812df62008-11-09 12:46:09 +00009641 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009643 if (*usedlen < 2)
9644 {
9645 /* Should we give an error message for #<text? */
9646 *usedlen = 1;
9647 return NULL;
9648 }
9649#ifdef FEAT_EVAL
9650 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9651 (long)i);
9652 if (result == NULL)
9653 {
9654 *errormsg = (char_u *)"";
9655 return NULL;
9656 }
9657#else
9658 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 }
9662 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009663 {
9664 buf = buflist_findnr(i);
9665 if (buf == NULL)
9666 {
9667 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9668 return NULL;
9669 }
9670 if (lnump != NULL)
9671 *lnump = ECMD_LAST;
9672 if (buf->b_fname == NULL)
9673 {
9674 result = (char_u *)"";
9675 valid = 0; /* Must have ":p:h" to be valid */
9676 }
9677 else
9678 result = buf->b_fname;
9679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 break;
9681
9682#ifdef FEAT_SEARCHPATH
9683 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009684 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 if (result == NULL)
9686 {
9687 *errormsg = (char_u *)"";
9688 return NULL;
9689 }
9690 resultbuf = result; /* remember allocated string */
9691 break;
9692#endif
9693
9694#ifdef FEAT_AUTOCMD
9695 case SPEC_AFILE: /* file name for autocommand */
9696 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009697 if (result != NULL && !autocmd_fname_full)
9698 {
9699 /* Still need to turn the fname into a full path. It is
9700 * postponed to avoid a delay when <afile> is not used. */
9701 autocmd_fname_full = TRUE;
9702 result = FullName_save(autocmd_fname, FALSE);
9703 vim_free(autocmd_fname);
9704 autocmd_fname = result;
9705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (result == NULL)
9707 {
9708 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9709 return NULL;
9710 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009711 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 break;
9713
9714 case SPEC_ABUF: /* buffer number for autocommand */
9715 if (autocmd_bufnr <= 0)
9716 {
9717 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9718 return NULL;
9719 }
9720 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9721 result = strbuf;
9722 break;
9723
9724 case SPEC_AMATCH: /* match name for autocommand */
9725 result = autocmd_match;
9726 if (result == NULL)
9727 {
9728 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9729 return NULL;
9730 }
9731 break;
9732
9733#endif
9734 case SPEC_SFILE: /* file name for ":so" command */
9735 result = sourcing_name;
9736 if (result == NULL)
9737 {
9738 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9739 return NULL;
9740 }
9741 break;
9742#if defined(FEAT_CLIENTSERVER)
9743 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009744 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9745 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 result = strbuf;
9747 break;
9748#endif
9749 }
9750
9751 resultlen = (int)STRLEN(result); /* length of new string */
9752 if (src[*usedlen] == '<') /* remove the file name extension */
9753 {
9754 ++*usedlen;
9755#ifdef RISCOS
9756 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9757#else
9758 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9759#endif
9760 resultlen = (int)(s - result);
9761 }
9762#ifdef FEAT_MODIFY_FNAME
9763 else if (!skip_mod)
9764 {
9765 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9766 &resultlen);
9767 if (result == NULL)
9768 {
9769 *errormsg = (char_u *)"";
9770 return NULL;
9771 }
9772 }
9773#endif
9774 }
9775
9776 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9777 {
9778 if (valid != VALID_HEAD + VALID_PATH)
9779 /* xgettext:no-c-format */
9780 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9781 else
9782 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9783 result = NULL;
9784 }
9785 else
9786 result = vim_strnsave(result, resultlen);
9787 vim_free(resultbuf);
9788 return result;
9789}
9790
9791/*
9792 * Concatenate all files in the argument list, separated by spaces, and return
9793 * it in one allocated string.
9794 * Spaces and backslashes in the file names are escaped with a backslash.
9795 * Returns NULL when out of memory.
9796 */
9797 static char_u *
9798arg_all()
9799{
9800 int len;
9801 int idx;
9802 char_u *retval = NULL;
9803 char_u *p;
9804
9805 /*
9806 * Do this loop two times:
9807 * first time: compute the total length
9808 * second time: concatenate the names
9809 */
9810 for (;;)
9811 {
9812 len = 0;
9813 for (idx = 0; idx < ARGCOUNT; ++idx)
9814 {
9815 p = alist_name(&ARGLIST[idx]);
9816 if (p != NULL)
9817 {
9818 if (len > 0)
9819 {
9820 /* insert a space in between names */
9821 if (retval != NULL)
9822 retval[len] = ' ';
9823 ++len;
9824 }
9825 for ( ; *p != NUL; ++p)
9826 {
9827 if (*p == ' ' || *p == '\\')
9828 {
9829 /* insert a backslash */
9830 if (retval != NULL)
9831 retval[len] = '\\';
9832 ++len;
9833 }
9834 if (retval != NULL)
9835 retval[len] = *p;
9836 ++len;
9837 }
9838 }
9839 }
9840
9841 /* second time: break here */
9842 if (retval != NULL)
9843 {
9844 retval[len] = NUL;
9845 break;
9846 }
9847
9848 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009849 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 if (retval == NULL)
9851 break;
9852 }
9853
9854 return retval;
9855}
9856
9857#if defined(FEAT_AUTOCMD) || defined(PROTO)
9858/*
9859 * Expand the <sfile> string in "arg".
9860 *
9861 * Returns an allocated string, or NULL for any error.
9862 */
9863 char_u *
9864expand_sfile(arg)
9865 char_u *arg;
9866{
9867 char_u *errormsg;
9868 int len;
9869 char_u *result;
9870 char_u *newres;
9871 char_u *repl;
9872 int srclen;
9873 char_u *p;
9874
9875 result = vim_strsave(arg);
9876 if (result == NULL)
9877 return NULL;
9878
9879 for (p = result; *p; )
9880 {
9881 if (STRNCMP(p, "<sfile>", 7) != 0)
9882 ++p;
9883 else
9884 {
9885 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009886 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (errormsg != NULL)
9888 {
9889 if (*errormsg)
9890 emsg(errormsg);
9891 vim_free(result);
9892 return NULL;
9893 }
9894 if (repl == NULL) /* no match (cannot happen) */
9895 {
9896 p += srclen;
9897 continue;
9898 }
9899 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9900 newres = alloc(len);
9901 if (newres == NULL)
9902 {
9903 vim_free(repl);
9904 vim_free(result);
9905 return NULL;
9906 }
9907 mch_memmove(newres, result, (size_t)(p - result));
9908 STRCPY(newres + (p - result), repl);
9909 len = (int)STRLEN(newres);
9910 STRCAT(newres, p + srclen);
9911 vim_free(repl);
9912 vim_free(result);
9913 result = newres;
9914 p = newres + len; /* continue after the match */
9915 }
9916 }
9917
9918 return result;
9919}
9920#endif
9921
9922#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009923static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9924 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9926static frame_T *ses_skipframe __ARGS((frame_T *fr));
9927static int ses_do_frame __ARGS((frame_T *fr));
9928static int ses_do_win __ARGS((win_T *wp));
9929static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9930static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9931static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9932
9933/*
9934 * Write openfile commands for the current buffers to an .exrc file.
9935 * Return FAIL on error, OK otherwise.
9936 */
9937 static int
9938makeopens(fd, dirnow)
9939 FILE *fd;
9940 char_u *dirnow; /* Current directory name */
9941{
9942 buf_T *buf;
9943 int only_save_windows = TRUE;
9944 int nr;
9945 int cnr = 1;
9946 int restore_size = TRUE;
9947 win_T *wp;
9948 char_u *sname;
9949 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009950 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009951 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009952 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009953 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009954 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
9956 if (ssop_flags & SSOP_BUFFERS)
9957 only_save_windows = FALSE; /* Save ALL buffers */
9958
9959 /*
9960 * Begin by setting the this_session variable, and then other
9961 * sessionable variables.
9962 */
9963#ifdef FEAT_EVAL
9964 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9965 return FAIL;
9966 if (ssop_flags & SSOP_GLOBALS)
9967 if (store_session_globals(fd) == FAIL)
9968 return FAIL;
9969#endif
9970
9971 /*
9972 * Close all windows but one.
9973 */
9974 if (put_line(fd, "silent only") == FAIL)
9975 return FAIL;
9976
9977 /*
9978 * Now a :cd command to the session directory or the current directory
9979 */
9980 if (ssop_flags & SSOP_SESDIR)
9981 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009982 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9983 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 return FAIL;
9985 }
9986 else if (ssop_flags & SSOP_CURDIR)
9987 {
9988 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9989 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009990 || fputs("cd ", fd) < 0
9991 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9992 || put_eol(fd) == FAIL)
9993 {
9994 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 vim_free(sname);
9998 }
9999
10000 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010001 * If there is an empty, unnamed buffer we will wipe it out later.
10002 * Remember the buffer number.
10003 */
10004 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10005 return FAIL;
10006 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10007 return FAIL;
10008 if (put_line(fd, "endif") == FAIL)
10009 return FAIL;
10010
10011 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 * Now save the current files, current buffer first.
10013 */
10014 if (put_line(fd, "set shortmess=aoO") == FAIL)
10015 return FAIL;
10016
10017 /* Now put the other buffers into the buffer list */
10018 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10019 {
10020 if (!(only_save_windows && buf->b_nwindows == 0)
10021 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10022 && buf->b_fname != NULL
10023 && buf->b_p_bl)
10024 {
10025 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10026 : buf->b_wininfo->wi_fpos.lnum) < 0
10027 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10028 return FAIL;
10029 }
10030 }
10031
10032 /* the global argument list */
10033 if (ses_arglist(fd, "args", &global_alist.al_ga,
10034 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10035 return FAIL;
10036
10037 if (ssop_flags & SSOP_RESIZE)
10038 {
10039 /* Note: after the restore we still check it worked!*/
10040 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10041 || put_eol(fd) == FAIL)
10042 return FAIL;
10043 }
10044
10045#ifdef FEAT_GUI
10046 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10047 {
10048 int x, y;
10049
10050 if (gui_mch_get_winpos(&x, &y) == OK)
10051 {
10052 /* Note: after the restore we still check it worked!*/
10053 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10054 return FAIL;
10055 }
10056 }
10057#endif
10058
10059 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010060 * May repeat putting Windows for each tab, when "tabpages" is in
10061 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010062 * Don't use goto_tabpage(), it may change directory and trigger
10063 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010065 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010066 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010067 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010069 int need_tabnew = FALSE;
10070
Bram Moolenaar18144c82006-04-12 21:52:12 +000010071 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010073 tabpage_T *tp = find_tabpage(tabnr);
10074
10075 if (tp == NULL)
10076 break; /* done all tab pages */
10077 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010078 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010079 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010080 tab_topframe = topframe;
10081 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010082 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010083 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010084 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010085 tab_topframe = tp->tp_topframe;
10086 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010087 if (tabnr > 1)
10088 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
Bram Moolenaar18144c82006-04-12 21:52:12 +000010091 /*
10092 * Before creating the window layout, try loading one file. If this
10093 * is aborted we don't end up with a number of useless windows.
10094 * This may have side effects! (e.g., compressed or network file).
10095 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010096 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010097 {
10098 if (ses_do_win(wp)
10099 && wp->w_buffer->b_ffname != NULL
10100 && !wp->w_buffer->b_help
10101#ifdef FEAT_QUICKFIX
10102 && !bt_nofile(wp->w_buffer)
10103#endif
10104 )
10105 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010106 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010107 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10108 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010109 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010110 if (!wp->w_arg_idx_invalid)
10111 edited_win = wp;
10112 break;
10113 }
10114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010116 /* If no file got edited create an empty tab page. */
10117 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10118 return FAIL;
10119
Bram Moolenaar18144c82006-04-12 21:52:12 +000010120 /*
10121 * Save current window layout.
10122 */
10123 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010125 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010127 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10128 return FAIL;
10129 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10130 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
Bram Moolenaar18144c82006-04-12 21:52:12 +000010132 /*
10133 * Check if window sizes can be restored (no windows omitted).
10134 * Remember the window number of the current window after restoring.
10135 */
10136 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010137 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010138 {
10139 if (ses_do_win(wp))
10140 ++nr;
10141 else
10142 restore_size = FALSE;
10143 if (curwin == wp)
10144 cnr = nr;
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaar18144c82006-04-12 21:52:12 +000010147 /* Go to the first window. */
10148 if (put_line(fd, "wincmd t") == FAIL)
10149 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010150
Bram Moolenaar18144c82006-04-12 21:52:12 +000010151 /*
10152 * If more than one window, see if sizes can be restored.
10153 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10154 * resized when moving between windows.
10155 * Do this before restoring the view, so that the topline and the
10156 * cursor can be set. This is done again below.
10157 */
10158 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10159 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010160 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010161 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
Bram Moolenaar18144c82006-04-12 21:52:12 +000010163 /*
10164 * Restore the view of the window (options, file, cursor, etc.).
10165 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010166 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010167 {
10168 if (!ses_do_win(wp))
10169 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010170 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10171 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010172 return FAIL;
10173 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10174 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010175 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010176 }
10177
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010178 /* The argument index in the first tab page is zero, need to set it in
10179 * each window. For further tab pages it's the window where we do
10180 * "tabedit". */
10181 cur_arg_idx = next_arg_idx;
10182
Bram Moolenaar18144c82006-04-12 21:52:12 +000010183 /*
10184 * Restore cursor to the current window if it's not the first one.
10185 */
10186 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10187 || put_eol(fd) == FAIL))
10188 return FAIL;
10189
10190 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010191 * Restore window sizes again after jumping around in windows, because
10192 * the current window has a minimum size while others may not.
10193 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010194 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010195 return FAIL;
10196
Bram Moolenaar18144c82006-04-12 21:52:12 +000010197 /* Don't continue in another tab page when doing only the current one
10198 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010199 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010200 break;
10201 }
10202
10203 if (ssop_flags & SSOP_TABPAGES)
10204 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010205 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10206 || put_eol(fd) == FAIL)
10207 return FAIL;
10208 }
10209
Bram Moolenaar9c102382006-05-03 21:26:49 +000010210 /*
10211 * Wipe out an empty unnamed buffer we started in.
10212 */
10213 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10214 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010215 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010216 return FAIL;
10217 if (put_line(fd, "endif") == FAIL)
10218 return FAIL;
10219 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10220 return FAIL;
10221
10222 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10223 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10224 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226
10227 /*
10228 * Lastly, execute the x.vim file if it exists.
10229 */
10230 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10231 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010232 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 || put_line(fd, "endif") == FAIL)
10234 return FAIL;
10235
10236 return OK;
10237}
10238
10239 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010240ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 FILE *fd;
10242 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010243 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 int n = 0;
10246 win_T *wp;
10247
10248 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10249 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010250 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 {
10252 if (!ses_do_win(wp))
10253 continue;
10254 ++n;
10255
10256 /* restore height when not full height */
10257 if (wp->w_height + wp->w_status_height < topframe->fr_height
10258 && (fprintf(fd,
10259 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10260 n, (long)wp->w_height, Rows / 2, Rows) < 0
10261 || put_eol(fd) == FAIL))
10262 return FAIL;
10263
10264 /* restore width when not full width */
10265 if (wp->w_width < Columns && (fprintf(fd,
10266 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10267 n, (long)wp->w_width, Columns / 2, Columns) < 0
10268 || put_eol(fd) == FAIL))
10269 return FAIL;
10270 }
10271 }
10272 else
10273 {
10274 /* Just equalise window sizes */
10275 if (put_line(fd, "wincmd =") == FAIL)
10276 return FAIL;
10277 }
10278 return OK;
10279}
10280
10281/*
10282 * Write commands to "fd" to recursively create windows for frame "fr",
10283 * horizontally and vertically split.
10284 * After the commands the last window in the frame is the current window.
10285 * Returns FAIL when writing the commands to "fd" fails.
10286 */
10287 static int
10288ses_win_rec(fd, fr)
10289 FILE *fd;
10290 frame_T *fr;
10291{
10292 frame_T *frc;
10293 int count = 0;
10294
10295 if (fr->fr_layout != FR_LEAF)
10296 {
10297 /* Find first frame that's not skipped and then create a window for
10298 * each following one (first frame is already there). */
10299 frc = ses_skipframe(fr->fr_child);
10300 if (frc != NULL)
10301 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10302 {
10303 /* Make window as big as possible so that we have lots of room
10304 * to split. */
10305 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10306 || put_line(fd, fr->fr_layout == FR_COL
10307 ? "split" : "vsplit") == FAIL)
10308 return FAIL;
10309 ++count;
10310 }
10311
10312 /* Go back to the first window. */
10313 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10314 ? "%dwincmd k" : "%dwincmd h", count) < 0
10315 || put_eol(fd) == FAIL))
10316 return FAIL;
10317
10318 /* Recursively create frames/windows in each window of this column or
10319 * row. */
10320 frc = ses_skipframe(fr->fr_child);
10321 while (frc != NULL)
10322 {
10323 ses_win_rec(fd, frc);
10324 frc = ses_skipframe(frc->fr_next);
10325 /* Go to next window. */
10326 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10327 return FAIL;
10328 }
10329 }
10330 return OK;
10331}
10332
10333/*
10334 * Skip frames that don't contain windows we want to save in the Session.
10335 * Returns NULL when there none.
10336 */
10337 static frame_T *
10338ses_skipframe(fr)
10339 frame_T *fr;
10340{
10341 frame_T *frc;
10342
10343 for (frc = fr; frc != NULL; frc = frc->fr_next)
10344 if (ses_do_frame(frc))
10345 break;
10346 return frc;
10347}
10348
10349/*
10350 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10351 * the Session.
10352 */
10353 static int
10354ses_do_frame(fr)
10355 frame_T *fr;
10356{
10357 frame_T *frc;
10358
10359 if (fr->fr_layout == FR_LEAF)
10360 return ses_do_win(fr->fr_win);
10361 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10362 if (ses_do_frame(frc))
10363 return TRUE;
10364 return FALSE;
10365}
10366
10367/*
10368 * Return non-zero if window "wp" is to be stored in the Session.
10369 */
10370 static int
10371ses_do_win(wp)
10372 win_T *wp;
10373{
10374 if (wp->w_buffer->b_fname == NULL
10375#ifdef FEAT_QUICKFIX
10376 /* When 'buftype' is "nofile" can't restore the window contents. */
10377 || bt_nofile(wp->w_buffer)
10378#endif
10379 )
10380 return (ssop_flags & SSOP_BLANK);
10381 if (wp->w_buffer->b_help)
10382 return (ssop_flags & SSOP_HELP);
10383 return TRUE;
10384}
10385
10386/*
10387 * Write commands to "fd" to restore the view of a window.
10388 * Caller must make sure 'scrolloff' is zero.
10389 */
10390 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010391put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 FILE *fd;
10393 win_T *wp;
10394 int add_edit; /* add ":edit" command to view */
10395 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010396 int current_arg_idx; /* current argument index of the window, use
10397 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
10399 win_T *save_curwin;
10400 int f;
10401 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010402 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403
10404 /* Always restore cursor position for ":mksession". For ":mkview" only
10405 * when 'viewoptions' contains "cursor". */
10406 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10407
10408 /*
10409 * Local argument list.
10410 */
10411 if (wp->w_alist == &global_alist)
10412 {
10413 if (put_line(fd, "argglobal") == FAIL)
10414 return FAIL;
10415 }
10416 else
10417 {
10418 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10419 flagp == &vop_flags
10420 || !(*flagp & SSOP_CURDIR)
10421 || wp->w_localdir != NULL, flagp) == FAIL)
10422 return FAIL;
10423 }
10424
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010425 /* Only when part of a session: restore the argument index. Some
10426 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010427 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx <= WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010428 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010430 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 || put_eol(fd) == FAIL)
10432 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010433 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 }
10435
10436 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010437 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 {
10439 /*
10440 * Load the file.
10441 */
10442 if (wp->w_buffer->b_ffname != NULL
10443#ifdef FEAT_QUICKFIX
10444 && !bt_nofile(wp->w_buffer)
10445#endif
10446 )
10447 {
10448 /*
10449 * Editing a file in this buffer: use ":edit file".
10450 * This may have side effects! (e.g., compressed or network file).
10451 */
10452 if (fputs("edit ", fd) < 0
10453 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10454 return FAIL;
10455 }
10456 else
10457 {
10458 /* No file in this buffer, just make it empty. */
10459 if (put_line(fd, "enew") == FAIL)
10460 return FAIL;
10461#ifdef FEAT_QUICKFIX
10462 if (wp->w_buffer->b_ffname != NULL)
10463 {
10464 /* The buffer does have a name, but it's not a file name. */
10465 if (fputs("file ", fd) < 0
10466 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10467 return FAIL;
10468 }
10469#endif
10470 do_cursor = FALSE;
10471 }
10472 }
10473
10474 /*
10475 * Local mappings and abbreviations.
10476 */
10477 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10478 && makemap(fd, wp->w_buffer) == FAIL)
10479 return FAIL;
10480
10481 /*
10482 * Local options. Need to go to the window temporarily.
10483 * Store only local values when using ":mkview" and when ":mksession" is
10484 * used and 'sessionoptions' doesn't include "options".
10485 * Some folding options are always stored when "folds" is included,
10486 * otherwise the folds would not be restored correctly.
10487 */
10488 save_curwin = curwin;
10489 curwin = wp;
10490 curbuf = curwin->w_buffer;
10491 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10492 f = makeset(fd, OPT_LOCAL,
10493 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10494#ifdef FEAT_FOLDING
10495 else if (*flagp & SSOP_FOLDS)
10496 f = makefoldset(fd);
10497#endif
10498 else
10499 f = OK;
10500 curwin = save_curwin;
10501 curbuf = curwin->w_buffer;
10502 if (f == FAIL)
10503 return FAIL;
10504
10505#ifdef FEAT_FOLDING
10506 /*
10507 * Save Folds when 'buftype' is empty and for help files.
10508 */
10509 if ((*flagp & SSOP_FOLDS)
10510 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010511# ifdef FEAT_QUICKFIX
10512 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10513# endif
10514 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 {
10516 if (put_folds(fd, wp) == FAIL)
10517 return FAIL;
10518 }
10519#endif
10520
10521 /*
10522 * Set the cursor after creating folds, since that moves the cursor.
10523 */
10524 if (do_cursor)
10525 {
10526
10527 /* Restore the cursor line in the file and relatively in the
10528 * window. Don't use "G", it changes the jumplist. */
10529 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10530 (long)wp->w_cursor.lnum,
10531 (long)(wp->w_cursor.lnum - wp->w_topline),
10532 (long)wp->w_height / 2, (long)wp->w_height) < 0
10533 || put_eol(fd) == FAIL
10534 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10535 || put_line(fd, "exe s:l") == FAIL
10536 || put_line(fd, "normal! zt") == FAIL
10537 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10538 || put_eol(fd) == FAIL)
10539 return FAIL;
10540 /* Restore the cursor column and left offset when not wrapping. */
10541 if (wp->w_cursor.col == 0)
10542 {
10543 if (put_line(fd, "normal! 0") == FAIL)
10544 return FAIL;
10545 }
10546 else
10547 {
10548 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10549 {
10550 if (fprintf(fd,
10551 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10552 (long)wp->w_cursor.col,
10553 (long)(wp->w_cursor.col - wp->w_leftcol),
10554 (long)wp->w_width / 2, (long)wp->w_width) < 0
10555 || put_eol(fd) == FAIL
10556 || put_line(fd, "if s:c > 0") == FAIL
10557 || fprintf(fd,
10558 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10559 (long)wp->w_cursor.col) < 0
10560 || put_eol(fd) == FAIL
10561 || put_line(fd, "else") == FAIL
10562 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10563 || put_eol(fd) == FAIL
10564 || put_line(fd, "endif") == FAIL)
10565 return FAIL;
10566 }
10567 else
10568 {
10569 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10570 || put_eol(fd) == FAIL)
10571 return FAIL;
10572 }
10573 }
10574 }
10575
10576 /*
10577 * Local directory.
10578 */
10579 if (wp->w_localdir != NULL)
10580 {
10581 if (fputs("lcd ", fd) < 0
10582 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10583 || put_eol(fd) == FAIL)
10584 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010585 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 }
10587
10588 return OK;
10589}
10590
10591/*
10592 * Write an argument list to the session file.
10593 * Returns FAIL if writing fails.
10594 */
10595 static int
10596ses_arglist(fd, cmd, gap, fullname, flagp)
10597 FILE *fd;
10598 char *cmd;
10599 garray_T *gap;
10600 int fullname; /* TRUE: use full path name */
10601 unsigned *flagp;
10602{
10603 int i;
10604 char_u buf[MAXPATHL];
10605 char_u *s;
10606
10607 if (gap->ga_len == 0)
10608 return put_line(fd, "silent! argdel *");
10609 if (fputs(cmd, fd) < 0)
10610 return FAIL;
10611 for (i = 0; i < gap->ga_len; ++i)
10612 {
10613 /* NULL file names are skipped (only happens when out of memory). */
10614 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10615 if (s != NULL)
10616 {
10617 if (fullname)
10618 {
10619 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10620 s = buf;
10621 }
10622 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10623 return FAIL;
10624 }
10625 }
10626 return put_eol(fd);
10627}
10628
10629/*
10630 * Write a buffer name to the session file.
10631 * Also ends the line.
10632 * Returns FAIL if writing fails.
10633 */
10634 static int
10635ses_fname(fd, buf, flagp)
10636 FILE *fd;
10637 buf_T *buf;
10638 unsigned *flagp;
10639{
10640 char_u *name;
10641
10642 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010643 * the session file will be sourced.
10644 * Don't do this for ":mkview", we don't know the current directory.
10645 * Don't do this after ":lcd", we don't keep track of what the current
10646 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 if (buf->b_sfname != NULL
10648 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010649 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010650#ifdef FEAT_AUTOCHDIR
10651 && !p_acd
10652#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010653 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 name = buf->b_sfname;
10655 else
10656 name = buf->b_ffname;
10657 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10658 return FAIL;
10659 return OK;
10660}
10661
10662/*
10663 * Write a file name to the session file.
10664 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10665 * characters.
10666 * Returns FAIL if writing fails.
10667 */
10668 static int
10669ses_put_fname(fd, name, flagp)
10670 FILE *fd;
10671 char_u *name;
10672 unsigned *flagp;
10673{
10674 char_u *sname;
10675 int retval = OK;
10676 int c;
10677
10678 sname = home_replace_save(NULL, name);
10679 if (sname != NULL)
10680 name = sname;
10681 while (*name != NUL)
10682 {
10683#ifdef FEAT_MBYTE
10684 {
10685 int l;
10686
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010687 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 {
10689 /* copy a multibyte char */
10690 while (--l >= 0)
10691 {
10692 if (putc(*name, fd) != *name)
10693 retval = FAIL;
10694 ++name;
10695 }
10696 continue;
10697 }
10698 }
10699#endif
10700 c = *name++;
10701 if (c == '\\' && (*flagp & SSOP_SLASH))
10702 /* change a backslash to a forward slash */
10703 c = '/';
10704 else if ((vim_strchr(escape_chars, c) != NULL
10705#ifdef BACKSLASH_IN_FILENAME
10706 && c != '\\'
10707#endif
10708 ) || c == '#' || c == '%')
10709 {
10710 /* escape a special character with a backslash */
10711 if (putc('\\', fd) != '\\')
10712 retval = FAIL;
10713 }
10714 if (putc(c, fd) != c)
10715 retval = FAIL;
10716 }
10717 vim_free(sname);
10718 return retval;
10719}
10720
10721/*
10722 * ":loadview [nr]"
10723 */
10724 static void
10725ex_loadview(eap)
10726 exarg_T *eap;
10727{
10728 char_u *fname;
10729
10730 fname = get_view_file(*eap->arg);
10731 if (fname != NULL)
10732 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010733 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 vim_free(fname);
10735 }
10736}
10737
10738/*
10739 * Get the name of the view file for the current buffer.
10740 */
10741 static char_u *
10742get_view_file(c)
10743 int c;
10744{
10745 int len = 0;
10746 char_u *p, *s;
10747 char_u *retval;
10748 char_u *sname;
10749
10750 if (curbuf->b_ffname == NULL)
10751 {
10752 EMSG(_(e_noname));
10753 return NULL;
10754 }
10755 sname = home_replace_save(NULL, curbuf->b_ffname);
10756 if (sname == NULL)
10757 return NULL;
10758
10759 /*
10760 * We want a file name without separators, because we're not going to make
10761 * a directory.
10762 * "normal" path separator -> "=+"
10763 * "=" -> "=="
10764 * ":" path separator -> "=-"
10765 */
10766 for (p = sname; *p; ++p)
10767 if (*p == '=' || vim_ispathsep(*p))
10768 ++len;
10769 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10770 if (retval != NULL)
10771 {
10772 STRCPY(retval, p_vdir);
10773 add_pathsep(retval);
10774 s = retval + STRLEN(retval);
10775 for (p = sname; *p; ++p)
10776 {
10777 if (*p == '=')
10778 {
10779 *s++ = '=';
10780 *s++ = '=';
10781 }
10782 else if (vim_ispathsep(*p))
10783 {
10784 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010785#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 || defined(VMS)
10787 if (*p == ':')
10788 *s++ = '-';
10789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010791 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 }
10793 else
10794 *s++ = *p;
10795 }
10796 *s++ = '=';
10797 *s++ = c;
10798 STRCPY(s, ".vim");
10799 }
10800
10801 vim_free(sname);
10802 return retval;
10803}
10804
10805#endif /* FEAT_SESSION */
10806
10807/*
10808 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10809 * Return FAIL for a write error.
10810 */
10811 int
10812put_eol(fd)
10813 FILE *fd;
10814{
10815 if (
10816#ifdef USE_CRNL
10817 (
10818# ifdef MKSESSION_NL
10819 !mksession_nl &&
10820# endif
10821 (putc('\r', fd) < 0)) ||
10822#endif
10823 (putc('\n', fd) < 0))
10824 return FAIL;
10825 return OK;
10826}
10827
10828/*
10829 * Write a line to "fd".
10830 * Return FAIL for a write error.
10831 */
10832 int
10833put_line(fd, s)
10834 FILE *fd;
10835 char *s;
10836{
10837 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10838 return FAIL;
10839 return OK;
10840}
10841
10842#ifdef FEAT_VIMINFO
10843/*
10844 * ":rviminfo" and ":wviminfo".
10845 */
10846 static void
10847ex_viminfo(eap)
10848 exarg_T *eap;
10849{
10850 char_u *save_viminfo;
10851
10852 save_viminfo = p_viminfo;
10853 if (*p_viminfo == NUL)
10854 p_viminfo = (char_u *)"'100";
10855 if (eap->cmdidx == CMD_rviminfo)
10856 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010857 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10858 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 EMSG(_("E195: Cannot open viminfo file for reading"));
10860 }
10861 else
10862 write_viminfo(eap->arg, eap->forceit);
10863 p_viminfo = save_viminfo;
10864}
10865#endif
10866
10867#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010868/*
10869 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010870 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 void
10873dialog_msg(buff, format, fname)
10874 char_u *buff;
10875 char *format;
10876 char_u *fname;
10877{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 if (fname == NULL)
10879 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010880 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882#endif
10883
10884/*
10885 * ":behave {mswin,xterm}"
10886 */
10887 static void
10888ex_behave(eap)
10889 exarg_T *eap;
10890{
10891 if (STRCMP(eap->arg, "mswin") == 0)
10892 {
10893 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10894 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10895 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10896 set_option_value((char_u *)"keymodel", 0L,
10897 (char_u *)"startsel,stopsel", 0);
10898 }
10899 else if (STRCMP(eap->arg, "xterm") == 0)
10900 {
10901 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10902 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10903 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10904 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10905 }
10906 else
10907 EMSG2(_(e_invarg2), eap->arg);
10908}
10909
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010910#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10911/*
10912 * Function given to ExpandGeneric() to obtain the possible arguments of the
10913 * ":behave {mswin,xterm}" command.
10914 */
10915 char_u *
10916get_behave_arg(xp, idx)
10917 expand_T *xp UNUSED;
10918 int idx;
10919{
10920 if (idx == 0)
10921 return (char_u *)"mswin";
10922 if (idx == 1)
10923 return (char_u *)"xterm";
10924 return NULL;
10925}
10926#endif
10927
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928#ifdef FEAT_AUTOCMD
10929static int filetype_detect = FALSE;
10930static int filetype_plugin = FALSE;
10931static int filetype_indent = FALSE;
10932
10933/*
10934 * ":filetype [plugin] [indent] {on,off,detect}"
10935 * on: Load the filetype.vim file to install autocommands for file types.
10936 * off: Load the ftoff.vim file to remove all autocommands for file types.
10937 * plugin on: load filetype.vim and ftplugin.vim
10938 * plugin off: load ftplugof.vim
10939 * indent on: load filetype.vim and indent.vim
10940 * indent off: load indoff.vim
10941 */
10942 static void
10943ex_filetype(eap)
10944 exarg_T *eap;
10945{
10946 char_u *arg = eap->arg;
10947 int plugin = FALSE;
10948 int indent = FALSE;
10949
10950 if (*eap->arg == NUL)
10951 {
10952 /* Print current status. */
10953 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10954 filetype_detect ? "ON" : "OFF",
10955 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10956 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10957 return;
10958 }
10959
10960 /* Accept "plugin" and "indent" in any order. */
10961 for (;;)
10962 {
10963 if (STRNCMP(arg, "plugin", 6) == 0)
10964 {
10965 plugin = TRUE;
10966 arg = skipwhite(arg + 6);
10967 continue;
10968 }
10969 if (STRNCMP(arg, "indent", 6) == 0)
10970 {
10971 indent = TRUE;
10972 arg = skipwhite(arg + 6);
10973 continue;
10974 }
10975 break;
10976 }
10977 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10978 {
10979 if (*arg == 'o' || !filetype_detect)
10980 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010981 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 filetype_detect = TRUE;
10983 if (plugin)
10984 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010985 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 filetype_plugin = TRUE;
10987 }
10988 if (indent)
10989 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010990 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 filetype_indent = TRUE;
10992 }
10993 }
10994 if (*arg == 'd')
10995 {
10996 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010997 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 }
10999 }
11000 else if (STRCMP(arg, "off") == 0)
11001 {
11002 if (plugin || indent)
11003 {
11004 if (plugin)
11005 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011006 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 filetype_plugin = FALSE;
11008 }
11009 if (indent)
11010 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011011 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 filetype_indent = FALSE;
11013 }
11014 }
11015 else
11016 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011017 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 filetype_detect = FALSE;
11019 }
11020 }
11021 else
11022 EMSG2(_(e_invarg2), arg);
11023}
11024
11025/*
11026 * ":setfiletype {name}"
11027 */
11028 static void
11029ex_setfiletype(eap)
11030 exarg_T *eap;
11031{
11032 if (!did_filetype)
11033 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11034}
11035#endif
11036
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 static void
11038ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011039 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
11041#ifdef FEAT_DIGRAPHS
11042 if (*eap->arg != NUL)
11043 putdigraph(eap->arg);
11044 else
11045 listdigraphs();
11046#else
11047 EMSG(_("E196: No digraphs in this version"));
11048#endif
11049}
11050
11051 static void
11052ex_set(eap)
11053 exarg_T *eap;
11054{
11055 int flags = 0;
11056
11057 if (eap->cmdidx == CMD_setlocal)
11058 flags = OPT_LOCAL;
11059 else if (eap->cmdidx == CMD_setglobal)
11060 flags = OPT_GLOBAL;
11061#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11062 if (cmdmod.browse && flags == 0)
11063 ex_options(eap);
11064 else
11065#endif
11066 (void)do_set(eap->arg, flags);
11067}
11068
11069#ifdef FEAT_SEARCH_EXTRA
11070/*
11071 * ":nohlsearch"
11072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 static void
11074ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011075 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
11077 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011078 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079}
11080
11081/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011082 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 * Sets nextcmd to the start of the next command, if any. Also called when
11084 * skipping commands to find the next command.
11085 */
11086 static void
11087ex_match(eap)
11088 exarg_T *eap;
11089{
11090 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011091 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 char_u *end;
11093 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011094 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011095
11096 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011097 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011098 else
11099 {
11100 EMSG(e_invcmd);
11101 return;
11102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
11104 /* First clear any old pattern. */
11105 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011106 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
11108 if (ends_excmd(*eap->arg))
11109 end = eap->arg;
11110 else if ((STRNICMP(eap->arg, "none", 4) == 0
11111 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11112 end = eap->arg + 4;
11113 else
11114 {
11115 p = skiptowhite(eap->arg);
11116 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011117 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 p = skipwhite(p);
11119 if (*p == NUL)
11120 {
11121 /* There must be two arguments. */
11122 EMSG2(_(e_invarg2), eap->arg);
11123 return;
11124 }
11125 end = skip_regexp(p + 1, *p, TRUE, NULL);
11126 if (!eap->skip)
11127 {
11128 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11129 {
11130 eap->errmsg = e_trailing;
11131 return;
11132 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011133 if (*end != *p)
11134 {
11135 EMSG2(_(e_invarg2), p);
11136 return;
11137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138
11139 c = *end;
11140 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011141 match_add(curwin, g, p + 1, 10, id);
11142 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011143 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 }
11145 }
11146 eap->nextcmd = find_nextcmd(end);
11147}
11148#endif
11149
11150#ifdef FEAT_CRYPT
11151/*
11152 * ":X": Get crypt key
11153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 static void
11155ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011156 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011158 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11159 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160}
11161#endif
11162
11163#ifdef FEAT_FOLDING
11164 static void
11165ex_fold(eap)
11166 exarg_T *eap;
11167{
11168 if (foldManualAllowed(TRUE))
11169 foldCreate(eap->line1, eap->line2);
11170}
11171
11172 static void
11173ex_foldopen(eap)
11174 exarg_T *eap;
11175{
11176 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11177 eap->forceit, FALSE);
11178}
11179
11180 static void
11181ex_folddo(eap)
11182 exarg_T *eap;
11183{
11184 linenr_T lnum;
11185
11186 /* First set the marks for all lines closed/open. */
11187 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11188 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11189 ml_setmarked(lnum);
11190
11191 /* Execute the command on the marked lines. */
11192 global_exe(eap->arg);
11193 ml_clearmarked(); /* clear rest of the marks */
11194}
11195#endif